Python Telegram 机器人作业队列

标签 python python-telegram-bot job-queue

我正在尝试设置一个每小时发送消息的机器人,我尝试了在谷歌和堆栈溢出中找到的所有内容,但似乎无法使作业队列工作。这是我的代码,我几乎是从官方 python-telegram-bot git hub 获取的。这是代码,非常感谢任何帮助。

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()

这是我得到的回溯

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

最佳答案

当你按下开始键时,这就会成功:

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()

关于Python Telegram 机器人作业队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69071392/

相关文章:

python - Numpy 无法正确恢复维度(tofile 和 fromfile 调用)

python - 如何在 python 中执行 <xor> 例如enc_price = pad <xor> 价格

javascript - 聊天字段上的 Telegram Bot 键盘

Python Telegram Bot : Access to contact information

php - 什么构成作业队列中的重试?

node.js - 公牛队列 : Ensure unique job within time period by using partial timestamp in jobId

Python 覆盖率报告仅覆盖测试文件

python - sklearn 维度问题 "Found array with dim 3. Estimator expected <= 2"

Python Telegram Bot 的 `get_chat_members_count` 和避免洪水限制或如何使用包装器和装饰器

node.js - 使用 bull 进行作业处理微服务