Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

[Telegram action] enhancement: added send photo capability #3553

Merged
merged 1 commit into from
Dec 16, 2015

Conversation

paolodenti
Copy link
Contributor

Added method to send pictures to Telegram.
Pictures are downloaded from a URL and sent to Telegram
An optional caption can be added to the picture.

Can be used to send surveillance systems screenshots.

Usage example

rule "Send telegram with image"
when
    Item Light_GF_Living_Table changed
then
    sendTelegramPhoto("bot1", "http://www.openhab.org/assets/images/openhab-logo-top.png", "sent from openHAB")
end

The third parameter (caption) can be null (no caption is sent)

parts[0] = new StringPart("chat_id", groupTokens.get(group)
.getChatId());
parts[1] = new FilePart("photo", new ByteArrayPartSource(
String.format("image.%s", imageType), imageFromURL));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest something like what I added to the mail action in order to use the actual file name from the URL, or fall back to a generic name:

String fileName = photoURL.replaceFirst(".*/([^/?]+).*", "$1");
fileName = StringUtils.isNotBlank(fileName) ? fileName : String.format("image.%s", imageType);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@watou
If you are suggesting to get the name from the url, in my opinion this is not a good idea.
The url usually is something like http://..../get.php?date=11122015&height=24&width=250&res=high&....
In general the content type of the image is sent along the http headers and not in the name itself.

My code makes an analysis of the actual bytes received in order to guess the content type received, so that the telegram is happy with the file extension. And the name itself is not used by telegram apart from getting the extension, it is there just because required from the ByteArrayPartSource.
If you make a "save as" from telegram desktop the actual filename proposed is not the sent one but it is something like 83456634889_1232656545353543282341.jpg

So in my opinion my approach is fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, your correct use of the file name extension to match the image type will make the generic "image" name unimportant to users. This is a different scenario from attaching any arbitrary file to an email, where the name can actually matter. Thanks for the detailed response.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@wtrucci
Copy link

wtrucci commented Dec 16, 2015

Tested sending a jpg photo from my mobotix cam! Very fast usefull!!!

Thanks @paolodenti for your great work

@watou
Copy link
Contributor

watou commented Dec 16, 2015

Will a 2-second HTTP timeout be sufficient if the user attempts to send a very, very large image? Aside from that question, this PR lgtm.

@paolodenti
Copy link
Contributor Author

@watou
John, after your suggestion I went through he documentation and I understood that the soTimeout is not a connection timeout (as I wrongly supposed) but is it a data transfer timeout.
So, yes, it is low. I will put it to something quite higher.

Thanks for the very useful suggestion.

Do you want me to squash the 2 commits?

@paolodenti
Copy link
Contributor Author

@watou
a different timeout for pictures has been set to 10 seconds.
Moreover a timeout was missing from http get for img download (it was left to default, no timeout), it has been set to 10 seconds, like for http post to telegram servers.

I squashed the 2 commits into 1

Thanks

watou added a commit that referenced this pull request Dec 16, 2015
[Telegram action] enhancement: added send photo capability
@watou watou merged commit 326c98f into openhab:master Dec 16, 2015
@watou
Copy link
Contributor

watou commented Dec 16, 2015

Thanks @paolodenti!

@paolodenti paolodenti deleted the telegram branch December 16, 2015 11:35
@hakan42 hakan42 added this to the 1.8.0 milestone Dec 16, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants