Telegram Bot与Telegraf.js-发送消息进行聊天

Non*_*ono 6 node.js telegram telegram-bot telegraf

我想用Node.js创建一个Telegram Bot,并且正在使用Telegraf。我知道我可以回答这样的消息:

app.hears('hi', (ctx) => ctx.reply('Hey there!'))
Run Code Online (Sandbox Code Playgroud)

但是,如何在没有收到消息的情况下发送消息?我想读取文件,并且总是在文件更改后才想发送消息。

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);
})
Run Code Online (Sandbox Code Playgroud)

如果有人可以帮助我会很好。

Vit*_*kov 7

您可以使用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);
})
Run Code Online (Sandbox Code Playgroud)

  • `app.telegram.sendMessage(chatId, "File content at: " + new Date() + " is: \n" + file)` (4认同)