# Simple - 'telegrambot' server

**URL:** <https://discourse.nodered.org/t/simple-telegrambot-server/84331>\
**Category:** Share Your Projects\
**Created:** [6 January 2024 11:32 UTC](https://discourse.nodered.org/t/simple-telegrambot-server/84331 "2024-01-06T11:32:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [6 January 2024 11:32 UTC](https://discourse.nodered.org/t/simple-telegrambot-server/84331/1 "2024-01-06T11:32:17Z")

</div>

**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.

 ![bot_server_A](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/ce946805f88ec9d421517bc0b0cdfd6b87441fe8.png)

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`

 ![bot_server_B](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6a7c74b078abf004bb40a2e9ed8a44e0d00fd38.png)

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 January 2024 17:37 UTC](https://discourse.nodered.org/t/simple-telegrambot-server/84331/2 "2024-01-06T17:37:07Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/161adc6446949d2d466a6968bccb0e5b10c08e66.png)

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!
