(python) Telegram bot-如何定期发送消息?
(python) Telegram bot- how to send messages periodically?
我对我的电报机器人感到进退两难。假设我必须创建一个函数,它会询问每个连接到机器人的用户,每个 week/month 一次,一个问题:
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)
我如何在电报机器人中创建一个作业计划程序(或任何其他解决方案)以每周触发一次我的功能?
p.s。请不要 "while True"+time.sleep() 解决方案。循环永远卡住了,我试过了。
尝试使用cron。您只需要将新的聊天 ID 存储在某个地方(文件、数据库等)并从您的脚本中读取它。
例如每天晚上 9 点 (21:00) 运行 send_messages.py
:
0 21 * * * python send_messages.py
在函数中定义作业时需要使用上下文参数。看这个例子:
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
。
要定期发送消息,您可以使用JobQueue Extention from python-telegram-bot
举个例子
from telegram.ext import Updater, CommandHandler
def callback_alarm(bot, job):
bot.send_message(chat_id=job.context, text='Alarm')
def callback_timer(bot, update, job_queue):
bot.send_message(chat_id=update.message.chat_id,
text='Starting!')
job_queue.run_repeating(callback_alarm, 5, context=update.message.chat_id)
def stop_timer(bot, update, job_queue):
bot.send_message(chat_id=update.message.chat_id,
text='Stoped!')
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
命令停止。
我对我的电报机器人感到进退两难。假设我必须创建一个函数,它会询问每个连接到机器人的用户,每个 week/month 一次,一个问题:
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)
我如何在电报机器人中创建一个作业计划程序(或任何其他解决方案)以每周触发一次我的功能? p.s。请不要 "while True"+time.sleep() 解决方案。循环永远卡住了,我试过了。
尝试使用cron。您只需要将新的聊天 ID 存储在某个地方(文件、数据库等)并从您的脚本中读取它。
例如每天晚上 9 点 (21:00) 运行 send_messages.py
:
0 21 * * * python send_messages.py
在函数中定义作业时需要使用上下文参数。看这个例子:
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
。
要定期发送消息,您可以使用JobQueue Extention from python-telegram-bot
举个例子
from telegram.ext import Updater, CommandHandler
def callback_alarm(bot, job):
bot.send_message(chat_id=job.context, text='Alarm')
def callback_timer(bot, update, job_queue):
bot.send_message(chat_id=update.message.chat_id,
text='Starting!')
job_queue.run_repeating(callback_alarm, 5, context=update.message.chat_id)
def stop_timer(bot, update, job_queue):
bot.send_message(chat_id=update.message.chat_id,
text='Stoped!')
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
命令停止。