SimpleCloudNotifier/scnserver/website/api_more.html

269 lines
13 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
{{template|header.[theme].html}}
</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>
{{template|theme_switch.[theme].html}}
<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>
<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>
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.
When you open the app you can click on the account tab to see you unique <code>user_id</code> and <code>key</code>.
These two values are used to identify and authenticate your device so that send messages can be routed to your phone.
</p>
<p>
You can at any time generate new <code>key</code>s in the app with different permissions.
</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>
By default you can send up to 50 messages per day per device.
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>.
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).
</p>
<p>
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.
</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",
"messagecount": 634,
"quota":17,
"quota_max":100,
"scn_msg_id":"..."
}</pre>
<p>
If the operation is <b>not</b> successful the API will respond with a 4xx or 500 HTTP statuscode.
</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>
<td data-label="Explanation">The user_id was not found, the key is wrong or the [user_id, key] combination does not have the SEND permissions on the specified channel</td>
</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.
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.
</p>
<pre class="red-code">{
"success": false,
"error": 2101,
"errhighlight": -1,
"message": "Daily quota reached (100)"
}</pre>
</div>
<h2>Message Content</h2>
<div class="section">
<p>
Every message must have a title set.
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.
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}" \
--data "key={key}" \
--data "title={message_title}" \
--data "content={message_content}" \
{{config|baseURL}}/</pre>
</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}" \
--data "key={key}" \
--data "title={message_title}" \
--data "priority={0|1|2}" \
{{config|baseURL}}/</pre>
</div>
<h2>Channel</h2>
<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}" \
--data "key={key}" \
--data "title={message_title}" \
--data "channel={my_channel}" \
{{config|baseURL}}/</pre>
</div>
<h2>Permissions</h2>
<div class="section">
<p>
A user account can have multiple keys with different permissions.
A Key has one or more permissions assigned:
</p>
<table class="permlist_table">
<thead>
<tr>
<th>Permission</th>
<th>Identifier</th>
<th>Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Permission" >ADMIN</td>
<td data-label="Identifier" ><code>A</code></td>
<td data-label="Explanation">Allows modification of the current user, creating/editing keys, channels, subsriptions, etc. and includes all other permissions</td>
</tr>
<tr>
<td data-label="Permission" >CHANNEL READ</td>
<td data-label="Identifier" ><code>CR</code></td>
<td data-label="Explanation">Allows reading and listing messages</td>
</tr>
<tr>
<td data-label="Permission" >CHANNEL SEND</td>
<td data-label="Identifier" ><code>CS</code></td>
<td data-label="Explanation">Allows sending messages</td>
</tr>
<tr>
<td data-label="Permission" >USER READ</td>
<td data-label="Identifier" ><code>UR</code></td>
<td data-label="Explanation">Allows querying the current user</td>
</tr>
</tbody>
</table>
<p>
Keys can also be scoped to specific channels.
A Key can either have access to all channels the user has access to, or only to a subset.
The permitted channels can either be channels of the user or foreign channels with an active subscription.
</p>
<p>
A common use case is to create a key with only the <code>CS</code> (Channel Send) permission and only a single channel.
This key can then be used to send messages without having full access to the account.
</p>
</div>
<h2>Message Uniqueness (Idempotency)</h2>
<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> or <code>head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32</code>).
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.
</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}" \
--data "key={key}" \
--data "title={message_title}" \
--data "msg_id={message_id}" \
{{config|baseURL}}/</pre>
<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}" \
--data "key={key}" \
--data "title={message_title}" \
--data "timestamp={unix_timestamp}" \
{{config|baseURL}}/</pre>
</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/>
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)
</p>
<div class="yellow-code">{{template|scn_send.html}}</div>
</div>
</div>
</body>
</html>