Added support to send an Image directly (#11346)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2021-10-09 15:35:18 +02:00 committed by GitHub
parent 83fd01498a
commit becb8a48f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 6 deletions

View File

@ -47,8 +47,23 @@ The parameter `message` is **mandatory**, the `title` parameter defaults to what
- `sendPushsaferMonospaceMessage(String message, @Nullable String title)` - This method is used to send a monospace message. - `sendPushsaferMonospaceMessage(String message, @Nullable String title)` - This method is used to send a monospace message.
- `sendPushsaferAttachmentMessage(String message, @Nullable String title, String attachment, @Nullable String contentType, @Nullable String authentication)` - This method is used to send a message with an image attachment. It takes a local path or url to the image attachment (parameter `attachment` **mandatory**), an optional `contentType` to define the content-type of the attachment (default: `"jpeg"`, possible values: `"jpeg"`, `"png"`, `"gif"`) and an optional `authentication` for the given URL to define the authentication if needed (default: `""`, example: `"user:password"`). - `sendPushsaferAttachmentMessage(String message, @Nullable String title, String attachment, @Nullable String contentType, @Nullable String authentication)` - This method is used to send a message with an image attachment. It takes a local path or URL to the image attachment (parameter `attachment` **mandatory**), an optional `contentType` to define the content-type of the attachment (default: `"jpeg"`, possible values: `"jpeg"`, `"png"`, `"gif"`) and an optional `authentication` for the given URL to define the credentials to authenticate a user if needed (default: `""`, example: `"user:password"`).
- `sendPushsaferURLMessage(String message, @Nullable String title, String url, @Nullable String urlTitle)` - This method is used to send a message with an URL. A supplementary `url` to show with the message and a `urlTitle` for the URL, otherwise just the URL is shown. - `sendPushsaferURLMessage(String message, @Nullable String title, String url, @Nullable String urlTitle)` - This method is used to send a message with an URL. A supplementary `url` to show with the message and a `urlTitle` for the URL, otherwise just the URL is shown.
- `sendPushsaferPriorityMessage(String message, @Nullable String title, @Nullable Integer priority)` - This method is used to send a priority message. Parameter `priority` is the priority (`-2`, `-1`, `0`, `1`, `2`) to be used (default: `2`). - `sendPushsaferPriorityMessage(String message, @Nullable String title, @Nullable Integer priority)` - This method is used to send a priority message. Parameter `priority` is the priority (`-2`, `-1`, `0`, `1`, `2`) to be used (default: `2`).
## Example
demo.rules
```java
```java
val actions = getActions("pushsafer", "pushsafer:pushsafer-account:account")
// send message with attachment
actions.sendPushsaferAttachmentMessage("Hello World!", "openHAB", "/path/to/my-local-image.png", "png", null)
actions.sendPushsaferAttachmentMessage("Hello World!", "openHAB", "https://www.openhab.org/openhab-logo-square.png", "png", "user:password")
actions.sendPushsaferAttachmentMessage("Hello World!", "openHAB", "data:[<media type>][;base64],<data>", null, null)
// in case you want to send the content of an Image Item (RawType)
actions.sendPushsaferAttachmentMessage("Hello World!", "openHAB", myImageItem.state.toFullString, null, null)
```

View File

@ -310,9 +310,10 @@ public class PushsaferMessageBuilder {
body.addFieldPart(MESSAGE_KEY_TIME2LIVE, new StringContentProvider(String.valueOf(time2live)), null); body.addFieldPart(MESSAGE_KEY_TIME2LIVE, new StringContentProvider(String.valueOf(time2live)), null);
if (attachment != null) { if (attachment != null) {
String localAttachment = attachment;
final String encodedString; final String encodedString;
try { try {
if (attachment.startsWith("http")) { if (localAttachment.startsWith("http")) {
Properties headers = new Properties(); Properties headers = new Properties();
headers.put("User-Agent", "Mozilla/5.0"); headers.put("User-Agent", "Mozilla/5.0");
if (!authentication.isBlank()) { if (!authentication.isBlank()) {
@ -324,7 +325,9 @@ public class PushsaferMessageBuilder {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format("Skip sending the message as content '%s' does not exist.", attachment)); String.format("Skip sending the message as content '%s' does not exist.", attachment));
} }
encodedString = "data:" + contentType + ";base64," + content; encodedString = "data:image/" + contentType + ";base64," + content;
} else if (localAttachment.startsWith("data:")) {
encodedString = localAttachment;
} else { } else {
File file = new File(attachment); File file = new File(attachment);
if (!file.exists()) { if (!file.exists()) {

View File

@ -30,9 +30,9 @@ sendPushsaferMonospaceMessageActionDescription = This method is used to send a m
sendPushsaferAttachmentMessageActionLabel = send a plain text message with an image attachment sendPushsaferAttachmentMessageActionLabel = send a plain text message with an image attachment
sendPushsaferAttachmentMessageActionDescription = This method is used to send a message with an image attachment. sendPushsaferAttachmentMessageActionDescription = This method is used to send a message with an image attachment.
sendPushsaferMessageActionInputAttachmentLabel = Image Attachment sendPushsaferMessageActionInputAttachmentLabel = Image Attachment
sendPushsaferMessageActionInputAttachmentDescription = A local path or url to the image. sendPushsaferMessageActionInputAttachmentDescription = A local path or URL to the image.
sendPushsaferMessageActionInputContentTypeLabel = Image Type sendPushsaferMessageActionInputContentTypeLabel = Image Type
sendPushsaferMessageActionInputContentTypeDescription = The image type of the attachment. Defaults to "jpeg", possible values "jpeg,png,gif". sendPushsaferMessageActionInputContentTypeDescription = The image type of the attachment. Defaults to "jpeg", possible values "jpeg" ,"png" or "gif".
sendPushsaferMessageActionInputAuthenticationLabel = Authentication sendPushsaferMessageActionInputAuthenticationLabel = Authentication
sendPushsaferMessageActionInputAuthenticationDescription = Basic access authentication for HTTP(S) requests. Default: "", Example: "user:password". sendPushsaferMessageActionInputAuthenticationDescription = Basic access authentication for HTTP(S) requests. Default: "", Example: "user:password".

View File

@ -73,7 +73,7 @@ sendPushsaferAttachmentMessageActionDescription = Action zum Versenden einer Tex
sendPushsaferMessageActionInputAttachmentLabel = Bild-Anhang sendPushsaferMessageActionInputAttachmentLabel = Bild-Anhang
sendPushsaferMessageActionInputAttachmentDescription = Lokaler Pfad oder URL zum Anhang. sendPushsaferMessageActionInputAttachmentDescription = Lokaler Pfad oder URL zum Anhang.
sendPushsaferMessageActionInputContentTypeLabel = Bild-Typ sendPushsaferMessageActionInputContentTypeLabel = Bild-Typ
sendPushsaferMessageActionInputContentTypeDescription = Der Bild-Typ für den Anhang. Default: "jpeg", mögliche Werte "jpeg,png,gif". sendPushsaferMessageActionInputContentTypeDescription = Der Bild-Typ für den Anhang. Default: "jpeg", mögliche Werte "jpeg", "png" oder "gif".
sendPushsaferMessageActionInputAuthenticationLabel = Authentifizierung sendPushsaferMessageActionInputAuthenticationLabel = Authentifizierung
sendPushsaferMessageActionInputAuthenticationDescription = Basisauthentifizierung für HTTP(S) Aufrufe. Default: "", Beispiel: "user:passwort". sendPushsaferMessageActionInputAuthenticationDescription = Basisauthentifizierung für HTTP(S) Aufrufe. Default: "", Beispiel: "user:passwort".