I need to take chat_id in repeating task to send users some repeating but personalized messages. How can I take chat_id in repeating func?
def repeating(context):
context.bot.send_message(chat_id=<CHAT_ID>,text=res["message"])
def main():
updater = Updater(constants.BOT_API_KEY, use_context= True)
job_queue = JobQueue()
dp = updater.dispatcher
job_queue.set_dispatcher(dp)
job_queue.run_repeating(callback=repeating,interval=5)
updater.start_polling()
job_queue.start()
updater.idle()
main()
2
Answers
For me, the easiest way is to store those values in dict.
In code it should look like this:
The methods
JobQueue.run_*
have a parameter calledcontext
that can be used to pass information to the callback. In the callback, the contents of this parameter are available ascontext.job.context
. Please have a look atrun_repeating
JobQueue
where the latter two showcase how to use the
context
parameter.Also note that you should not manually instantiate a
JobQueue
– useupdater.job_queue
instead. Otherwise you’ll also have to take care of actually starting and shutting down theJobQueue
.Disclaimer: I’m currently the maintainer of
python-telegram-bot
.