commit 52d6339390dac6776c75d63ea21f4e20f594a7f3 Author: wartana Date: Tue Feb 24 22:49:28 2026 +0800 feat: Add `send-speedtest.sh` script. diff --git a/send-speedtest.sh b/send-speedtest.sh new file mode 100644 index 0000000..8716e7b --- /dev/null +++ b/send-speedtest.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +# Exit immediately if a command exits with a non-zero status. +set -e + +# Check for required environment variables +if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then + echo "Error: TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID environment variables must be set." >&2 + exit 1 +fi + +# The MESIN variable is expected to be in the environment. Default if not set. +: "${MESIN:=Unknown Host}" + +RESULT_JSON=$(/usr/local/bin/speedtest --accept-license --accept-gdpr --format=json) + +# Check if speedtest command was successful and returned valid JSON +if ! echo "$RESULT_JSON" | jq -e . >/dev/null 2>&1; then + echo "Error: speedtest command failed or did not return valid JSON." >&2 + exit 1 +fi + +# Use a single, more efficient jq call to extract and calculate all necessary data. +# The 'eval' command then assigns these to shell variables. +# Note: Quoting values from jq to handle potential spaces in names. +eval $(echo "$RESULT_JSON" | jq -r ' + "LATENCY=\(.ping.latency) + JITTER=\(.ping.jitter) + SERVER=\"\(.server.name)\" + ISP=\"\(.isp)\" + RESULTURL=\(.result.url) + PUBLIC_IP=\(.interface.externalIp) + D_MBPS=\(.download.bandwidth * 8 / 1000000) + U_MBPS=\(.upload.bandwidth * 8 / 1000000)" +') + +MSG="Hasil Speedtest dari: *$MESIN* +ISP: *$ISP* +IP: *$PUBLIC_IP* +Server: *$SERVER* +Latency: *${LATENCY}ms* +Jitter: *${JITTER}ms* +Download: *$(printf "%.2f" "$D_MBPS") Mbps* +Upload: *$(printf "%.2f" "$U_MBPS") Mbps* +Result: $RESULTURL" + +# Use --data-urlencode for the text to handle special characters safely +# and set parse_mode to Markdown to enable bold formatting. +curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \ + -d chat_id="$TELEGRAM_CHAT_ID" \ + -d parse_mode=Markdown \ + --data-urlencode "text=$MSG"