Node-RED and Telegram

If you already have a Telegram bot setup in Node-RED, see my post, Better notifications with Telegram and Node-RED to send notifications with Telegram formatting and logging.

In my humble opinion, Telegram is probably the best way to send and receive notifications from Home Assistant. You can create groups for everyone in your house, you can respond to notifications, receive pictures, videos or even gifs. It’s a great service…and doesn’t cost a thing. The setup isn’t too bad, but it does require some programming in YAML.

Here is the official documentation on creating a chatbot within Home Assistant. Although you’ll have to create a Telegram Bot first. Make sure do both of these things. Also, here’s all the cool things a Telegram Bot can do once it’s setup.

Alright so now that I’ve gotten all my swooning out of the way, you’ll need to first add the bot to your configuration file, as per the official documentation. If you’re not, you should definitely be using the secrets.yaml

**One thing to remember, for how we’re setting up our Telegram bot in this particular instance, you’ll want to set the platform to “broadcast” and not “polling”. If you set it up as polling, your Home Assistant log will get flooded with Telegram bot multiple instances errors, after you setup the Node-RED side of things.**

Once this is done, you can use a call service node to test the Telegram bot. Here’s how it should look to test:

Connect an inject node, and press the tab to test.

You should receive a “Test” message from your telegram bot. You can also see the notifications with a picture from my doorbell. We’ll get to that in another post.

Now you have working Telegram bot notifications within Home Assistant. Not to bad right? Here’s an example of a notification with a button to respond. I use these in my “Alarm has been armed” notifications, as well as the “Alarm has been triggered” notification. We can disarm the alarm when it’s armed for whatever reason, or disarm it when it is triggered. Here’s what the call service node configuration looks like:

This is how it looks when the notification is received.

Telegram bots respond to commands, that are sent with a forward slash. /arm and /disarm are examples, but your Telegram bot doesn’t know what to do with them yet.

So how do we get Telegram to respond to the /disarm command that is sent when the Disarm button is pressed? This requires installing some new nodes so we can run the bot in Node-RED. To start, click the top right hamburger menu and go to “Manage palette”.

Once there, in the search bar, search for “node-red-contrib-chatbot“. You’ll see one result. This is Redbot, an incredibly good Node-RED chatbot. Install it and wait a few seconds. You’ll get a notification when it’s done. This installs a bunch of new nodes, 74 to be exact, as of this writing. This initially will look very daunting at first, but it’s really not, and we’ll go through the basic setup.

Before we continue, if you don’t recall or can’t find the API token that was given to you during your bots creation, go talk to the BotFather and send him the /token command, and he’ll give you the API token for your bot. On we go…

There are two nodes that you need to pull out, Telegram In, and Telegram Out.

Put those guys in a new tab in Node-RED. Open up one of them, doesn’t matter which, and click the edit button to the right of Bot configuration (development).

Fill it out with all this info:

Click Add, which will take you back to the main node configuration. Use the drop down under Bot configuration (development) and select the bot you just created.

Go to the other Telegram node, and use the pull down to select the bot you created. Both of these nodes are now configured.

The Telegram Receiver, or Telegram In, node is what listens for commands when you send them to your bot, which it grabs and sends out through it’s output. The Telegram Sender, or Telegram Out, node receives any commands or messages and sends them to the chat. We need to put some logic in between them so the bot knows what do when we tell it. We’ll start super simple so you can see exactly what is happening.

First, grab a Command node; and place it after the Receiver node. Open up the node and type /hello in the command field.

The command node will listen to everything coming from the Receiver Node and will grab all of the /hello commands. Next, grab a text node and place it after the command node. Type Hello, {{firstName}}! in the message box. This will respond with your first name (or whatever you used when creating you Telegram account). If you want a smiley, add :smile:.

Here’s what you have now:

You have simple logic that will respond with “Hello, Justin!” when it receives the /hello command. Let’s test it out. Open up your Telegram app and send /hello to your bot.

Now you have a working Telegram bot.

So, lets take what we know now, and make the /arm and /disarm commands (or whatever commands you want) work. We’re going to need another command node, and set it to grab /arm commands. Connect a new text node to the command node and set the message to “The House has been Armed”, and finally add a call service node to the end of it. Below is how the service node is configured in my example, but you can use it to anything you like. You can use it to send you a picture of one of your cameras, tell you what temperature the house is, etc.

Now, connect everything up to the Send and Receiver nodes and you’ll have this:

To review, the receiver node receives any messages you send to the bot. The command node filters out the command you program it with. The text node sends whatever message you program it with as a response from the bot. The calls service node is not part of the bot, it’s just wired in so that the bots messages will trigger the service. Here’s what the completed bot looks like with arm and disarm both setup

3 thoughts on “Node-RED and Telegram

  • Good post Justin, thank you. I managed to get it all working for me on my Telegram account -and owner of the bot- I cannot get it to work in parallel for me wife’s phone. She does not get any messages from the bot, only me. i have initiated a chat from her phone to my bot. The bot is in her contact list. I am new to all of this and cannot figure out the setting for her to get the same messages that i do. Thanks for your help !!!

    Like

    • So it looks like there’s a few things I need to update the post with.

      I’m on mobile right now, so I cannot give you the complete answer, but if you want to give others immediate access, got to the Bot Father and disable privacy mode by typing /setprivacy and choosing your bot. Create a group and make the Bot an admin. Invite others to the group that you’d like to have access to the bot. In this way, you’ll both get the same messages so everyone knows what’s happening at the same time.

      EDIT:
      Alright, so I have better info now that I’m back at my desk. On top of what I said before, make sure that you have users you want access to your bots indicated in the Reciever/Sender nodes, as well as a “Authorized” node directly after the Reciever node. I’ll update the guide when I have a moment.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s