-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Question: How to shutdown properly? #801
Comments
How are you running your bot? Webhooks? Polling and updater.idle? |
I'm using Polling and updater.idle. Just for reference, here is the implementation |
What platform are you on? You didn't really provide the required info in the issue template. |
Sorry for not providing the info. I didnt see it as an issue but rather as an question. Im on an Linux root server |
Have you tried something like updater.stop()
os.exit() ? |
Yes. Doesnt work |
Something like
does also not work |
hi, speaking in the group gave me the solution,
and call it in a new thread in the function that is called when the shutdown command is sent, like:
|
Thank you! I can confirm that it works perfectly. This really helps me a lot. I would argue that this needs to be part of the documentation / wiki |
just to add to your correct answer, remember to import threading on the module: |
I just spent so much time trying to replicate this. so for people like me I am going to provide the full code here: from telegram.ext import Updater, InlineQueryHandler, CommandHandler
import requests
import re
import settings
import threading
def shutdown():
updater.stop()
updater.is_idle = False
def stop(bot, update):
threading.Thread(target=shutdown).start()
def main():
dp = updater.dispatcher
dp.add_handler(CommandHandler('stop', stop))
updater.bot.send_message(chat_id=settings.my_id, text="Here is a message from the bot")
updater.start_polling()
print("Bot is waiting for user input")
updater.idle()
print("Last row is reached")
updater = Updater(settings.AUTH_TOKEN)
if __name__ == '__main__':
main() where settings.py has AUTH_TOKEN='XXXXXXX:YYYYYYYYYYYYYYYYYYYYYYYYYYYYY' |
Calling updater.stop() and then setting updater.is_idle to False is most incorrect way to stop a bot because it will lose all persistence, see telegram.ext.updater.Updater.signal_handler method. The correct way would be to send usual fully supported by library signal: |
I really fear to bring up a question that was answered 1000 times now but since i searched the issues and didn't find anything related, examples didn't include anything relevant and i didn't get a satisfying answer in the telegram group, i have to ask:
How to shutdown the bot without executing another script with
os.execl(os.dirname(__file__) + '/shutdown.py', '--')
?Is this really the only way to shutdown? Seems weird (in my case) to create an empty script that is just there to server the purpose to shutdown. I don't want to dump anything on exit. I would really just like to shutdown cleanly and easily. I can't imagine that this is such a huge problem.
EDIT: Well, not even the provided example works. I didn't test it before because it's not an option for me
The text was updated successfully, but these errors were encountered: