Piotr F
Piotr F

Reputation: 55

Python Telegram Bot Job Queue

I am trying to setup a bot that sends messages every hour, I tried everything I found in google and here in stack overflow, but can't seem to make job queue work. Here is my code, I pretty much took it from official python-telegram-bot git hub. Here is the code, any help is highly appreciated.

from telegram.ext import Updater

u = Updater('bot_token', use_context=True)
j = u.job_queue

def callback_minute(context):
    context.bot.send_message(chat_id='my_chat_id', text='One message every minute')

job_minute = j.run_repeating(callback_minute, interval=10)
u.start_polling()
u.idle()

This is traceback I am getting

Error submitting job "callback_minute (trigger: interval[0:00:10], next run at: 2021-09-06 08:50:47 UTC)" to executor "default"
Traceback (most recent call last):
  File "C:\Users\aeriu\AppData\Local\Programs\Python\Python39\lib\site-packages\apscheduler\schedulers\base.py", line 974, in _process_jobs
    executor.submit_job(job, run_times)
  File "C:\Users\aeriu\AppData\Local\Programs\Python\Python39\lib\site-packages\apscheduler\executors\base.py", line 71, in submit_job
    self._do_submit_job(job, run_times)
  File "C:\Users\aeriu\AppData\Local\Programs\Python\Python39\lib\site-packages\apscheduler\executors\pool.py", line 22, in _do_submit_job
    f = self._pool.submit(run_job, job, job._jobstore_alias, run_times, self._logger.name)
  File "C:\Users\aeriu\AppData\Local\Programs\Python\Python39\lib\concurrent\futures\thread.py", line 163, in submit
    raise RuntimeError('cannot schedule new futures after '
RuntimeError: cannot schedule new futures after interpreter shutdown

Upvotes: 2

Views: 3193

Answers (1)

Ziur Olpa
Ziur Olpa

Reputation: 2112

This will do the trick, when you press start:

from telegram.ext import Updater, CommandHandler

updater = Updater('YOUR_BOT_TOKEN')

def callback_minute(context):
    context.bot.send_message(chat_id='my_chat_id', text='One message every minute')

def set_timer(update,context):
    due= 10  
    chat_id = update.message.chat_id
    context.job_queue.run_repeating(self.callback_minute, due, context=chat_id, name=str(chat_id))

dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", self.set_timer))
updater.start_polling()
updater.idle()

Upvotes: 4

Related Questions