New Telegram Binding - Tester and Feedback welcome

As I know you have to deinstall old binding.

The rules have to update too, because the syntax has changed a little bit.

  1. Enter your chat IDs in Paper UI
  2. add those line with var… from bonding examples.
  3. change syntax of action to send telegram

I had 15 rules, took me less than a hour

Any tips/example to facilitate my transition from the 1.14 telegram action to the 2.50 binding? I just removed the action and installed the binding. Everything was working fine in 1.14 but I figure I might as well transition now (just as I did for MQTT)

If you click on binding name you get the help-site with description and examples:

As I have already set it for the telegram 1.14 action , do I need to create a new bot the BotFather again, or can I use the existing authentication token?
is the information I placed in the telegram.cfg under services still relevant?
I have modified my rule:

val mqttActions = getActions(“mqtt”, “mqtt:broker:mosquitto”)

rule “OP1 Monitor to OFF”
when
Time cron “0 0 19 1/1 * ? *”
then
mqttActions.publishMQTT(“cmnd/sonoff-AEI-op1/POWER”, “Off”)
val telegramAction = getActions(telegram","telegram:telegramBot:bot1)
telegramAction.sendTelegram(“OP1 Monitor is switched OFF”)
logInfo(“Info”,“OP1 Monitor OFF Rule fired”)

end
But I dont think this is correct?

You have to enter your chatIDs in paperUI and as you see in the examples first value to give in telegram action ist your chatID as along value
telegramAction.sendTelegram(123456L,“OP1 Monitor is switched OFF”)

Sorry for short reply: post on phone :wink:

EDIT: I was having an issue earlier trying to get Telegram to send me an image from one of my cameras, after going through my old script, I figured out where it went wrong. I got my rule working, in case anyone is interested, this is how I use Telegram to send images from my cameras on my request:

rule "Outside Image Request"
when
    Item Proxy_OutsideCamera changed from OFF to ON
then
    logInfo("telegram","outside image requested")
    val telegramAction = getActions("telegram","telegram:telegramBot:23707ee5")
    telegramAction.sendTelegramQuery([ChatID],"Which outside camera do you want an image from?", "Reply_O_Camera","Driveway", "FrontYard", "EastYard", "BackYard")
end

rule "Outside Image Request Handler"
when
    Item TG_ReplyId received update Reply_O_Camera
then
    logInfo("telegram","outside image being sent")
    val telegramAction = getActions("telegram","telegram:telegramBot:2b155b22")
    var outsideimage = "/etc/openhab2/scripts/./"+TG_LastMessageText.state.toString+".sh"
    if (telegramMessage.state.toString == "Driveway"){
        executeCommandLine(outsideimage)
        telegramAction.sendTelegramAnswer(TG_ReplyId.state.toString, "Here is the latest image from the driveway camera.")}
    if (telegramMessage.state.toString == "FrontYard"){
        executeCommandLine(outsideimage)
        telegramAction.sendTelegramAnswer(TG_ReplyId.state.toString, "Here is the latest image from the front yard camera.")}
    if (telegramMessage.state.toString == "EastYard"){
        executeCommandLine(outsideimage)
        telegramAction.sendTelegramAnswer(TG_ReplyId.state.toString, "Here is the latest image from the east yard camera.")}
    if (telegramMessage.state.toString == "BackYard"){
        executeCommandLine(outsideimage)
        telegramAction.sendTelegramAnswer(TG_ReplyId.state.toString, "Here is the latest image from the back yard camera.")}
end

The basic script to call the image from my cameras is (I have the same script for each camera, but the camera number and file name change for each):

#!/bin/bash
wget http://motioneye:8765/picture/1/current -O /etc/openhab2/tmp/Driveway.png
curl -s -X POST "https://api.telegram.org/bot[token]/sendPhoto" -F chat_id=[ChatID] -F photo="@/etc/openhab2/tmp/Driveway.png"
echo rm Driveway.png

This works, the pictures are sent to me through Telegram, but I do not get the telegramAction.sendTelegramAnswer. So, this needs some fine tuning, in my log I am still getting an error:

2020-01-04 09:43:05.816 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Outside Image Handler': Instance is not a TelegramActions class.

Any help is appreciated!

~John

@ZzetT
Would it be possible in the near future to support commands also?
In the old action you were able to send commands to openHAB by specifying the ItemName and payload.
It would be great if a same or better solution would be supported here.
I’m thinking of something like you issue a command which returns a list (buttons in Telegram) with the preset items (which you mostly want to control from Telegram, like Thermostats) and after selecting the Item you were presented with a preset commands list.

Hi @rkrisi,

yes that’s what I had in mind as well. However, I don’t think that the old action supported the reception of messages at all. At least I didn’t see anything in the Java code nor in the documentation.

I can’t give you a time frame and I’m not sure how complex this would be. I think such a binding is probably similar to the hue emulation where items are exposed. For now, I’m working on the multi line support for queries (currently a question can only have one line of answers).
I’ll keep you posted when I have any news for you.

1 Like

Ok thanks!

