-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The need for stop_polling in async version #2386
Comments
u close session first then stop polling |
To gracefully stop polling when using import asyncio
from telebot.async_telebot import AsyncTeleBot
API_TOKEN = 'YOUR_BOT_API_TOKEN'
bot = AsyncTeleBot(API_TOKEN)
# Start polling
async def start_polling():
await bot.polling(non_stop=True)
# Gracefully stop polling
async def stop_polling():
await bot.stop_polling() # This will stop the polling process
await bot.close_session() # Close the bot's aiohttp session
async def main():
try:
await start_polling()
except (KeyboardInterrupt, SystemExit):
print("Gracefully stopping the bot...")
await stop_polling()
if __name__ == '__main__':
asyncio.run(main()) |
Thank you @coder2020official and @FaridMalekpour for your answers. But, sorry, maybe I don't understand something, but there's literally no such method as In [7]: bot = AsyncTeleBot(token)
In [9]: bot.stop_polling()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In [9], line 1
----> 1 bot.stop_polling()
AttributeError: 'AsyncTeleBot' object has no attribute 'stop_polling' |
Hi, yes, I am currently working on this, hence this issue is still open. Will notify you when PR is ready |
You may try my PR version out if you want. |
I tried you patch and it worked for me like a charm. I tried both the way @FaridMalekpour suggested and from inside the @self.bot.message_handler(commands=['stop_bot'])
async def handle_stop_bot(message: Message):
if message.from_user.id == self.admin:
await self.bot.send_message(
message.chat.id,
"Bot is now shutting down gracefully."
)
await self.bot.stop_polling() |
4.22.0
Gnu/Linux
3.10.7
I see how to gracefully stop polling using
TeleBot
class (#2098), but how to properly stop bot/polling usingAsyncTeleBot
?I tried
AsyncTeleBot.close_session
, but it gives an error.I can catch it and handle myself, but maybe there's a better way?
The text was updated successfully, but these errors were encountered: