Telegram is a messaging service for iOS, Android and other devices. A great advantage of Telegram is that it provides an open web service which can be used to send Telegram messages via software. More information about Telegram can be found here – the API documentation is available here.
To send Telegram messages for the NETx BMS Server, the so called Bot API can be used. This is used to communicate with so called Telegram bots. Bots are special accounts that do not require a phone number. This tutorial explains how a Telegram Bot can be created and how the NETx BMS Server can send messages to this bot.
In order to use this functionality, it is assumed that you have installed the Telegram app on your smart phone. The Telegram app can be download from Apple iTunes or Google Play Store. In addition, it is assumed to you have created a Telegram account. More information about Telegram can be found here.
Installation of the HTTP module
Sending messages to a Telegram bot is done via HTTP post requests. Therefore, the HTTP module of the NETx BMS Server has to be installed first. The installation of the HTTP module is done via the extension manager of the NETx BMS Server. For information about the installation of the HTTP module can be found here.
Creating a Telegram bot
As next, a Telegram bot has to be created within your Telegram account. Bots can be created and managed with the "father" of all bots – the BotFather. The BothFather is a special bot that can be used by any account to create and manage your bots.
To talk with the BotFather, open your telegram app, click on the search button and search for "BotFather". Add the BotFather to your contact list and open a chat with him.
Within the chat you will find an overview of available commands. Telegram commands are starting with a slash ("/"). To create a new bot, enter the following command:
/newbot
For creating a bot, you have to define a name and username. Both have to end with "_bot".
Now you have created a new bot. To access a bot via the HTTP API, an authentication token is necessary. This token has to be send within the URL of each HTTP request. The token for your bot is shown within the chat after having created the bot. Write down the token – we will need it within the NETx BMS Server configuration afterwards.
Open a chat with your bot
After having created your bot, you have to open a chat with him. Click on search button within your Telegram app, enter your bot name and add him to your contact list. Afterwards, click on "start" to start the chat and send any message (e.g. "hello") to the bot.
As next we need to retrieve the so called chat_id. The chat_id is necessary to send messages via the HTTP API. To get the chat_id, open your webbrowser and the enter the following URL (<your bot token> has to be replaced by the authentication token of your bot):
https://api.telegram.org/bot<your bot token>/getUpdates
You will get a JSON response which the message that you have sent to your bot.
{ ok: true, result: [ { update_id: 43423455, message: { message_id: 8, from: { id: 54365464, is_bot: false, first_name: "<your first name>", last_name: "<your last name>", language_code: "de" }, chat: { id: 54365464, first_name: "<your first name>", last_name: "<your last name>", type: "private" }, date: 1512549481, text: "Hello" } } ] }
Write down the id within the JSON object "chat" – we will need it afterwards within the NETx BMS Server.
Configuring the HTTP module
To a message to your bot via the HTTP API of telegram, the following HTTP POST request has to be used (<your bot token> has to be replaced by the authentication token of your bot):
https://api.telegram.org/bot<your bot token>/sendMessage
As body the following JSON object has to be sent:
{ "text":"<your message>", "chat_id":<your chat id> }
<your message> has to be replaced by text that you would like to send and <your chat id> with the chat_id that you determined within the previous step of this tutorial.
To configure this within the HTTP module, open the "Resouce definitions" within the menu "HTTP" of the NETx BMS Studio. Add the following definition:
The HTTP module will replace the placeholder "@value@" by the value of ther Server Item which will be used to send a message. The placeholder "{0}" will be replaced by the first parameter within the "Datapoint definitions" (for more information see the HTTP tutorial).
Then open the "Datapoint definitions" within the menu "HTTP" of the NETx BMS Studio. Add the following definition:
<your message> has to be replaced by text that you would like to send and <your chat id> with the chat_id that you determined within the previous step of this tutorial.
Then, save all definitions files and restart the NETx BMS Server.
Sending Telegram messages
After having restarted the NETx BMS Server, a new Server Item called "Message" is created within the tree NETx\XIO\Http.
This writeonly item can now be used to send messages to your bot chat directly.