mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2025-04-11 08:31:51 +00:00
improved logging, error handling on update
This commit is contained in:
parent
471ad21e90
commit
737d54d365
36
bashbot.sh
36
bashbot.sh
@ -11,7 +11,7 @@
|
|||||||
# This file is public domain in the USA and all free countries.
|
# This file is public domain in the USA and all free countries.
|
||||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||||
#
|
#
|
||||||
#### $$VERSION$$ v0.96-pre-41-g15f6da8
|
#### $$VERSION$$ v0.96-pre-43-g471ad21
|
||||||
#
|
#
|
||||||
# Exit Codes:
|
# Exit Codes:
|
||||||
# - 0 sucess (hopefully)
|
# - 0 sucess (hopefully)
|
||||||
@ -398,7 +398,7 @@ sendJsonResult(){
|
|||||||
BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${1}")"
|
BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${1}")"
|
||||||
else
|
else
|
||||||
BOTSENT[ERROR]="999"
|
BOTSENT[ERROR]="999"
|
||||||
BOTSENT[DESCRIPTION]="Timeout or broken/no connection"
|
BOTSENT[DESCRIPTION]="Send to telegram not possible, timeout/broken/no connection"
|
||||||
fi
|
fi
|
||||||
# log error
|
# log error
|
||||||
printf "%s: RESULT=%s FUNC=%s CHAT[ID]=%s ERROR=%s DESC=%s ACTION=%s\n" "$(date)"\
|
printf "%s: RESULT=%s FUNC=%s CHAT[ID]=%s ERROR=%s DESC=%s ACTION=%s\n" "$(date)"\
|
||||||
@ -506,20 +506,22 @@ process_client() {
|
|||||||
CHAT[ID]="${UPD["result",${num},"message","chat","id"]}"
|
CHAT[ID]="${UPD["result",${num},"message","chat","id"]}"
|
||||||
USER[ID]="${UPD["result",${num},"message","from","id"]}"
|
USER[ID]="${UPD["result",${num},"message","from","id"]}"
|
||||||
# log message on debug
|
# log message on debug
|
||||||
if [[ "${debug}" = *"debug"* ]]; then
|
[[ -n "${debug}" ]] && printf "\n%s: New Message ==========\n%s\n" "$(date)" "$UPDATE" >>"${LOGDIR}/MESSAGE.log"
|
||||||
printf "%s: update received FROM=%s CHAT=%s iQEUERY=%s\n" "$(date)" "${USER[ID]}" "${CHAT[ID]}" "${iQUERY[ID]}"
|
|
||||||
printf "\n%s: New Message ==========\n%s\n" "$(date)" "$UPDATE" >>"${LOGDIR}/MESSAGE.log"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# check for uers / groups to ignore
|
# check for uers / groups to ignore
|
||||||
if [ -n "${USER[ID]}" ]; then
|
[ -n "${USER[ID]}" ] && [[ " ${!BASHBOT_BLOCKED[*]} " == *" ${USER[ID]} "* ]] && return
|
||||||
[[ " ${!BASHBOT_BLOCKED[*]} " == *" ${USER[ID]} "* ]] && return
|
jssh_readDB_async "BASHBOT_BLOCKED" "${BLOCKEDFILE}"
|
||||||
jssh_readDB_async "BASHBOT_BLOCKED" "${BLOCKEDFILE}"
|
|
||||||
fi
|
# process per message type
|
||||||
if [ -z "${iQUERY[ID]}" ]; then
|
if [ -z "${iQUERY[ID]}" ]; then
|
||||||
|
MESSAGE[0]="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's#\\/#/#g')"
|
||||||
process_message "${num}" "${debug}"
|
process_message "${num}" "${debug}"
|
||||||
|
[[ -n "${debug}" ]] && printf "%s: update received FROM=%s CHAT=%s CMD=%s\n"\
|
||||||
|
"$(date)" "${USER[USERNAME]} (${USER[ID]})" "${CHAT[USERNAME]}${CHAT[TITLE]} (${CHAT[ID]})" "${MESSAGE%% *}"
|
||||||
else
|
else
|
||||||
process_inline "${num}" "${debug}"
|
process_inline "${num}" "${debug}"
|
||||||
|
[[ -n "${debug}" ]] && printf "%s: iQuery received FROM=%s iQUERY=%s\n"\
|
||||||
|
"$(date)" "${iQUERY[USERNAME]} (${iQUERY[USER_ID]})" "${iQUERY[0]}"
|
||||||
fi
|
fi
|
||||||
#####
|
#####
|
||||||
# process inline and message events
|
# process inline and message events
|
||||||
@ -670,7 +672,6 @@ process_inline() {
|
|||||||
process_message() {
|
process_message() {
|
||||||
local num="$1"
|
local num="$1"
|
||||||
# Message
|
# Message
|
||||||
MESSAGE[0]="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's#\\/#/#g')"
|
|
||||||
MESSAGE[ID]="${UPD["result",${num},"message","message_id"]}"
|
MESSAGE[ID]="${UPD["result",${num},"message","message_id"]}"
|
||||||
|
|
||||||
# Chat ID is now parsed when update isreceived
|
# Chat ID is now parsed when update isreceived
|
||||||
@ -819,27 +820,32 @@ start_bot() {
|
|||||||
while true; do
|
while true; do
|
||||||
# adaptive sleep in ms rounded to next 0.1 s
|
# adaptive sleep in ms rounded to next 0.1 s
|
||||||
sleep "$(_round_float "${nextsleep}e-3" "1")"
|
sleep "$(_round_float "${nextsleep}e-3" "1")"
|
||||||
((nextsleep+= stepsleep , nextsleep= nextsleep>maxsleep ?maxsleep:nextsleep))
|
|
||||||
# get next update
|
# get next update
|
||||||
UPDATE="$(getJson "$UPD_URL$OFFSET" 2>/dev/null | "${JSONSHFILE}" -s -b -n 2>/dev/null | iconv -f utf-8 -t utf-8 -c)"
|
UPDATE="$(getJson "$UPD_URL$OFFSET" 2>/dev/null | "${JSONSHFILE}" -s -b -n 2>/dev/null | iconv -f utf-8 -t utf-8 -c)"
|
||||||
# did we ge an responsn0r
|
# did we ge an responsn0r
|
||||||
if [ -n "${UPDATE}" ]; then
|
if [ -n "${UPDATE}" ]; then
|
||||||
# we got something, do processing
|
# we got something, do processing
|
||||||
|
[ "${OFFSET}" = "-999" ] &&\
|
||||||
|
printf "%s: Recovered from timeout/broken/no connection, continue with telegram updates\n"\
|
||||||
|
"$(date)" >>"${ERRORLOG}"
|
||||||
# escape bash $ expansion bug
|
# escape bash $ expansion bug
|
||||||
UPDATE="${UPDATE//$/\\$}"
|
UPDATE="${UPDATE//$/\\$}"
|
||||||
# Offset
|
# Offset
|
||||||
OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)"
|
OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)"
|
||||||
((OFFSET++))
|
((OFFSET++))
|
||||||
|
|
||||||
|
((nextsleep+= stepsleep , nextsleep= nextsleep>maxsleep ?maxsleep:nextsleep))
|
||||||
if [ "$OFFSET" != "1" ]; then
|
if [ "$OFFSET" != "1" ]; then
|
||||||
nextsleep="100"
|
nextsleep="100"
|
||||||
process_updates "${DEBUG}"
|
process_updates "${DEBUG}"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
# ups, something bad happend, wait maxsleep
|
# ups, something bad happend, wait maxsleep*10
|
||||||
(( nextsleep=maxsleep*2 ))
|
(( nextsleep=nextsleep*2 , nextsleep= nextsleep>maxsleep*10 ?maxsleep*10:nextsleep ))
|
||||||
printf "%s: Timeout or broken/no connection on telegram update, sleep %ds\n"\
|
[ "${OFFSET}" = "-999" ] &&\
|
||||||
|
printf "%s: Repeated timeout/broken/no connection on telegram update, sleep %ds\n"\
|
||||||
"$(date)" "$(_round_float "${nextsleep}e-3")" >>"${ERRORLOG}"
|
"$(date)" "$(_round_float "${nextsleep}e-3")" >>"${ERRORLOG}"
|
||||||
|
OFFSET="-999"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
@ -289,5 +289,5 @@ Note: If you disable automatic retry, se above, you disable also connection prob
|
|||||||
#### [Prev Getting started](2_usage.md)
|
#### [Prev Getting started](2_usage.md)
|
||||||
#### [Next Expert Use](4_expert.md)
|
#### [Next Expert Use](4_expert.md)
|
||||||
|
|
||||||
#### $$VERSION$$ v0.96-pre-41-g15f6da8
|
#### $$VERSION$$ v0.96-pre-42-g38164ba
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user