def fetch_updates(self): self.logger.debug('Fetching telegram updates...') updates_by_user = {} try: fetched_updates = [] for entry in TelegramUpdate.find( {'in_process': { '$exists': False }}, [('created', 1)]): update = entry.update({'in_process': { '$exists': False }}, {'in_process': True}) fetched_updates.append(update) self.logger.debug('Fetched {0} updates'.format( len(fetched_updates))) for update in fetched_updates: user_id = update.user_id if not updates_by_user.get(user_id): updates_by_user[user_id] = [] updates_by_user[user_id].append(update) except Exception as e: err = "{0}\nCan't load telegram updates from mongo".format(e) self.logger.error(err, exc_info=1) return updates_by_user
async def test_process_user_updates(user): update = TelegramUpdate.create(user_id=user.id, request_type='text', status_message_id=2, message={ 'message_id': 1, 'date': datetime.datetime.now(), 'from': { 'id': user.id, 'username': '******' }, 'chat': { 'id': 123, 'type': 'private' }, 'text': 'test text' }, created=datetime.datetime( 2016, 9, 1, 12, 30, 4)) dealer = EvernoteDealer() handler = TextHandler() handler.evernote.create_note = AsyncMock() handler.evernote.update_note = AsyncMock() handler.telegram.editMessageText = AsyncMock() dealer.handlers['text'] = [handler] await dealer.process_user_updates(user, [update]) await asyncio.sleep(0.1) assert handler.evernote.update_note.call_count == 1 assert handler.telegram.editMessageText.call_count == 1
def test_fetch_updates(): TelegramUpdate.create(user_id=1, request_type='text', status_message_id=2, message={'data': 'ok'}, created=datetime.datetime(2016, 9, 1, 12, 30, 4)) TelegramUpdate.create(user_id=1, request_type='text', status_message_id=3, message={'data': 'woohoo'}, created=datetime.datetime(2016, 9, 1, 12, 30, 1)) TelegramUpdate.create(user_id=2, request_type='text', status_message_id=4, message={'data': 'yeah!'}, created=datetime.datetime(2016, 9, 1, 12, 30, 2)) dealer = EvernoteDealer() user_updates = dealer.fetch_updates() updates = user_updates[1] updates2 = user_updates[2] assert len(updates) == 2 assert len(updates2) == 1 assert updates[0].created < updates2[0].created < updates[1].created assert updates[0].status_message_id == 3 assert updates[1].status_message_id == 2 assert updates2[0].status_message_id == 4
async def list_updates(request): updates = [update.save_data() for update in TelegramUpdate.find()] params = {'queue': updates} response = aiohttp_jinja2.render_template('queue.html', request, params) return response
def on_message_sent(future_message): reply = future_message.result() TelegramUpdate.create(user_id=user.id, request_type=request_type, status_message_id=reply['message_id'], message=message.raw)
async def cleanup(self, user: User, update: TelegramUpdate): update.delete()