1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #1389 from coder2020official/master

Polling is now asynchronous, and some readme fixes.
This commit is contained in:
Badiboy 2021-12-12 13:28:57 +03:00 committed by GitHub
commit 373ee4b45b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 53 additions and 25 deletions

View File

@ -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 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) * [How can I handle reocurring ConnectionResetErrors?](#how-can-i-handle-reocurring-connectionreseterrors)
* [The Telegram Chat Group](#the-telegram-chat-group) * [The Telegram Chat Group](#the-telegram-chat-group)
* [Telegram Channel](#telegram-channel)
* [More examples](#more-examples) * [More examples](#more-examples)
* [Bots using this API](#bots-using-this-api) * [Bots using this API](#bots-using-this-api)
@ -781,6 +782,10 @@ Get help. Discuss. Chat.
* Join the [pyTelegramBotAPI Telegram Chat Group](https://telegram.me/joinchat/Bn4ixj84FIZVkwhk2jag6A) * 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 ## More examples
* [Echo Bot](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/echo_bot.py) * [Echo Bot](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/echo_bot.py)

View File

@ -84,4 +84,5 @@ async def back_callback(call: types.CallbackQuery):
bot.add_custom_filter(ProductsCallbackFilter()) bot.add_custom_filter(ProductsCallbackFilter())
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -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.send_message(message.chat.id, 'I accepted a new user!')
await bot.approve_chat_join_request(message.chat.id, message.from_user.id) await bot.approve_chat_join_request(message.chat.id, message.from_user.id)
bot.polling(skip_pending=True) import asyncio
asyncio.run(bot.polling())

View File

@ -30,4 +30,5 @@ async def my_chat_m(message: types.ChatMemberUpdated):
@bot.message_handler(content_types=util.content_type_service) @bot.message_handler(content_types=util.content_type_service)
async def delall(message: types.Message): async def delall(message: types.Message):
await bot.delete_message(message.chat.id,message.message_id) await bot.delete_message(message.chat.id,message.message_id)
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -9,4 +9,6 @@ async def answer_for_admin(message):
# Register filter # Register filter
bot.add_custom_filter(asyncio_filters.IsAdminFilter(bot)) bot.add_custom_filter(asyncio_filters.IsAdminFilter(bot))
bot.polling()
import asyncio
asyncio.run(bot.polling())

View File

@ -40,4 +40,5 @@ async def bye_user(message):
bot.add_custom_filter(MainFilter()) bot.add_custom_filter(MainFilter())
bot.add_custom_filter(IsAdmin()) bot.add_custom_filter(IsAdmin())
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -14,4 +14,5 @@ async def not_admin(message):
# Do not forget to register # Do not forget to register
bot.add_custom_filter(telebot.asyncio_filters.ChatFilter()) bot.add_custom_filter(telebot.asyncio_filters.ChatFilter())
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -19,4 +19,5 @@ async def text_filter(message):
bot.add_custom_filter(telebot.asyncio_filters.IsReplyFilter()) bot.add_custom_filter(telebot.asyncio_filters.IsReplyFilter())
bot.add_custom_filter(telebot.asyncio_filters.ForwardFilter()) bot.add_custom_filter(telebot.asyncio_filters.ForwardFilter())
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -17,4 +17,5 @@ async def text_filter(message):
bot.add_custom_filter(telebot.asyncio_filters.TextMatchFilter()) bot.add_custom_filter(telebot.asyncio_filters.TextMatchFilter())
bot.add_custom_filter(telebot.asyncio_filters.TextStartsFilter()) bot.add_custom_filter(telebot.asyncio_filters.TextStartsFilter())
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -71,4 +71,5 @@ bot.add_custom_filter(asyncio_filters.IsDigitFilter())
# set saving states into file. # set saving states into file.
bot.enable_saving_states() # you can delete this if you do not need to save states bot.enable_saving_states() # you can delete this if you do not need to save states
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -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='<i>Done!</i>', parse_mode='HTML') await bot.edit_message_text(chat_id=message.chat.id, message_id=result_message.id, text='<i>Done!</i>', parse_mode='HTML')
bot.polling(skip_pending=True) import asyncio
asyncio.run(bot.polling())

View File

@ -23,4 +23,5 @@ async def echo_message(message):
await bot.reply_to(message, message.text) await bot.reply_to(message, message.text)
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -24,4 +24,5 @@ async def photo_send(message: telebot.types.Message):
bot.polling(skip_pending=True) import asyncio
asyncio.run(bot.polling())

View File

@ -36,4 +36,5 @@ bot.setup_middleware(SimpleMiddleware(2))
async def start(message): async def start(message):
await bot.send_message(message.chat.id, 'Hello!') await bot.send_message(message.chat.id, 'Hello!')
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -45,4 +45,5 @@ async def start(message, data: dict):
await bot.send_message(message.chat.id, data['response']) await bot.send_message(message.chat.id, data['response'])
bot.polling() import asyncio
asyncio.run(bot.polling())

View File

@ -15,4 +15,6 @@ bot.register_message_handler(start_executor, commands=['start']) # Start command
# bot.register_edited_message_handler(*args, **kwargs) # bot.register_edited_message_handler(*args, **kwargs)
# And other functions.. # And other functions..
bot.polling(skip_pending=True)
import asyncio
asyncio.run(bot.polling())

View File

@ -24,4 +24,6 @@ async def photos_send(message: telebot.types.Message):
bot.polling(skip_pending=True)
import asyncio
asyncio.run(bot.polling())

View File

@ -10,4 +10,6 @@ async def send_welcome(message):
async def echo_all(message): async def echo_all(message):
await bot.reply_to(message, message.text) 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

View File

@ -11,4 +11,6 @@ async def update_listener(messages):
bot.set_update_listener(update_listener) bot.set_update_listener(update_listener)
bot.polling()
import asyncio
asyncio.run(bot.polling())

View File

@ -211,7 +211,7 @@ class AsyncTeleBot:
json_updates = await asyncio_helper.get_updates(self.token, offset, limit, timeout, allowed_updates, request_timeout) 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] 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, request_timeout: int=20, allowed_updates: Optional[List[str]]=None,
none_stop: Optional[bool]=None): none_stop: Optional[bool]=None):
""" """
@ -240,10 +240,10 @@ class AsyncTeleBot:
non_stop = none_stop non_stop = none_stop
if skip_pending: if skip_pending:
asyncio.run(self.skip_updates()) await self.skip_updates()
asyncio.run(self._process_polling(non_stop, interval, timeout, request_timeout, allowed_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): allowed_updates: Optional[List[str]]=None, *args, **kwargs):
""" """
Wrap polling with infinite loop and exception handling to avoid bot stops polling. 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. so unwanted updates may be received for a short period of time.
""" """
if skip_pending: if skip_pending:
asyncio.run(self.skip_updates()) await self.skip_updates()
self._polling = True self._polling = True
while self._polling: while self._polling:
try: try:
asyncio.run( self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout, await self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout,
allowed_updates=allowed_updates, *args, **kwargs) ) allowed_updates=allowed_updates, *args, **kwargs)
except Exception as e: except Exception as e:
if logger_level and logger_level >= logging.ERROR: if logger_level and logger_level >= logging.ERROR:
logger.error("Infinity polling exception: %s", str(e)) logger.error("Infinity polling exception: %s", str(e))