This article is about sending a message via Telegram. A Telegram account/number is required.

Prepare Telegram API

  1. Open a Telegram Client. I will use the webclient at https://web.telegram.org/.
  2. Create a new Chat with the user “BotFather” (do not accidentally use “@BotFather”, “BotFather:V” or other fake-accounts).

  3. In the chat click “Start”.

  4. Enter /newbot in the chat. The BotFather will ask you how you will call it – like a Name. It will also ask for a Username. That one must be unique worldwide. It may be a little bit difficult to find a free name, but you can just try. Finally BotFather will cenerate a HTTP API Key for you.
  5. Search for your Bot in the contact List. You can use the name or the username. Because the username is unique this will have better results. Start a chat with your bot.

  6. Click on “Start” and send a message with any content.
  7. Open the following Website with your Browser. Replace the <token> part with the token you received from BotFather.
    https://api.telegram.org/bot<token>/getUpdates
    The answer will look like this:

    If the answer is empty like below, send another message to your bot and try again.

  8. Get the id from the answer. We just need the number of it without the “id”:. In the example above it starts with 35 and ends with 40.
  9. To verify the correct ID, open the following URL in the webbrowser. Replace <token> with your token and <chatId> with the id.
    https://api.telegram.org/bot<token>/sendMessage?chat_id=<chatId>&text=testing
  10. If everything was correct, your Bot will now send the message “testing” in the Chat and the answer from the website looks like this:

  11. Now we can start to configure the use of the Bot in OpenHab.

Install Telegram Action

Browse to your Paper UI interface of OpenHab and go to “Add-ons” (1). The first tab “Actions” (2) is already selected. Click on “Install” (3) next to the “Telegram Action”-Entry.

 

 

Configure Telegram

Connect to OpenHab using SSH via Putty. Execute the following command to edit the telegram.cfg file:

/etc/openhab2/services/telegram.cfg

At the end add the settings for Telegram:

bots=<BotName>
bot1.chatId=<ChatId>
bot1.token=<token>

Save the file with Ctrl+O and exit with Ctrl+X.

Send Telegram message

To send a Telegram message, we need to create a rule that is triggered if a rule applies.
I will configure a message in case a smoke detector changes the state.

Execute this command to create a new rule-file in the rules folder:

sudo nano /etc/openhab2/rules/smokedetector.rules

Enter the following rule configuration:

rule "Alarm Smoke Detector Office"
when
    Item RauchmelderEGArbeitszimmer_1_SmokeDetectorAlarmStatus changed
then
    sendTelegram("TelegramHome",""Smoke Detector at the office triggered! New Status: %s", RauchmelderEGArbeitszimmer_1_SmokeDetectorAlarmStatus.state.toString)
end

Remark: To get the Item name, you can take a look at the Paper UI at Configuration => Items. the smaller text below the title is the item name.

Save the file with Ctrl+O and exit with Ctrl+X.
If your smoke detector status will change, you will get a telegram message now.