From 395a9502c01b8b0292380ba685e829eaffd67edd Mon Sep 17 00:00:00 2001 From: Rob Pearce Date: Sat, 2 Dec 2023 09:51:56 +1100 Subject: [PATCH] Agg -g option to send action notifications via Telegram API. Fixed some cosmetic errors in logfile. --- aircon.sh | 99 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 89 insertions(+), 10 deletions(-) diff --git a/aircon.sh b/aircon.sh index 81b9b8e..b72bded 100755 --- a/aircon.sh +++ b/aircon.sh @@ -62,6 +62,9 @@ function usage() { echo " -A db Log actions to given influxdb database." echo " -c Cron mode. Only show output if actions were taken." echo " -D dbhost Specify influxdb hostname for -I and -A options (default: localhost)" + echo " -g file Send Tele(g)ram notifications via api.telegran.org. File format is:" + echo " bottoken" + echo " chatid" echo " -f file Specify an alternate config file." echo " -h Show this text." echo " -i x.x.x.x Specify IP address for aircon (default is $DEFAULT_AIRCON_IP)" @@ -617,7 +620,7 @@ function generate_actions() { # populates global: nairconcommands & airconcmd if [[ -n $globaction ]]; then gen_aircon_command -1 ${globaction} fi - echo -e "${GREEN}${BOLD}ok${PLAIN}" + [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -e "${GREEN}${BOLD}ok${PLAIN}" } # gen_aircon_command zone_idx "command1 command2 etc" @@ -985,8 +988,6 @@ function weekdaytofull() { echo "$full" } - - function conderror() { error "Invalid condition: $1" echo -e "${RED} Start: $2" @@ -1786,8 +1787,34 @@ function combine_commands() { return 0 } +function reword_problem() { #1="problem text" + local orig="$1" new="" + if [[ $orig == *" is too "* ]]; then + new=$(sed -E 's/^([A-Za-z]+) is too ([A-Za-z]+)/the zone "\1" was too \2/' <<<"$orig") + elif [[ $orig == *" is owner not_home "* ]]; then + new=$(sed -E 's/^([A-Za-z]+) is .*/the owner of the zone "\1" was not at home/' <<<"$orig") + elif [[ $orig == *" is force close"* ]]; then + new=$(sed -E 's/^([A-Za-z]+) is .*/the zone "\1" should be closed at this time/' <<<"$orig") + elif [[ $orig == *" is n/a"* ]]; then + new=$(sed -E 's/^([A-Za-z]+) is .*/the zone "\1" should be the "myzone" at this time/' <<<"$orig") + else + new="$orig" + fi + new=$(sed 's/&/%26/g' <<<"$new") + echo "$new" +} + +function telegram_send() { #1=msg + local res msg="$1" + #action "sending to telegram: curl -s --data \"text=$msg\" --data \"chat_id=$TELEGRAM_CHAT\" --data \"parse_mode=markdown\" 'https://api.telegram.org/bot'$TELEGRAM_TOKEN'/sendMessage' " + res=$(curl -s --data "text=$msg" --data "chat_id=$TELEGRAM_CHAT" --data "parse_mode=markdown" 'https://api.telegram.org/bot'$TELEGRAM_TOKEN'/sendMessage' 2>&1) + #action "telegram result: $res" +} + function run_commands() { local x tfile combinejq jcmd jurl res jqres + local thisprob thisfix prevprob + local botwords="" txt fixnum for x in ${!airconcmd[@]}; do #echo RUNNING myair $AIRCON_IP ${airconcmd[$x]} @@ -1797,15 +1824,42 @@ function run_commands() { done jurl=$(combine_commands) - res=$(curl -s -g "$jurl" 3>/dev/null) - jqres=$(echo "$res" | $JQ -r '.ack' 2>/dev/null) - if [[ $jqres != "true" ]]; then - error "Myair API call failed:" - echo -e "$RED curl -s -g $jurl$PLAIN" - return 1 + if [[ $? -eq 0 ]]; then + res=$(curl -s -g "$jurl" 3>/dev/null) + jqres=$(echo "$res" | $JQ -r '.ack' 2>/dev/null) + if [[ $jqres != "true" ]]; then + error "Myair API call failed:" + echo -e "$RED curl -s -g $jurl$PLAIN" + echo -e "$RED Result:" + echo "$jqres" | sed 's/_^/ /' + return 1 + fi fi + + if [[ $USETELEGRAM -eq 1 ]]; then + prevprob="" + botwords="" + for x in ${!airconcmd[@]}; do + thisprob=${airconproblem[$x]} + thisfix=$(sed 's/&/%26/g' <<<"${airconcomment[$x]}") + if [[ $thisprob != $prevprob ]]; then + # send our existing buffer + [[ -n $botwords ]] && telegram_send "$botwords" + botwords="" + txt=$(reword_problem "$thisprob") + botwords=$(printf 'I noticed that _%s_, so I did this:%%0a' "$txt") + fixnum=1 + prevprob="$thisprob" + fi + botwords="$botwords$(printf ' %d. %s%%0a' $fixnum "$thisfix")" + fixnum=$((fixnum + 1)) + done + [[ -n $botwords ]] && telegram_send "$botwords" + fi + } + function profile() { local diff [[ $profiler != 1 ]] && return @@ -1908,6 +1962,10 @@ csvfile="$DEFAULT_CSVFILE" sanitycheck=0 RULEFORMAT=ansi robtest=0 +USETELEGRAM="" +TELEGRAM_FILE="" +TELEGRAM_CHAT="" +TELEGRAM_TOKEN="" # check for config file option first if [[ $* == *-f\ * ]]; then @@ -1925,7 +1983,7 @@ fi ALLARGS="$ALLARGS $*" -optstring="aA:bcD:f:hHi:I:k:l:Lo:pmRsSt:T:wWy" +optstring="aA:bcD:f:g:hHi:I:k:l:Lo:pmRsSt:T:wWy" while getopts "$optstring" i $ALLARGS; do case "$i" in a) @@ -1946,6 +2004,10 @@ while getopts "$optstring" i $ALLARGS; do f) CONFIGFILE=${OPTARG} ;; + g) + USETELEGRAM=1 + TELEGRAM_FILE=${OPTARG} + ;; h) usage; exit 1; @@ -2021,6 +2083,23 @@ if [[ -n $influxdb ]]; then influx_init "$influxdb" || exit 1 fi +if [[ $USETELEGRAM -eq 1 ]]; then + if [[ -z $TELEGRAM_FILE || ! -f $TELEGRAM_FILE ]]; then + error "Telegram auth file '$TELEGRAM_FILE' doesn't exist." + exit 1 + fi + TELEGRAM_TOKEN=$(awk NF $TELEGRAM_FILE | head -1) + TELEGRAM_CHAT=$(awk NF $TELEGRAM_FILE | tail -1) + if [[ -z $TELEGRAM_TOKEN ]]; then + error "Telegram token is empty. Check that auth file '$TELEGRAM_FILE' first line is token, second line is chat id." + exit 1 + fi + if [[ -z $TELEGRAM_CHAT ]]; then + error "Telegram chat id is empty. Check that auth file '$TELEGRAM_FILE' first line is token, second line is chat id." + exit 1 + fi +fi + if [[ $makeconfig -eq 1 ]]; then gen_config rv=$?