Telegram BOT message loop + delay

Hello,

I have a Telegram BOT running, fetching messages from a DB and sending them into a channel. These messages actually consist of 2 messages, the first is a map (location), the second is a text message. If the next message contains the same location, the previous message is deleted and the updated is placed.

When posting 'single' messages, this works like a charm. However (this wasn't the core reason for this posting), sometimes the message is posted first and the location second. No clue what that is causing, I have a delay node between function and Telegram sender node of 1 second.

But, long story short, I'm changing my code into a loop, since I was missing some messages if more are sent in a short period. Now I SELECT the last xx entries from DB and looping though them, sending all messages newer than when checking in the last loop. No comes the messy part. All code is executed directly, but buffered / delayed by the delay node. When trying to delete the previous message, this ID was not known (yet) when executing the code. Resulting in the location not being deleted. I tried to workaround via setTimeout, but this doesn't seem to be allowed in a for loop.

Any suggestions maybe?

Not entirely sure what your issue is but maybe you want to create a queue - perhaps using a flow variable. Your SQL query can push entries onto the queue variable and another function node could action the queue, shortening the array on each process loop by slicing the first entry from the array.

Just make sure you put some bounds on the length of the array so you don't crash Node-RED b running out of stack space.

Thanks for replying. I start by injecting a query each minute into the DB to see if we have any new entries. If so, I fetch the last one. But sometimes there are more entries within one minute. I want to solve that by fetching the last 5 entries, which should be good enough. Looping through this payload array I send the messages towards the BOT. So far, so good.

Where my issue starts is that I fetch the msg.payload.sentMessageId as output of the Telegram sender node. With my previous solution all worked fine. When I want to delete the last entry, it works as it should. When looping, the loop performs quicker than I can fetch the sentMessageId per each sent message. The loop is finished within a fraction of a second, but I want to delay each message towards the BOT by one second, to prevent spamming.

My flow is like;

Inject (1m) > Function (query) > DB > Function (handling the payload array) > Delay (1s) > Telegram Sender > Function (save sentMessageId)

Hoping its a bit more clear now?

Think I solved it by counting the sent messages upfront. When sending the message I perform "counter++" and when deleting "counter--". That way I know what the "future message" (few seconds ahead) will be which might have to be deleted. Probably not the way to get an award for the best solution, but for now it seems to fit my needs :slight_smile:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.