我想创建一个电报机器人与Node.js和我正在使用它的Telegraf。我知道我可以回复这样的消息:
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);
})如果有人能帮我的话就太好了。
发布于 2017-05-06 03:05:59
您可以使用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);
})
发布于 2017-08-23 15:39:10
app.on('message', function (ctx, next) {
ctx.telegram.sendMessage(ctx.message.chat.id,
"File content at: " + new Date() + " is: \n" + file
)
});https://stackoverflow.com/questions/43793351
复制相似问题