SimpleCloudNotifier/scnserver/website/api_more.html

218 lines
10 KiB
HTML
Raw Normal View History

2022-11-18 21:25:40 +01:00
<!DOCTYPE html>
<html lang="en">
<head>
2022-11-26 16:30:30 +01:00
{{template|header.[theme].html}}
2022-11-18 21:25:40 +01:00
</head>
<body>
<div id="copyinfo">
<a tabindex="-1" href="https://www.blackforestbytes.com">&#169; blackforestbytes</a>
<a tabindex="-1" href="https://www.mikescher.com">made by Mike Schw&ouml;rer</a>
</div>
2022-11-26 16:30:30 +01:00
{{template|theme_switch.[theme].html}}
2022-11-21 22:52:44 +01:00
2022-11-26 16:30:30 +01:00
<div id="mainpnl">
<a tabindex="-1" href="/documentation/swagger" class="button bordered edge-btn" id="tl_linkDocs"><span class="icn-openapi"></span><span class="tl_btntxt">API Documentation</span></a>
<a tabindex="-1" href="/" class="button bordered edge-btn" id="tr_link">Send</a>
2022-11-18 21:25:40 +01:00
<a tabindex="-1" href="/" class="linkcaption"><h1>Simple Cloud Notifier</h1></a>
<h2>Introduction</h2>
<div class="section">
<p>
With this API you can send push notifications to your phone.
</p>
<p>
2022-11-21 22:52:44 +01:00
To receive them you will need to install the <a href="https://play.google.com/store/apps/details?id=com.blackforestbytes.simplecloudnotifier">SimpleCloudNotifier</a> app from the play store.
2023-04-21 21:45:16 +02:00
When you open the app you can click on the account tab to see you unique <code>user_id</code> and <code>key</code>.
2022-11-18 21:25:40 +01:00
These two values are used to identify and authenticate your device so that send messages can be routed to your phone.
</p>
<p>
2023-04-21 21:45:16 +02:00
You can at any time generate a new <code>key</code> in the app and invalidate the old one.
2022-11-18 21:25:40 +01:00
</p>
<p>
There is also a <a href="/">web interface</a> for this API to manually send notifications to your phone or to test your setup.
</p>
</div>
<h2>Quota</h2>
<div class="section">
<p>
2022-11-21 22:52:44 +01:00
By default you can send up to 50 messages per day per device.
2022-11-18 21:25:40 +01:00
If you need more you can upgrade your account in the app to get 1000 messages per day, this has the additional benefit of removing ads and supporting the development of the app (and making sure I can pay the server costs).
</p>
</div>
<h2>API Requests</h2>
<div class="section">
<p>
To send a new notification you send a <code>POST</code> request to the URL <code>{{config|baseURL}}/</code>.
2022-11-21 22:52:44 +01:00
All Parameters can either directly be submitted as URL parameters or they can be put into the POST body (either multipart/form-data or JSON).
2022-11-18 21:25:40 +01:00
</p>
<p>
2023-04-21 21:45:16 +02:00
You <i>need</i> to supply a valid <code>[user_id, key]</code> pair and a <code>title</code> for your message, all other parameter are optional.
2022-11-18 21:25:40 +01:00
</p>
</div>
<h2>API Response</h2>
<div class="section">
<p>
If the operation was successful the API will respond with an HTTP statuscode 200 and an JSON payload indicating the send message and your remaining quota
</p>
<pre class="red-code">{
"success":true,
"message":"Message sent",
2022-11-26 16:30:30 +01:00
"messagecount": 634,
2022-11-18 21:25:40 +01:00
"quota":17,
2022-11-26 16:30:30 +01:00
"quota_max":100,
"scn_msg_id":"..."
2022-11-18 21:25:40 +01:00
}</pre>
<p>
2022-11-21 22:52:44 +01:00
If the operation is <b>not</b> successful the API will respond with a 4xx or 500 HTTP statuscode.
2022-11-18 21:25:40 +01:00
</p>
<table class="scode_table">
<thead>
<tr>
<th>Statuscode</th>
<th>Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Statuscode">200 (OK)</td>
<td data-label="Explanation">Message sent</td>
</tr>
<tr>
<td data-label="Statuscode">400 (Bad Request)</td>
<td data-label="Explanation">The request is invalid (missing parameters or wrong values)</td>
</tr>
<tr>
<td data-label="Statuscode">401 (Unauthorized)</td>
2023-04-21 21:45:16 +02:00
<td data-label="Explanation">The user_id was not found or the key is wrong</td>
2022-11-18 21:25:40 +01:00
</tr>
<tr>
<td data-label="Statuscode">403 (Forbidden)</td>
<td data-label="Explanation">The user has exceeded its daily quota - wait 24 hours or upgrade your account</td>
</tr>
<tr>
<td data-label="Statuscode">500 (Internal Server Error)</td>
<td data-label="Explanation">There was an internal error while sending your data - try again later</td>
</tr>
</tbody>
</table>
<p>
There is also always a JSON payload with additional information.
2022-11-26 16:30:30 +01:00
The <code>success</code> field is always there and in the error case you can read the <code>message</code> field to get a more information about the problem.
2022-11-18 21:25:40 +01:00
</p>
<pre class="red-code">{
2022-11-21 22:52:44 +01:00
"success": false,
"error": 2101,
"errhighlight": -1,
"message": "Daily quota reached (100)"
2022-11-18 21:25:40 +01:00
}</pre>
</div>
<h2>Message Content</h2>
<div class="section">
<p>
Every message must have a title set.
2022-11-21 22:52:44 +01:00
But you also (optionally) add more content, while the title has a max length of 120 characters, the content can be up to 10.000 characters.
2022-11-18 21:25:40 +01:00
You can see the whole message with title and content in the app or when clicking on the notification.
</p>
<p>
If needed the content can be supplied in the <code>content</code> parameter.
</p>
<pre>curl \
--data "user_id={userid}" \
2023-04-21 21:45:16 +02:00
--data "key={key}" \
2022-11-18 21:25:40 +01:00
--data "title={message_title}" \
--data "content={message_content}" \
{{config|baseURL}}/</pre>
2022-11-18 21:25:40 +01:00
</div>
<h2>Message Priority</h2>
<div class="section">
<p>
Currently you can send a message with three different priorities: 0 (low), 1 (normal) and 2 (high).
In the app you can then configure a different behaviour for different priorities, e.g. only playing a sound if the notification is high priority.
</p>
<p>
Priorites are either 0, 1 or 2 and are supplied in the <code>priority</code> parameter.
If no priority is supplied the message will get the default priority of 1.
</p>
<pre>curl \
--data "user_id={userid}" \
2023-04-21 21:45:16 +02:00
--data "key={key}" \
2022-11-18 21:25:40 +01:00
--data "title={message_title}" \
--data "priority={0|1|2}" \
{{config|baseURL}}/</pre>
2022-11-18 21:25:40 +01:00
</div>
2022-11-26 16:30:30 +01:00
<h2>Channel</h2>
2022-11-21 22:52:44 +01:00
<div class="section">
<p>
By default all messages are sent to the user default channel (typically <code>main</code>)
You can specify a different channel with the <code>channel</code> parameter, if the channel does not already exist it will be created.
Channel names are case-insensitive and can only contain letters, numbers, underscores and minuses ( <code>/[[:alnum:]\-_]+/</code> )
</p>
<pre>curl \
--data "user_id={userid}" \
2023-04-21 21:45:16 +02:00
--data "key={key}" \
2022-11-21 22:52:44 +01:00
--data "title={message_title}" \
--data "channel={my_channel}" \
{{config|baseURL}}/</pre>
2022-11-21 22:52:44 +01:00
</div>
2022-11-30 21:39:14 +01:00
<h2>Message Uniqueness (Idempotency)</h2>
2022-11-18 21:25:40 +01:00
<div class="section">
<p>
Sometimes your script can run in an environment with an unstable connection and you want to implement an automatic re-try mechanism to send a message again if the last try failed due to bad connectivity.
</p>
<p>
To ensure that a message is only send once you can generate a unique id for your message (I would recommend a simple <code>uuidgen</code>).
2022-11-21 22:52:44 +01:00
If you send a message with a UUID that was already used in the near past the API still returns OK, but no new message is sent.
2022-11-18 21:25:40 +01:00
</p>
<p>
The message_id is optional - but if you want to use it you need to supply it via the <code>msg_id</code> parameter.
</p>
<pre>curl \
--data "user_id={userid}" \
2023-04-21 21:45:16 +02:00
--data "key={key}" \
2022-11-18 21:25:40 +01:00
--data "title={message_title}" \
--data "msg_id={message_id}" \
{{config|baseURL}}/</pre>
2022-11-18 21:25:40 +01:00
<p>
Be aware that the server only saves send messages for a short amount of time. Because of that you can only use this to prevent duplicates in a short time-frame, older messages with the same ID are probably already deleted and the message will be send again.
</p>
</div>
<h2>Custom Time</h2>
<div class="section">
<p>
You can modify the displayed timestamp of a message by sending the <code>timestamp</code> parameter. The format must be a valid UNIX timestamp (elapsed seconds since 1970-01-01 GMT)
</p>
<p>
The custom timestamp must be within 48 hours of the current time. This parameter is only intended to supply a more precise value in case the message sending was delayed.
</p>
<pre>curl \
--data "user_id={userid}" \
2023-04-21 21:45:16 +02:00
--data "key={key}" \
2022-11-18 21:25:40 +01:00
--data "title={message_title}" \
--data "timestamp={unix_timestamp}" \
{{config|baseURL}}/</pre>
2022-11-18 21:25:40 +01:00
</div>
<h2>Bash script example</h2>
<div class="section">
<p>
Depending on your use case it can be useful to create a bash script that handles things like resending messages if you have connection problems or waiting if there is no quota left.<br/>
2022-11-21 22:52:44 +01:00
Here is an example how such a scrippt could look like, you can put it into <code>/usr/local/sbin</code> and call it with <code>scn_send "title" "content"</code> (or with more parameters, see the script itself)
2022-11-18 21:25:40 +01:00
</p>
2022-11-26 16:30:30 +01:00
{{template|scn_send.[theme].html}}
2022-11-18 21:25:40 +01:00
</div>
</div>
</body>
</html>