[英]Telegram Bot with Telegraf.js - Send messages to chat
I want to create a Telegram Bot with Node.js and I am using Telegraf for it. 我想用Node.js创建一个Telegram Bot,我正在使用Telegraf 。 I know I can answer to messages like this: 我知道我可以回答这样的消息:
app.hears('hi', (ctx) => ctx.reply('Hey there!'))
But how can I send a message without getting a message before? 但是如何在不收到消息之前发送消息? I want to read a file and always when the file got changed I want to send a message. 我想读取一个文件,并且总是在文件发生变化时我想发送一条消息。
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);
})
Would be nice if someone can help me with it. 如果有人可以帮助我,那会很好。
You can use app.telegram.sendMessage
for that, see following snippet. 您可以使用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); })
app.on('message', function (ctx, next) {
ctx.telegram.sendMessage(ctx.message.chat.id,
"File content at: " + new Date() + " is: \n" + file
)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.