2019-04-16 18:43:54 +00:00
#### [Home](../README.md)
2019-04-10 20:23:24 +00:00
## Best Practices
2019-04-16 11:29:49 +00:00
### Customize commands.sh only
2019-04-10 20:23:24 +00:00
2019-04-16 11:29:49 +00:00
To ease Updates never change ```bashbot.sh```, instead individual commands should go to ```commands.sh``` . Insert your Bot commands in the ```case ... esac``` block:
2019-04-10 20:23:24 +00:00
```bash
case "$MESSAGE" in
'/echo') # my first own command, echo MESSAGE
send_normal_message "${CHAT[ID]}" "${MESSAGE}"
;;
################################################
2019-04-14 21:08:11 +00:00
# DEFAULT commands start here, do not edit below this!
2019-04-10 20:23:24 +00:00
'/info')
2019-04-11 12:08:14 +00:00
bashbot_info "${CHAT[ID]}"
2019-04-10 20:23:24 +00:00
;;
esac
```
2019-04-16 11:29:49 +00:00
### Seperate logic from commands
2019-04-11 12:08:14 +00:00
2019-04-16 11:29:49 +00:00
If a command need more than 2-3 lines of code, you should use a function to seperate logic from command. Place your functions in a seperate file, e.g. ```mycommands.inc.sh``` and source it from bashbot.sh. Example:
2019-04-10 20:23:24 +00:00
```bash
2019-04-16 11:29:49 +00:00
source "mycommands.inc.sh"
2019-04-10 20:23:24 +00:00
case "$MESSAGE" in
2019-04-16 14:45:26 +00:00
'/process') # logic for /process is done in process_message
result="$(process_message "$MESSAGE")"
2019-04-16 11:29:49 +00:00
send_normal_message "${CHAT[ID]}" "$result"
2019-04-10 20:23:24 +00:00
;;
################################################
2019-04-14 21:08:11 +00:00
# DEFAULT commands start here, do not edit below this!
2019-04-10 20:23:24 +00:00
'/info')
2019-04-11 12:08:14 +00:00
bashbot_info "${CHAT[ID]}"
2019-04-10 20:23:24 +00:00
;;
'/start')
send_action "${CHAT[ID]}" "typing"
2019-04-11 12:08:14 +00:00
bashbot_help "${CHAT[ID]}"
2019-04-10 20:23:24 +00:00
;;
esac
```
```bash
#!/bin/bash
2019-04-16 11:29:49 +00:00
# file: mycommands.inc.sh
2019-04-10 20:23:24 +00:00
process_message() {
2019-04-16 11:29:49 +00:00
local ARGS="${1#/* }" # remove command
2019-04-11 07:59:56 +00:00
local TEXT OUTPUT=""
2019-04-10 20:23:24 +00:00
2019-04-16 11:29:49 +00:00
# process every word in MESSAGE, avoid globbing
2019-04-10 20:23:24 +00:00
set -f
for WORD in $ARGS
do
set +f
# process links
if [[ "$WORD" == "https://"* ]]; then
REPORT="$(dosomething_with_link "$WORD")"
# no link, add as text
else
2019-04-11 07:59:56 +00:00
TEXT="$(echo "${TEXT} $WORD")"
2019-04-10 20:23:24 +00:00
continue
fi
2019-04-11 07:59:56 +00:00
# compose result
2019-04-10 20:23:24 +00:00
OUTPUT="* ${REPORT} ${WORD} ${TEXT}"
TEXT=""
done
# return result, reset globbing in case we had no ARGS
set +f
echo "${OUTPUT}${TEXT}"
}
```
### Test your Bot with shellcheck
2019-04-11 07:59:56 +00:00
Shellcheck is a static linter for shell scripts providing excellent tips and hints for shell coding pittfalls. You can [use it online ](https://www.shellcheck.net/ ) or [install it on your system ](https://github.com/koalaman/shellcheck#installing ).
All bashbot scripts are linted by shellcheck.
Shellcheck examples:
```bash
$ shellcheck -x mybotcommands.inc.sh
Line 17:
TEXT="$(echo "${TEXT} $WORD")"
^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
```
2019-04-14 21:08:11 +00:00
As you can see my ```mybotcommands.inc.sh``` contains an useless echo command in 'TEXT=' assigment and can be replaced by ```TEXT="${TEXT}${WORD}"```
2019-04-11 07:59:56 +00:00
```bash
2019-04-19 09:28:12 +00:00
$ shellcheck -x examples/notify
2019-04-11 07:59:56 +00:00
OK
2019-04-19 09:28:12 +00:00
$ shellcheck -x examples/question
2019-04-11 07:59:56 +00:00
OK
$ shellcheck -x commands.sh
OK
$ shellcheck -x bashbot.sh
In bashbot.sh line 123:
text="$(echo "$text" | sed 's/ mynewlinestartshere /\r\n/g')" # hack for linebreaks in startproc scripts
^-- SC2001: See if you can use ${variable//search/replace} instead.
In bashbot.sh line 490:
CONTACT[USER_ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","contact","user_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' < "$TMP")"
^-- SC2034: CONTACT appears unused. Verify it or export it.
```
2019-04-16 14:45:26 +00:00
The example show two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is fixed and much faster as the "echo | sed" solution.
2019-04-14 21:08:11 +00:00
The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-)
2019-04-10 20:23:24 +00:00
2019-04-16 14:45:26 +00:00
#### [Prev Best Practice](5_practice.md)
#### [Next Functions Reference](6_reference.md)
2019-04-22 08:23:28 +00:00
#### $$VERSION$$ v0.62-0-g5d5dbae
2019-04-10 20:23:24 +00:00