From b703853b28f5084c792c6c5201f1b608cfb09c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 17 Nov 2018 14:26:29 +0100 Subject: [PATCH] send.bash example --- examples/scn_send.sh | 77 ++++++++++++++++++++++++++++++++++++++ web/api_more.php | 89 ++++++++++++++++++++++++++++++++++++++++++++ web/css/style.css | 11 ++++++ 3 files changed, 177 insertions(+) create mode 100644 examples/scn_send.sh diff --git a/examples/scn_send.sh b/examples/scn_send.sh new file mode 100644 index 0000000..9f5dd8d --- /dev/null +++ b/examples/scn_send.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash + +# +# Call with `scn_send title` +# or `scn_send title content` +# or `scn_send title content priority` +# +# + +if [ "$#" -lt 1 ]; then + echo "no title supplied via parameter" + exit 1 +fi + +################################################################################ +# INSERT YOUR DATA HERE # +################################################################################ +user_id=999 +user_key="????????????????????????????????????????????????????????????????" +################################################################################ + +title=$1 +content="" + +if [ "$#" -gt 1 ]; then + content=$2 +fi + +priority=1 + +if [ "$#" -gt 2 ]; then + priority=$3 +fi + +usr_msg_id=$(uuidgen) + +while true ; do + + curlresp=$(curl -s -o /dev/null -w "%{http_code}" \ + -d "user_id=$user_id" -d "user_key=$user_key" -d "title=$title" \ + -d "content=$content" -d "priority=$priority" -d "msg_id=$usr_msg_id" \ + https://scn.blackforestbytes.com/send.php) + + if [ "$curlresp" == 200 ] ; then + echo "Successfully send" + exit 0 + fi + + if [ "$curlresp" == 400 ] ; then + echo "Bad request - something went wrong" + exit 1 + fi + + if [ "$curlresp" == 401 ] ; then + echo "Unauthorized - wrong userid/userkey" + exit 1 + fi + + if [ "$curlresp" == 403 ] ; then + echo "Quota exceeded - wait one hour before re-try" + sleep 3600 + fi + + if [ "$curlresp" == 412 ] ; then + echo "Precondition Failed - No device linked" + exit 1 + fi + + if [ "$curlresp" == 500 ] ; then + echo "Internal server error - waiting for better times" + sleep 60 + fi + + # if none of the above matched we probably hav no network ... + echo "Send failed (response code $curlresp) ... try again in 5s" + sleep 5 +done \ No newline at end of file diff --git a/web/api_more.php b/web/api_more.php index 8019d27..3c44f27 100644 --- a/web/api_more.php +++ b/web/api_more.php @@ -187,6 +187,95 @@ 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.

+ +

Bash script example

+
+

+ 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" +

+
#!/usr/bin/env bash
+
+#
+# Call with   `scn_send title`
+#        or   `scn_send title content`
+#        or   `scn_send title content priority`
+#
+#
+
+if [ "$#" -lt 1 ]; then
+    echo "no title supplied via parameter"
+    exit 1
+fi
+
+################################################################################
+# INSERT YOUR DATA HERE                                                        #
+################################################################################
+user_id=999
+user_key="????????????????????????????????????????????????????????????????"
+################################################################################
+
+title=$1
+content=""
+
+if [ "$#" -gt 1 ]; then
+    content=$2
+fi
+
+priority=1
+
+if [ "$#" -gt 2 ]; then
+    priority=$3
+fi
+
+usr_msg_id=$(uuidgen)
+
+while true ; do
+
+    curlresp=$(curl -s -o /dev/null -w "%{http_code}" \
+                    -d "user_id=$user_id" -d "user_key=$user_key" -d "title=$title" \
+                    -d "content=$content" -d "priority=$priority" -d "msg_id=$usr_msg_id" \
+                    https://scn.blackforestbytes.com/send.php)
+
+    if [ "$curlresp" == 200 ] ; then
+        echo "Successfully send"
+        exit 0
+    fi
+
+    if [ "$curlresp" == 400 ] ; then
+        echo "Bad request - something went wrong"
+        exit 1
+    fi
+
+    if [ "$curlresp" == 401 ] ; then
+        echo "Unauthorized - wrong userid/userkey"
+        exit 1
+    fi
+
+    if [ "$curlresp" == 403 ] ; then
+        echo "Quota exceeded - wait one hour before re-try"
+        sleep 3600
+    fi
+
+    if [ "$curlresp" == 412 ] ; then
+        echo "Precondition Failed - No device linked"
+        exit 1
+    fi
+
+    if [ "$curlresp" == 500 ] ; then
+        echo "Internal server error - waiting for better times"
+        sleep 60
+    fi
+
+    # if none of the above matched we probably hav no network ...
+    echo "Send failed (response code $curlresp) ... try again in 5s"
+    sleep 5
+done
+
+

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

+
diff --git a/web/css/style.css b/web/css/style.css index d02057f..b8c885a 100644 --- a/web/css/style.css +++ b/web/css/style.css @@ -42,6 +42,11 @@ body border-left: .25rem solid #E53935; } +.yellow-code +{ + border-left: .25rem solid #FFCB05; +} + #mainpnl input, #mainpnl textarea { @@ -235,4 +240,10 @@ table.scode_table th:nth-child(2) { .linkcaption:hover { text-decoration: none; +} + +pre, pre span +{ + font-family: Menlo, Consolas, monospace; + background: #F9F9F9;; } \ No newline at end of file