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
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
os.system('kill %d' % os.getpid())
does also not work
hi, speaking in the group gave me the solution,
you just need to make a function like
def shutdown():
updater.stop()
updater.is_idle = False
and call it in a new thread in the function that is called when the shutdown command is sent, like:
def stop(bot, update):
threading.Thread(target=shutdown).start()
updater.dispatcher.add_handler(CommandHandler('stop', stop))
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
hi, speaking in the group gave me the solution,
you just need to make a function likedef shutdown(): updater.stop() updater.is_idle = Falseand call it in a new thread in the function that is called when the shutdown command is sent, like:
def stop(bot, update): threading.Thread(target=shutdown).start()
updater.dispatcher.add_handler(CommandHandler('stop', stop))
just to add to your correct answer, remember to import threading on the module:
( just add "import threading" at the top! )
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:
os.kill(os.getpid(), signal.SIGINT)
You can call it in any callback without creating of extra thread.
Most helpful comment
hi, speaking in the group gave me the solution,
you just need to make a function like
and call it in a new thread in the function that is called when the shutdown command is sent, like:
updater.dispatcher.add_handler(CommandHandler('stop', stop))