Эта страница является
переводом страницы
Telegram. Перевод выполнен на 100%.
Telegram
Telegram is a module to sent messages from i3 pro toTelegram. Telegram bot will send you messages set in iRidium Studio, when an event is activated (for example, messages can be received from leakage sensors, smoke detectors and other equipment).
documentation was last updated on 20.4.2021
Download the example
Download an example of a project:
Creating a Telegram Bot
1 Open Telegram, enter BotFather in the search line;
2 Choose a found bot and activtae a dialogue with it by pressing the Start button;
3 Send the /newbot command to create your own bot;
4 BotFather will suggest that you choose a name for your bot, enter the name;
5 Enter a unique username for the bot with _bot at the end;
6 After a successful registration BotFather will send you a message with a toke that must be saved for further work.
Integration of Module to Project
We have a project that demonstrates the work of Telegram module. To integrate the module to your project, do the following:
1 Download a project with a module for Telegram;
2 Open the module in iRidium Studio;
3 Merge your project with a module for Telegram:
4 Open the Main file in the SCRIPT section, the file contains an example;
5 Change parameters in the example:
- device Name - the name of the driver for Telegram;
- chat ID - ID of a user of a group;
- token of the bot - the token of a bot;
- timeout between messages - a delay to send messages in miliseconds;
- print Log - a varianle that determines whether to display a log or not (true, false).
Getting chat ID:
- Open Telegram and enter userinfobot in the search line;
- Choose a found bot, activate a dialogue with it and press the Start button;
- You'll get information about your Telegram account in a message: login, id, name, surname.
Gettiing the chat ID of a group:
- Add the bot to the group (the bot is added via a regular addition of members and search of a bot by name);
- Start the bot with the help of the /start command;
- You can get a list of updates for your bot via the following link, where <YourBOTToken> is replaced with the token of your bot: https://api.telegram.org/bot<YourBOTToken>/getUpdates;
- You'll get an answer like this:
The id field of the chat object is the id of your group.
6 Write a data handler in the Main script following the example. Set an event that will trigger sending a message and the text of a message.
6.1. An example of sending a message when a button is pressed
1 Replace values in the chat ID and token of the bot fields with those that are used by the Telegram bot;
2 Create send_mail()function and write the text of a message that will be sent by Telegram bot;
3 Assign the send_mail() function to a button with the help of 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. An example of sending a message when a value changed in a driver tag
1 Replace values in the chat ID and token of the bot fields with those that are used by the Telegram bot;
2 Replace the value of "Name Device" with the name of a driver, which value will be tracked and sent to Telegram.
3 Add to the IR.EVENT_TAG_CHANGE listener channels which values will be tracked and the text of a message that will 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. An example of sending a message when a certain date comes
1 Replace values in the chat ID and token of the bot fields with those that are used by the Telegram bot;
2 Write in the IR.EVENT_GLOBAL_TAG_CHANGE listener the date and time to send a message and the text of a message.
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
Start of Bot by End-user
To start working with a Telegram bot, an end-user must do the following.
1 Give the chat ID of his account or chat to the integrator.
2 Find the bot by its username;
3 Activate a dialogue and start the bot by sending the /start command.