Skip to content
PekawayPekaway
Integrate Telegram

Integrate Telegram

Using the Telegram Messenger, you can bring the control to your smartphone. You specify commands that you send to the bot, and functions are carried out depending on the settings in Node-RED. For example, you can be notified when certain temperatures are reached, or you can use the bot to switch relays, for example.
The prerequisite for this is an active internet connection. Telegram and the BotApi serve as a "middleman" and establish the necessary connections via the messenger. All data therefore runs through this service. You must be aware of this. That is why there are various other ways to access the system on the go, such as via our IOT bridge or a VPN.
Botfather
In Telegram, we first look for the Botfather, which we use to create a new bot. To do this, we write /newbot and follow the instructions from Telegram until we receive our token, which we will later use in Node-RED.
The Botfather asks you for a name for the bot, which will later be displayed in the chat list, and for a user name that uniquely identifies the bot.

Node-RED

In Node-RED we install the module node-red-contrib-chatbot via the palette
Now we can drag a Telegram-In Node into the workspace. In this we set the name of our bot and pass the token previously created in Telegram. We immediately create a debug node and connect it to our Telegram-In Node. The whole thing we take over and open our bot in Telegram. If we now start our bot with /start, we can already see the first data in the Node-RED debug window.
 
We can now filter which commands were entered by querying what is in msg.payload.content and letting Node-RED act accordingly.
For the sake of clarity, it is best to use the built-in HTTP API. You can find the API's options here:
For example, if an http-request node sends a PUT request to <RPI-IP> :1880/toggle/relay/1 , Relay 1 will be switched on or off depending on the current status. You can import this small example with the following code:

[{"id":"ce3d3f8aa8573a8c","type":"http request","z":"f4e99ff4808f0b96","name":"toggle relay 1","method":"PUT","ret":" txt","paytoqs":"ignore","url":" 192.168.1.131:1880/toggle/relay/1","tls":"","persist":false,"proxy":""," authType":"","credentials":{},"x":980,"y":220,"wires":[["542d1ed806fd2726"]]},{"id":"0a40e56749790e1b","type" :"chatbot message","z" :"f4e99ff4808f0b96","name":"","message":[{"message":""}],"language":"none","x":1450,"y":220,"wires" :[["feb1be1f901608b8"]]},{"id":"20f6cff1783e35f2","type":"function","z":"f4e99ff4808f0b96","name":"catch reponse","func":"var state = msg.payload.state\nmsg.payload = \"Relay 1 set to: \" + state\nreturn msg;","outputs":1,"noerr":0, "initialize":"","finalize":"","libs":[],"x":1300,"y":220,"wires":[["0a40e56749790e1b"]]},{"id" :"5ae01a56e2c337c8","type":"function","z":"f4e99ff4808f0b96","name":"check command","func":"if ( msg.payload.content == \"/relay1\" ) {\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y": 220,"wires":[["ce3d3f8aa8573a8c"]]},{"id":"542d1ed806fd2726","type":"json","z":"f4e99ff4808f0b96","name":"","property" :"payload","action":"","pretty":false,"x":1130,"y":220,"wires":[["20f6cff1783e35f2","4b8b20e2f6f60cc2"]]}]

All you have to do is insert the Telegram In/Out Node and adjust the http request node so that your system can be requested.

Tips

Remember that if you set Node-RED so that only your ChatID has access, the bot is publicly discoverable for everyone. However, if you only allow messages for yourself, no one else can do anything with them as long as you keep the token under lock and key.
You also have to start the bot in Telegram before it can send you messages. This is a good thing, because otherwise any bot that somehow got hold of your ChatID could constantly send you messages.
Fun fact : Someone once made a video about it...
Leave a comment

Your email address will not be published..

more tutorials

Open Source needs support

become Patron now

More projects, more tutorials and more supported devices. Our community is growing and would appreciate your support. So become a patron now to always receive the latest content and support the VAN PI project at the same time.

Become a Patron
Cart 0

Your cart is currently empty.

Start Shopping