Setting up Node-RED to interact with a Telegram Bot

Node-RED and Telegram

Node-RED can interface with a Telegram Bot which enables you to send messages directly from Node-RED to Telegram.

To do this first set up a Telegram Bot. You will need to message the Botfather to do this, instructions are available on Telegram’s site.

Telegram's Botfather
Telegram’s Botfather awaiting your message

Install the Node-RED telegrambot using npm:

npm install node-red-contrib-telegrambot

Restart Node-RED and you should now have Telegram Bot nodes.

To configure the Telegram node you will need the name of your bot (not the user name!) and the token issued by the Botfather.

To send a message with your bot you need to structure your msg.payload like this:

msg.payload = {chatId :346959227, type : 'message', content : "Hello!"}

UPDATE! I’ve just found a much easier way to get your chatId, all you need to do is start a conversation with the IDBot on telegram and send it the message /getid far simpler than the way I have described below!

Before you try to connect to your bot using telegram send it some messages using a telegram client first.

The tricky bit is finding the chatId, to do this you need to go to the url:{REPLACE_THIS_WITH_YOUR_ACCESS_TOKEN!}/getUpdates

Then send your bot some messages, you might need to do this a few times as it seems to need pending messages for this to work. I found it tricky to catch it while the message was still considered pending. When there isn’t a pending message it looks like this:


With perseverance eventually you should see something like this:

{"ok":true,"result":[{"update_id":867122933, "message":{"message_id":75,"from":{"id":346959227,"first_name":"Ktinkerer","username":"ktinkerer"} ,"chat":{"id":346959227,"first_name":"Ktinkerer","username":"ktinkerer","type":"private"} ,"date":1473101671,"text":"Hello"}}]}

The chat ID is in bold in this example. Add this to Node-RED as the chatId and you should be able to send messages using Telegram.

Using msg.payload as the content as shown below also works to pass on a payload to the telegram bot.

msg.payload = {chatId :346959227, type : 'message', content : msg.payload}


Add a Comment

Your email address will not be published. Required fields are marked *