Skip to content

Commit

Permalink
Added support to send an Image directly (openhab#11346)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp authored Oct 9, 2021
1 parent 407834c commit 19c9240
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
17 changes: 16 additions & 1 deletion bundles/org.openhab.binding.pushsafer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

- `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.

- `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)
```
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,10 @@ public ContentProvider build() throws PushsaferCommunicationException {
body.addFieldPart(MESSAGE_KEY_TIME2LIVE, new StringContentProvider(String.valueOf(time2live)), null);

if (attachment != null) {
String localAttachment = attachment;
final String encodedString;
try {
if (attachment.startsWith("http")) {
if (localAttachment.startsWith("http")) {
Properties headers = new Properties();
headers.put("User-Agent", "Mozilla/5.0");
if (!authentication.isBlank()) {
Expand All @@ -324,7 +325,9 @@ public ContentProvider build() throws PushsaferCommunicationException {
throw new IllegalArgumentException(
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 {
File file = new File(attachment);
if (!file.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ sendPushsaferMonospaceMessageActionDescription = This method is used to send a m
sendPushsaferAttachmentMessageActionLabel = send a plain text message with an image attachment
sendPushsaferAttachmentMessageActionDescription = This method is used to send a message with an 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
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
sendPushsaferMessageActionInputAuthenticationDescription = Basic access authentication for HTTP(S) requests. Default: "", Example: "user:password".

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ sendPushsaferAttachmentMessageActionDescription = Action zum Versenden einer Tex
sendPushsaferMessageActionInputAttachmentLabel = Bild-Anhang
sendPushsaferMessageActionInputAttachmentDescription = Lokaler Pfad oder URL zum Anhang.
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
sendPushsaferMessageActionInputAuthenticationDescription = Basisauthentifizierung für HTTP(S) Aufrufe. Default: "", Beispiel: "user:passwort".

Expand Down

0 comments on commit 19c9240

Please sign in to comment.