From 505595b9f94ab0bcd1ffb4ecd2027eefeae38d7d Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Wed, 21 Jul 2021 03:48:44 -0500 Subject: [PATCH] Added custom configuration file location support --- example_configs/httpcheck_telegram | 9 +++----- http_check_telegram | 36 ++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/example_configs/httpcheck_telegram b/example_configs/httpcheck_telegram index c851025..28b5ff0 100644 --- a/example_configs/httpcheck_telegram +++ b/example_configs/httpcheck_telegram @@ -1,7 +1,4 @@ -# Bot Info -CHATID="5555555" -KEY="THISISAUNIQUEANDPRIVATEKEY" - -# Site list (file location) -CHKLIST="${HOME}/.site_check" +CHATID="5555555" #Chat ID for the Telegram room/conversation with bot +KEY="THISISAUNIQUEANDPRIVATEKEY" #Your bot's API key +CHKLIST="${HOME}/.site_check" #A file containing a list of sites/URLs to check diff --git a/http_check_telegram b/http_check_telegram index e046f01..f4d5d0a 100755 --- a/http_check_telegram +++ b/http_check_telegram @@ -1,9 +1,41 @@ #!/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin +CONFIG=$1 + +# BEGIN CONFIG CHECK +### Provides KEY, CHATID, CHKLIST +if [ -n "$CONFIG" ]; then + if [ -f "$CONFIG" ]; then + source $CONFIG + else + printf '%s\n' "$CONFIG: File does not exist." >&2 + exit 10 + fi +else + if [ -f ".httpcheck_telegram" ]; then + source .httpcheck_telegram + else + printf '%s\n' "No configuration file found." >&2 + exit 10 + fi +fi +# Check if config was loaded by examining all configuration variables, exit if not +if [ -z "$KEY" ]; then + printf '%s\n' "Configuration not set correctly: KEY not set" >&2 + ERROR11=true +fi +if [ -z "$CHATID" ]; then + printf '%s\n' "Configuration not set correctly: CHATID not set" >&2 + ERROR11=true +fi +if [ -z "$CHKLIST" ]; then + printf '%s\n' "Configuration not set correctly: CHKLIST not set" >&2 + ERROR11=true +fi +[ "$ERROR11" = "true" ] && exit 11 +# END CONFIG CHECK -# Load Telegram bot info -source .httpcheck_telegram #Provides KEY, CHATID, CHKLIST TIME="10" URL="https://api.telegram.org/bot$KEY/sendMessage"