def post(self): urlfetch.set_default_fetch_deadline(60) body = json.loads(self.request.body) logging.info('request body:') logging.info(body) self.response.write(json.dumps(body)) update_id = body['update_id'] message = body['message'] message_id = message.get('message_id') date = message.get('date') text = message.get('text') fr = message.get('from') chat = message['chat'] chat_id = chat['id'] if not text: logging.info('no text') return def reply(msg=None): send(msg, chat_id) lastUpdate = getCurrentUpdate(chat_id) if lastUpdate >= update_id: logging.info('Already at update ' + str(lastUpdate)) return else: setCurrentUpdate(chat_id, update_id) if getSetting('userRegistration') != 'True' and not Entity.User.get(str(fr['id'])): reply('Registrierung ist geschlossen.') return # COMMANDS if text.startswith('/'): if UserController.isAdmin(str(fr['id'])): if text == '/start': reply(u'Bot enabled') setSetting('enabled', 'True') return if text == '/stop': reply(u'Bot disabled') setSetting('enabled','False') return if text == '/enableUserRegistration': reply(u'User registration enabled') setSetting('userRegistration', 'True') return if text == '/disableUserRegistration': reply(u'User registration disabled') setSetting('userRegistration', 'False') return if text.lower() == '/listusers': allUsers = Entity.User.getAll() answer = u'' for user in allUsers: answer = answer + user.firstName + u'\t' + user.chatID + u'\n' reply(answer) return if text.startswith('/deleteUser '): if UserController.deleteUser(str(fr['id']) , text.split()[1]): reply(u'User ' + text.split()[1] + u' deleted.') return if text.startswith('/setAdmin'): split = text.split() if len(split) == 3: if UserController.setAdmin(str(fr['id']), split[1], (split[2] == 'True')): reply(u'User ' + split[1] + u' admin set to ' + split[2]) return else: logging.warn("Set Admin request by " + str(fr['id']) + " not successful.") # END COMMANDS if getSetting('enabled') != 'True': logging.info('Bot is disabled') return # For dev if '/setName' in text: split = text.split() senderFirstName = split[split.index('/setName')+1] del split[split.index('/setName')+1] split.remove('/setName') text = ' '.join(split) message['text']=text message['from']['first_name']=senderFirstName if '/setUser' in text: split = text.split() senderID = split[split.index('/setUser')+1] del split[split.index('/setUser')+1] split.remove('/setUser') text = ' '.join(split) message['text']=text message['from']['id']=senderID Responder.respondTo(message, chat_id)
def post(self): urlfetch.set_default_fetch_deadline(60) body = json.loads(self.request.body) logging.info('request body:') logging.info(body) self.response.write(json.dumps(body)) update_id = body['update_id'] message = body['message'] message_id = message.get('message_id') date = message.get('date') text = message.get('text') fr = message.get('from') chat = message['chat'] chat_id = chat['id'] if not text: logging.info('no text') return def reply(msg=None): if msg: resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({ 'chat_id': str(chat_id), 'text': msg.encode("utf-8"), 'disable_web_page_preview': 'true', })).read() else: logging.error('no msg specified') resp = None logging.info('send response:') logging.info(resp) lastUpdate = getCurrentUpdate(chat_id) if lastUpdate >= update_id: logging.info('Already at update ' + str(lastUpdate)) return else: setCurrentUpdate(chat_id, update_id) if getSetting('userRegistration') != 'True' and not Entity.User.get(str(fr['id'])): reply('Registrierung ist geschlossen.') return # COMMANDS if text.startswith('/'): if UserController.isAdmin(str(fr['id'])): if text == '/start': reply(u'Bot enabled') setSetting('enabled', 'True') return if text == '/stop': reply(u'Bot disabled') setEnabled('enabled','False') return if text == '/enableUserRegistration': reply(u'User registration enabled') setSetting('userRegistration', 'True') return if text == '/disableUserRegistration': reply(u'User registration disabled') setSetting('userRegistration', 'False') return if text.lower() == '/listusers': allUsers = Entity.User.getAll() answer = u'' for user in allUsers: answer = answer + user.firstName + u'\t' + user.chatID + u'\n' reply(answer) return if text.startswith('/deleteUser '): if UserController.deleteUser(str(fr['id']) , text.split()[1]): reply(u'User ' + text.split()[1] + u' deleted.') return if text.startswith('/setAdmin'): split = text.split() if len(split) == 3: if UserController.setAdmin(str(fr['id']), split[1], (split[2] == 'True')): reply(u'User ' + split[1] + u' admin set to ' + split[2]) return else: logging.warn("Set Admin request by " + str(fr['id']) + " not successful.") # END COMMANDS if getSetting('enabled') != 'True': logging.info('Bot is disabled') return # For dev if '/setName' in text: split = text.split() senderFirstName = split[split.index('/setName')+1] del split[split.index('/setName')+1] split.remove('/setName') text = ' '.join(split) message['text']=text message['from']['first_name']=senderFirstName if '/setUser' in text: split = text.split() senderID = split[split.index('/setUser')+1] del split[split.index('/setUser')+1] split.remove('/setUser') text = ' '.join(split) message['text']=text message['from']['id']=senderID # CUSTOMIZE FROM HERE rep = Responder.respondTo(message, chat_id) if rep: reply(rep)