Send Telegram Alarm Notification
Send a Telegram message when a device alarm is created using the Rule Engine and the Telegram Bot API.
Use case
Section titled “Use case”Thermostat A sends temperature telemetry. When the temperature goes outside the acceptable range (below −15 °C or above 30 °C), ThingsBoard:
- creates an alarm
- builds a Telegram message payload with the alarm type and device name
- sends the message to a Telegram chat via the Telegram Bot API
The notification is sent only when an alarm is created — not when it is updated.
Prerequisites
Section titled “Prerequisites”- A Telegram account with access to the Telegram app or web client
Step 1. Set up Telegram Bot
Section titled “Step 1. Set up Telegram Bot”Step 1.1. Create bot and get token
Section titled “Step 1.1. Create bot and get token”- Open Telegram and start a chat with @BotFather.
- Send the
/newbotcommand and follow the prompts to set a name and username for your bot. - After creation, BotFather replies with an authorization token. It looks like:
Save this token — you will need it as110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
{YOUR_BOT_TOKEN}in the REST API Call URL.
Step 1.2. Get the Chat ID
Section titled “Step 1.2. Get the Chat ID”The Chat ID identifies the recipient of the notification.
- Send any message to your bot in the Telegram app.
- Open the following URL in your browser, replacing
{YOUR_BOT_TOKEN}with your token:https://api.telegram.org/bot{YOUR_BOT_TOKEN}/getUpdates - In the JSON response, find the
idfield insidemessage.chat:The value of{"result": [{"message": {"chat": {"id": 337878729}}}]}idis yourchat_id. Save it — you will use it in the Transformation Script.
Step 2. Create rule chain
Section titled “Step 2. Create rule chain”You can import a ready-made rule chain or build it manually.
Option 1. Import rule chain
- Download the Telegram Alarm Notification rule chain as a JSON file and import it into your instance.
Option 2. Create manually
Create a new rule chain named Telegram Alarm Notification, open it, and add the following nodes:
Step 2.1. Filter Script node
Section titled “Step 2.1. Filter Script node”Add a Filter Script node connected to the Input node.
Passes the message only when the temperature is outside the acceptable range.
- Name:
Check Temperature Range - Add the following script:
return msg.temperature < -15 || msg.temperature > 30;return msg.temperature < -15 || msg.temperature > 30;Step 2.2. Clear Alarm node
Section titled “Step 2.2. Clear Alarm node”Add a Clear Alarm node and connect it to the Check Temperature Range node with relation type False.
Clears the alarm when the temperature returns to the acceptable range.
- Name:
Clear Temperature Alarm - Alarm type:
High Temperature
Step 2.3. Create Alarm node
Section titled “Step 2.3. Create Alarm node”Add a Create Alarm node connected to the Check Temperature Range node with relation type True.
Creates the alarm when the temperature is out of range.
- Name:
Create Temperature Alarm - Alarm type:
High Temperature - Alarm severity:
Critical
Step 2.4. Transformation Script node
Section titled “Step 2.4. Transformation Script node”Add a Transformation Script node connected to the Create Temperature Alarm node with relation type Created.
Builds the Telegram message payload. The Telegram Bot API requires two fields: chat_id and text.
- Name:
New Telegram Message
Replace 337878729 with the chat_id from Step 1.2.
var newMsg = {};newMsg.text = '"' + metadata.alarmType + '"' + ' alarm was created for device: "' + metadata.deviceName + '"';newMsg.chat_id = 337878729; // replace with your chat_idreturn {msg: newMsg, metadata: metadata, msgType: msgType};var newMsg = {};newMsg.text = '"' + metadata.alarmType + '"' + ' alarm was created for device: "' + metadata.deviceName + '"';newMsg.chat_id = 337878729; // replace with your chat_idreturn {msg: newMsg, metadata: metadata, msgType: msgType};Step 2.5. REST API Call node
Section titled “Step 2.5. REST API Call node”Add a REST API Call node connected to the New Telegram Message node with relation type Success.
Sends the message payload to the Telegram Bot API.
-
Name:
Send Telegram Message -
Endpoint URL pattern:
https://api.telegram.org/bot{YOUR_BOT_TOKEN}/sendMessageReplace
{YOUR_BOT_TOKEN}with the token from Step 1.1. -
Request method:
POST -
Headers:
- Key:
Content-Type - Value:
application/json
- Key:
Step 2.6. Check node connections
Section titled “Step 2.6. Check node connections”- Input ⇾ Check Temperature Range
- Check Temperature Range ⇾
True⇾ Create Temperature Alarm - Create Temperature Alarm ⇾
Created⇾ New Telegram Message - New Telegram Message ⇾
Success⇾ Send Telegram Message
Save the rule chain.
Step 3. Route telemetry to the rule chain
Section titled “Step 3. Route telemetry to the rule chain”In the Root Rule Chain, add a Rule Chain node connected to the Message Type Switch node with relation type Post telemetry:
- Name:
Telegram Alarm Notification - Rule Chain:
Telegram Alarm Notification
Save the rule chain.
Step 4. Test the setup
Section titled “Step 4. Test the setup”Create a device Thermostat A with the thermostat profile if you haven’t already.
Post a temperature value that exceeds 30 °C. Replace $ACCESS_TOKEN with the access token of Thermostat A.
curl -v -X POST \--header "Content-Type: application/json" \--data '{"temperature": 32}' \https://eu.thingsboard.cloud/api/v1/$ACCESS_TOKEN/telemetryExpected result:
A High Temperature Critical alarm is created on Thermostat A and your Telegram bot delivers a message to the configured chat:
"High Temperature" alarm was created for device: "Thermostat A"See also
Section titled “See also”- REST API Call node — full node reference
- Create and Clear Alarms — alarm rule basics
- Send Alarm Email to Customer — email-based alarm notification
- Notification Center — no-code alternative for alarm notifications