2

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
CallMeStag
  • 5,467
  • 1
  • 7
  • 22
Piotr F
  • 55
  • 6
  • Note that you'll have to start the job queue - which is done automatically, once you call `u.start_polling/webhook` - otherwise it won't process any jobs – CallMeStag Sep 06 '21 at 08:24
  • Tried that, it sends me an error ```Error submitting job "callback_minute (trigger: interval[0:00:10], next run at: 2021-09-06 08:34:21 UTC)" to executor "default"``` – Piotr F Sep 06 '21 at 08:35
  • Is that the complete traceback? which ptb version are you on? The snippet works fine for me … – CallMeStag Sep 06 '21 at 08:45
  • I am on version 13.7 and I edited main post to paste whole traceback ``` – Piotr F Sep 06 '21 at 08:49
  • That traceback can not be produced by the snippet that you posted. you probably called `job_queue.stop()` somewhere … Please show a proper minimal working example. There is a guide on MWEs specifically for PTB [here](https://telegra.ph/Minimal-Working-Example-for-PTB-07-18) – CallMeStag Sep 06 '21 at 08:56
  • I managed to get it to work, I pasted the code into google colab and it works there, but on my pc it gives the traceback that I pasted, I even removed and installed ptb again, but the result is still the same. – Piotr F Sep 06 '21 at 09:26

1 Answers1

4

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()
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ziur Olpa
  • 1,839
  • 1
  • 12
  • 27
  • thank you very much! This helped me a lot! However do you know how to stop the automatic message sender with another command like /stop? – bbfl May 08 '22 at 07:50
  • 1
    @bbfl now I'm not sure any more, is not fresh to me anymore, but i guess you need to add a handler to the dispatcher, with a function that makes contex.job_queue.stop() – Ziur Olpa May 09 '22 at 07:12