我对电报机器人有两难选择。假设我必须创建一个函数,该函数将向每个连接到该机器人的用户每周/每月一次询问一个问题:
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] 删除。
我来说两句