(python)Telegram bot-如何定期发送消息?

易变的

我对电报机器人有两难选择。假设我必须创建一个函数,该函数将向每个连接到该机器人的用户每周/每月一次询问一个问题:

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,但没有更新:

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

j.run_once(callback_30, 30)

如何在电报bot中创建Job Scheduler(或任何其他解决方案)以每周一次触发我的功能?ps请没有“ while True” + time.sleep()解决方案。循环一直持续下去,我尝试过。

拉美

在函数中定义作业时,需要使用context参数。看这个例子:

   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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

python-telegram-bot - 发送消息

如何使用python-telegram-bot转发消息

如果发送了某些消息,如何运行代码(Telegram Bot)

Telegram Bot Telegraf:付款成功后如何发送消息

PHP Telegram Bot多次发送相同的消息

在 Telegram Messenger Bot 中发送消息

如何从事件循环外部(例如,从python-telegram-bot线程)发送带有discord.py的消息?

CallbackQueryHandler 回复消息而不是編輯消息 - Python Telegram Bot

Python Telegram Bot 错误

如何通过python-telegram-bot接收file_id?

如何在Heroku上设置python-telegram-bot webhook?

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

如何使用 python-telegram-bot 返回 InLineKeyboardButton

如何在python中为Telegram bot设置webhook?

如何处理和删除python-telegram-bot中的“加入群组”消息?

Telegram Bot 不回复消息

如何使用 python bot 将本地 html 文件发送到 Telegram

C# Telegram.Bot SendTextMessageAsync 不发送任何消息

如何设置Telegram Bot Webhook?

Python Bot按顺序发送消息

python-telegram-bot 內聯錯誤:未指定輸入消息內容

我怎麼知道一條消息是否被 python-telegram-bot 刪除了?

使用 python-telegram-bot 包向电报机器人发送语音命令

Telegram bot:如何使用嵌入式键盘发送消息并同时隐藏自定义键盘?

我正在用python3和python-telegram-bot编写一个python机器人,我希望它在发送消息之前显示“正在输入...”

Telegram Bot如何从频道或组中删除或删除消息或媒体

如何从特定消息的回复中获取信息 - Telegram Bot

Telegram bot:忽略纯文本消息

如何使用Telegram Bot API发送表情符号?