Skip to main content

Telegram

Telegram is a module for sending messages from i3 Pro to Telegram. A Telegram bot sends messages configured in iRidi Studio when an event is triggered (for example, notifications from leak sensors, smoke detectors, and other equipment).

Documentation updated 01.01.2024

Tip

Download the example:

Download the project template:

Creating a Telegram bot​

  1. Open Telegram, enter BotFather in the search field ;

  2. Select the bot found in the search, open a conversation with it, and click Start ;

  3. Send the command /newbot to create your bot;

  4. BotFather will ask you to choose a name for your bot. Enter a name;

  5. Choose a unique username for the bot. The name must end with _bot ;

  6. After successful registration, BotFather will send you a message with a token that you must save for later use.

Integrating the module into a project​

We have prepared a project that demonstrates the Telegram module. To integrate the module into a project:

  1. Download the project template containing the Telegram module;

  2. Open the module in iRidi Studio;

  3. Merge your project with the Telegram module:

Telegram2.png

  1. In the SCRIPT section, open the Main file containing the example;

  2. Change the parameters in the example:

Telegram1.png

  • device Name - Telegram driver name;
  • chat ID - user or group ID;
  • token of the bot - i ;
  • timeout between messages - delay between messages, in milliseconds;
  • print Log - a variable that determines whether logs are output (true, false).

Getting the chat ID:

  1. Open Telegram, enter userinfobot in the search field ;
  2. Select the bot found in the search, open a conversation with it, and click Start ;
  3. The message provides information about your Telegram account: login, id, first name, last name .

Getting a group's chat ID:

  1. Add the bot to the group (use the usual member-adding procedure and search for the bot by name);
  2. Start the bot with the command /start ;
  3. You can get a list of updates for your bot at the following link, where **** is replaced with your bot's token: https://api.telegram.org/bot/getUpdates ;
  4. You will receive a response similar to this:

Telegram4.png

In the chat object, the id field is your group ID.

  1. Following the example, write a data handler in the Main script. Specify the event that sends the message and the message text.

6.1. Example of sending a message when a button is pressed"

  1. Replace the chat ID and token of the bot values with those used by the Telegram bot;

  2. Create the function send_mail() and enter the message text to be sent by the Telegram bot;

  3. Bind the function send_mail() to a graphical item (button) using the Script call macro.

var l_pTelegram;

IR.AddListener(IR.EVENT_START, 0, function(){
l_pTelegram = new Telegram(
/* device Name */ "Telegram",
/* chat ID */ "0000",
/* token of the bot */ "botToken",
/* timeout between messages */ 200,
/* print Log */ true);

});

function send_mail()
{
l_pTelegram.SendMessage("text message");
}

6.2. Example of sending a message when driver tag values change"

  1. Replace the chat ID and token of the bot values with those used by the Telegram bot;

  2. Replace ** "Name Device" ** with the name of the driver whose values you want to monitor and send to Telegram.

  3. In the IR.EVENT_TAG_CHANGE listener, add the required channels whose values you want to monitor and the message text to be sent to Telegram.

var l_pTelegram;

IR.AddListener(IR.EVENT_START, 0, function(){
l_pTelegram = new Telegram(
/* device Name */ "Telegram",
/* chat ID */ "0000",
/* token of the bot */ "botToken",
/* timeout between messages */ 200,
/* print Log */ true);

var driver = IR.GetDevice("Name Device"); // driver name
});


IR.AddListener(IR.EVENT_TAG_CHANGE, driver, function(name,value)
{
if (name == "Address 1" && value == 1) // if the driver feedback "Address 1" changed to 1
{
l_pTelegram.SendMessage("Back door opened!"); // text of notification
}
});

6.3. Example of sending a message on a specified date"

  1. Replace the chat ID and token of the bot values with those used by the Telegram bot;

  2. In the IR.EVENT_GLOBAL_TAG_CHANGE listener, specify the date and time to send the message and the message text.

var l_pTelegram;

IR.AddListener(IR.EVENT_START, 0, function(){
l_pTelegram = new Telegram(
/* device Name */ "Telegram",
/* chat ID */ "0000",
/* token of the bot */ "botToken",
/* timeout between messages */ 200,
/* print Log */ true);

});


IR.SetGlobalListener(IR.EVENT_GLOBAL_TAG_CHANGE, function (name, value)
{
// conditions:
if (IR.GetVariable("System.Date.MM_DD") == "03/14" && IR.GetVariable("System.Time.Hour") == 10 && IR.GetVariable("System.Time.Minutes") == 0)
{
l_pTelegram.SendMessage("Happy Birthday Mike!"); // text of notification
}
});

IR.SubscribeTagChange("System.Time.Minutes"); // subscribe on minutes to check condition each minute

Starting the bot as an end user​

To start using the Telegram bot, the end user must:

  1. Provide the chat ID of their account or group chat to the integrator;

  2. Find the bot by its username ;

  3. Open a conversation and start the bot by sending the command /start .