I’m also not sure that this was supported, but I remembered that there were a screenshot somewhere with an explanation that it you can send states also.
Just out of curiosity, why you need to expose the items?
You can query all items from the API I think, and you “show” that for the user when a specific message is received (which is already implemented).
That way when a message is received with a command, you can change item states, don’t you?
I just want to understand why it is needed, and maybe to reduce the work needed for this function…

Yes maybe it’s not difficult. We will see.
A better comparision might be the Runtime Commands interface: https://www.openhab.org/docs/administration/runtime.html

I think those commands (or at least a subsest) would look quite similar for the Telegram API.

Hello, Alex.

Is it possible to add an option to change default telegram API server URL and port to my own? In Russia api.telegram.org blocked, but I have a mirror.

Hi @morchee, there is an active PR from @Genix to support proxies. Would that maybe help you?

1 Like

Thanx, Alex, I solved the problem in another way, through Nginx.

Is there a way to cancel or revoke a question?
There is an issue with the ReplyId:

.items

String telegramMessage "Telegram Message" { channel = "telegram:telegramBot:ec80dac6:lastMessageText" }
String telegramReplyId "Telegram Reply Id" { channel = "telegram:telegramBot:ec80dac6:replyId" }
Switch telegramPresenceTimerTimeout

.rules

var Timer telegramPresenceTimer = null
(...)
rule "Presence Update"
when
	Item presence received command OFF
then
val telegramAction = getActions("telegram","telegram:telegramBot:ec80dac6")
telegramAction.sendTelegramQuery("Bist du noch zu Hause?", "ID123", "Ja", "Nein")
telegramPresenceTimer = createTimer(now.plusSeconds(Integer::parseInt(telegramPresenceTimerDuration.state.toString))) [|
	telegramPresenceTimerTimout.sendCommand(ON)
]

rule "Telegram presence question timeout detected"
when
	Item telegramPresenceTimerTimeout received command ON
then
	val telegramAction = getActions("telegram","telegram:telegramBot:ec80dac6")
	telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "Nehme an, du bist nicht mehr hier")
end

Result (log set to debug):

Adding chatId 58824331, replyId ID123 and messageId 413
AnswerCallbackQuery for chatId 58824331 and replyId ID123 is the callbackId 543561343449429349
Failed to send telegram message: Bad Request: query is too old and response timeout expired or query ID is invalid

But, this rule is executed without issues:

rule "Reply handler for presence"
when
	Item telegramReplyId received update ID123
then
	val telegramAction = getActions("telegram","telegram:telegramBot:ec80dac6")
	telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "OK")
end

