Agg -g option to send action notifications via Telegram API.

Fixed some cosmetic errors in logfile.
This commit is contained in:
Rob Pearce 2023-12-02 09:51:56 +11:00
parent 091aa8aa17
commit 395a9502c0
1 changed files with 89 additions and 10 deletions

View File

@ -62,6 +62,9 @@ function usage() {
echo " -A db Log actions to given influxdb database." echo " -A db Log actions to given influxdb database."
echo " -c Cron mode. Only show output if actions were taken." 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 " -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 " -f file Specify an alternate config file."
echo " -h Show this text." echo " -h Show this text."
echo " -i x.x.x.x Specify IP address for aircon (default is $DEFAULT_AIRCON_IP)" 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 if [[ -n $globaction ]]; then
gen_aircon_command -1 ${globaction} gen_aircon_command -1 ${globaction}
fi 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" # gen_aircon_command zone_idx "command1 command2 etc"
@ -985,8 +988,6 @@ function weekdaytofull() {
echo "$full" echo "$full"
} }
function conderror() { function conderror() {
error "Invalid condition: $1" error "Invalid condition: $1"
echo -e "${RED} Start: $2" echo -e "${RED} Start: $2"
@ -1786,8 +1787,34 @@ function combine_commands() {
return 0 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() { function run_commands() {
local x tfile combinejq jcmd jurl res jqres local x tfile combinejq jcmd jurl res jqres
local thisprob thisfix prevprob
local botwords="" txt fixnum
for x in ${!airconcmd[@]}; do for x in ${!airconcmd[@]}; do
#echo RUNNING myair $AIRCON_IP ${airconcmd[$x]} #echo RUNNING myair $AIRCON_IP ${airconcmd[$x]}
@ -1797,15 +1824,42 @@ function run_commands() {
done done
jurl=$(combine_commands) jurl=$(combine_commands)
if [[ $? -eq 0 ]]; then
res=$(curl -s -g "$jurl" 3>/dev/null) res=$(curl -s -g "$jurl" 3>/dev/null)
jqres=$(echo "$res" | $JQ -r '.ack' 2>/dev/null) jqres=$(echo "$res" | $JQ -r '.ack' 2>/dev/null)
if [[ $jqres != "true" ]]; then if [[ $jqres != "true" ]]; then
error "Myair API call failed:" error "Myair API call failed:"
echo -e "$RED curl -s -g $jurl$PLAIN" echo -e "$RED curl -s -g $jurl$PLAIN"
echo -e "$RED Result:"
echo "$jqres" | sed 's/_^/ /'
return 1 return 1
fi 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() { function profile() {
local diff local diff
[[ $profiler != 1 ]] && return [[ $profiler != 1 ]] && return
@ -1908,6 +1962,10 @@ csvfile="$DEFAULT_CSVFILE"
sanitycheck=0 sanitycheck=0
RULEFORMAT=ansi RULEFORMAT=ansi
robtest=0 robtest=0
USETELEGRAM=""
TELEGRAM_FILE=""
TELEGRAM_CHAT=""
TELEGRAM_TOKEN=""
# check for config file option first # check for config file option first
if [[ $* == *-f\ * ]]; then if [[ $* == *-f\ * ]]; then
@ -1925,7 +1983,7 @@ fi
ALLARGS="$ALLARGS $*" 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 while getopts "$optstring" i $ALLARGS; do
case "$i" in case "$i" in
a) a)
@ -1946,6 +2004,10 @@ while getopts "$optstring" i $ALLARGS; do
f) f)
CONFIGFILE=${OPTARG} CONFIGFILE=${OPTARG}
;; ;;
g)
USETELEGRAM=1
TELEGRAM_FILE=${OPTARG}
;;
h) h)
usage; usage;
exit 1; exit 1;
@ -2021,6 +2083,23 @@ if [[ -n $influxdb ]]; then
influx_init "$influxdb" || exit 1 influx_init "$influxdb" || exit 1
fi 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 if [[ $makeconfig -eq 1 ]]; then
gen_config gen_config
rv=$? rv=$?