Simple - 'telegrambot' server

Situation:
I have a number of Node-RED flows running on four RPi-4Bs performing various tasks around my home.
Quite a few of these flows report a situation by sending text and/or a photo to Telegram on my mobile phone.

Problem:
Sometimes I lose track of a 'telegram' bot-name and end-up deploying another Telegram node (with the same bot-name) which causes problems as (I found out to my cost) you can only run a single instance.

Solution:
First of all I must emphase this is a simple solution for a 'telegrambot' server.

As you can see there is a single 'sender' and receiver' node for each 'bot-name', while mqtt-in and mqtt-out nodes are the main connection-points to the flow.
The topic the 'mqtt-in' node subscribes to is... telegram_out/#
The topic the 'mqtt-out' node publishes to is left blank as the topic-name will be supplied by the 'change' node that is joined to it.

Each flow that wants to send a message to a specific Telegram bot needs to include these details in its mqtt-out node. As an example, here's part of the flow for one of my Infrared detectors.
The topic inside the mqtt-out node is telegram_out/xxx_bot

Going back to look at the top flow. The 'function' node labelled "split out 'bot' part" contains this...

const topicParts = msg.topic.split('/');

// Selecting a specific part of the topic
const selectedPart = topicParts[1]; // "sensors"

msg.topic = selectedPart;
return msg;

The 'switch' node directs msg.payload to the appropriate 'telegrambot' sender node.
I'm using a 'change' node (after the 'telegrambot' receiver node) to set the topic so the feedback is sent to the appropriate flow via the mqtt-out node.

I hope someone finds this idea useful?

1 Like

Thanks for sharing Dave. Though I only run 1 live instance of Node-RED, I do something similar.

However, I use MQTT as an intermediary so it doesn't matter where I run the bot, I can send to any of my around 4 bots using an MQTT topic from anywhere on the network.

I also use a standardised format for messages and do some checking at the point of sending so that I can have a title and a body for my most common message types.

Of course, messages from Telegram can be handled the same way - as it happens, I don't bother right now because I don't need to, but easy enough to do.

Not going to show the MQTT-in nodes as they contain the bot names but this is the main bit of flow:

As you can see, I can still send direct to the bot output if I need to for some more specialist messaging.

The same flow tab has the inbound message process and a bunch of test messages I can send if I need to. It also contains the main inbound process which is to be able to remotely query and control my lights.


I'd love to find time to play with the newer features - especially the ability to make a bot into a full web app. I really want to try coupling that with UIBUILDER. Just haven't had time!