Telegram

Telegram – OpenHAB integration


Using the OpenHAB available binding it’s possible to send Telegram messages triggered by events.

What is Telegram ?

Telegram is a competitor of many other chat systems, but it has a plus with the BOTs that enables many automation features.

How to integrate Telegram in OpenHAB

Assuming that the installation of the Telegram app and the registration to the service have already be done.

Create the BOT, get the token and the chatId

Create the new bot using the instructions of the BotFather bot. At the end, the TOKEN linked with this new bot will be displayed.

Search the bot given_name, E.g.: writing Openhab in the search field:

@OpenHab

and send a dummy text:

test

Write down in a browser the following URL with the TOKEN given by BotFather:

https://api.telegram.org/bot'TOKEN'>/getUpdates

In the JSON output:

{{"ok":true,"result":[{"update_id":136050211,
"message":{"message_id":120,"from":{"id":144558195,"is_bot":false,"first_name":"Guido","last_name":"Ottaviani","username":"guiott","language_code":"root"},"chat":{"id":144558195,"first_name":"Guido","last_name":"Ottaviani","username":"guiott","type":"private"},"date":1522098871,"text":"test"}}]}

Annotate the chatId returned:

144558195
Configure OpenHAB

Use the TOKEN and the ChatId to configure the Telegram ACTION

Edit

services/telegram.cfg

# Read http://www.instructables.com/id/Telegram-Bots-for-beginners/
# to see how to set up bots and find your chat ids
#

# bots=bot1,bot2

# bot1.chatId=22334455
# bot1.token=xxxxxx

# bot2.chatId=654321
# bot2.token=yyyyyyyyyyy

bots=bot1

bot1.chatId=144558195
bot1.token=12345678:ABCDEFGHIJKLMNOPQRSTUVWXYZ

More bots can be configured as shown in the commented lines.

Examples
  • Forward an SMS message received to the Telegram bot

rules/sms.rules

rule "SMS to Telegram"
when
    Item SmsMsg changed
then
    var MSG = (SmsMsg.state.toString)
    var TEL = (SmsTel.state.toString)
    logInfo("SmsRec", "SMS Received")
    sendTelegram("bot1"," " + MSG + "\r\n" + TEL)
end
  • Send a Telegram message when a dash button key is pressed

rules/dash.rules

rule "My GBC Dash Button pressed"
when
    Channel "amazondashbutton:dashbutton:040949bf:press" triggered
then
    logInfo("amazondashbutton", "GBC Dash Button has been pressed")
    L_SalDX.sendCommand(OFF)
    sendTelegram("bot1","GBC dash button")
end

rule "My NERF Dash Button pressed"
when
    Channel "amazondashbutton:dashbutton:d8d813aa:press" triggered
then
    logInfo("amazondashbutton", "NERF Dash Button has been pressed")
    L_SalDX.sendCommand(ON)
    sendTelegram("bot1","NERF dash button")
end
  • Send a Telegram message when the electric power consumption exceed a given treshold

rules/modbus.rules

...
else if (Power > 2500 && Power <= 3000)
	{
        //val results = executeCommandLine(Cmd+"B",5000)
        //logInfo("Exec",results)
        sendTelegram("bot1","Power = %d W", Power)
        sendCommand(LedB, OFF)
        sendCommand(LedG, ON)
        sendCommand(LedR, ON)
        set_timer = createTimer(now.plusSeconds(0.1))
        [
           sendCommand(LedB, ON)
		   set_timer = null
        ]         
    }
...
Links

Share