我想用Node.js创建一个Telegram Bot,我正在使用 Telegraf.我知道我可以回答这样的消息: app.hears('hi', (ctx) = ctx.reply('Hey there!')) 但是如何在不收到消息之前发送消息?我想读取一个文件,并且总
app.hears('hi', (ctx) => ctx.reply('Hey there!'))
但是如何在不收到消息之前发送消息?我想读取一个文件,并且总是在文件发生变化时我想发送一条消息.
const Telegraf = require('telegraf');
var fs = require('fs');
const app = new Telegraf(process.env.BOT_TOKEN);
var filePath = "C:\\path\\to\\my\\file.txt";
fs.watchFile(filePath, function() {
file = fs.readFileSync(filePath);
// Send message to chat or group with the file content here
console.log("File content at: " + new Date() + " is: \n" + file);
})
如果有人可以帮助我,那会很好.
您可以使用app.telegram.sendMessage,请参阅以下代码段.const Telegraf = require('telegraf');
var fs = require('fs');
const app = new Telegraf(process.env.BOT_TOKEN);
var filePath = "C:\\path\\to\\my\\file.txt";
fs.watchFile(filePath, function() {
file = fs.readFileSync(filePath);
app.telegram.sendMessage("File content at: " + new Date() + " is: \n" + file);
})
