Add clear=true|false support to actions

pull/225/head
Philipp Heckel 2022-04-22 23:07:35 -04:00
parent 8f4a1db1f0
commit 12f85cceb1
4 changed files with 72 additions and 45 deletions

View File

@ -675,7 +675,8 @@ is the only required one:
"priority": 4, "priority": 4,
"attach": "https://filesrv.lan/space.jpg", "attach": "https://filesrv.lan/space.jpg",
"filename": "diskspace.jpg", "filename": "diskspace.jpg",
"click": "https://homecamera.lan/xasds1h2xsSsa/" "click": "https://homecamera.lan/xasds1h2xsSsa/",
"actions": [{ "action": "view", "label": "Admin panel", "url": "https://filesrv.lan/admin" }]
}' }'
``` ```
@ -692,7 +693,8 @@ is the only required one:
"priority": 4, "priority": 4,
"attach": "https://filesrv.lan/space.jpg", "attach": "https://filesrv.lan/space.jpg",
"filename": "diskspace.jpg", "filename": "diskspace.jpg",
"click": "https://homecamera.lan/xasds1h2xsSsa/" "click": "https://homecamera.lan/xasds1h2xsSsa/",
"actions": [{ "action": "view", "label": "Admin panel", "url": "https://filesrv.lan/admin" }]
} }
``` ```
@ -708,7 +710,8 @@ is the only required one:
"priority": 4, "priority": 4,
"attach": "https://filesrv.lan/space.jpg", "attach": "https://filesrv.lan/space.jpg",
"filename": "diskspace.jpg", "filename": "diskspace.jpg",
"click": "https://homecamera.lan/xasds1h2xsSsa/" "click": "https://homecamera.lan/xasds1h2xsSsa/",
"actions": [{ "action": "view", "label": "Admin panel", "url": "https://filesrv.lan/admin" }]
}) })
}) })
``` ```
@ -727,7 +730,8 @@ is the only required one:
"priority": 4, "priority": 4,
"attach": "https://filesrv.lan/space.jpg", "attach": "https://filesrv.lan/space.jpg",
"filename": "diskspace.jpg", "filename": "diskspace.jpg",
"click": "https://homecamera.lan/xasds1h2xsSsa/" "click": "https://homecamera.lan/xasds1h2xsSsa/",
"actions": [{ "action": "view", "label": "Admin panel", "url": "https://filesrv.lan/admin" }]
}` }`
req, _ := http.NewRequest("POST", "https://ntfy.sh/", strings.NewReader(body)) req, _ := http.NewRequest("POST", "https://ntfy.sh/", strings.NewReader(body))
http.DefaultClient.Do(req) http.DefaultClient.Do(req)
@ -745,6 +749,7 @@ is the only required one:
"filename"="diskspace.jpg" "filename"="diskspace.jpg"
"tags"=@("warning","cd") "tags"=@("warning","cd")
"click"= "https://homecamera.lan/xasds1h2xsSsa/" "click"= "https://homecamera.lan/xasds1h2xsSsa/"
"actions"=@[@{ "action"="view", "label"="Admin panel", "url"="https://filesrv.lan/admin" }]
} | ConvertTo-Json } | ConvertTo-Json
Invoke-RestMethod -Method 'Post' -Uri $uri -Body $body -ContentType "application/json" -UseBasicParsing Invoke-RestMethod -Method 'Post' -Uri $uri -Body $body -ContentType "application/json" -UseBasicParsing
``` ```
@ -760,7 +765,8 @@ is the only required one:
"priority": 4, "priority": 4,
"attach": "https://filesrv.lan/space.jpg", "attach": "https://filesrv.lan/space.jpg",
"filename": "diskspace.jpg", "filename": "diskspace.jpg",
"click": "https://homecamera.lan/xasds1h2xsSsa/" "click": "https://homecamera.lan/xasds1h2xsSsa/",
"actions": [{ "action": "view", "label": "Admin panel", "url": "https://filesrv.lan/admin" }]
}) })
) )
``` ```
@ -779,7 +785,8 @@ is the only required one:
"priority": 4, "priority": 4,
"attach": "https://filesrv.lan/space.jpg", "attach": "https://filesrv.lan/space.jpg",
"filename": "diskspace.jpg", "filename": "diskspace.jpg",
"click": "https://homecamera.lan/xasds1h2xsSsa/" "click": "https://homecamera.lan/xasds1h2xsSsa/",
"actions": [["action": "view", "label": "Admin panel", "url": "https://filesrv.lan/admin" ]]
]) ])
] ]
])); ]));
@ -796,7 +803,7 @@ all the supported fields:
| `title` | - | *string* | `Some title` | Message [title](#message-title) | | `title` | - | *string* | `Some title` | Message [title](#message-title) |
| `tags` | - | *string array* | `["tag1","tag2"]` | List of [tags](#tags-emojis) that may or not map to emojis | | `tags` | - | *string array* | `["tag1","tag2"]` | List of [tags](#tags-emojis) that may or not map to emojis |
| `priority` | - | *int (one of: 1, 2, 3, 4, or 5)* | `4` | Message [priority](#message-priority) with 1=min, 3=default and 5=max | | `priority` | - | *int (one of: 1, 2, 3, 4, or 5)* | `4` | Message [priority](#message-priority) with 1=min, 3=default and 5=max |
| `actions` | - | *JSON array* | *(see [actiom buttons](#action-buttons))* | Custom [user action buttons](#action-buttons) for notifications | | `actions` | - | *JSON array* | *(see [action buttons](#action-buttons))* | Custom [user action buttons](#action-buttons) for notifications |
| `click` | - | *URL* | `https://example.com` | Website opened when notification is [clicked](#click-action) | | `click` | - | *URL* | `https://example.com` | Website opened when notification is [clicked](#click-action) |
| `attach` | - | *URL* | `https://example.com/file.jpg` | URL of an attachment, see [attach via URL](#attach-file-from-url) | | `attach` | - | *URL* | `https://example.com/file.jpg` | URL of an attachment, see [attach via URL](#attach-file-from-url) |
| `filename` | - | *string* | `file.jpg` | File name of the attachment | | `filename` | - | *string* | `file.jpg` | File name of the attachment |
@ -854,7 +861,7 @@ As an example, here's how you can create the above notification using this forma
``` ```
curl \ curl \
-d "You left the house. Turn down the A/C?" \ -d "You left the house. Turn down the A/C?" \
-H "Actions: view, Open portal, https://home.nest.com/; \ -H "Actions: view, Open portal, https://home.nest.com/, clear=true; \
http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65" \ http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65" \
ntfy.sh/myhome ntfy.sh/myhome
``` ```
@ -862,7 +869,7 @@ As an example, here's how you can create the above notification using this forma
=== "ntfy CLI" === "ntfy CLI"
``` ```
ntfy publish \ ntfy publish \
--actions="view, Open portal, https://home.nest.com/; \ --actions="view, Open portal, https://home.nest.com/, clear=true; \
http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65" \ http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65" \
myhome \ myhome \
"You left the house. Turn down the A/C?" "You left the house. Turn down the A/C?"
@ -872,7 +879,7 @@ As an example, here's how you can create the above notification using this forma
``` http ``` http
POST /myhome HTTP/1.1 POST /myhome HTTP/1.1
Host: ntfy.sh Host: ntfy.sh
Actions: view, Open portal, https://home.nest.com/; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65 Actions: view, Open portal, https://home.nest.com/, clear=true; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65
You left the house. Turn down the A/C? You left the house. Turn down the A/C?
``` ```
@ -883,7 +890,7 @@ As an example, here's how you can create the above notification using this forma
method: 'POST', method: 'POST',
body: 'You left the house. Turn down the A/C?', body: 'You left the house. Turn down the A/C?',
headers: { headers: {
'Actions': 'view, Open portal, https://home.nest.com/; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65' 'Actions': 'view, Open portal, https://home.nest.com/, clear=true; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65'
} }
}) })
``` ```
@ -891,14 +898,14 @@ As an example, here's how you can create the above notification using this forma
=== "Go" === "Go"
``` go ``` go
req, _ := http.NewRequest("POST", "https://ntfy.sh/myhome", strings.NewReader("You left the house. Turn down the A/C?")) req, _ := http.NewRequest("POST", "https://ntfy.sh/myhome", strings.NewReader("You left the house. Turn down the A/C?"))
req.Header.Set("Actions", "view, Open portal, https://home.nest.com/; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65") req.Header.Set("Actions", "view, Open portal, https://home.nest.com/, clear=true; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65")
http.DefaultClient.Do(req) http.DefaultClient.Do(req)
``` ```
=== "PowerShell" === "PowerShell"
``` powershell ``` powershell
$uri = "https://ntfy.sh/myhome" $uri = "https://ntfy.sh/myhome"
$headers = @{ Actions="view, Open portal, https://home.nest.com/; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65" } $headers = @{ Actions="view, Open portal, https://home.nest.com/, clear=true; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65" }
$body = "You left the house. Turn down the A/C?" $body = "You left the house. Turn down the A/C?"
Invoke-RestMethod -Method 'Post' -Uri $uri -Headers $headers -Body $body -UseBasicParsing Invoke-RestMethod -Method 'Post' -Uri $uri -Headers $headers -Body $body -UseBasicParsing
``` ```
@ -907,7 +914,7 @@ As an example, here's how you can create the above notification using this forma
``` python ``` python
requests.post("https://ntfy.sh/myhome", requests.post("https://ntfy.sh/myhome",
data="You left the house. Turn down the A/C?", data="You left the house. Turn down the A/C?",
headers={ "Actions": "view, Open portal, https://home.nest.com/; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65" }) headers={ "Actions": "view, Open portal, https://home.nest.com/, clear=true; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65" })
``` ```
=== "PHP" === "PHP"
@ -917,7 +924,7 @@ As an example, here's how you can create the above notification using this forma
'method' => 'POST', 'method' => 'POST',
'header' => 'header' =>
"Content-Type: text/plain\r\n" . "Content-Type: text/plain\r\n" .
"Actions: view, Open portal, https://home.nest.com/; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65", "Actions: view, Open portal, https://home.nest.com/, clear=true; http, Turn down, https://api.nest.com/device/XZ1D2, body=target_temp_f=65",
'content' => 'You left the house. Turn down the A/C?' 'content' => 'You left the house. Turn down the A/C?'
] ]
])); ]));
@ -937,7 +944,8 @@ Alternatively, the same actions can be defined as **JSON array**, if the notific
{ {
"action": "view", "action": "view",
"label": "Open portal", "label": "Open portal",
"url": "https://home.nest.com/" "url": "https://home.nest.com/",
"clear": true
}, },
{ {
"action": "http", "action": "http",
@ -956,7 +964,8 @@ Alternatively, the same actions can be defined as **JSON array**, if the notific
{ {
"action": "view", "action": "view",
"label": "Open portal", "label": "Open portal",
"url": "https://home.nest.com/" "url": "https://home.nest.com/",
"clear": true
}, },
{ {
"action": "http", "action": "http",
@ -981,7 +990,8 @@ Alternatively, the same actions can be defined as **JSON array**, if the notific
{ {
"action": "view", "action": "view",
"label": "Open portal", "label": "Open portal",
"url": "https://home.nest.com/" "url": "https://home.nest.com/",
"clear": true
}, },
{ {
"action": "http", "action": "http",
@ -1004,7 +1014,8 @@ Alternatively, the same actions can be defined as **JSON array**, if the notific
{ {
action: "view", action: "view",
label: "Open portal", label: "Open portal",
url: "https://home.nest.com/" url: "https://home.nest.com/",
clear: true
}, },
{ {
action: "http", action: "http",
@ -1029,7 +1040,8 @@ Alternatively, the same actions can be defined as **JSON array**, if the notific
{ {
"action": "view", "action": "view",
"label": "Open portal", "label": "Open portal",
"url": "https://home.nest.com/" "url": "https://home.nest.com/",
"clear": true
}, },
{ {
"action": "http", "action": "http",
@ -1054,6 +1066,7 @@ Alternatively, the same actions can be defined as **JSON array**, if the notific
"action"="view" "action"="view"
"label"="Open portal" "label"="Open portal"
"url"="https://home.nest.com/" "url"="https://home.nest.com/"
"clear"=true
}, },
@{ @{
"action"="http", "action"="http",
@ -1076,7 +1089,8 @@ Alternatively, the same actions can be defined as **JSON array**, if the notific
{ {
"action": "view", "action": "view",
"label": "Open portal", "label": "Open portal",
"url": "https://home.nest.com/" "url": "https://home.nest.com/",
"clear": true
}, },
{ {
"action": "http", "action": "http",
@ -1102,7 +1116,8 @@ Alternatively, the same actions can be defined as **JSON array**, if the notific
[ [
"action": "view", "action": "view",
"label": "Open portal", "label": "Open portal",
"url": "https://home.nest.com/" "url": "https://home.nest.com/",
"clear": true
], ],
[ [
"action": "http", "action": "http",
@ -1355,11 +1370,12 @@ And the same example using [JSON publishing](#publish-as-json):
The `view` action supports the following fields: The `view` action supports the following fields:
| Field | Required | Type | Example | Description | | Field | Required | Type | Default | Example | Description |
|----------|----------|----------|-----------------------|------------------------------------------------| |----------|----------|-----------|---------|-----------------------|--------------------------------------------------|
| `action` | ✔️ | *string* | `view` | Action type (**must be `view`**) | | `action` | ✔️ | *string* | - | `view` | Action type (**must be `view`**) |
| `label` | ✔️ | *string* | `Turn on light` | Label of the action button in the notification | | `label` | ✔️ | *string* | - | `Turn on light` | Label of the action button in the notification |
| `url` | ✔️ | *URL* | `https://example.com` | URL to open when action is tapped | | `url` | ✔️ | *URL* | - | `https://example.com` | URL to open when action is tapped |
| `clear` | - | *boolean* | `false` | `true` | Clear notification after action button is tapped |
### Send Android broadcast ### Send Android broadcast
The `broadcast` action **sends an [Android broadcast](https://developer.android.com/guide/components/broadcasts) intent The `broadcast` action **sends an [Android broadcast](https://developer.android.com/guide/components/broadcasts) intent
@ -1368,9 +1384,14 @@ or [Tasker](https://play.google.com/store/apps/details?id=net.dinglisch.android.
you can do everything your phone is capable of. Examples include taking pictures, launching/killing apps, change device you can do everything your phone is capable of. Examples include taking pictures, launching/killing apps, change device
settings, write/read files, etc. settings, write/read files, etc.
By default, the intent **`io.heckel.ntfy.USER_ACTION`** is broadcast, though this can be changed with the `intent` parameter (see below). By default, the intent action **`io.heckel.ntfy.USER_ACTION`** is broadcast, though this can be changed with the `intent` parameter (see below).
To send extras, use the `extras` parameter. Currently, **only string extras are supported**. To send extras, use the `extras` parameter. Currently, **only string extras are supported**.
!!! info
If you have no idea what this is, check out the [automation apps](subscribe/phone.md#automation-apps) section, which shows
how to integrate Tasker and MacroDroid **with screenshots**. The action button integration is identical, except that
you have to use **the intent action `io.heckel.ntfy.USER_ACTION`** instead.
Here's an example using the [`X-Actions` header](#using-a-header): Here's an example using the [`X-Actions` header](#using-a-header):
=== "Command line (curl)" === "Command line (curl)"
@ -1612,12 +1633,13 @@ And the same example using [JSON publishing](#publish-as-json):
The `broadcast` action supports the following fields: The `broadcast` action supports the following fields:
| Field | Required | Type | Example | Description | | Field | Required | Type | Default | Example | Description |
|----------|----------|------------------|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |----------|----------|------------------|------------------------------|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `action` | ✔️ | *string* | `broadcast` | Action type (**must be `broadcast`**) | | `action` | ✔️ | *string* | - | `broadcast` | Action type (**must be `broadcast`**) |
| `label` | ✔️ | *string* | `Turn on light` | Label of the action button in the notification | | `label` | ✔️ | *string* | - | `Turn on light` | Label of the action button in the notification |
| `intent` | - | *string* | `com.example.AN_INTENT` | Android intent name, **default is `io.heckel.ntfy.USER_ACTION`** | | `intent` | - | *string* | `io.heckel.ntfy.USER_ACTION` | `com.example.AN_INTENT` | Android intent name, **default is `io.heckel.ntfy.USER_ACTION`** |
| `extras` | - | *map of strings* | *see above* | Android intent extras. Currently, only string extras are supported. When publishing as JSON, extras are passed as a map. When the simple format is used, use `extras.<param>=<value>`. | | `extras` | - | *map of strings* | - | *see above* | Android intent extras. Currently, only string extras are supported. When publishing as JSON, extras are passed as a map. When the simple format is used, use `extras.<param>=<value>`. |
| `clear` | - | *boolean* | `false` | `true` | Clear notification after action button is tapped |
### Send HTTP request ### Send HTTP request
The `http` action **sends a HTTP request when the action button is tapped**. You can use this to trigger REST APIs The `http` action **sends a HTTP request when the action button is tapped**. You can use this to trigger REST APIs
@ -1885,14 +1907,15 @@ And the same example using [JSON publishing](#publish-as-json):
The `http` action supports the following fields: The `http` action supports the following fields:
| Field | Required | Type | Example | Description | | Field | Required | Type | Default | Example | Description |
|-----------|----------|--------------------|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------| |-----------|----------|--------------------|-----------|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
| `action` | ✔️ | *string* | `http` | Action type (**must be `http`**) | | `action` | ✔️ | *string* | - | `http` | Action type (**must be `http`**) |
| `label` | ✔️ | *string* | `Open garage door` | Label of the action button in the notification | | `label` | ✔️ | *string* | - | `Open garage door` | Label of the action button in the notification |
| `url` | ✔️ | *string* | `https://ntfy.sh/mytopic` | URL to which the HTTP request will be sent | | `url` | ✔️ | *string* | - | `https://ntfy.sh/mytopic` | URL to which the HTTP request will be sent |
| `method` | - | *GET/POST/PUT/...* | `GET` | HTTP method to use for request, **default is POST (!)** | | `method` | - | *GET/POST/PUT/...* | `POST` ⚠️ | `GET` | HTTP method to use for request, **default is POST** ⚠️ |
| `headers` | - | *map of strings* | *see above* | HTTP headers to pass in request. When publishing as JSON, headers are passed as a map. When the simple format is used, use `headers.<header1>=<value>`. | | `headers` | - | *map of strings* | - | *see above* | HTTP headers to pass in request. When publishing as JSON, headers are passed as a map. When the simple format is used, use `headers.<header1>=<value>`. |
| `body` | - | *string* | `some body, somebody?` | HTTP body | | `body` | - | *string* | *empty* | `some body, somebody?` | HTTP body |
| `clear` | - | *boolean* | `false` | `true` | Clear notification after HTTP request succeeds. If the request fails, the notification is not cleared. |
## Click action ## Click action
You can define which URL to open when a notification is clicked. This may be useful if your notification is related You can define which URL to open when a notification is clicked. This may be useful if your notification is related

View File

@ -39,7 +39,7 @@ var (
errHTTPBadRequestAttachmentsExpiryBeforeDelivery = &errHTTP{40015, http.StatusBadRequest, "invalid request: attachment expiry before delayed delivery date", "https://ntfy.sh/docs/publish/#scheduled-delivery"} errHTTPBadRequestAttachmentsExpiryBeforeDelivery = &errHTTP{40015, http.StatusBadRequest, "invalid request: attachment expiry before delayed delivery date", "https://ntfy.sh/docs/publish/#scheduled-delivery"}
errHTTPBadRequestWebSocketsUpgradeHeaderMissing = &errHTTP{40016, http.StatusBadRequest, "invalid request: client not using the websocket protocol", "https://ntfy.sh/docs/subscribe/api/#websockets"} errHTTPBadRequestWebSocketsUpgradeHeaderMissing = &errHTTP{40016, http.StatusBadRequest, "invalid request: client not using the websocket protocol", "https://ntfy.sh/docs/subscribe/api/#websockets"}
errHTTPBadRequestJSONInvalid = &errHTTP{40017, http.StatusBadRequest, "invalid request: request body must be message JSON", "https://ntfy.sh/docs/publish/#publish-as-json"} errHTTPBadRequestJSONInvalid = &errHTTP{40017, http.StatusBadRequest, "invalid request: request body must be message JSON", "https://ntfy.sh/docs/publish/#publish-as-json"}
errHTTPBadRequestActionsInvalid = &errHTTP{40018, http.StatusBadRequest, "invalid request: actions are invalid format", "https://ntfy.sh/docs/publish/#action-buttons"} errHTTPBadRequestActionsInvalid = &errHTTP{40018, http.StatusBadRequest, "invalid request: actions not valid", "https://ntfy.sh/docs/publish/#action-buttons"}
errHTTPNotFound = &errHTTP{40401, http.StatusNotFound, "page not found", ""} errHTTPNotFound = &errHTTP{40401, http.StatusNotFound, "page not found", ""}
errHTTPUnauthorized = &errHTTP{40101, http.StatusUnauthorized, "unauthorized", "https://ntfy.sh/docs/publish/#authentication"} errHTTPUnauthorized = &errHTTP{40101, http.StatusUnauthorized, "unauthorized", "https://ntfy.sh/docs/publish/#authentication"}
errHTTPForbidden = &errHTTP{40301, http.StatusForbidden, "forbidden", "https://ntfy.sh/docs/publish/#authentication"} errHTTPForbidden = &errHTTP{40301, http.StatusForbidden, "forbidden", "https://ntfy.sh/docs/publish/#authentication"}

View File

@ -45,8 +45,9 @@ type attachment struct {
type action struct { type action struct {
ID string `json:"id"` ID string `json:"id"`
Action string `json:"action"` Action string `json:"action"` // "view", "broadcast", or "http"
Label string `json:"label"` // "view", "broadcast", or "http" Label string `json:"label"` // action button label
Clear bool `json:"clear"` // clear notification after successful execution
URL string `json:"url,omitempty"` // used in "view" and "http" actions URL string `json:"url,omitempty"` // used in "view" and "http" actions
Method string `json:"method,omitempty"` // used in "http" action, default is POST (!) Method string `json:"method,omitempty"` // used in "http" action, default is POST (!)
Headers map[string]string `json:"headers,omitempty"` // used in "http" action Headers map[string]string `json:"headers,omitempty"` // used in "http" action

View File

@ -121,6 +121,9 @@ func parseActionsFromSimple(s string) ([]*action, error) {
newAction.Action = value newAction.Action = value
case "label": case "label":
newAction.Label = value newAction.Label = value
case "clear":
lvalue := strings.ToLower(value)
newAction.Clear = lvalue == "true" || lvalue == "yes" || lvalue == "1"
case "url": case "url":
newAction.URL = value newAction.URL = value
case "method": case "method":