Star

Notifications and Alarms on your smartphone using Telegram Bot

Overview

Telegram provides a possibility to create Telegram Bots, which are considered as third-party applications. So, In this tutorial, we are going to demonstrate how you can create a Telegram Bot
and configure your ThingsBoard rule engine to be able to send notifications to Telegram App using Rest API Call extension.

Use case

This tutorial is based on the create & clear alarms tutorial and it’s use case. We will reuse the rule chains from above mentioned tutorial and will add few more rule nodes to integrate with Telegram

Let’s assume your device is using DHT22 sensor to collect and push temperature readings to ThingsBoard. DHT22 sensor is good for -40 to 80°C temperature readings.We want to generate Alarms if temperature is out of good range and send notifications to Telegram App when the alarm was created.

In this tutorial we will configure ThingsBoard Rule Engine to:

Prerequisites

We assume you have completed the following guides and reviewed the articles listed below:

Message flow

In this section, we explain the purpose of each node in this tutorial:

Creation of the Telegram Bot

The BotFather is the main bot that will help you to create new bots and change their settings.

Once the creation of the bot is finished, you can generate an authorization token for your new bot. The token is a string that looks like this - ‘110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw’ that is required to authorize the bot.

Prerequisites :

Getting the Chat ID

In the next step, we need to retrieve a Chat ID. The Chat ID is needed to send messages via the HTTP API.

There are several ways to get the Chat ID:

https://api.telegram.org/bot"YOUR_BOT_TOKEN"/getUpdates

"YOUR_BOT_TOKEN" has to be replaced by the authentication token of your bot, e.g.:

https://api.telegram.org/bot110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw/getUpdates

From the outcoming data you can find field ‘id’. This is the so-called chat_id.

image

image

After that, you can start to configure Rule engine to use Rest API Call extension.

Configure Rule Chains

In this tutorial, we used Rule Chains from create & clear alarms tutorial. We modified Rule Chain Create & Clear Alarms by adding nodes that was described above in the section Message flow
and renamed this rule chain to: Create/Clear Alarms & send notifications to Telgram.


The following screenshots show how the above Rule Chains should look like:

image

image


Download the attached json file for the Create/Clear Alarms & send notifications to Telgram rule chain.

The following section shows you how to modify this rule chain from scratch.

Modify Create/Clear Alarm & Send Email

Adding the required nodes

In this rule chain, you will create 2 nodes as it will be explained in the following sections:

Node A: Transform Script
{"chat_id" : "PUT YOUR CHOSEN CHAT_ID", "text" : "SOME MESSAGE YOU WANT TO RECEIVE"}
 var newMsg ={};
 newMsg.text = '"' +  msg.name + '"' + " alarm was created for device: " + '"' + metadata.deviceName + '"';
 newMsg.chat_id = 337878729; //has to be replaced by the actual chat id
 return {msg: newMsg, metadata: metadata, msgType: msgType};

image

Node B: REST API Call

image

Post telemetry and verify

For posting device telemetry we will use the Rest APIs, Telemetry upload APIs. For this we will need to copy device access token from then device Thermostat Home.

image

Lets post temperature = 99. Alarm should be created:

curl -v -X POST -d '{"temperature":99}' http://localhost:8080/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

**you need to replace $ACCESS_TOKEN with actual device token**

You should understand that message won’t be sent to the Telegram App when the alarm was updated, only in the case when the alarm will be created.

Finally, we can see that the message was received with the correct values:

image

image

Also, you can:

Please refer to the links under the See Also section to see how to do this.


See Also

Next steps