add debug mode, remove old test mode

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-10 11:33:41 +02:00
parent bf00d33122
commit 73b7734fa3
3 changed files with 48 additions and 32 deletions

View File

@ -12,7 +12,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v0.72-dev-2-g17d3573
#### $$VERSION$$ v0.72-dev-4-gbf00d33
#
# Exit Codes:
# - 0 sucess (hopefully)
@ -117,18 +117,6 @@ elif [ ! -w "${COUNTFILE}" ]; then
exit 2
fi
COMMANDS="${BASHBOT_ETC:-.}/commands.sh"
if [ "$1" != "source" ]; then
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
${CLEAR}
echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}"
ls -l "${COMMANDS}"
exit 3
fi
# shellcheck source=./commands.sh
source "${COMMANDS}" "source"
fi
BOTTOKEN="$(cat "${TOKENFILE}")"
URL='https://api.telegram.org/bot'$BOTTOKEN
@ -158,6 +146,18 @@ unset USER
declare -A BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE
export BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE
COMMANDS="${BASHBOT_ETC:-.}/commands.sh"
if [ "$1" != "source" ]; then
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
${CLEAR}
echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}"
ls -l "${COMMANDS}"
exit 3
fi
# shellcheck source=./commands.sh
source "${COMMANDS}" "source"
fi
send_normal_message() {
local text="${2}"
@ -381,21 +381,19 @@ _is_function()
[ "$(LC_ALL=C type -t "$1")" = "function" ]
}
process_updates() {
MAX_PROCESS_NUMBER=$(echo "$UPDATE" | sed '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')
MAX_PROCESS_NUMBER="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')"
for ((PROCESS_NUMBER=0; PROCESS_NUMBER<=MAX_PROCESS_NUMBER; PROCESS_NUMBER++)); do
if [ "$1" = "test" ]; then
process_client "$1"
else
process_client "$1" &
fi
process_client "$1"
done
}
process_client() {
iQUERY[ID]="$(JsonGetString <<<"${UPDATE}" '"result",'"$PROCESS_NUMBER"',"inline_query","id"')"
iQUERY[ID]="$(JsonGetString <<<"${UPDATE}" '"result",'"${PROCESS_NUMBER}"',"inline_query","id"')"
if [ "${iQUERY[ID]}" = "" ]; then
process_message "$PROCESS_NUMBER"
[[ "$1" = *"debug"* ]] && echo "$UPDATE" >>"MESSAGE.log"
process_message "$PROCESS_NUMBER" "$1"
else
[ "$INLINE" != "0" ] && _is_function process_inline && process_inline "$PROCESS_NUMBER"
[[ "$1" = *"debug"* ]] && echo "$UPDATE" >>"INLINE.log"
[ "$INLINE" != "0" ] && _is_function process_inline && process_inline "$PROCESS_NUMBER" "$1"
fi
# Tmux
copname="$ME"_"${CHAT[ID]}"
@ -494,28 +492,29 @@ process_message() {
# main get updates loop, should never terminate
start_bot() {
local DEBUG="$1"
local OFFSET=0
local mysleep="100" # ms
local addsleep="100"
local maxsleep="$(( ${BASHBOT_SLEEP:-5000} + 100 ))"
[[ "${DEBUG}" = *"debug" ]] && exec &>>"DEBUG.log"
[ "${DEBUG}" != "" ] && date && echo "Start BASHBOT in Mode ${DEBUG}"
[[ "${DEBUG}" = "xdebug"* ]] && set -x
while true; do {
UPDATE="$(curl -s "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n)"
# Offset
OFFSET="$(echo "$UPDATE" | grep '\["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+1))
if [ "$OFFSET" != "1" ]; then
mysleep="100"
if [ "$1" = "test" ]; then
process_updates "$1"
else
process_updates "$1" &
fi
process_updates "${DEBUG}" &
fi
# adaptive sleep in ms rounded to next lower second
sleep "${mysleep%???}"; mysleep=$((mysleep+addsleep)); [ "${mysleep}" -gt "${maxsleep}" ] && mysleep="${maxsleep}"
[ "${mysleep}" -gt "999" ] && sleep "${mysleep%???}"
mysleep=$((mysleep+addsleep)); [ "${mysleep}" -gt "${maxsleep}" ] && mysleep="${maxsleep}"
}
done
}

View File

@ -4,7 +4,10 @@
The Bots standard commands are in ```commands.sh``` file. You must not add your commands to 'commands.sh', instead place them in ```mycommands.sh```, there you also find examples how to process messages and send out text. See [Best practices](5_practice.md) for more information.
Once you're done with editing 'mycommands.sh' start the Bot with ```./bashbot.sh start```.
If some thing doesn't work as it should, debug with ```bash -x bashbot.sh```. To stop the Bot run ```./bashbot.sh kill```
To stop the Bot run ```./bashbot.sh kill```
If some thing doesn't work as it should, you can debug with ```./bashbot.sh startbot DEBUG``` where DEBUG can be 'debug', 'xdebug' or 'xdebugx'.
See [Bashbot Development](7_develop.md) for more information.
To use the functions provided in this script in other scripts simply source bashbot: ```source bashbot.sh```
@ -179,5 +182,5 @@ send_action "${CHAT[ID]}" "action"
#### [Prev Create Bot](1_firstbot.md)
#### [Next Advanced Usage](3_advanced.md)
#### $$VERSION$$ v0.72-dev-0-g6afa177
#### $$VERSION$$ v0.72-dev-4-gbf00d33

View File

@ -4,6 +4,20 @@ This section is about help and best practices for new bashbot developers. The ma
bashbot development is done on github. If you want to provide fixes or new features [fork bashbot on githup](https://help.github.com/en/articles/fork-a-repo) and provide changes as [pull request on github](https://help.github.com/en/articles/creating-a-pull-request).
### Debuging Bashbot
In normal mode of operation all bashbot output is discarded one more correct sent to TMUX console.
To get these messages (and more) you can start bashbot in the current shell ```./bashbot.sh startbot```. Now you can see all output or erros from bashbot.
In addition you can change the change the level of verbosity by adding a third argumant.
```
"debug" redirects all output to "DEBUG.log", in addtion every update is logged in "MESSAGE.LOG" and "INLINE.log"
"debugterm same as debug but output and errors are sent to terminal
"xdebug" same as debug plus set bash option '-x' to log any executed command
"xdebugterm" same as xdebug, but output and errors are sent to terminal
```
### Setup your develop environment
1. install git, install [shellcheck](5_practice.md#Test-your-Bot-with-shellcheck)
@ -117,5 +131,5 @@ fi
#### [Prev Function Reference](6_reference.md)
#### [Next Bashbot Environment](8_custom.md)
#### $$VERSION$$ v0.72-dev-0-g6afa177
#### $$VERSION$$ v0.72-dev-4-gbf00d33