4

Context

  • Telegraf.js Version: 3.38.0
  • Node.js Version: 12.0.0
  • Operating System: Ubuntu 16.04.6 LTS (Xenial Xerus) "Glitch server"

Expected Behavior

Hi, I'm not sure if this is a bug, but I am not finding anything and i asking you for help, I tank you anyway.

I developed a bot for a group that basically reads user commands and gives a response based on the subject of the command. The bot remains off for the whole day and it activates himself through a chron job at 19:00. Furthermore , if certain conditions have occurred , through a chron job at 21:00 and at 00:00 it sends other messages. The bot works.

Current Behavior

After a few days, when the bot uses the telegram method messages.sendMessage() it sends the message twice; while when using the ctx.reply method of telegraf it works fine.

When the bot starts to duplicate messages, the other chron jobs from 21:00 and 00:00 even if the check of the variable that should prevent sending messages is FALSE , it still sends messages (always and only those that use messages.sendMessage()).

The strange thing is this: due to many console logs used in the code i understand that the bot behaves well because it does not actually go through the function that uses messages.sendMessage (the control is FALSE) but still sends the message anyway.

Also when duplicating messages (always via the console log) in the function it passes only once. The bot does not return any errors and continues to work by responding correctly via the ctx.reply.

It is not a server problem as it continues to have this behavior even locally. The only way I've found to make this work is to change the Telegram token. If I restart the process the problem does not fix until I change the Token.

I apologize in advance for my bad english and if I have not explained myself well. This is the first time something so strange happens to me and before bothering you I searched the web but I didn't find anything. It looks like there are some messages in the "cache" (probably wrong). Furthermore, I specify that the two messages that the bot sends have two different update_id; as if the function were called twice (which I exclude through the console logs).

I thank everyone for your help and for your time

Failure Information (for bugs)

No errors from bot or from telegram; the flow of program is correct . The console logs are printed i correct way

Code

cron.schedule("00 19 * * *", function() {
    console.log("Start Chron");
    getWord();
});

function getWord() {
    try {
        console.log("getParole function base"); // one console log is printed
        firebase.getLastWordFirebase().then(async value => {
            var messaggio = "";
            for (let i = 0; i < value[0]["parole"].length; i++) {
                messaggio = messaggio + value[0]["parole"][i].toUpperCase() + "\n";
            }
            await db.set("soluzione", value[0]["soluzione"]).write();
            bot.telegram.sendMessage(orsoID, messaggio).then(m => { //the bot send the correct message twice XD; the messages have two different update_id
                bot.telegram.pinChatMessage(orsoID, m.message_id); 
            });
        });
    } catch (error) {
        console.log("Last Word error: " + error);
    }
}

cron.schedule("0 20 * * *", function() {
    console.log("Help Chron");
    if (!db.get("win").value()) {  //value of win is True
        console.log("Win: " + db.get("win").value()); //this console log does not print
        win_help();
    }
});

async function win_help() {
    if (db.get("win").value() === false) { //win is true , bot does not pass 
        try {
            await db.set("control", true).write(); //the value remains at false the bot does not pass there
            console.log('win_help function') //the console log does not print
            bot.telegram
                .sendMessage( //the bot send message twice XD XD
                    orsoID,
                    " <b>RISPOSTE ILLIMITATE</b>: la parola orsa inizia per <b>" +
                    db.get("soluzione").value().charAt(0).toUpperCase() +"</b> ", { parse_mode: "HTML" }
                )
                .then(m => {
                    bot.telegram.pinChatMessage(orsoID, m.message_id);
                });
        } catch (error) {
            console.log("Aiuto error: " + error);
            bot.telegram.sendMessage(
                orsoID,
                "Help"
            );
        }
    }
}

List of modules

  • "axios": "^0.21.0",
  • "bluebird": "^3.7.2",
  • "cross-fetch": "^3.0.6",
  • "cors": "^2.8.5",
  • "express": "^4.17.1",
  • "firebase-admin": "^9.4.1",
  • "i": "^0.3.6",
  • "lowdb": "^1.0.0",
  • "node-cron": "^2.0.3",
  • "node-fetch": "^2.6.1",
  • "node-schedule": "^1.3.2",
  • "telegraf": "^3.38.0",
  • "telegraf-command-parts": "^1.0.3"
  • 1
    Are you sure the script/cron isn't triggered twice? So maybe a second bot is running in the background causing duplicate messages. – 0stone0 Dec 09 '20 at 12:21
  • Thanks for the reply; I'm sure because only this script is present on the server; it also happened a second time after changing the token. I solved it again by changing the Telegram token – Spianopolese Dec 09 '20 at 15:01
  • Just a side note: `then` method just returns a promise. So without `await`, the function from its argument cannot be caught by an outer `try ... catch ... `. Concerning debugging: throw an exception before (or after) `pinChatMessage` for a duplicate message, perhaps it could help. – ruvim Dec 10 '20 at 07:59
  • @Spianopolese I have the same problem with PHP, I really don't know whats happening because the same code works perfectly several months until starts to sending message twice. – JuanFernandoz Sep 28 '21 at 17:38

0 Answers0