From 801ca1558e2400ffaccd84245f4edd5923aa1874 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sun, 19 Nov 2017 01:05:50 +0200 Subject: [PATCH] Upd examples. --- examples/check_user_language.py | 6 ++---- examples/echo_bot.py | 18 ++++++------------ examples/inline_bot.py | 9 ++++----- examples/proxy_and_emojize.py | 6 ++---- 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/examples/check_user_language.py b/examples/check_user_language.py index add535af..091e2d0f 100644 --- a/examples/check_user_language.py +++ b/examples/check_user_language.py @@ -8,6 +8,7 @@ import logging from aiogram import Bot, types from aiogram.dispatcher import Dispatcher from aiogram.types import ParseMode +from aiogram.utils.executor import start_pooling from aiogram.utils.markdown import * API_TOKEN = 'BOT TOKEN HERE' @@ -39,7 +40,4 @@ async def main(): if __name__ == '__main__': - try: - loop.run_until_complete(main()) - except KeyboardInterrupt: - loop.stop() + start_pooling(dp, loop=loop, skip_updates=True) diff --git a/examples/echo_bot.py b/examples/echo_bot.py index 2e8eba35..8452b8a5 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -3,6 +3,7 @@ import logging from aiogram import Bot, types from aiogram.dispatcher import Dispatcher +from aiogram.utils.executor import start_pooling API_TOKEN = 'BOT TOKEN HERE' @@ -30,18 +31,11 @@ async def echo(message: types.Message): await bot.send_message(message.chat.id, message.text) -async def main(): - count = await dp.skip_updates() - print(f"Skipped {count} updates.") - await dp.start_pooling() - - if __name__ == '__main__': - try: - loop.run_until_complete(main()) - except KeyboardInterrupt: - loop.stop() + start_pooling(dp, loop=loop, skip_updates=True) # Also you can use another execution method - # >>> from aiogram.utils.executor import start_pooling - # >>> start_pooling(dp, loop=loop, on_startup=main, on_shutdown=shutdown) + # >>> try: + # >>> loop.run_until_complete(main()) + # >>> except KeyboardInterrupt: + # >>> loop.stop() diff --git a/examples/inline_bot.py b/examples/inline_bot.py index e4cd7125..d60bfe66 100644 --- a/examples/inline_bot.py +++ b/examples/inline_bot.py @@ -3,6 +3,7 @@ import logging from aiogram import Bot, types from aiogram.dispatcher import Dispatcher +from aiogram.utils.executor import start_pooling API_TOKEN = 'BOT TOKEN HERE' @@ -15,12 +16,10 @@ dp = Dispatcher(bot) @dp.inline_handler() async def inline_echo(inline_query: types.InlineQuery): - item = types.InlineQueryResultArticle('1', 'echo', types.InputTextMessageContent(inline_query.query)) + item = types.InlineQueryResultArticle(id='1', title='echo', + input_message_content=types.InputTextMessageContent(inline_query.query)) await bot.answer_inline_query(inline_query.id, results=[item], cache_time=1) if __name__ == '__main__': - try: - loop.run_until_complete(dp.start_pooling()) - except KeyboardInterrupt: - loop.stop() + start_pooling(dp, loop=loop, skip_updates=True) diff --git a/examples/proxy_and_emojize.py b/examples/proxy_and_emojize.py index 60246384..362ecff5 100644 --- a/examples/proxy_and_emojize.py +++ b/examples/proxy_and_emojize.py @@ -7,6 +7,7 @@ from aiogram import Bot, types from aiogram.dispatcher import Dispatcher from aiogram.types import ParseMode from aiogram.utils.emoji import emojize +from aiogram.utils.executor import start_pooling from aiogram.utils.markdown import text, bold, italic, code # Configure bot here @@ -66,7 +67,4 @@ async def main(): if __name__ == '__main__': - try: - loop.run_until_complete(main()) - except KeyboardInterrupt: - loop.stop() + start_pooling(dp, loop=loop, skip_updates=True)