I want to create a Telegram Bot with Node.js and I am using Telegraf for it. 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.
Post one message from User to the Bot. Open https://api.telegram.org/bot<Bot_token>/getUpdates page. Find this message and navigate to the result->message->chat->id key. Use this ID as the [chat_id] parameter to send personal messages to the User.
Yes you can, you have to invite the bot into the group, grab the chat_id of the group and send the message as you are sending a normal message to a private user.
You can use app.telegram.sendMessage
for that, see following snippet.
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(chatId, "File content at: " + new Date() + " is: \n" + file);
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With