I’ve already changed replyIds (becoming unusable after too many failed attempts, and of course restarted openhab as well.

Thanks!

Update:
After lots of testing, (with different bot instance ID, chatID and ReplyID),it worked out of the box (suddenly).
After 2-3 attempts, it stopped working again, same message as described above.
Debug Logs:

#### 1st attempt, revoked via openhaben: OK
2020-01-15 22:19:48.119 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId 58824331, replyId Reply_Id and messageId 427
2020-01-15 22:20:32.774 [DEBUG] [ng.telegram.internal.TelegramHandler] - Received callbackId 386351340853778559 for chatId 58824331 and replyId Reply_Id
2020-01-15 22:20:47.623 [DEBUG] [ng.telegram.internal.TelegramHandler] - Received callbackId 386351342428893059 for chatId 58824331 and replyId Reply_Id
2020-01-15 22:20:48.122 [DEBUG] [binding.telegram.bot.TelegramActions] - AnswerCallbackQuery for chatId 58824331 and replyId Reply_Id is the callbackId 386351342428893059
2020-01-15 22:20:48.187 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 427 for chatId 58824331 and replyId Reply_Id
#### 2nd attempt, revoked via openhab: OK
2020-01-15 22:22:51.863 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId 58824331, replyId Reply_Id and messageId 429
2020-01-15 22:22:56.225 [DEBUG] [ng.telegram.internal.TelegramHandler] - Received callbackId 386351342541008859 for chatId 58824331 and replyId Reply_Id
2020-01-15 22:22:56.228 [DEBUG] [binding.telegram.bot.TelegramActions] - AnswerCallbackQuery for chatId 58824331 and replyId Reply_Id is the callbackId 386351342541008859
2020-01-15 22:22:56.358 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 429 for chatId 58824331 and replyId Reply_Id
#### 3rd attempt, revoked via openhab: FAILED
2020-01-15 22:23:03.848 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId 58824331, replyId Reply_Id and messageId 431
2020-01-15 22:23:10.852 [DEBUG] [binding.telegram.bot.TelegramActions] - AnswerCallbackQuery for chatId 58824331 and replyId Reply_Id is the callbackId 386351342541008859
2020-01-15 22:23:10.918 [WARN ] [binding.telegram.bot.TelegramActions] - Failed to send telegram message: Bad Request: query is too old and response timeout expired or query ID is invalid
#### 3rd attempt, answered via telegram (external): OK
2020-01-15 22:23:25.915 [DEBUG] [ng.telegram.internal.TelegramHandler] - Received callbackId 386351339710776448 for chatId 58824331 and replyId Reply_Id
2020-01-15 22:23:25.918 [DEBUG] [binding.telegram.bot.TelegramActions] - AnswerCallbackQuery for chatId 58824331 and replyId Reply_Id is the callbackId 386351339710776448
2020-01-15 22:23:25.963 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 441 for chatId 58824331 and replyId Reply_Id
#### 4th attempt, revoked via openhab: FAILED
2020-01-15 22:24:35.730 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId 58824331, replyId Reply_Id and messageId 443
2020-01-15 22:24:40.732 [DEBUG] [binding.telegram.bot.TelegramActions] - AnswerCallbackQuery for chatId 58824331 and replyId Reply_Id is the callbackId 386351339710776448
2020-01-15 22:24:40.779 [WARN ] [binding.telegram.bot.TelegramActions] - Failed to send telegram message: Bad Request: query is too old and response timeout expired or query ID is invalid
#### 5th attempt, revoked via openhab: FAILED
2020-01-15 23:16:26.002 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId -398886268, replyId Reply_Id and messageId 478
2020-01-15 23:17:26.009 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId null for chatId -398886268 and replyId NULL

It seems that the Received callbackId is missing for some reason and the previous callbackId is used in this case. A new callbackId is further only received after answering question from external. Any thoughts about this or what I can try to verbose logging?

Update:
When switching the chatID, it is working, but after some attempts, it is in failed state again.

@jsable
Thanks for the example, looks great.

Does the telegram binding (I know the API does) allow mp4 or gif files to be sent and displayed in a message? Pushover can do a gif but I like the way telegram can be used to unlock a door after you watch a camera feed of who pushed the doorbell.

Telegram can send mp4s, but to make sure, I just modified my script to send an mp4 from a downloaded video from my ring doorbell (downloaded manually, this binding stopped working).
The command looks like this:

curl -s -X POST "https://api.telegram.org/bot[token]/sendVideo" -F chat_id=[ChatID] -F video="@/etc/openhab2/tmp/ring.mp4"

After a quick search, Telegram can send gifs, but I don’t know the syntax for that.

~John

EDIT - I ran this from the command line, not through an OpenHab script or rule, but it can be incorporated.

EDIT 2 - I just searched, the sendVideo command should work for gifs and animations, I did not test this though.

Hi,
Perhaps sendDocument : https://core.telegram.org/bots/api#senddocument can be a solution to send animated gif.(https://community.flowxo.com/t/animated-gifs-in-telegram/2731/2)

Hey @Max_Mustermann,

It is not possible to send an answer by yourself when the user didn’t click any buttons. The reason is that when the user makes a choice, a callback id (the one you see in the logs) is sent back and this id must be used to send the answer message back to the client. If you use a timer and automatically send an answer, you don’t have that callback id.

@jsable
Adding the sendVideo API to the OH Telegram binding is on my list.

1 Like

@ZzetT thanks for your reply, but it worked perfectly 7 times since my last comment (and is still working).
I’m not able to see and callbackId in the logs with DEBUG enabled:

  • Adding chatId …” is written when i send the Question,
  • remove messageId …” when i send the answer 1 minute later
 2020-01-15 23:24:08.740 [DEBUG] [ng.telegram.internal.TelegramHandler] - Trying to dispose Telegram client
2020-01-15 23:24:08.740 [DEBUG] [ng.telegram.internal.TelegramHandler] - Telegram client closed
2020-01-15 23:25:30.703 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId -398986269, replyId Reply_Id and messageId 483
2020-01-15 23:25:30.709 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 483 for chatId -398986269 and replyId Reply_Id
2020-01-15 23:25:48.354 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId -398986269, replyId Reply_Id and messageId 485
2020-01-15 23:26:48.360 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 485 for chatId -398986269 and replyId Reply_Id
2020-01-16 09:10:26.072 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId -398986269, replyId Reply_Id and messageId 487
2020-01-16 09:11:26.076 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 487 for chatId -398986269 and replyId Reply_Id
2020-01-16 14:42:35.699 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId -398986269, replyId Reply_Id and messageId 489
2020-01-16 14:43:35.702 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 489 for chatId -398986269 and replyId Reply_Id
2020-01-16 17:31:55.004 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId -398986269, replyId Reply_Id and messageId 492
2020-01-16 17:32:55.011 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 492 for chatId -398986269 and replyId Reply_Id
2020-01-16 18:25:37.457 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId -398986269, replyId Reply_Id and messageId 494
2020-01-16 18:26:37.460 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 494 for chatId -398986269 and replyId Reply_Id
2020-01-16 19:22:28.813 [DEBUG] [binding.telegram.bot.TelegramActions] - Adding chatId -398986269, replyId Reply_Id and messageId 496
2020-01-16 19:23:28.816 [DEBUG] [binding.telegram.bot.TelegramActions] - remove messageId 496 for chatId -398986269 and replyId Reply_Id