From e92946301f3da31b474604e5dc4b0f191218844c Mon Sep 17 00:00:00 2001 From: _run Date: Sun, 12 Dec 2021 15:07:30 +0500 Subject: [PATCH 1/3] Asyncio.run back --- .../asynchronous_telebot/CallbackData_example.py | 3 ++- examples/asynchronous_telebot/chat_join_request.py | 3 ++- .../asynchronous_telebot/chat_member_example.py | 3 ++- .../custom_filters/admin_filter_example.py | 6 ++++-- .../custom_filters/general_custom_filters.py | 3 ++- .../custom_filters/id_filter_example.py | 3 ++- .../custom_filters/is_filter_example.py | 3 ++- .../custom_filters/text_filter_example.py | 3 ++- examples/asynchronous_telebot/custom_states.py | 3 ++- .../asynchronous_telebot/download_file_example.py | 3 ++- examples/asynchronous_telebot/echo_bot.py | 3 ++- examples/asynchronous_telebot/exception_handler.py | 3 ++- .../middleware/flooding_middleware.py | 3 ++- examples/asynchronous_telebot/middleware/i18n.py | 3 ++- examples/asynchronous_telebot/register_handler.py | 4 +++- examples/asynchronous_telebot/send_file_example.py | 4 +++- .../asynchronous_telebot/skip_updates_example.py | 4 +++- examples/asynchronous_telebot/update_listener.py | 4 +++- telebot/async_telebot.py | 14 +++++++------- 19 files changed, 49 insertions(+), 26 deletions(-) diff --git a/examples/asynchronous_telebot/CallbackData_example.py b/examples/asynchronous_telebot/CallbackData_example.py index 0386bec..fad37c1 100644 --- a/examples/asynchronous_telebot/CallbackData_example.py +++ b/examples/asynchronous_telebot/CallbackData_example.py @@ -84,4 +84,5 @@ async def back_callback(call: types.CallbackQuery): bot.add_custom_filter(ProductsCallbackFilter()) -bot.polling() +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/chat_join_request.py b/examples/asynchronous_telebot/chat_join_request.py index 5262ebd..85bf8c3 100644 --- a/examples/asynchronous_telebot/chat_join_request.py +++ b/examples/asynchronous_telebot/chat_join_request.py @@ -8,4 +8,5 @@ async def make_some(message: telebot.types.ChatJoinRequest): await bot.send_message(message.chat.id, 'I accepted a new user!') await bot.approve_chat_join_request(message.chat.id, message.from_user.id) -bot.polling(skip_pending=True) \ No newline at end of file +import asyncio +asyncio.run(bot.polling()) \ No newline at end of file diff --git a/examples/asynchronous_telebot/chat_member_example.py b/examples/asynchronous_telebot/chat_member_example.py index 4d90036..30f47e2 100644 --- a/examples/asynchronous_telebot/chat_member_example.py +++ b/examples/asynchronous_telebot/chat_member_example.py @@ -30,4 +30,5 @@ async def my_chat_m(message: types.ChatMemberUpdated): @bot.message_handler(content_types=util.content_type_service) async def delall(message: types.Message): await bot.delete_message(message.chat.id,message.message_id) -bot.polling() +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/custom_filters/admin_filter_example.py b/examples/asynchronous_telebot/custom_filters/admin_filter_example.py index 5a508c4..8d3b2b5 100644 --- a/examples/asynchronous_telebot/custom_filters/admin_filter_example.py +++ b/examples/asynchronous_telebot/custom_filters/admin_filter_example.py @@ -1,6 +1,6 @@ from telebot.async_telebot import AsyncTeleBot from telebot import asyncio_filters -bot = AsyncTeleBot('TOKEN') +bot = AsyncTeleBot('1297441208:AAGiez5xhzai5russPtPKmZjbdjybW4T0U8') # Handler @bot.message_handler(chat_types=['supergroup'], is_chat_admin=True) @@ -9,4 +9,6 @@ async def answer_for_admin(message): # Register filter bot.add_custom_filter(asyncio_filters.IsAdminFilter(bot)) -bot.polling() + +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/custom_filters/general_custom_filters.py b/examples/asynchronous_telebot/custom_filters/general_custom_filters.py index 1b36beb..dfeeb88 100644 --- a/examples/asynchronous_telebot/custom_filters/general_custom_filters.py +++ b/examples/asynchronous_telebot/custom_filters/general_custom_filters.py @@ -40,4 +40,5 @@ async def bye_user(message): bot.add_custom_filter(MainFilter()) bot.add_custom_filter(IsAdmin()) -bot.polling() +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/custom_filters/id_filter_example.py b/examples/asynchronous_telebot/custom_filters/id_filter_example.py index 5a07963..85e909b 100644 --- a/examples/asynchronous_telebot/custom_filters/id_filter_example.py +++ b/examples/asynchronous_telebot/custom_filters/id_filter_example.py @@ -14,4 +14,5 @@ async def not_admin(message): # Do not forget to register bot.add_custom_filter(telebot.asyncio_filters.ChatFilter()) -bot.polling() +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/custom_filters/is_filter_example.py b/examples/asynchronous_telebot/custom_filters/is_filter_example.py index 961fd0f..46a7988 100644 --- a/examples/asynchronous_telebot/custom_filters/is_filter_example.py +++ b/examples/asynchronous_telebot/custom_filters/is_filter_example.py @@ -19,4 +19,5 @@ async def text_filter(message): bot.add_custom_filter(telebot.asyncio_filters.IsReplyFilter()) bot.add_custom_filter(telebot.asyncio_filters.ForwardFilter()) -bot.polling() +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/custom_filters/text_filter_example.py b/examples/asynchronous_telebot/custom_filters/text_filter_example.py index 84aaee9..e2b7564 100644 --- a/examples/asynchronous_telebot/custom_filters/text_filter_example.py +++ b/examples/asynchronous_telebot/custom_filters/text_filter_example.py @@ -17,4 +17,5 @@ async def text_filter(message): bot.add_custom_filter(telebot.asyncio_filters.TextMatchFilter()) bot.add_custom_filter(telebot.asyncio_filters.TextStartsFilter()) -bot.polling() +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/custom_states.py b/examples/asynchronous_telebot/custom_states.py index 36132d8..56546bc 100644 --- a/examples/asynchronous_telebot/custom_states.py +++ b/examples/asynchronous_telebot/custom_states.py @@ -71,4 +71,5 @@ bot.add_custom_filter(asyncio_filters.IsDigitFilter()) # set saving states into file. bot.enable_saving_states() # you can delete this if you do not need to save states -bot.polling() \ No newline at end of file +import asyncio +asyncio.run(bot.polling()) \ No newline at end of file diff --git a/examples/asynchronous_telebot/download_file_example.py b/examples/asynchronous_telebot/download_file_example.py index 5105d9d..6952586 100644 --- a/examples/asynchronous_telebot/download_file_example.py +++ b/examples/asynchronous_telebot/download_file_example.py @@ -17,4 +17,5 @@ async def new_message(message: telebot.types.Message): await bot.edit_message_text(chat_id=message.chat.id, message_id=result_message.id, text='Done!', parse_mode='HTML') -bot.polling(skip_pending=True) +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/echo_bot.py b/examples/asynchronous_telebot/echo_bot.py index 940aecc..cd23537 100644 --- a/examples/asynchronous_telebot/echo_bot.py +++ b/examples/asynchronous_telebot/echo_bot.py @@ -23,4 +23,5 @@ async def echo_message(message): await bot.reply_to(message, message.text) -bot.polling() +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/exception_handler.py b/examples/asynchronous_telebot/exception_handler.py index f1da60f..b37f945 100644 --- a/examples/asynchronous_telebot/exception_handler.py +++ b/examples/asynchronous_telebot/exception_handler.py @@ -24,4 +24,5 @@ async def photo_send(message: telebot.types.Message): -bot.polling(skip_pending=True) +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/middleware/flooding_middleware.py b/examples/asynchronous_telebot/middleware/flooding_middleware.py index de70702..4a2d37b 100644 --- a/examples/asynchronous_telebot/middleware/flooding_middleware.py +++ b/examples/asynchronous_telebot/middleware/flooding_middleware.py @@ -36,4 +36,5 @@ bot.setup_middleware(SimpleMiddleware(2)) async def start(message): await bot.send_message(message.chat.id, 'Hello!') -bot.polling() \ No newline at end of file +import asyncio +asyncio.run(bot.polling()) \ No newline at end of file diff --git a/examples/asynchronous_telebot/middleware/i18n.py b/examples/asynchronous_telebot/middleware/i18n.py index 3c3196e..81281bc 100644 --- a/examples/asynchronous_telebot/middleware/i18n.py +++ b/examples/asynchronous_telebot/middleware/i18n.py @@ -45,4 +45,5 @@ async def start(message, data: dict): await bot.send_message(message.chat.id, data['response']) -bot.polling() +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/register_handler.py b/examples/asynchronous_telebot/register_handler.py index 76d194d..f78c5d3 100644 --- a/examples/asynchronous_telebot/register_handler.py +++ b/examples/asynchronous_telebot/register_handler.py @@ -15,4 +15,6 @@ bot.register_message_handler(start_executor, commands=['start']) # Start command # bot.register_edited_message_handler(*args, **kwargs) # And other functions.. -bot.polling(skip_pending=True) + +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/send_file_example.py b/examples/asynchronous_telebot/send_file_example.py index e67f8d8..974ae8e 100644 --- a/examples/asynchronous_telebot/send_file_example.py +++ b/examples/asynchronous_telebot/send_file_example.py @@ -24,4 +24,6 @@ async def photos_send(message: telebot.types.Message): -bot.polling(skip_pending=True) + +import asyncio +asyncio.run(bot.polling()) diff --git a/examples/asynchronous_telebot/skip_updates_example.py b/examples/asynchronous_telebot/skip_updates_example.py index c149cb2..89ff797 100644 --- a/examples/asynchronous_telebot/skip_updates_example.py +++ b/examples/asynchronous_telebot/skip_updates_example.py @@ -10,4 +10,6 @@ async def send_welcome(message): async def echo_all(message): await bot.reply_to(message, message.text) -bot.polling(skip_pending=True)# Skip pending skips old updates + +import asyncio +asyncio.run(bot.polling(skip_pending=True)) # to skip updates diff --git a/examples/asynchronous_telebot/update_listener.py b/examples/asynchronous_telebot/update_listener.py index 75488cf..5e41cc4 100644 --- a/examples/asynchronous_telebot/update_listener.py +++ b/examples/asynchronous_telebot/update_listener.py @@ -11,4 +11,6 @@ async def update_listener(messages): bot.set_update_listener(update_listener) -bot.polling() \ No newline at end of file + +import asyncio +asyncio.run(bot.polling()) \ No newline at end of file diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 3b8317d..8234b5f 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -211,7 +211,7 @@ class AsyncTeleBot: json_updates = await asyncio_helper.get_updates(self.token, offset, limit, timeout, allowed_updates, request_timeout) return [types.Update.de_json(ju) for ju in json_updates] - def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20, + async def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20, request_timeout: int=20, allowed_updates: Optional[List[str]]=None, none_stop: Optional[bool]=None): """ @@ -240,10 +240,10 @@ class AsyncTeleBot: non_stop = none_stop if skip_pending: - asyncio.run(self.skip_updates()) - asyncio.run(self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates)) + await self.skip_updates() + await self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates) - def infinity_polling(self, timeout: int=20, skip_pending: bool=False, request_timeout: int=20, logger_level=logging.ERROR, + async def infinity_polling(self, timeout: int=20, skip_pending: bool=False, request_timeout: int=20, logger_level=logging.ERROR, allowed_updates: Optional[List[str]]=None, *args, **kwargs): """ Wrap polling with infinite loop and exception handling to avoid bot stops polling. @@ -263,12 +263,12 @@ class AsyncTeleBot: so unwanted updates may be received for a short period of time. """ if skip_pending: - asyncio.run(self.skip_updates()) + await self.skip_updates() self._polling = True while self._polling: try: - asyncio.run( self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout, - allowed_updates=allowed_updates, *args, **kwargs) ) + await self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout, + allowed_updates=allowed_updates, *args, **kwargs) except Exception as e: if logger_level and logger_level >= logging.ERROR: logger.error("Infinity polling exception: %s", str(e)) From ed5b47cb962dd62f88618700a56cdf70aa7209c0 Mon Sep 17 00:00:00 2001 From: _run Date: Sun, 12 Dec 2021 15:11:42 +0500 Subject: [PATCH 2/3] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index b751585..23923a2 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ * [How can I distinguish a User and a GroupChat in message.chat?](#how-can-i-distinguish-a-user-and-a-groupchat-in-messagechat) * [How can I handle reocurring ConnectionResetErrors?](#how-can-i-handle-reocurring-connectionreseterrors) * [The Telegram Chat Group](#the-telegram-chat-group) + * [Telegram Channel](#telegram-channel) * [More examples](#more-examples) * [Bots using this API](#bots-using-this-api) @@ -780,7 +781,11 @@ Bot instances that were idle for a long time might be rejected by the server whe Get help. Discuss. Chat. * Join the [pyTelegramBotAPI Telegram Chat Group](https://telegram.me/joinchat/Bn4ixj84FIZVkwhk2jag6A) + +## Telegram Channel +Join the [News channel](https://t.me/pyTelegramBotAPI). Here we will post releases and updates. + ## More examples * [Echo Bot](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/echo_bot.py) From 096d58ae71bcde06a50fa857bee1a6ca460f7389 Mon Sep 17 00:00:00 2001 From: _run Date: Sun, 12 Dec 2021 15:13:07 +0500 Subject: [PATCH 3/3] Update admin_filter_example.py --- .../asynchronous_telebot/custom_filters/admin_filter_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/asynchronous_telebot/custom_filters/admin_filter_example.py b/examples/asynchronous_telebot/custom_filters/admin_filter_example.py index 8d3b2b5..7236e4b 100644 --- a/examples/asynchronous_telebot/custom_filters/admin_filter_example.py +++ b/examples/asynchronous_telebot/custom_filters/admin_filter_example.py @@ -1,6 +1,6 @@ from telebot.async_telebot import AsyncTeleBot from telebot import asyncio_filters -bot = AsyncTeleBot('1297441208:AAGiez5xhzai5russPtPKmZjbdjybW4T0U8') +bot = AsyncTeleBot('TOKEN') # Handler @bot.message_handler(chat_types=['supergroup'], is_chat_admin=True)