From fd72b512f8b3c401b6500f8217bc357e2286f45a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Get your user-id and user-key from the android or iOS app. Most parameters are optional, you can send a message with only a title (default priority and channel will be used)
And send notifications to your phone by performing a POST request against {{config|baseURL}}/
from anywhere
-curl \
- --data "user_id=${userid}" \
- --data "key=${key}" \
- --data "title=${message_title}" \
- --data "content=${message_body}" \
- --data "priority=${0|1|2}" \
- --data "msg_id=$(uuidgen)" \
- --data "timestamp=$(date +%s)" \
- --data "channel={channel_name}" \
- --data "sender_name=$(hostname)" \
+curl \
+ --data "user_id=${userid}" \
+ --data "key=${key}" \
+ --data "title=${message_title}" \
+ --data "content=${message_body}" \
+ --data "priority=${0|1|2}" \
+ --data "msg_id=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32)" \
+ --data "timestamp=$(date +%s)" \
+ --data "channel={channel_name}" \
+ --data "sender_name=$(hostname)" \
{{config|baseURL}}/
diff --git a/scnserver/website/api_more.html b/scnserver/website/api_more.html
index 263f64a..a2c0d43 100644
--- a/scnserver/website/api_more.html
+++ b/scnserver/website/api_more.html
@@ -171,7 +171,7 @@
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.
- To ensure that a message is only send once you can generate a unique id for your message (I would recommend a simple uuidgen
).
+ To ensure that a message is only send once you can generate a unique id for your message (I would recommend a simple uuidgen
or head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32
).
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.
@@ -210,7 +210,7 @@
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.
Here is an example how such a scrippt could look like, you can put it into /usr/local/sbin
and call it with scn_send "title" "content"
(or with more parameters, see the script itself)
-#!/usr/bin/env bash - -# -# Wrapper around SCN ( https://scn.blackforestbytes.com/ ) -# ======================================================== -# -# ./scn_send [@channel] title [content] [priority] -# -# -# Call with scn_send "${title}" -# or scn_send "${title}" ${content}" -# or scn_send "${title}" ${content}" "${priority:0|1|2}" -# or scn_send "@${channel} "${title}" -# or scn_send "@${channel} "${title}" ${content}" -# or scn_send "@${channel} "${title}" ${content}" "${priority:0|1|2}" -# -# - -################################################################################ -# INSERT YOUR DATA HERE # -################################################################################ -user_id="999" -user_key="??" -################################################################################ - -usage() { - echo "Usage: " - echo " scn_send [@channel] title [content] [priority]" - echo "" -} - -args=( "$@" ) - -title=$1 -content="" -channel="" -priority=1 -usr_msg_id="$(uuidgen)" -sendtime="$(date +%s)" -sender="$(hostname)" - -if [ ${#args[@]} -lt 1 ]; then - echo "[ERROR]: no title supplied via parameter" 1>&2 - usage - exit 1 -fi - -if [[ "${args[0]}" =~ ^@.* ]]; then - channel="${args[0]}" - unset "args[0]" - channel="${channel:1}" -fi - -if [ ${#args[@]} -lt 1 ]; then - echo "[ERROR]: no title supplied via parameter" 1>&2 - usage - exit 1 -fi - -title="${args[0]}" -content="" - -if [ ${#args[@]} -gt 1 ]; then - content="${args[0]}" - unset "args[0]" -fi - -if [ ${#args[@]} -gt 1 ]; then - priority="${args[0]}" - unset "args[0]" -fi - -if [ ${#args[@]} -gt 1 ]; then - echo "Too many arguments to scn_send" 1>&2 - usage - exit 1 -fi - - -while true ; do - - curlresp=$(curl --silent \ - --output /dev/null \ - --write-out "%{http_code}" \ - --data "user_id=$user_id" \ - --data "key=$key" \ - --data "title=$title" \ - --data "timestamp=$sendtime" \ - --data "content=$content" \ - --data "priority=$priority" \ - --data "msg_id=$usr_msg_id" \ - --data "channel=$channel" \ - --data "sender_name=$sender" \ - "https://scn.blackforestbytes.com/" ) - - if [ "$curlresp" == 200 ] ; then - echo "Successfully send" - exit 0 - fi - - if [ "$curlresp" == 400 ] ; then - echo "Bad request - something went wrong" 1>&2 - exit 1 - fi - - if [ "$curlresp" == 401 ] ; then - echo "Unauthorized - wrong userid/userkey" 1>&2 - exit 1 - fi - - if [ "$curlresp" == 403 ] ; then - echo "Quota exceeded - wait one hour before re-try" 1>&2 - sleep 3600 - fi - - if [ "$curlresp" == 412 ] ; then - echo "Precondition Failed - No device linked" 1>&2 - exit 1 - fi - - if [ "$curlresp" == 500 ] ; then - echo "Internal server error - waiting for better times" 1>&2 - sleep 60 - fi - - # if none of the above matched we probably hav no network ... - echo "Send failed (response code $curlresp) ... try again in 5s" 1>&2 - sleep 5 -done - -- diff --git a/scnserver/website/scn_send.html b/scnserver/website/scn_send.html new file mode 100644 index 0000000..81e2913 --- /dev/null +++ b/scnserver/website/scn_send.html @@ -0,0 +1,142 @@ +
#!/usr/bin/env bash
+
+#
+# Wrapper around SCN ( https://scn.blackforestbytes.com/ )
+# ========================================================
+#
+# ./scn_send [@channel] title [content] [priority]
+#
+#
+# Call with scn_send "${title}"
+# or scn_send "${title}" ${content}"
+# or scn_send "${title}" ${content}" "${priority:0|1|2}"
+# or scn_send "@${channel} "${title}"
+# or scn_send "@${channel} "${title}" ${content}"
+# or scn_send "@${channel} "${title}" ${content}" "${priority:0|1|2}"
+#
+#
+
+################################################################################
+# INSERT YOUR DATA HERE #
+################################################################################
+user_id="999" # your user_id
+user_key="??" # use userkey with SEND permissions on the used channel
+################################################################################
+
+usage() {
+ echo "Usage: "
+ echo " scn_send [@channel] title [content] [priority]"
+ echo ""
+}
+
+function cfgcol { [ -t 1 ] && [ -n "$(tput colors)" ] && [ "$(tput colors)" -ge 8 ]; }
+
+function rederr() { if cfgcol; then >&2 echo -e "\x1B[31m$1\x1B[0m"; else >&2 echo "$1"; fi; }
+function green() { if cfgcol; then echo -e "\x1B[32m$1\x1B[0m"; else echo "$1"; fi; }
+
+args=( "$@" )
+
+title=$1
+content=""
+channel=""
+priority=1
+usr_msg_id="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32)"
+sendtime="$(date +%s)"
+sender="$(hostname)"
+
+if [ ${#args[@]} -lt 1 ]; then
+ rederr "[ERROR]: no title supplied via parameter"
+ usage
+ exit 1
+fi
+
+if [[ "${args[0]}" =~ ^@.* ]]; then
+ channel="${args[0]}"
+ unset "args[0]"
+ channel="${channel:1}"
+fi
+
+if [ ${#args[@]} -lt 1 ]; then
+ rederr "[ERROR]: no title supplied via parameter"
+ usage
+ exit 1
+fi
+
+title="${args[0]}"
+content=""
+
+if [ ${#args[@]} -gt 1 ]; then
+ content="${args[0]}"
+ unset "args[0]"
+fi
+
+if [ ${#args[@]} -gt 1 ]; then
+ priority="${args[0]}"
+ unset "args[0]"
+fi
+
+if [ ${#args[@]} -gt 1 ]; then
+ rederr "Too many arguments to scn_send"
+ usage
+ exit 1
+fi
+
+
+while true ; do
+
+ outf="$(mktemp)"
+
+ curlresp=$(curl --silent \
+ --output "${outf}" \
+ --write-out "%{http_code}" \
+ --data "user_id=$user_id" \
+ --data "key=$user_key" \
+ --data "title=$title" \
+ --data "timestamp=$sendtime" \
+ --data "content=$content" \
+ --data "priority=$priority" \
+ --data "msg_id=$usr_msg_id" \
+ --data "channel=$channel" \
+ --data "sender_name=$sender" \
+ "https://scn.blackforestbytes.com/" )
+
+ curlout="$(cat "$outf")"
+ rm "$outf"
+
+ if [ "$curlresp" == 200 ] ; then
+ green "Successfully send"
+ exit 0
+ fi
+
+ if [ "$curlresp" == 400 ] ; then
+ rederr "Bad request - something went wrong"
+ echo "$curlout"
+ echo ""
+ exit 1
+ fi
+
+ if [ "$curlresp" == 401 ] ; then
+ rederr "Unauthorized - wrong userid/userkey"
+ exit 1
+ fi
+
+ if [ "$curlresp" == 403 ] ; then
+ rederr "Quota exceeded - wait 5 min before re-try"
+ sleep 300
+ fi
+
+ if [ "$curlresp" == 412 ] ; then
+ rederr "Precondition Failed - No device linked"
+ exit 1
+ fi
+
+ if [ "$curlresp" == 500 ] ; then
+ rederr "Internal server error - waiting for better times"
+ sleep 60
+ fi
+
+ # if none of the above matched we probably have no network ...
+ rederr "Send failed (response code $curlresp) ... try again in 5s"
+ sleep 5
+done
+
-#!/usr/bin/env bash - -# -# Wrapper around SCN ( https://scn.blackforestbytes.com/ ) -# ======================================================== -# -# ./scn_send [@channel] title [content] [priority] -# -# -# Call with scn_send "${title}" -# or scn_send "${title}" ${content}" -# or scn_send "${title}" ${content}" "${priority:0|1|2}" -# or scn_send "@${channel} "${title}" -# or scn_send "@${channel} "${title}" ${content}" -# or scn_send "@${channel} "${title}" ${content}" "${priority:0|1|2}" -# -# - -################################################################################ -# INSERT YOUR DATA HERE # -################################################################################ -user_id="999" -key="??" -################################################################################ - -usage() { - echo "Usage: " - echo " scn_send [@channel] title [content] [priority]" - echo "" -} - -args=( "$@" ) - -title=$1 -content="" -channel="" -priority=1 -usr_msg_id="$(uuidgen)" -sendtime="$(date +%s)" -sender="$(hostname)" - -if [ ${#args[@]} -lt 1 ]; then - echo "[ERROR]: no title supplied via parameter" 1>&2 - usage - exit 1 -fi - -if [[ "${args[0]}" =~ ^@.* ]]; then - channel="${args[0]}" - unset "args[0]" - channel="${channel:1}" -fi - -if [ ${#args[@]} -lt 1 ]; then - echo "[ERROR]: no title supplied via parameter" 1>&2 - usage - exit 1 -fi - -title="${args[0]}" -content="" - -if [ ${#args[@]} -gt 1 ]; then - content="${args[0]}" - unset "args[0]" -fi - -if [ ${#args[@]} -gt 1 ]; then - priority="${args[0]}" - unset "args[0]" -fi - -if [ ${#args[@]} -gt 1 ]; then - echo "Too many arguments to scn_send" 1>&2 - usage - exit 1 -fi - - -while true ; do - - curlresp=$(curl --silent \ - --output /dev/null \ - --write-out "%{http_code}" \ - --data "user_id=$user_id" \ - --data "key=$key" \ - --data "title=$title" \ - --data "timestamp=$sendtime" \ - --data "content=$content" \ - --data "priority=$priority" \ - --data "msg_id=$usr_msg_id" \ - --data "channel=$channel" \ - --data "sender_name=$sender" \ - "https://scn.blackforestbytes.com/" ) - - if [ "$curlresp" == 200 ] ; then - echo "Successfully send" - exit 0 - fi - - if [ "$curlresp" == 400 ] ; then - echo "Bad request - something went wrong" 1>&2 - exit 1 - fi - - if [ "$curlresp" == 401 ] ; then - echo "Unauthorized - wrong userid/userkey" 1>&2 - exit 1 - fi - - if [ "$curlresp" == 403 ] ; then - echo "Quota exceeded - wait one hour before re-try" 1>&2 - sleep 3600 - fi - - if [ "$curlresp" == 412 ] ; then - echo "Precondition Failed - No device linked" 1>&2 - exit 1 - fi - - if [ "$curlresp" == 500 ] ; then - echo "Internal server error - waiting for better times" 1>&2 - sleep 60 - fi - - # if none of the above matched we probably hav no network ... - echo "Send failed (response code $curlresp) ... try again in 5s" 1>&2 - sleep 5 -done -\ No newline at end of file diff --git a/scnserver/website/scn_send.sh.txt b/scnserver/website/scn_send.sh.txt new file mode 100644 index 0000000..bd03dbe --- /dev/null +++ b/scnserver/website/scn_send.sh.txt @@ -0,0 +1,141 @@ +#!/usr/bin/env bash + +# +# Wrapper around SCN ( https://scn.blackforestbytes.com/ ) +# ======================================================== +# +# ./scn_send [@channel] title [content] [priority] +# +# +# Call with scn_send "${title}" +# or scn_send "${title}" ${content}" +# or scn_send "${title}" ${content}" "${priority:0|1|2}" +# or scn_send "@${channel} "${title}" +# or scn_send "@${channel} "${title}" ${content}" +# or scn_send "@${channel} "${title}" ${content}" "${priority:0|1|2}" +# +# + +################################################################################ +# INSERT YOUR DATA HERE # +################################################################################ +user_id="999" # your user_id +user_key="??" # use userkey with SEND permissions on the used channel +################################################################################ + +usage() { + echo "Usage: " + echo " scn_send [@channel] title [content] [priority]" + echo "" +} + +function cfgcol { [ -t 1 ] && [ -n "$(tput colors)" ] && [ "$(tput colors)" -ge 8 ]; } + +function rederr() { if cfgcol; then >&2 echo -e "\x1B[31m$1\x1B[0m"; else >&2 echo "$1"; fi; } +function green() { if cfgcol; then echo -e "\x1B[32m$1\x1B[0m"; else echo "$1"; fi; } + +args=( "$@" ) + +title=$1 +content="" +channel="" +priority=1 +usr_msg_id="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32)" +sendtime="$(date +%s)" +sender="$(hostname)" + +if [ ${#args[@]} -lt 1 ]; then + rederr "[ERROR]: no title supplied via parameter" + usage + exit 1 +fi + +if [[ "${args[0]}" =~ ^@.* ]]; then + channel="${args[0]}" + unset "args[0]" + channel="${channel:1}" +fi + +if [ ${#args[@]} -lt 1 ]; then + rederr "[ERROR]: no title supplied via parameter" + usage + exit 1 +fi + +title="${args[0]}" +content="" + +if [ ${#args[@]} -gt 1 ]; then + content="${args[0]}" + unset "args[0]" +fi + +if [ ${#args[@]} -gt 1 ]; then + priority="${args[0]}" + unset "args[0]" +fi + +if [ ${#args[@]} -gt 1 ]; then + rederr "Too many arguments to scn_send" + usage + exit 1 +fi + + +while true ; do + + outf="$(mktemp)" + + curlresp=$(curl --silent \ + --output "${outf}" \ + --write-out "%{http_code}" \ + --data "user_id=$user_id" \ + --data "key=$user_key" \ + --data "title=$title" \ + --data "timestamp=$sendtime" \ + --data "content=$content" \ + --data "priority=$priority" \ + --data "msg_id=$usr_msg_id" \ + --data "channel=$channel" \ + --data "sender_name=$sender" \ + "https://scn.blackforestbytes.com/" ) + + curlout="$(cat "$outf")" + rm "$outf" + + if [ "$curlresp" == 200 ] ; then + green "Successfully send" + exit 0 + fi + + if [ "$curlresp" == 400 ] ; then + rederr "Bad request - something went wrong" + echo "$curlout" + echo "" + exit 1 + fi + + if [ "$curlresp" == 401 ] ; then + rederr "Unauthorized - wrong userid/userkey" + exit 1 + fi + + if [ "$curlresp" == 403 ] ; then + rederr "Quota exceeded - wait 5 min before re-try" + sleep 300 + fi + + if [ "$curlresp" == 412 ] ; then + rederr "Precondition Failed - No device linked" + exit 1 + fi + + if [ "$curlresp" == 500 ] ; then + rederr "Internal server error - waiting for better times" + sleep 60 + fi + + # if none of the above matched we probably have no network ... + rederr "Send failed (response code $curlresp) ... try again in 5s" + sleep 5 +done