首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python-telegram-bot中使用Jobqueue

如何在Python-telegram-bot中使用Jobqueue
EN

Stack Overflow用户
提问于 2018-09-28 21:56:05
回答 3查看 10.5K关注 0票数 4

我已经能够通过读取docs非常容易地制作一个机器人,但作业队列并不像它写的那样工作。run_daily方法使用datetime.time对象在特定时间发送消息,但此代码既不执行发送消息的工作,也不显示任何错误。它一直在运行

代码语言:javascript
复制
    import datetime
    from telegram import bot
    from telegram.ext import Updater
    def callback_minute(bot, job):
        bot.send_message(chat_id=475838704, text='PlEaSe wOrK!')

    def main():
        updater = Updater()
        bot = updater.bot
        job = updater.job_queue

        dispatcher = updater.dispatcher

        job.run_daily(callback_minute, time=datetime.time(6,33,00))

        updater.start_polling()
        updater.idle()

    if __name__ == '__main__':
        main()
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-21 03:58:45

也许这会有所帮助:

代码语言:javascript
复制
from telegram.ext import Updater, CommandHandler

def daily_job(bot, update, job_queue):
    """ Running on Mon, Tue, Wed, Thu, Fri = tuple(range(5)) """
    bot.send_message(chat_id=<YOUR CHAT ID>, text='Setting a daily notifications!')
    t = datetime.time(10, 00, 00, 000000)
    job_queue.run_daily(notify_assignees, t, days=tuple(range(5)), context=update)

def notify_assignees(bot, job):
    bot.send_message(chat_id=<CHAT ID>, text="Some text!")

updater = Updater(<BOT_TOKEN>)
updater.dispatcher.add_handler(CommandHandler('notify', daily_job, pass_job_queue=True))
updater.start_polling()

然后对机器人/notify

票数 5
EN

Stack Overflow用户

发布于 2020-01-06 01:47:18

python-telegram-botJobQueue Extention的简单使用示例

代码语言:javascript
复制
from telegram.ext import Updater, CommandHandler

def callback_alarm(bot, job):
    bot.send_message(chat_id=job.context, text='Wait for another 10 Seconds')

def callback_timer(bot, update, job_queue):
    bot.send_message(chat_id=update.message.chat_id,
                      text='Wait for 10 seconds')
    job_queue.run_repeating(callback_alarm, 10, context=update.message.chat_id)

def Stop_timer(bot, update, job_queue):
    bot.send_message(chat_id=update.message.chat_id,
                      text='Stopped!')
    job_queue.stop()

updater = Updater("YOUR_TOKEN")
updater.dispatcher.add_handler(CommandHandler('start', callback_timer, pass_job_queue=True))
updater.dispatcher.add_handler(CommandHandler('stop', Stop_timer, pass_job_queue=True))

updater.start_polling()

/start命令将启动JobQueue,并以5秒为间隔发送一条消息,队列可以通过/stop命令停止。

票数 2
EN

Stack Overflow用户

发布于 2021-01-23 02:54:54

使用以下代码:

代码语言:javascript
复制
from telegram.ext import CommandHandler, Updater

my_token = 'YOUR TOKEN'
updater = Updater(my_token, use_context=True)
job_queue = updater.job_queue

def send_message_job(context):
    context.bot.send_message(chat_id='@YOUR CHANELL ID',text='job executed')

job_queue.run_repeating(send_message_job,interval=10.0,first=0.0)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52556939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档