我正在用nodeJS在JavaScript中制作一个带有Telegraf框架的电报机器人。我想在每周一上午9:00向用户发送一条消息如何在telegraf js中创建基于时间的触发器?
发布于 2020-04-16 13:42:11
Telegraf没有为您提供开箱即用的功能,因为它是一个以抽象API调用为目标的bot框架。相反,您应该使用任务调度库(如node-cron)来调度任务。
使用node-cron的示例
const Telegraf = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN)
const cron = require('node-cron');
cron.schedule('0 9 * * MON', () => {
// send the message here
bot.telegram.sendMessage(12345678, "scheduled message");
});
bot.launch()如果您想了解更多信息,请访问refer this tutorial
发布于 2020-09-12 01:08:31
如果您不想使用任何其他库,请尝试使用Date对象。
let goalTime = new Date(December 17, 2020 13:24:00)并使用if检查Date.now():
let currentTime = Date.now()
if (currentTime == goalTime) {
ctx.reply("your message")
}用于每周发送消息(以下代码仅为示例,您也可以使用小时和分钟来指定时间):
const goalTime = new Date(December 17, 2020 13:24:00)
const weekday = goalTime.getDay()
let currentTime = Date.now
let currentDay = currentTime.getDay()
if (currentDay == weekday) {
ctx.reply("your message")
}有关Date的更多详细信息,请参阅this link。
https://stackoverflow.com/questions/60991341
复制相似问题