diff --git a/examples/asynchronous_telebot/chat_join_request.py b/examples/asynchronous_telebot/chat_join_request.py index 6b2bfb7..5262ebd 100644 --- a/examples/asynchronous_telebot/chat_join_request.py +++ b/examples/asynchronous_telebot/chat_join_request.py @@ -1,5 +1,5 @@ from telebot.async_telebot import AsyncTeleBot -import asyncio + import telebot bot = AsyncTeleBot('TOKEN') @@ -8,4 +8,4 @@ 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) -asyncio.run(bot.polling(skip_pending=True)) \ No newline at end of file +bot.polling(skip_pending=True) \ 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 7806cfd..4d90036 100644 --- a/examples/asynchronous_telebot/chat_member_example.py +++ b/examples/asynchronous_telebot/chat_member_example.py @@ -1,6 +1,6 @@ from telebot import types,util from telebot.async_telebot import AsyncTeleBot -import asyncio + bot = AsyncTeleBot('TOKEN') #chat_member_handler. When status changes, telegram gives update. check status from old_chat_member and new_chat_member. @@ -30,4 +30,4 @@ 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) -asyncio.run(bot.polling()) +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 3aee738..5a508c4 100644 --- a/examples/asynchronous_telebot/custom_filters/admin_filter_example.py +++ b/examples/asynchronous_telebot/custom_filters/admin_filter_example.py @@ -1,4 +1,3 @@ -import asyncio from telebot.async_telebot import AsyncTeleBot from telebot import asyncio_filters bot = AsyncTeleBot('TOKEN') @@ -10,4 +9,4 @@ async def answer_for_admin(message): # Register filter bot.add_custom_filter(asyncio_filters.IsAdminFilter(bot)) -asyncio.run(bot.polling()) +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 dfeeb88..1b36beb 100644 --- a/examples/asynchronous_telebot/custom_filters/general_custom_filters.py +++ b/examples/asynchronous_telebot/custom_filters/general_custom_filters.py @@ -40,5 +40,4 @@ async def bye_user(message): bot.add_custom_filter(MainFilter()) bot.add_custom_filter(IsAdmin()) -import asyncio -asyncio.run(bot.polling()) +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 5878bc7..5a07963 100644 --- a/examples/asynchronous_telebot/custom_filters/id_filter_example.py +++ b/examples/asynchronous_telebot/custom_filters/id_filter_example.py @@ -1,6 +1,5 @@ from telebot.async_telebot import AsyncTeleBot import telebot -import asyncio bot = AsyncTeleBot('TOKEN') @@ -15,5 +14,4 @@ async def not_admin(message): # Do not forget to register bot.add_custom_filter(telebot.asyncio_filters.ChatFilter()) - -asyncio.run(bot.polling()) +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 20857be..961fd0f 100644 --- a/examples/asynchronous_telebot/custom_filters/is_filter_example.py +++ b/examples/asynchronous_telebot/custom_filters/is_filter_example.py @@ -1,6 +1,6 @@ from telebot.async_telebot import AsyncTeleBot import telebot -import asyncio + bot = AsyncTeleBot('TOKEN') @@ -19,4 +19,4 @@ async def text_filter(message): bot.add_custom_filter(telebot.asyncio_filters.IsReplyFilter()) bot.add_custom_filter(telebot.asyncio_filters.ForwardFilter()) -asyncio.run(bot.polling()) +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 57513ea..84aaee9 100644 --- a/examples/asynchronous_telebot/custom_filters/text_filter_example.py +++ b/examples/asynchronous_telebot/custom_filters/text_filter_example.py @@ -1,6 +1,5 @@ from telebot.async_telebot import AsyncTeleBot import telebot -import asyncio bot = AsyncTeleBot('TOKEN') @@ -18,4 +17,4 @@ async def text_filter(message): bot.add_custom_filter(telebot.asyncio_filters.TextMatchFilter()) bot.add_custom_filter(telebot.asyncio_filters.TextStartsFilter()) -asyncio.run(bot.polling()) +bot.polling() diff --git a/examples/asynchronous_telebot/custom_states.py b/examples/asynchronous_telebot/custom_states.py index 2f0257a..36132d8 100644 --- a/examples/asynchronous_telebot/custom_states.py +++ b/examples/asynchronous_telebot/custom_states.py @@ -1,7 +1,6 @@ import telebot from telebot import asyncio_filters from telebot.async_telebot import AsyncTeleBot -import asyncio bot = AsyncTeleBot('TOKEN') @@ -72,4 +71,4 @@ 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 -asyncio.run(bot.polling()) \ No newline at end of file +bot.polling() \ No newline at end of file diff --git a/examples/asynchronous_telebot/echo_bot.py b/examples/asynchronous_telebot/echo_bot.py index 24cbe3f..940aecc 100644 --- a/examples/asynchronous_telebot/echo_bot.py +++ b/examples/asynchronous_telebot/echo_bot.py @@ -4,7 +4,6 @@ # It echoes any incoming text messages. from telebot.async_telebot import AsyncTeleBot -import asyncio bot = AsyncTeleBot('TOKEN') @@ -24,4 +23,4 @@ async def echo_message(message): await bot.reply_to(message, message.text) -asyncio.run(bot.polling()) +bot.polling() diff --git a/examples/asynchronous_telebot/register_handler.py b/examples/asynchronous_telebot/register_handler.py index 04dabd4..76d194d 100644 --- a/examples/asynchronous_telebot/register_handler.py +++ b/examples/asynchronous_telebot/register_handler.py @@ -1,5 +1,4 @@ from telebot.async_telebot import AsyncTeleBot -import asyncio bot = AsyncTeleBot('TOKEN') async def start_executor(message): @@ -16,4 +15,4 @@ bot.register_message_handler(start_executor, commands=['start']) # Start command # bot.register_edited_message_handler(*args, **kwargs) # And other functions.. -asyncio.run(bot.polling(skip_pending=True)) +bot.polling(skip_pending=True) diff --git a/examples/asynchronous_telebot/skip_updates_example.py b/examples/asynchronous_telebot/skip_updates_example.py index dc2c157..c149cb2 100644 --- a/examples/asynchronous_telebot/skip_updates_example.py +++ b/examples/asynchronous_telebot/skip_updates_example.py @@ -1,5 +1,5 @@ from telebot.async_telebot import AsyncTeleBot -import asyncio + bot = AsyncTeleBot('TOKEN') @bot.message_handler(commands=['start', 'help']) @@ -10,4 +10,4 @@ async def send_welcome(message): async def echo_all(message): await bot.reply_to(message, message.text) -asyncio.run(bot.polling(skip_pending=True))# Skip pending skips old updates +bot.polling(skip_pending=True)# Skip pending skips old updates diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 48b6487..9f24d90 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -138,7 +138,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] - async def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20, + 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): """ @@ -167,10 +167,10 @@ class AsyncTeleBot: non_stop = none_stop if skip_pending: - await self.skip_updates() - await self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates) + asyncio.run(self.skip_updates()) + asyncio.run(self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates)) - async def infinity_polling(self, timeout: int=20, skip_pending: bool=False, request_timeout: int=20, logger_level=logging.ERROR, + 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. @@ -190,12 +190,12 @@ class AsyncTeleBot: so unwanted updates may be received for a short period of time. """ if skip_pending: - await self.skip_updates() + asyncio.run(self.skip_updates()) self._polling = True while self._polling: try: - await self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout, - allowed_updates=allowed_updates, *args, **kwargs) + asyncio.run( 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))