Skip to content
Stand with Ukraine flag

Send Telegram Alarm Notification

Send a Telegram message when a device alarm is created using the Rule Engine and the Telegram Bot API.

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.

  • A Telegram account with access to the Telegram app or web client
  1. Open Telegram and start a chat with @BotFather.
  2. Send the /newbot command and follow the prompts to set a name and username for your bot.
  3. After creation, BotFather replies with an authorization token. It looks like:
    110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
    Save this token — you will need it as {YOUR_BOT_TOKEN} in the REST API Call URL.

The Chat ID identifies the recipient of the notification.

  1. Send any message to your bot in the Telegram app.
  2. Open the following URL in your browser, replacing {YOUR_BOT_TOKEN} with your token:
    https://api.telegram.org/bot{YOUR_BOT_TOKEN}/getUpdates
  3. In the JSON response, find the id field inside message.chat:
    {
    "result": [{
    "message": {
    "chat": {
    "id": 337878729
    }
    }
    }]
    }
    The value of id is your chat_id. Save it — you will use it in the Transformation Script.

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:

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;

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

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

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_id
return {msg: newMsg, metadata: metadata, msgType: msgType};

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}/sendMessage

    Replace {YOUR_BOT_TOKEN} with the token from Step 1.1.

  • Request method: POST

  • Headers:

    • Key: Content-Type
    • Value: application/json
  • 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.

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.

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.

Terminal window
curl -v -X POST \
--header "Content-Type: application/json" \
--data '{"temperature": 32}' \
https://$THINGSBOARD_HOST/api/v1/$ACCESS_TOKEN/telemetry

Expected 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"