From 2e05e37c8f192a62b03f85ffec7e0c9f83e05979 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 1 Apr 2022 13:18:23 +0800 Subject: Add shell script for calling the API. --- README.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'README.md') diff --git a/README.md b/README.md index dc3e465..d12f369 100644 --- a/README.md +++ b/README.md @@ -77,4 +77,38 @@ def send_message(token: str, message: str): if __name__ == '__main__': send_message('', 'Hello, world!') -``` \ No newline at end of file +``` + +If you wish a unix tool instead, use the bash script below: + +```shell +#!/bin/bash + +# Disable globbing (expanding '*'), prevent some leaking exploits for security +set -f + +TOKEN='' + +# Replace newline with '\\n', using a bash extension +MESSAGE="${1//$'\n'/\\n}" +# Escape '"' (JSON) +MESSAGE="${MESSAGE//\"/\\\"}" +if [ -z "$MESSAGE" ] +then + # Telegram API refuses to send empty string, so we print a help message instead. + echo "Usage: $0 " + exit +fi +#echo "$MESSAGE" +curl -X POST https://kimikuri.keuin.cc/api/message \ + -H 'Content-Type: application/json' \ + --data "{\"token\":\"$TOKEN\",\"message\":\"$MESSAGE\"}" \ + --connect-timeout 10 --retry 3 +``` + +This script requires [cURL](https://curl.se/), which is shipped with almost all linux distros by default. +Place your API token in `$TOKEN`, then execute the script like this: + +```shell +./kimikuri.sh "Hello, world!" +``` -- cgit v1.2.3