(python) Telegram bot-如何定期发送消息? [英] (python) Telegram bot- how to send messages periodically?

查看:42
本文介绍了(python) Telegram bot-如何定期发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于我的电报机器人的进退两难.假设我必须创建一个函数,它会询问每个连接到机器人的用户,每周/每月一次,一个问题:

I have a dilemma regarding my telegram bot. Let's say I have to create a function that will ask, every user connected to the bot, one time per week/month, a question:

def check_the_week(bot, update):
reply_keyboard = [['YES', 'NO']]
bot.send_message(
    chat_id=update.message.chat_id,
    text=report,

    reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))  # sends the total nr of hours
update.reply_text("Did you report all you working hour on freshdesk for this week?",
                  ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))

if update.message.text == "YES":
    update.message.reply_text(text="Are you sure?",
                              reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))

    # Asks confirmation
    if update.message.text == "YES":
        update.message.reply_text(text="Thank you for reporting your working hours in time!")

    elif update.message.text == "NO":
        update.message.reply_text(text="Please, check you time reports and add missing")

elif update.message.text == "NO":
    update.message.reply_text(text="Please, check you time reports and add missing")

我希望这个功能每周触发一次.我正在考虑使用 JobQueue.问题是,在这种情况下,函数应该有两个参数——bot AND job_queue,但没有更新:

I want this function to be triggered every week. I was thinking about using JobQueue. The problem is that in this case the function should have two parameters- bot AND job_queue, but no update:

def callback_30(bot, job):
    bot.send_message(chat_id='@examplechannel',
    text='A single message with 30s delay')

j.run_once(callback_30, 30)

如何在电报机器人中创建作业调度程序(或任何其他解决方案)以每周触发一次我的功能?附言请不要while True"+time.sleep() 解决方案.循环一直卡住,我试过了.

How can I create a Job Scheduler (or any other solution) in telegram bot to be trigger my function once a week? p.s. No "while True"+time.sleep() solutions please. The loop just stuck forever, I tried it.

推荐答案

在函数中定义作业时需要使用上下文参数.看这个例子:

You need to use the context parameter when defining the job in your function. Look at this example:

   from telegram.ext import Updater, CommandHandler, MessageHandler,    Filters, InlineQueryHandler


def sayhi(bot, job):
    job.context.message.reply_text("hi")

def time(bot, update,job_queue):
    job = job_queue.run_repeating(sayhi, 5, context=update)

def main():
    updater = Updater("BOT TOKEN")
    dp = updater.dispatcher
    dp.add_handler(MessageHandler(Filters.text , time,pass_job_queue=True))


    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

现在在您需要 update. 的回调函数中输入 job.context 代替.

Now in your call back function wherever you need update. type job.context instead.

这篇关于(python) Telegram bot-如何定期发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