From ab9a0541a513bd8dc2b960ee4240e2bf97e4281e Mon Sep 17 00:00:00 2001 From: mrbesen Date: Mon, 16 Sep 2019 13:20:14 +0200 Subject: [PATCH] added Message editing --- bot.sh | 30 +++++++++++++++++++++++------- func.sh | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/bot.sh b/bot.sh index 9985b60..9bd9a4a 100755 --- a/bot.sh +++ b/bot.sh @@ -7,15 +7,18 @@ echo "pwd: " pwd . config.sh -. func.sh +#. func.sh +msg="a" +ident="" # variable to identify changes in the problems #hdd usage . hdd.sh used=$(hddUsage) if [ "$used" -gt "$hddlimit" ]; then - echo "warn!" + echo "HDD warn!" hddTop=$(hddTop) - send "warnung server used $used% of the storage $hddTop" + printf -v msg '%b\n\nwarnung server used %b%% of the storage\n%b' "${msg}" "$used" "$hddTop" + ident="$ident:hdd" fi unset -f hddTop unset -f hddUsage @@ -25,7 +28,8 @@ unset -f hddUsage notrunning=$(checkServices "$services") if [ "$notrunning" != "" ]; then echo "Serive warn!" - send "warnung%20the%20following%20services%20are%20not%20running%20$notrunning" + printf -v msg '%b\n\nwarnung the following services are not running\n%b' "${msg}" "$notrunning" + ident="$ident:$notrunning" fi unset -f checkServices @@ -35,7 +39,8 @@ cpu=$(cpuUsage) if [ "$cpu" -gt "$cpulimit" ]; then echo "cpu limit!" proc=$(cpuTop) - send "warnung%20CPU%20Usage%20is%20high%20$cpu%25%0A$proc" + printf -v msg '%b\n\nwarnung CPU Usage is high %b%%\n%b' "${msg}" "$cpu" "$proc" + ident="$ident:cpu" fi unset -f cpuUsage unset -f cpuTop @@ -46,7 +51,8 @@ mem=$(memUsage) if [ "$mem" -gt "$memlimit" ]; then echo "mem limit!" proc=$(memTop) - send "warnung%20Memory%20Usage%20is%20high%20$mem%25%0A$proc" + printf -v msg '%b\n\nwarnung Memory Usage is high %b%%\n%b' "${msg}" "$mem" "$proc" + ident="$ident:mem" fi unset -f memUsage unset -f memTop @@ -56,6 +62,16 @@ unset -f memTop checkCert "/etc/letsencrypt/live/mrbesen.de/cert.pem" if [ "$?" -gt "0" ]; then echo "cert expired!" - send "warnung%20cert%20expires%20soon" + printf -v msg '%b\n\nwarnung cert expires soon' "${msg}" + ident="$ident:cert" fi unset -f checkCert + +. func.sh +if [[ "${#msg}" -gt "1" ]]; then + #remove prepend + msg=$(echo "$msg" | tail -n +3) + send "$ident" "$msg" +else + resetFile +fi diff --git a/func.sh b/func.sh index 98a1909..6a82c51 100755 --- a/func.sh +++ b/func.sh @@ -1,6 +1,47 @@ #!/bin/bash -#1. arg = text +fname="lastmsg.bot" + +#1. arg= ident, 2. arg = text send() { - echo "Sending $1 to ${userid}" - curl --data "chat_id=${userid}&text=$1" -s "https://api.telegram.org/bot${token}/sendMessage" > /dev/null + #check file + mapfile -t file < $fname + fid=${file[0]} + msgid=${file[1]} + count=${file[2]} + params="chat_id=${userid}&text=$2" + #echo "read: fid: ${fid}, msgid: ${msgid}, count: ${count}" + + if [ "$fid" == "$1" ]; then + #update only + method="editMessageText" + date=$(date '+%F %H:%M') + params="message_id=${msgid}&${params}" + count=$((count+1)) + printf -v params "%b\n\n(%b x wiederholt am %b)" "${params}" "${count}" "${date}" + else + method="sendMessage" + count="0" + fi + + #echo "Sending $2 to ${userid}" + + #request + raw=$(curl --data "${params}" "https://api.telegram.org/bot${token}/${method}" 2> /dev/null) + + #parse response + #echo "recieved: $raw" + msgid=$(python3 -c "import sys, json; print(json.loads('$raw'.replace('\n',''))['result']['message_id'])") + #echo "msgid: ${msgid}, count: ${count}" + + #write to file + echo "$1" > $fname #ident + echo "${msgid}" >> $fname #msgid + echo "${count}" >> $fname #count +} + +#used to rset the file, when nothing is sent +resetFile() { + echo "..." > $fname #ident -> never match + echo "-100" >> $fname #msgid + echo "-1000" >> $fname #count }