Example #1
0
    async def process_user_updates(self, user,
                                   update_list: List[TelegramUpdate]):
        start_ts = time.time()
        self.logger.debug(
            'Start update list processing (user_id = {0})'.format(user.id))
        for update in update_list:
            try:
                for handler in self.__handlers[update.request_type]:
                    await handler.execute(user, update)

                text = '✅ {0} saved ({1:.2} s)'.format(
                    update.request_type.capitalize(),
                    time.time() - start_ts)
                asyncio.ensure_future(
                    self._telegram_api.editMessageText(
                        user.telegram_chat_id, update.status_message_id, text))
            except TokenExpired:
                asyncio.ensure_future(
                    self.edit_telegram_message(
                        user.telegram_chat_id, update.status_message_id,
                        '⛔️ Evernote access token is expired. Send /start to get new token'
                    ))
            except Exception as e:
                self.logger.error(e, exc_info=1)
                FailedUpdate.create(error=traceback.format_exc(),
                                    **update.save_data())
                asyncio.ensure_future(
                    self.edit_telegram_message(
                        user.telegram_chat_id, update.status_message_id,
                        '❌ Something went wrong. Please, try again'))

        self.logger.debug('Cleaning up...')
        for update in update_list:
            for handler in self.__handlers[update.request_type]:
                await handler.cleanup(user, update)

        self.logger.debug(
            'Done. (user_id = {0}). Processing takes {1} s'.format(
                user.id,
                time.time() - start_ts))
 async def handle_request(self, user: User, request_type: str, message: Message):
     chat_id = user.telegram_chat_id
     handler = self.handlers[request_type]
     reply = await self.async_send_message(chat_id, '🔄 Accepted')
     status_message_id = reply['message_id']
     try:
         await handler.execute(
             user,
             status_message_id=status_message_id,
             request_type=request_type,
             message=message
         )
     except TelegramBotError as e:
         FailedUpdate.create(
             user_id=user.id,
             request_type=request_type,
             status_message_id=status_message_id,
             message=message.raw,
             error=traceback.format_exc()
         )
         await self.api.editMessageText(chat_id, status_message_id, e.message, e.reply_markup)
         raise e