Creating a Telegram Bot to Send Automated Messages Using Telegraf.js

I am building a Telegram bot using Node.js with the help of Telegraf.js. I can respond to incoming messages like this:

app.hears('hello', (ctx) => ctx.reply('Hello there!')) 

However, I want to send messages automatically when a file changes. Here’s the code I have so far:

const { Telegraf } = require('telegraf');
const fs = require('fs');

const bot = new Telegraf(process.env.BOT_TOKEN);
const targetFile = 'C:\path\to\tracked\file.txt';

fs.watch(targetFile, (eventType, filename) => {
    if (filename) {
        const content = fs.readFileSync(targetFile, 'utf8');
        // Send message with file content to chat or group
        console.log(`Content of ${filename} at ${new Date()}: 
${content}`);
    }
});

Can anyone guide me on how I can automate sending messages with the contents of the file when it updates?

I’ve implemented a similar system for monitoring changes in configuration files. Here’s a refined approach that might help:

Instead of fs.watch(), consider using the chokidar library. It’s more reliable across different operating systems and handles various edge cases better.

const chokidar = require('chokidar');
const { Telegraf } = require('telegraf');

const bot = new Telegraf(process.env.BOT_TOKEN);
const targetFile = 'C:\\path\\to\\file.txt';
const chatId = YOUR_CHAT_ID;

chokidar.watch(targetFile).on('change', (path) => {
  const content = fs.readFileSync(path, 'utf8');
  bot.telegram.sendMessage(chatId, `File updated:\n${content}`);
});

bot.launch();

This method has worked well in production environments. Remember to implement error handling and perhaps add a debounce mechanism if the file changes frequently to avoid excessive messages.

hey, i’ve done something similar before. you’re on the right track, but try using the ‘chokidar’ library instead of fs.watch(). it’s more reliable. here’s a quick example:

const chokidar = require('chokidar');
const bot = new Telegraf(process.env.BOT_TOKEN);
const chatId = YOUR_CHAT_ID;

chokidar.watch(targetFile).on('change', (path) => {
  const content = fs.readFileSync(path, 'utf8');
  bot.telegram.sendMessage(chatId, `File updated:\n${content}`);
});

bot.launch();

this should work better for you. good luck!

I’ve actually implemented something similar for monitoring log files in a production environment. Here’s what worked well for me:

Instead of using fs.watch(), I found fs.watchFile() to be more reliable, especially for larger files. It polls the file periodically, which consumes more resources but avoids some edge cases.

const fs = require(‘fs’);
const { Telegraf } = require(‘telegraf’);

const bot = new Telegraf(process.env.BOT_TOKEN);
const targetFile = ‘C:\path\to\file.txt’;
const chatId = YOUR_CHAT_ID;

fs.watchFile(targetFile, (curr, prev) => {
if (curr.mtime !== prev.mtime) {
const content = fs.readFileSync(targetFile, ‘utf8’);
bot.telegram.sendMessage(chatId, File updated:\n${content});
}
});

bot.launch();

This approach has been rock-solid for me. Just remember to handle potential errors, like file read failures or network issues when sending messages. Also, consider rate limiting if your file changes frequently to avoid spamming the chat.