def process_update(self, update, session=None): self.logger.debug('Started processing the update ' + str(update)) if 'message' in update: msg = update['message'] chat_id = msg['chat']['id'] chat_data = session.query(Chat).filter(Chat.id == chat_id).first() if chat_data is None: self.logger.debug('Unknown chat. Registering...') chat_data = Chat(id=chat_id) session.add(chat_data) session.commit() self.logger.debug('Commited') # Ignore pin updates if 'pinned_message' in msg: pass # /cancel elif 'text' in msg and self.command_check( chat_id, msg['text']) and msg['text'].startswith('/cancel'): self.set_handler(chat_id, None) self.send_message(chat_id, _('main.cancelled')) # Answer to previously requested command elif chat_id in self.active_calls: call = self.active_calls[chat_id] self.logger.debug('%s has assigned handler %s.%s' % (chat_id, call['module'], call['method'])) if call['module'] not in sys.modules: # TODO Send a message that says that the command is not available self.logger.warning('%s handler module is unavailable' % (chat_id)) del self.active_calls[chat_id] return call_module = sys.modules[call['module']] try: method = getattr(call_module, call['method']) except AttributeError as e: self.logger.warning( '%s handler module \'%s\' has no method \'%s\'!', (chat_id, call['module'], call['method'])) del self.active_calls[chat_id] return if method: args = [] kwargs = {} if 'args' in call and isinstance(call['args'], type(list)): args = call['args'] if 'kwargs' in call and isinstance(call['kwargs'], type(dict)): kwargs = call['kwargs'] method(self, msg, *self.active_calls[chat_id]['args'], **kwargs) # New command request elif 'text' in msg and self.command_check(chat_id, msg['text']): self.logger.debug('%s requested a command %s' % (chat_id, msg['text'])) self.execute_command(msg) # The bot was added to a new chat elif 'bot_added' in msg: chat_data.added = msg['from']['id'] self.execute_command(msg, command='/start') self.logger.info('New chat: ' + str(chat_id) + ' ' + msg['chat']['title']) # The bot was kicked elif 'bot_kicked' in msg: chat_data.active = False self.logger.info('Kicked from chat: ' + str(chat_id) + ' ' + msg['chat']['title']) # Chat was transformed to a supergroup elif 'migrate_to_chat_id' in msg: session.delete( session.query(Chat).get(msg['migrate_to_chat_id'])) chat_data.id = msg['migrate_to_chat_id'] session.commit()