From 00af5b86c8eb49313357081c352ad12029761723 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 26 Dec 2020 20:37:07 +0100 Subject: [PATCH 01/51] doc: update dev hints for debian --- doc/7_develop.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/doc/7_develop.md b/doc/7_develop.md index 8439441..64b3633 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -180,22 +180,24 @@ You don't need all these files after you're finished with your cool new bot. Let's create a stripped down version: -- delete all modules you do not need from 'modules', e.g. 'modules/inline.sh' if you don't use inline queries -- delete not needed standard commands and messages from 'commands.sh' -- delete not needed commands and functions from 'mycommands.sh' -- run ```dev/make-standalone.sh``` to create a a stripped down version of your bo +- delete all modules you do not need from `modules`, e.g. `modules/inline.sh` if you don't use inline queries +- delete not needed standard commands and messages from `commands.sh` +- delete not needed commands and functions from `mycommands.sh` +- run `dev/make-standalone.sh` to create a a stripped down version of your bot Now have a look at the directory 'standalone', here you find the files 'bashbot.sh' and 'commands.sh' containing everything to run your bot. [Download make-standalone.sh](https://github.com/topkecleon/telegram-bot-bash/blob/master/dev/make-standalone.sh) from github. ### Setup your develop environment -1. install the commands git, [shellcheck](5_practice.md#Test-your-Bot-with-shellcheck), bc, pandoc, bc, codespell +1. install the commands git [shellcheck](5_practice.md#Test-your-Bot-with-shellcheck) bc pandoc zip codespell 2. setup your [environment for UTF-8](4_expert.md#Setting-up-your-Environment) -3. clone your bashbot fork to a new directory ```git clone https://github.com//telegram-bot-bash.git```, replace `````` with your username on github -4. create and change to your develop branch ```git checkout -b ```, replace `````` with the name you want to name it, e.g. 'develop' -5. give your (dev) fork a new version tag: ```git tag vx.xx```(optional) -6. setup github hooks by running ```dev/install-hooks.sh``` (optional) +3. clone your bashbot fork to a new directory `git clone https://github.com//telegram-bot-bash.git`, replace `` with your username on github +4. create and change to your develop branch `git checkout -b develop` +5. give your (dev) fork a new version tag: `git tag v1.xx` +6. setup github hooks by running `dev/install-hooks.sh` + +Note for Debian: Debian Buster has an old shellcheck version, pls update from [buster-backports](https://backports.debian.org/Instructions/) #### Test, Add, Push changes A typical bashbot develop loop looks as follow: @@ -349,5 +351,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.20-1-g239dd76 From 232a16bc90536379450752b6a65a4d42dee6894e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 26 Dec 2020 20:39:21 +0100 Subject: [PATCH 02/51] bin/ check files before include --- bin/bashbot_env.inc.sh | 16 ++++++++-------- doc/7_develop.md | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bin/bashbot_env.inc.sh b/bin/bashbot_env.inc.sh index 5e18fd9..22d850b 100644 --- a/bin/bashbot_env.inc.sh +++ b/bin/bashbot_env.inc.sh @@ -13,7 +13,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 18.12.2020 12:27 # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.20-2-g00af5b8 #=============================================================================== ############ @@ -36,15 +36,8 @@ BASHBOT_ETC="${BASHBOT_HOME}" if [ ! -r "${BASHBOT_HOME}/bashbot.sh" ]; then echo "Bashbot.sh not found in \"${BASHBOT_HOME}\"" exit 4 -else - # shellcheck disable=SC1090 - source "${BASHBOT_HOME}/bashbot.sh" source "$1" fi -# overwrite bot FILE regex to BASHBOT_ETC -# change this to the location you want to allow file uploads from -FILE_REGEX="${BASHBOT_ETC%/bin*}/.*" - # check for botconfig.jssh readable if [ ! -r "${BASHBOT_ETC}/botconfig.jssh" ]; then echo "Bashbot config file in \"${BASHBOT_ETC}\" does not exist or is not readable." @@ -56,6 +49,13 @@ if [ ! -r "${BASHBOT_VAR}/count.jssh" ]; then exit 3 fi +# shellcheck disable=SC1090 +source "${BASHBOT_HOME}/bashbot.sh" source "$1" + +# overwrite bot FILE regex to BASHBOT_ETC +# change this to the location you want to allow file uploads from +FILE_REGEX="${BASHBOT_ETC%/bin*}/.*" + # get and check ADMIN and NAME BOT_ADMIN="$(getConfigKey "botadmin")" BOT_NAME="$(getConfigKey "botname")" diff --git a/doc/7_develop.md b/doc/7_develop.md index 64b3633..c021373 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -351,5 +351,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v1.20-1-g239dd76 +#### $$VERSION$$ v1.20-2-g00af5b8 From 1bd1727ea06432abc27568888657297aa8ed7581 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 26 Dec 2020 20:43:45 +0100 Subject: [PATCH 03/51] braodcast.sh check for 403 erros --- bin/bashbot_env.inc.sh | 2 +- bin/send_broadcast.sh | 34 +++++++++++++++++++++++++--------- doc/7_develop.md | 2 +- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/bin/bashbot_env.inc.sh b/bin/bashbot_env.inc.sh index 22d850b..72109be 100644 --- a/bin/bashbot_env.inc.sh +++ b/bin/bashbot_env.inc.sh @@ -13,7 +13,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 18.12.2020 12:27 # -#### $$VERSION$$ v1.20-2-g00af5b8 +#### $$VERSION$$ v1.20-3-g232a16b #=============================================================================== ############ diff --git a/bin/send_broadcast.sh b/bin/send_broadcast.sh index 717e9bb..0af5c03 100755 --- a/bin/send_broadcast.sh +++ b/bin/send_broadcast.sh @@ -24,8 +24,13 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 16.12.2020 16:14 # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.20-3-g232a16b #=============================================================================== +# shellcheck disable=SC2059 + +#### +# minimum messages seen in a chat before send a broadcast to it +MINCOUNT=2 #### # broadcast is dangerous, without --doit we do a dry run ... @@ -63,10 +68,10 @@ case "$1" in shift ;; '') - echo "missing missing arguments" + printf "missing missing arguments\n" ;& "-h"*) - echo 'usage: send_message [-h|--help] [--groups|--both] [format] "message ...." [debug]' + printf 'usage: send_message [-h|--help] [--groups|--both] [format] "message ...." [debug]\n' exit 1 ;; '--h'*) @@ -84,14 +89,14 @@ source "${0%/*}/bashbot_env.inc.sh" "$2" # $3 debug declare -A SENDALL jssh_readDB_async "SENDALL" "${COUNTFILE}" if [ -z "${SENDALL[*]}" ]; then - echo -e "${ORANGE}Countfile not found or empty,${NC} " + printf "${ORANGE}Countfile not found or empty,${NC}\n" fi # loop over users - echo -e "${GREEN}Sending broadcast message to all users of ${BOT_NAME}${NC}${GREY}\c" + printf "${GREEN}Sending broadcast message to all users of ${BOT_NAME}${NC}${GREY}" { # dry run - [ -z "${DOIT}" ] && echo -e "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}" + [ -z "${DOIT}" ] && printf "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}\n" for USER in ${!SENDALL[*]} do @@ -100,14 +105,25 @@ fi [[ "${SENDTO}" != "users" && "${USER}" != *"-"* ]] && continue # ignore everything not a user or group [[ ! "${USER}" =~ ^[0-9-]*$ ]] && continue + # ignore chats with no count or lower MINCOUNT + [[ ! "${SENDALL[${USER}]}" =~ ^[0-9]*$ || "${SENDALL[${USER}]}" -lt "${MINCOUNT}" ]] && continue (( COUNT++ )) if [ -z "${DOIT}" ]; then - echo "${SEND}" "${USER}" "$1" "$2" + printf "${SEND} ${USER} $1 $2\n" else "${SEND}" "${USER}" "$1" "$2" - echo -e ".\c" 1>&2 + printf "." 1>&2 + # ups, kicked or banned ... + if [ "${BOTSENT[ERROR]}" = "403" ]; then + # remove chat from future broadcasts + jssh_insertKeyDB "${USER}" "${SENDALL[${USER}]} banned" "${COUNTFILE}" + printf "${ORANGE}Warning: bot banned from chat${NC} %s ${ORANGE}after${NC} %s ${ORANGE}commands${NC}\n"\ + "${USER}" "${SENDALL[${USER}]}" + fi sleep 0.1 fi done - echo -e "${NC}\nMessage \"$1\" sent to ${COUNT} ${SENDTO}${GROUPSALSO}." + # printout final stats message + printf "${NC}\n${GREEN}Message${NC} $1 ${GREEN}sent to${NC} ${COUNT} ${GREEN}${SENDTO}${GROUPSALSO}.${NC}\n" } | more + diff --git a/doc/7_develop.md b/doc/7_develop.md index c021373..9352b47 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -351,5 +351,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v1.20-2-g00af5b8 +#### $$VERSION$$ v1.20-3-g232a16b From 2e878fd8645a8d1cac3f2f675528fe2fcdcf808e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 26 Dec 2020 20:48:13 +0100 Subject: [PATCH 04/51] bin/ use printf --- bin/send_edit_message.sh | 6 +++--- bin/send_file.sh | 7 +++---- bin/send_message.sh | 7 +++---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/bin/send_edit_message.sh b/bin/send_edit_message.sh index 44ba733..5d32be8 100755 --- a/bin/send_edit_message.sh +++ b/bin/send_edit_message.sh @@ -22,7 +22,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 23.12.2020 16:52 # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.20-4-g1bd1727 #=============================================================================== #### @@ -42,10 +42,10 @@ case "$1" in shift ;; '') - echo "missing arguments" + printf "missing arguments\n" ;& "-h"*) - echo 'usage: send_edit_message [-h|--help] [format] "CHAT[ID]" "MESSAGE[ID]" "message ..." [debug]' + printf 'usage: send_edit_message [-h|--help] [format] "CHAT[ID]" "MESSAGE[ID]" "message ..." [debug]\n' exit 1 ;; '--h'*) diff --git a/bin/send_file.sh b/bin/send_file.sh index 1322855..f654aac 100755 --- a/bin/send_file.sh +++ b/bin/send_file.sh @@ -21,19 +21,18 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 25.12.2020 20:24 # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.20-4-g1bd1727 #=============================================================================== - #### # parse args SEND="upload_file" case "$1" in '') - echo "missing arguments" + printf "missing arguments\n" ;& "-h"*) - echo 'usage: send_file [-h|--help] "CHAT[ID]" "file" "caption ...." [debug]' + printf 'usage: send_file [-h|--help] "CHAT[ID]" "file" "caption ...." [debug]\n' exit 1 ;; '--h'*) diff --git a/bin/send_message.sh b/bin/send_message.sh index 2061439..7071fcd 100755 --- a/bin/send_message.sh +++ b/bin/send_message.sh @@ -21,10 +21,9 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 16.12.2020 11:34 # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.20-4-g1bd1727 #=============================================================================== - #### # parse args SEND="send_message" @@ -42,10 +41,10 @@ case "$1" in shift ;; '') - echo "missing arguments" + printf "missing arguments\n" ;& "-h"*) - echo 'usage: send_message [-h|--help] [format] "CHAT[ID]" "message ...." [debug]' + printf 'usage: send_message [-h|--help] [format] "CHAT[ID]" "message ...." [debug]\n' exit 1 ;; '--h'*) From 03bdecb05604e1d22926aa5dd1feabb66394bb89 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 26 Dec 2020 21:18:18 +0100 Subject: [PATCH 05/51] use printf --- bashbot.sh | 148 ++++++++++++++++++++------------------- bin/send_edit_message.sh | 2 +- bin/send_file.sh | 2 +- bin/send_message.sh | 2 +- 4 files changed, 78 insertions(+), 76 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index eb1410f..70058dd 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-0-g2e878fd # # Exit Codes: # - 0 success (hopefully) @@ -24,16 +24,17 @@ # - 7 can't get bottoken # - 8 curl/wget missing # - 10 not bash! -# shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117 +# shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 # emmbeded system may claim bash but it is not # check for bash like ARRAY handlung if ! (unset a; set -A a a; eval "a=(a b)"; eval '[ -n "${a[1]}" ]'; ) > /dev/null 2>&1; then - echo "Error: Current shell does not support ARRAY's, may be busbox ash shell. pls install a real bash!"; + printf "Error: Current shell does not support ARRAY's, may be busbox ash shell. pls install a real bash!\n" exit 10 fi # are we running in a terminal? +NN="\n" if [ -t 1 ] && [ -n "$TERM" ]; then CLEAR='clear' RED='\e[31m' @@ -41,16 +42,17 @@ if [ -t 1 ] && [ -n "$TERM" ]; then ORANGE='\e[35m' GREY='\e[1;30m' NC='\e[0m' + NN="${NN}" fi # telegram uses utf-8 characters, check if we have an utf-8 charset if [ "${LANG}" = "${LANG%[Uu][Tt][Ff]*}" ]; then - echo -e "${ORANGE}Warning: Telegram uses utf-8, but looks like you are using non utf-8 locale:${NC} ${LANG}" + printf "${ORANGE}Warning: Telegram uses utf-8, but looks like you are using non utf-8 locale:${NC} ${LANG}\n" fi # we need some bash 4+ features, check for old bash by feature if [ "$({ LC_ALL=C.utf-8 echo -e "\u1111"; } 2>/dev/null)" = "\u1111" ]; then - echo -e "${ORANGE}Warning: Missing unicode '\uxxxx' support, missing C.utf-8 locale or to old bash version.${NC}" + printf "${ORANGE}Warning: Missing unicode '\uxxxx' support, missing C.utf-8 locale or to old bash version.${NN}" fi @@ -117,7 +119,7 @@ debug_checks(){ { # some linux, e.g. manajro seems not to have C locale activated by default if _exists locale && [ "$(locale -a | grep -c -e "^C$" -e "^C.utf8$")" -lt 2 ]; then - echo -e "${ORANGE}Warning: locale ${NC}${GREY}C${NC}${ORANGE} and/or ${NC}${GREY}C.utf8${NC}${ORANGE} seems missing, use \"${NC}${GREY}locale -a${NC}${ORANGE}\" to show what locales are installed on your system.${NC}" + printf "${ORANGE}Warning: locale ${NC}${GREY}C${NC}${ORANGE} and/or ${NC}${GREY}C.utf8${NC}${ORANGE} seems missing, use \"${NC}${GREY}locale -a${NC}${ORANGE}\" to show what locales are installed on your system.${NN}" fi # get location and name of bashbot.sh @@ -134,7 +136,7 @@ if [ "${SCRIPT}" != "${REALME}" ] || [ "$1" = "source" ]; then fi BOTCOMMANDS="start, stop, status, help, init, suspendback, resumeback, killback" -[[ -z "$1" && -z "${SOURCE}" ]] && echo -e "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit +[[ -z "$1" && -z "${SOURCE}" ]] && printf "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NN}" && exit if [ "$1" = "help" ]; then HELP="${BASHBOT_HOME:-.}/README" if [ -n "${CLEAR}" ];then @@ -159,14 +161,14 @@ RUNUSER="${USER}" # USER is overwritten by bashbot array :-(, save original # OK everything setup, lets start if [[ -z "${SOURCE}" && -z "$BASHBOT_HOME" ]] && ! cd "${RUNDIR}" ; then - echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}" + printf "${RED}ERROR: Can't change to ${RUNDIR} ...${NN}" exit 1 else RUNDIR="." fi if [ ! -w "." ]; then - echo -e "${ORANGE}WARNING: ${RUNDIR} is not writeable!${NC}" + printf "${ORANGE}WARNING: ${RUNDIR} is not writeable!${NN}" ls -ld . fi @@ -190,12 +192,12 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then token="$(< "${TOKENFILE}")" # no old token, ask user elif [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then - echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!" + printf "Running headless, set BOTTOKEN or run ${SCRIPT} init first!\n" exit 2 else ${CLEAR} - echo -e "${RED}TOKEN MISSING.${NC}" - echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" + printf "${RED}TOKEN MISSING.${NN}" + printf "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NN}" read -r token fi [ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTCONFIG}.jssh" @@ -206,12 +208,12 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then if [ -r "${BOTADMIN}" ]; then admin="$(< "${BOTADMIN}")" elif [ -z "${CLEAR}" ]; then - echo "Running headless, set botadmin to AUTO MODE!" + printf "Running headless, set botadmin to AUTO MODE!\n" else ${CLEAR} - echo -e "${RED}BOTADMIN MISSING.${NC}" - echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}" - echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}" + printf "${RED}BOTADMIN MISSING.${NN}" + printf "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NN}" + printf "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NN}" read -r admin fi [ -z "${admin}" ] && admin='?' @@ -219,14 +221,14 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then fi # setup botacl file if [ ! -f "${BOTACL}" ]; then - echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}" + printf "${ORANGE}Create empty ${BOTACL} file.${NC}" printf '\n' >"${BOTACL}" fi # setup data dir file if [ ! -d "${DATADIR}" ]; then mkdir "${DATADIR}" elif [ ! -w "${DATADIR}" ]; then - echo -e "${RED}ERROR: Can't write to ${DATADIR}!.${NC}" + printf "${RED}ERROR: Can't write to ${DATADIR}!.${NN}" ls -ld "${DATADIR}" exit 2 fi @@ -238,7 +240,7 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh" fi elif [ ! -w "${COUNTFILE}.jssh" ]; then - echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}" + printf "${RED}ERROR: Can't write to ${COUNTFILE}!.${NN}" ls -l "${COUNTFILE}.jssh" exit 2 fi @@ -262,13 +264,13 @@ if [ -z "${BOTTOKEN}" ]; then BOTTOKEN="$(getConfigKey "bottoken")" if [ -z "${BOTTOKEN}" ]; then BOTERROR="Warning: can't get bot token, try to recover working config..." - echo -e "${ORANGE}${BOTERROR}${NC} \c" + printf "${ORANGE}${BOTERROR}${NC} " if [ -r "${BOTCONFIG}.jssh.ok" ]; then log_error "${BOTERROR}" - cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh"; echo "OK" + cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh"; printf "OK\n" BOTTOKEN="$(getConfigKey "bottoken")" else - echo -e "\n${RED}Error: Missing bot token! remove ${BOTCONFIG}.jssh and run \"bashbot.sh init\" may fix it.${NC}" + printf "\n${RED}Error: Missing bot token! remove ${BOTCONFIG}.jssh and run \"bashbot.sh init\" may fix it.${NN}" exit 7 fi fi @@ -276,16 +278,16 @@ fi # BOTTOKEN format checks if ! check_token "${BOTTOKEN}"; then - echo -e "${ORANGE}Warning: your bottoken may incorrect. it should have the following format:${NC}" - echo -e "${GREY}123456789${RED}:${GREY}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4${ORANGE} => ${NC}\c" - echo -e "${GREY}8-10 digits${RED}:${GREY}35 alphanumeric characters + '_-'${NC}" - echo -e "${ORANGE}Your current token is: '${GREY}^$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")${ORANGE}'${NC}" + printf "${ORANGE}Warning: your bottoken may incorrect. it should have the following format:${NN}" + printf "${GREY}123456789${RED}:${GREY}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4${ORANGE} => ${NC}" + printf "${GREY}8-10 digits${RED}:${GREY}35 alphanumeric characters + '_-'${NN}" + printf "${ORANGE}Your current token is: '${GREY}^$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")${ORANGE}'${NN}" if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}: ]]; then - echo -e "${ORANGE}Possible problem in the digits part, len is $(($(wc -c <<<"${BOTTOKEN%:*}")-1))${NC}" - [ -n "$(getConfigKey "botid")" ] && echo -e "${GREY}Did you mean: \"${NC}$(getConfigKey "botid")${GREY}\" ?${NC}" + printf "${ORANGE}Possible problem in the digits part, len is $(($(wc -c <<<"${BOTTOKEN%:*}")-1))${NN}" + [ -n "$(getConfigKey "botid")" ] && printf "${GREY}Did you mean: \"${NC}$(getConfigKey "botid")${GREY}\" ?${NN}" fi [[ ! "${BOTTOKEN}" =~ :[a-zA-Z0-9_-]{35}$ ]] &&\ - echo -e "${ORANGE}Possible problem in the characters part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NC}" + printf "${ORANGE}Possible problem in the characters part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NN}" fi @@ -317,7 +319,7 @@ export res CAPTION ME COMMANDS="${BASHBOT_ETC:-.}/commands.sh" if [ -z "${SOURCE}" ]; then if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then - echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}" + printf "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NN}" ls -l "${COMMANDS}" exit 3 fi @@ -338,11 +340,11 @@ done # do we have BSD sed if ! sed '1ia' /dev/null; then - echo -e "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NC}" + printf "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NN}" fi #jsonDB is now mandatory if ! _is_function jssh_newDB ; then - echo -e "${RED}ERROR: Mandatory module jsonDB is missing or not readable!" + printf "${RED}ERROR: Mandatory module jsonDB is missing or not readable!${NN}" exit 6 fi @@ -523,9 +525,9 @@ else else # ups, no curl AND no wget if [ -n "${BASHBOT_WGET}" ]; then - echo -e "${RED}Error: You set BASHBOT_WGET but no wget found!${NC}" + printf "${RED}Error: You set BASHBOT_WGET but no wget found!${NN}" else - echo -e "${RED}Error: curl and wget not found, install curl!${NC}" + printf "${RED}Error: curl and wget not found, install curl!${NN}" fi exit 8 fi @@ -649,7 +651,7 @@ getBotName() { # save botname and id setConfigKey "botname" "${BOTARRAY["result","username"]}" setConfigKey "botid" "${BOTARRAY["result","id"]}" - echo "${BOTARRAY["result","username"]}" + printf "${BOTARRAY["result","username"]}\n" } # pure bash implementation, done by KayM (@gnadelwartz) @@ -1102,42 +1104,42 @@ bot_init() { [ -n "${BASHBOT_HOME}" ] && cd "${BASHBOT_HOME}" || exit 1 local DEBUG="$1" # upgrade from old version - echo "Check for Update actions ..." + printf "Check for Update actions ...\n" local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash" [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${DATADIR}"; rmdir "${OLDTMP}"; } # no more existing modules [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" # load addons on startup - echo "Done." - echo "Initialize modules and addons ..." + printf "Done.\n" + printf "Initialize modules and addons ...\n" for addons in "${ADDONDIR:-.}"/*.sh ; do # shellcheck source=./modules/aliases.sh [ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}" done - echo "Done." + printf "Done.\n" if [[ ! -d "logs" ]]; then - echo "Move Logfiles ..." + printf "Move Logfiles ...\n" mkdir logs 2>/dev/null for MVLOG in DEBUG.log MESSAGE.log ERROR.log BASHBOT.log do [ -f "${MVLOG}" ] && mv "${MVLOG}" logs 2>/dev/null done - echo "Done." + printf "Done.\n" fi # setup bashbot [[ "${UID}" -eq "0" ]] && RUNUSER="nobody" - echo -n "Enter User to run bashbot [$RUNUSER]: " + printf "Enter User to run bashbot [$RUNUSER]: " read -r TOUSER [ -z "$TOUSER" ] && TOUSER="$RUNUSER" if ! id "$TOUSER" &>/dev/null; then - echo -e "${RED}User \"$TOUSER\" not found!${NC}" + printf "${RED}User \"$TOUSER\" not found!${NN}" exit 3 else # shellcheck disable=SC2009 oldbot="$(ps -fu "$TOUSER" | grep startbot | grep -v -e 'grep' -e '\-startbot' )" [ -n "${oldbot}" ] && \ - echo -e "${ORANGE}Warning: At least one not upgraded TMUX bot is running! You must stop it with kill command:${NC}\\n${oldbot}" - echo "Adjusting files and permissions for user \"${TOUSER}\" ..." + printf "${ORANGE}Warning: At least one not upgraded TMUX bot is running! You must stop it with kill command:${NN}${oldbot}\n" + printf "Adjusting files and permissions for user \"${TOUSER}\" ...\n" [ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc" chown -R "$TOUSER" . ./* chmod 711 . @@ -1146,45 +1148,45 @@ bot_init() { chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null # jsshDB must writeable by owner find . -name '*.jssh*' -exec chmod u+w \{\} + - echo "Done." + printf "Done.\n" fi # ask to check bottoken online if [ -z "$(getConfigKey "botid")" ]; then - echo -e "Seems to be your first init. Should I verify your bot token online? (y/N) N\b\c" + printf "Seems to be your first init. Should I verify your bot token online? (y/N) N\b" read -r ANSWER if [[ "${ANSWER}" =~ ^[Yy] ]]; then - echo -e "${GREEN}Contacting telegram to verify your bot token ...${NC}" + printf "${GREEN}Contacting telegram to verify your bot token ...${NN}" $0 botname fi fi # check if botconf if seems valid - echo -e "${GREEN}This is your bot config:${NC}" + printf "${GREEN}This is your bot config:${NN}" sed 's/^/\t/' "${BOTCONFIG}.jssh" | grep -vF '["bot_config_key"]' if [[ "$(getConfigKey "bottoken")" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ && "$(getConfigKey "botadmin")" =~ ^[0-9]+$ ]]; then - echo -e "Bot config seems to be valid. Should I make a backup copy? (Y/n) Y\b\c" + printf "Bot config seems to be valid. Should I make a backup copy? (Y/n) Y\b" read -r ANSWER if [[ -z "${ANSWER}" || "${ANSWER}" =~ ^[^Nn] ]]; then - echo "Copy bot config to ${BOTCONFIG}.jssh.ok ..." + printf "Copy bot config to ${BOTCONFIG}.jssh.ok ...\n" cp "${BOTCONFIG}.jssh" "${BOTCONFIG}.jssh.ok" fi else - echo -e "${ORANGE}Bot config may not complete, pls check.${NC}" + printf "${ORANGE}Bot config may not complete, pls check.${NN}" fi # show result ls -ld "${DATADIR}" "${LOGDIR}" ./*.jssh* ./*.sh 2>/dev/null } if ! _is_function send_message ; then - echo -e "${RED}ERROR: send_message is not available, did you deactivate ${MODULEDIR}/sendMessage.sh?${NC}" + printf "${RED}ERROR: send_message is not available, did you deactivate ${MODULEDIR}/sendMessage.sh?${NN}" exit 1 fi # get location of JSON.sh, download if not exist JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}" -[[ "${JSONSHFILE}" != *"/JSON.sh" ]] && echo -e "${RED}ERROR: \"${JSONSHFILE}\" ends not with \"JSONS.sh\".${NC}" && exit 3 +[[ "${JSONSHFILE}" != *"/JSON.sh" ]] && printf "${RED}ERROR: \"${JSONSHFILE}\" ends not with \"JSONS.sh\".${NN}" && exit 3 if [ ! -f "${JSONSHFILE}" ]; then - echo "Seems to be first run, Downloading ${JSONSHFILE}..." + printf "Seems to be first run, Downloading ${JSONSHFILE}...\n" [ "${SCRIPTDIR}/JSON.sh/JSON.sh" = "${JSONSHFILE}" ] &&\ mkdir "${SCRIPTDIR}/JSON.sh" 2>/dev/null && chmod +w "${SCRIPTDIR}/JSON.sh" getJson "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" >"${JSONSHFILE}" @@ -1210,14 +1212,14 @@ if [ -z "${SOURCE}" ]; then ME="$(getBotName)" if [ -n "${ME}" ]; then # ok we have a connection and got botname, save it - [ -n "${CLEAR}" ] && echo -e "${GREY}Bottoken is valid ...${NC}" + [ -n "${CLEAR}" ] && printf "${GREY}Bottoken is valid ...${NN}" jssh_updateKeyDB "botname" "${ME}" "${BOTCONFIG}" rm -f "${BOTCONFIG}.jssh.flock" else - echo -e "${GREY}Info: Can't get Botname from Telegram, try cached one ...${NC}" + printf "${GREY}Info: Can't get Botname from Telegram, try cached one ...${NN}" ME="$(getConfigKey "botname")" if [ -z "$ME" ]; then - echo -e "${RED}ERROR: No cached botname, can't continue! ...${NC}" + printf "${RED}ERROR: No cached botname, can't continue! ...${NN}" exit 1 fi fi @@ -1226,8 +1228,8 @@ if [ -z "${SOURCE}" ]; then ;;& # used to send output of background and interactive to chats "outproc") # $2 chat_id $3 identifier of job, internal use only! - [ -z "$3" ] && echo "No job identifier" && exit 3 - [ -z "$2" ] && echo "No chat to send to" && exit 3 + [ -z "$3" ] && printf "No job identifier\n" && exit 3 + [ -z "$2" ] && printf "No chat to send to\n" && exit 3 ME="$(getConfigKey "botname")" # read until terminated while read -r line ;do @@ -1289,7 +1291,7 @@ if [ -z "${SOURCE}" ]; then ;; # send message to all users 'broadcast') - echo -e "${ORANGE}Broadcast is a separate command now, see ${BASHBOT_HOME:-.}/bin/send_broadcast.sh --help${NC}" + printf "${ORANGE}Broadcast is a separate command now, see ${BASHBOT_HOME:-.}/bin/send_broadcast.sh --help${NN}" "${BASHBOT_HOME:-.}"/bin/send_broadcast.sh --help exit ;; @@ -1299,10 +1301,10 @@ if [ -z "${SOURCE}" ]; then SESSION="${ME:-_bot}-startbot" BOTPID="$(proclist "${SESSION}")" if [ -n "${BOTPID}" ]; then - echo -e "${GREEN}Bot is running with UID ${RUNUSER}.${NC}" + printf "${GREEN}Bot is running with UID ${RUNUSER}.${NN}" exit else - echo -e "${ORANGE}No Bot running with UID ${RUNUSER}.${NC}" + printf "${ORANGE}No Bot running with UID ${RUNUSER}.${NN}" exit 5 fi debug_checks "end status" "$@" @@ -1319,9 +1321,9 @@ if [ -z "${SOURCE}" ]; then printf "Session Name: %s\n" "${SESSION}" sleep 1 if [ -n "$(proclist "${SESSION}")" ]; then - echo -e "${GREEN}Bot started successfully.${NC}" + printf "${GREEN}Bot started successfully.${NN}" else - echo -e "${RED}An error occurred while starting the bot.${NC}" + printf "${RED}An error occurred while starting the bot.${NN}" exit 5 fi debug_checks "end start" "$@" @@ -1337,34 +1339,34 @@ if [ -z "${SOURCE}" ]; then # inform botadmin about stop ADMIN="$(getConfigKey "botadmin")" [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot ${ME} stopped ..." & - echo -e "${GREEN}OK. Bot stopped successfully.${NC}" + printf "${GREEN}OK. Bot stopped successfully.${NN}" else - echo -e "${RED}An error occurred while stopping bot.${NC}" + printf "${RED}An error occurred while stopping bot.${NN}" exit 5 fi else - echo -e "${ORANGE}No Bot running with UID ${RUNUSER}.${NC}" + printf "${ORANGE}No Bot running with UID ${RUNUSER}.${NN}" fi debug_checks "end stop" "$@" exit ;; # suspend, resume or kill background jobs "suspendb"*|"resumeb"*|"killb"*) - _is_function job_control || { echo -e "${RED}Module background is not available!${NC}"; exit 3; } + _is_function job_control || { printf "${RED}Module background is not available!${NN}"; exit 3; } ME="$(getConfigKey "botname")" job_control "$1" debug_checks "end background $1" "$@" ;; *) - echo -e "${RED}${REALME##*/}: unknown command${NC}" - echo -e "${RED}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit + printf "${RED}${REALME##*/}: unknown command${NN}" + printf "${RED}Available commands: ${GREY}${BOTCOMMANDS}${NN}" && exit exit 4 ;; esac # warn if root if [[ "${UID}" -eq "0" ]] ; then - echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}" - echo -e "${ORANGE}You are at HIGH RISK when running a Telegram BOT with root privileges!${NC}" + printf "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NN}" + printf "${ORANGE}You are at HIGH RISK when running a Telegram BOT with root privileges!${NN}" fi fi # end source diff --git a/bin/send_edit_message.sh b/bin/send_edit_message.sh index 5d32be8..75e9325 100755 --- a/bin/send_edit_message.sh +++ b/bin/send_edit_message.sh @@ -22,7 +22,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 23.12.2020 16:52 # -#### $$VERSION$$ v1.20-4-g1bd1727 +#### $$VERSION$$ v1.21-dev-0-g2e878fd #=============================================================================== #### diff --git a/bin/send_file.sh b/bin/send_file.sh index f654aac..d513983 100755 --- a/bin/send_file.sh +++ b/bin/send_file.sh @@ -21,7 +21,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 25.12.2020 20:24 # -#### $$VERSION$$ v1.20-4-g1bd1727 +#### $$VERSION$$ v1.21-dev-0-g2e878fd #=============================================================================== #### diff --git a/bin/send_message.sh b/bin/send_message.sh index 7071fcd..0d95d5f 100755 --- a/bin/send_message.sh +++ b/bin/send_message.sh @@ -21,7 +21,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 16.12.2020 11:34 # -#### $$VERSION$$ v1.20-4-g1bd1727 +#### $$VERSION$$ v1.21-dev-0-g2e878fd #=============================================================================== #### From de31231a24c1f6348c83c96a53602e87ddb6fb8a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 26 Dec 2020 21:29:16 +0100 Subject: [PATCH 06/51] printf part 2 --- addons/example.sh | 4 ++-- modules/background.sh | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/addons/example.sh b/addons/example.sh index 3feb6a8..a1e2632 100644 --- a/addons/example.sh +++ b/addons/example.sh @@ -4,7 +4,7 @@ # Addons can register to bashbot events at startup # by providing their name and a callback per event # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-1-g03bdecb # # If an event occurs each registered event function is called. # @@ -109,6 +109,6 @@ if [[ "$1" = "start"* ]]; then # Note: do not call any send message functions from EVENT_SEND! example_log(){ local send="$1"; shift - echo "$(date): Type: ${send} Args: $*" >>"${EXAMPLE_LOG}" + printf "%s: Type: %s Args: %s\n" "$(date)" "${send}" "$*" >>"${EXAMPLE_LOG}" } fi diff --git a/modules/background.sh b/modules/background.sh index e4ea11a..06a680c 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,8 +5,8 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -# shellcheck disable=SC1117 -#### $$VERSION$$ v1.20-0-g2ab00a2 +# shellcheck disable=SC1117,SC2059 +#### $$VERSION$$ v1.21-dev-1-g03bdecb # will be automatically sourced from bashbot @@ -129,7 +129,7 @@ job_control() { ADM="$(getConfigKey "botadmin")" debug_checks "Enter job_control" "${1}" for FILE in "${DATADIR:-.}/"*-back.cmd; do - [ "${FILE}" = "${DATADIR:-.}/*-back.cmd" ] && echo -e "${RED}No background processes.${NC}" && break + [ "${FILE}" = "${DATADIR:-.}/*-back.cmd" ] && printf "${RED}No background processes.${NN}" && break content="$(< "${FILE}")" CHAT="${content%%:*}" job="${content#*:}" From 79402eca27c9df629539f902c95bf0b787dd1f31 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 10:38:49 +0100 Subject: [PATCH 07/51] doc: send_file absolute path vs relative to UPLOADDIR --- bin/bashbot_env.inc.sh | 7 ++++--- doc/3_advanced.md | 9 ++++++--- doc/6_reference.md | 27 ++++++++++++++++++--------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/bin/bashbot_env.inc.sh b/bin/bashbot_env.inc.sh index 72109be..4d93c46 100644 --- a/bin/bashbot_env.inc.sh +++ b/bin/bashbot_env.inc.sh @@ -13,7 +13,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 18.12.2020 12:27 # -#### $$VERSION$$ v1.20-3-g232a16b +#### $$VERSION$$ v1.21-dev-2-gde31231 #=============================================================================== ############ @@ -52,9 +52,10 @@ fi # shellcheck disable=SC1090 source "${BASHBOT_HOME}/bashbot.sh" source "$1" -# overwrite bot FILE regex to BASHBOT_ETC +# overwrite bot FILE regex to BASHBOT_VAR # change this to the location you want to allow file uploads from -FILE_REGEX="${BASHBOT_ETC%/bin*}/.*" +UPLOADDIR="${BASHBOT_VAR%/bin*}" +FILE_REGEX="${UPLOADDIR}/.*" # get and check ADMIN and NAME BOT_ADMIN="$(getConfigKey "botadmin")" diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 7507f49..ef1e8db 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -106,7 +106,7 @@ echo -e "Your Message: ${test}\nbye!" #### message formatting and keyboards The output of the script will be processed by 'send_messages', so you can not only send text, but also keyboards, files, locations and more. -Each newline in the output will start an new message to the userr. To have line breaks in your message you must insert ' mynewlinestartshere ' or '\n' instead. +Each newline in the output will start an new message to the user. To have line breaks in your message you must insert ' mynewlinestartshere ' or '\n' instead. To open up a keyboard in an interactive script, print out the keyboard layout in the following way: ```bash @@ -114,8 +114,11 @@ echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No ``` Same goes for files: ```bash -echo "Text that will appear in chat? myfilelocationstartshere /home/user/doge.jpg" +echo "Text that will appear in chat? myfilelocationstartshere /home/user/dog.jpg" ``` +*Note*: Use an _absolute path name_ (starting with `/`), a relative path name is relative to `data-bot-bash/upload`! +See [send_file documentation](6_reference.md#send_file) for more information. + And buttons: ```bash echo "Text that will appear in chat. mybtextstartshere Click me myburlstartshere https://dealz.rrr.de" @@ -299,5 +302,5 @@ Note: If you disable automatic retry, se above, you disable also connection prob #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-2-gde31231 diff --git a/doc/6_reference.md b/doc/6_reference.md index c453f1d..9f18f72 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -114,20 +114,29 @@ The main use case for send_message is to process the output of interactive chats ##### send_file -send_file allows you to send different type's of files, e.g. photos, stickers, audio, media, etc. [see more](https://core.telegram.org/bots/api#sending-files) +send_file allows you to send different type's of files, e.g. photos, stickers, audio, media, etc. [see more](https://core.telegram.org/bots/api#sending-files). +It's recommended to use the _absolute path name_ (starting with `/`). A relative path is threated as relative to `data-bot-bash/upload` as default UPLOADDIR. -- file names must not contain ".." -- file names must not start with "." -- file names not starting with "/" are relative to $TMPDIR, e.g. ./data-bot-bash -- absolute filenames must match $FILE_REGEX -- FILE_REGEX is a regular expression, not shell globbing, test you rexexes: http://www.softlion.com/webTools/RegExpTest/ +For security reasons the following restrictions apply: + +- absolute path name must match the shell _regex_ FILE_REGEX (not file glob!) +- path must not start with "." and not contain ".." *usage:* send_file "${CHAT[ID]}" "file" "caption" *example:* ```bash -send_file "${CHAT[ID]}" "/home/user/doge.jpg" "Lool" -send_file "${CHAT[ID]}" "https://www.domain,com/something.gif" "Something" +# recommended absolute path +send_file "${CHAT[ID]}" "/home/user/dog.jpg" "My Dog" + +# relative to default dir: data-bot-bash/upload/dog.jpg +send_file "${CHAT[ID]}" "dog.jpg" "My Dog" + +# change to personal dir +UPLOADDIR="/home/user/myuploaddir" + +# relative to changed dir: /home/user/myuploaddir/dog.jpg +send_file "${CHAT[ID]}" "dog.jpg" "My Dog" ``` ##### send_album @@ -1145,5 +1154,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-2-gde31231 From 0bda9d9feb6670cd995dc5cab5b8acb911f311c3 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 10:49:51 +0100 Subject: [PATCH 08/51] doc: send_file better explanation --- doc/6_reference.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 9f18f72..ff8b502 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -114,28 +114,29 @@ The main use case for send_message is to process the output of interactive chats ##### send_file -send_file allows you to send different type's of files, e.g. photos, stickers, audio, media, etc. [see more](https://core.telegram.org/bots/api#sending-files). -It's recommended to use the _absolute path name_ (starting with `/`). A relative path is threated as relative to `data-bot-bash/upload` as default UPLOADDIR. +send_file allows can send different type's of files, e.g. photos, stickers, audio, media, etc. +[see Telegram API documentation](https://core.telegram.org/bots/api#sending-files). + +It's recommended to use __absolute path names__ (_starting with `/`_), as relative path names are threated as __relative to UPLOADDIR__ `data-bot-bash/upload`! For security reasons the following restrictions apply: -- absolute path name must match the shell _regex_ FILE_REGEX (not file glob!) -- path must not start with "." and not contain ".." +- absolute path name must match the __shell regex__ `FILE_REGEX` (_not file glob_) +- path must not start with `./` and not contain `../` *usage:* send_file "${CHAT[ID]}" "file" "caption" *example:* ```bash -# recommended absolute path +# recommended: absolute path send_file "${CHAT[ID]}" "/home/user/dog.jpg" "My Dog" -# relative to default dir: data-bot-bash/upload/dog.jpg +# relative to UPLOADDIR: data-bot-bash/upload/dog.jpg send_file "${CHAT[ID]}" "dog.jpg" "My Dog" -# change to personal dir +# change to personal upload dir UPLOADDIR="/home/user/myuploaddir" - -# relative to changed dir: /home/user/myuploaddir/dog.jpg +# relative to personal upload dir: /home/user/myuploaddir/dog.jpg send_file "${CHAT[ID]}" "dog.jpg" "My Dog" ``` @@ -1154,5 +1155,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v1.21-dev-2-gde31231 +#### $$VERSION$$ v1.21-dev-3-g79402ec From 874e37e40860ae4dc812531a5a67a318dc5f1792 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 11:18:42 +0100 Subject: [PATCH 09/51] remove stats command --- bashbot.sh | 44 +++++++------------------------------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 70058dd..1601896 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.21-dev-0-g2e878fd +#### $$VERSION$$ v1.21-dev-4-g0bda9d9 # # Exit Codes: # - 0 success (hopefully) @@ -1253,45 +1253,15 @@ if [ -z "${SOURCE}" ]; then debug_checks "end init" "$@" exit ;; - # print usage stats - "stats") - echo -e "${ORANGE}stats deprecated, see bin/bashbot_stats --help${NC}" - ME="$(getConfigKey "botname")" - declare -A STATS - jssh_readDB_async "STATS" "${COUNTFILE}" - for MSG in ${!STATS[*]} - do - [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue - (( USERS++ )) - done - for MSG in ${STATS[*]} - do - (( MESSAGES+=MSG )) - done - if [ "${USERS}" != "" ]; then - echo "A total of ${MESSAGES} messages from ${USERS} users are processed." - else - echo "No one used your bot so far ..." - fi - jssh_readDB_async "STATS" "${BLOCKEDFILE}" - for MSG in ${!STATS[*]} - do - [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue - (( BLOCKS++ )) - done - if [ "${BLOCKS}" != "" ]; then - echo -e "Note: ${BLOCKS} users are blocked by your bot:${GREY}" - sort -r "${BLOCKEDFILE}.jssh" - echo -e "${NC}\c" - fi - # show user created bot stats - _exec_if_function my_bashbot_stats "$@" - debug_checks "end $1" "$@" + # stats deprecated + "stats"|"count") + printf "${ORANGE}Stats is a separate command now, see bin/bashbot_stats.sh --help${NN}" + "${BASHBOT_HOME:-.}"/bin/bashbot_stats.sh --help exit ;; - # send message to all users + # broadcast deprecated 'broadcast') - printf "${ORANGE}Broadcast is a separate command now, see ${BASHBOT_HOME:-.}/bin/send_broadcast.sh --help${NN}" + printf "${ORANGE}Broadcast is a separate command now, see bin/send_broadcast.sh --help${NN}" "${BASHBOT_HOME:-.}"/bin/send_broadcast.sh --help exit ;; From cae6288598cbf79c4162221194cfcd7cf95ea449 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 11:26:38 +0100 Subject: [PATCH 10/51] CLEAR -> INTERACTIVE --- bashbot.sh | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 1601896..969b943 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.21-dev-4-g0bda9d9 +#### $$VERSION$$ v1.21-dev-5-g874e37e # # Exit Codes: # - 0 success (hopefully) @@ -36,7 +36,7 @@ fi # are we running in a terminal? NN="\n" if [ -t 1 ] && [ -n "$TERM" ]; then - CLEAR='clear' + INTERACTIVE='yes' RED='\e[31m' GREEN='\e[32m' ORANGE='\e[35m' @@ -139,7 +139,7 @@ BOTCOMMANDS="start, stop, status, help, init, suspendback, resumeback, killback" [[ -z "$1" && -z "${SOURCE}" ]] && printf "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NN}" && exit if [ "$1" = "help" ]; then HELP="${BASHBOT_HOME:-.}/README" - if [ -n "${CLEAR}" ];then + if [ -n "${INTERACTIVE}" ];then _exists w3m && w3m "$HELP.html" && exit _exists lynx && lynx "$HELP.html" && exit _exists less && less "$HELP.txt" && exit @@ -191,11 +191,10 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then if [ -r "${TOKENFILE}" ]; then token="$(< "${TOKENFILE}")" # no old token, ask user - elif [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then + elif [ -z "${INTERACTIVE}" ] && [ "$1" != "init" ]; then printf "Running headless, set BOTTOKEN or run ${SCRIPT} init first!\n" exit 2 else - ${CLEAR} printf "${RED}TOKEN MISSING.${NN}" printf "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NN}" read -r token @@ -207,10 +206,9 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then # convert old admin if [ -r "${BOTADMIN}" ]; then admin="$(< "${BOTADMIN}")" - elif [ -z "${CLEAR}" ]; then + elif [ -z "${INTERACTIVE}" ]; then printf "Running headless, set botadmin to AUTO MODE!\n" else - ${CLEAR} printf "${RED}BOTADMIN MISSING.${NN}" printf "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NN}" printf "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NN}" @@ -1212,7 +1210,7 @@ if [ -z "${SOURCE}" ]; then ME="$(getBotName)" if [ -n "${ME}" ]; then # ok we have a connection and got botname, save it - [ -n "${CLEAR}" ] && printf "${GREY}Bottoken is valid ...${NN}" + [ -n "${INTERACTIVE}" ] && printf "${GREY}Bottoken is valid ...${NN}" jssh_updateKeyDB "botname" "${ME}" "${BOTCONFIG}" rm -f "${BOTCONFIG}.jssh.flock" else @@ -1223,7 +1221,7 @@ if [ -z "${SOURCE}" ]; then exit 1 fi fi - [ -n "${CLEAR}" ] && printf "Bot Name: %s\n" "${ME}" + [ -n "${INTERACTIVE}" ] && printf "Bot Name: %s\n" "${ME}" [ "$1" = "botname" ] && exit ;;& # used to send output of background and interactive to chats From b123c3dbcedcd437b61ffb19612be952766bec61 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 12:34:39 +0100 Subject: [PATCH 11/51] better messages for invalid bot token --- bashbot.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 969b943..5f88a0a 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.21-dev-5-g874e37e +#### $$VERSION$$ v1.21-dev-6-gcae6288 # # Exit Codes: # - 0 success (hopefully) @@ -276,20 +276,20 @@ fi # BOTTOKEN format checks if ! check_token "${BOTTOKEN}"; then - printf "${ORANGE}Warning: your bottoken may incorrect. it should have the following format:${NN}" - printf "${GREY}123456789${RED}:${GREY}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4${ORANGE} => ${NC}" - printf "${GREY}8-10 digits${RED}:${GREY}35 alphanumeric characters + '_-'${NN}" - printf "${ORANGE}Your current token is: '${GREY}^$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")${ORANGE}'${NN}" + printf "${ORANGE}Warning: Your bot token is incorrect, it should have the following format:${NC}\n" + printf "${RED}:${NC}<35_alphanumeric_characters-hash> ${RED}e.g. =>${NC} 123456789${RED}:${NC}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4\n\n" + printf "${GREY}Your bot token: '${NC}${BOTTOKEN//:/${RED}:${NC}}'\n" + if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}: ]]; then - printf "${ORANGE}Possible problem in the digits part, len is $(($(wc -c <<<"${BOTTOKEN%:*}")-1))${NN}" - [ -n "$(getConfigKey "botid")" ] && printf "${GREY}Did you mean: \"${NC}$(getConfigKey "botid")${GREY}\" ?${NN}" + printf "${GREY}\tHint: Bot id len: ${NC}$(($(wc -c <<<"${BOTTOKEN%:*}")-1)) ${GREY}but should be${NC} 8-10\n" + [ -n "$(getConfigKey "botid")" ] && printf "\t${GREEN}Did you mean: \"${NC}$(getConfigKey "botid")${GREEN}\" ?${NN}" fi [[ ! "${BOTTOKEN}" =~ :[a-zA-Z0-9_-]{35}$ ]] &&\ - printf "${ORANGE}Possible problem in the characters part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NN}" + printf "${GREY}\tHint: Hash token: invalid character or not${NC} 35 ${GREY}characters long, len is ${NC}$(($(wc -c <<<"${BOTTOKEN#*:}")-1))\n" + printf "\n" fi - ################## # here we start with the real stuff BASHBOT_RETRY="" # retry by default From c0bce919375bcf7298bcca959754e0b677ead12f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 12:38:52 +0100 Subject: [PATCH 12/51] fix NN --- bashbot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 5f88a0a..525970d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.21-dev-6-gcae6288 +#### $$VERSION$$ v1.21-dev-7-gb123c3d # # Exit Codes: # - 0 success (hopefully) @@ -42,7 +42,7 @@ if [ -t 1 ] && [ -n "$TERM" ]; then ORANGE='\e[35m' GREY='\e[1;30m' NC='\e[0m' - NN="${NN}" + NN="${NC}\n" fi # telegram uses utf-8 characters, check if we have an utf-8 charset From 90c5d87761f59f63b38e714a118c88c0a9b7fddc Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 12:51:19 +0100 Subject: [PATCH 13/51] better messages when entering bot token and bot admin --- bashbot.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 525970d..d3e678c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.21-dev-7-gb123c3d +#### $$VERSION$$ v1.21-dev-8-gc0bce91 # # Exit Codes: # - 0 success (hopefully) @@ -195,9 +195,10 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then printf "Running headless, set BOTTOKEN or run ${SCRIPT} init first!\n" exit 2 else - printf "${RED}TOKEN MISSING.${NN}" + printf "${RED}ENTER BOT TOKEN...${NN}" printf "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NN}" read -r token + printf "\n" fi [ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTCONFIG}.jssh" @@ -209,10 +210,10 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then elif [ -z "${INTERACTIVE}" ]; then printf "Running headless, set botadmin to AUTO MODE!\n" else - printf "${RED}BOTADMIN MISSING.${NN}" - printf "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NN}" - printf "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NN}" + printf "${RED}ENTER BOT ADMIN...${NN}" + printf "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR PRESS ENTER\nTO MAKE FIRST USER TYPING '/start' BOT ADMIN${NN}" read -r admin + printf "\n" fi [ -z "${admin}" ] && admin='?' printf '["botadmin"]\t"%s"\n' "${admin}" >> "${BOTCONFIG}.jssh" @@ -285,7 +286,7 @@ if ! check_token "${BOTTOKEN}"; then [ -n "$(getConfigKey "botid")" ] && printf "\t${GREEN}Did you mean: \"${NC}$(getConfigKey "botid")${GREEN}\" ?${NN}" fi [[ ! "${BOTTOKEN}" =~ :[a-zA-Z0-9_-]{35}$ ]] &&\ - printf "${GREY}\tHint: Hash token: invalid character or not${NC} 35 ${GREY}characters long, len is ${NC}$(($(wc -c <<<"${BOTTOKEN#*:}")-1))\n" + printf "${GREY}\tHint: Hash contains invalid character or is not${NC} 35 ${GREY}characters long, hash len is ${NC}$(($(wc -c <<<"${BOTTOKEN#*:}")-1))\n" printf "\n" fi From 9bfc27a678f4d2d093cc817b84c131fcc1be3e50 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 13:12:38 +0100 Subject: [PATCH 14/51] save corrupt bot config on recover --- bashbot.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index d3e678c..a46d4c4 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.21-dev-8-gc0bce91 +#### $$VERSION$$ v1.21-dev-9-g90c5d87 # # Exit Codes: # - 0 success (hopefully) @@ -266,10 +266,11 @@ if [ -z "${BOTTOKEN}" ]; then printf "${ORANGE}${BOTERROR}${NC} " if [ -r "${BOTCONFIG}.jssh.ok" ]; then log_error "${BOTERROR}" + mv "${BOTCONFIG}.jssh" "${BOTCONFIG}.jssh.bad" cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh"; printf "OK\n" BOTTOKEN="$(getConfigKey "bottoken")" else - printf "\n${RED}Error: Missing bot token! remove ${BOTCONFIG}.jssh and run \"bashbot.sh init\" may fix it.${NN}" + printf "\n${RED}Error: Can't recover from missing bot token! Remove ${BOTCONFIG}.jssh and run${NC} bashbot.sh init\n" exit 7 fi fi From e8173281a8551e1a18ccbbadf2cb4532e27b684c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 14:37:07 +0100 Subject: [PATCH 15/51] adjust tests to manually download JSON.sh --- dev/all-tests.sh | 23 ++++++++++++++++++++--- dev/hooks/pre-commit.sh | 4 ++-- test/e-env-test.sh | 4 ++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/dev/all-tests.sh b/dev/all-tests.sh index fbb3f3e..c2530d3 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -1,7 +1,12 @@ #!/usr/bin/env bash -# this has to run once atfer git clone -# and every time we create new hooks -#### $$VERSION$$ v1.20-0-g2ab00a2 +############################################################# +# +# File: dev/all-tests.sh +# +# Description: run all tests, exit after failed test +# +#### $$VERSION$$ v1.21-dev-10-g9bfc27a +############################################################# # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -12,6 +17,7 @@ else echo "Sorry, no git repository $(pwd)" && exit 1 fi +########################## # create test environment TESTENV="/tmp/bashbot.test$$" mkdir "${TESTENV}" @@ -21,6 +27,13 @@ cd "test" || exit 1 # delete possible config rm -f "${TESTENV}/botconfig.jssh" "${TESTENV}/botacl" 2>/dev/null +# inject JSON.sh +mkdir "${TESTENV}/JSON.sh" +curl -sL "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" >"${TESTENV}/JSON.sh/JSON.sh" +chmod +x "${TESTENV}/JSON.sh/JSON.sh" + +######################## +#prepare and run tests #set -e fail=0 tests=0 @@ -45,6 +58,8 @@ do fi done +########################### +# cleanup depending on test state if [ "$fail" -eq 0 ]; then /bin/echo -n 'SUCCESS ' exitcode=0 @@ -56,6 +71,8 @@ else find "${TESTENV}/"* ! -name '[a-z]-*' -delete fi +######################### +# show test result and test logs echo -e "${passed} / ${tests}\\n" [ -d "${TESTENV}" ] && echo "Logfiles from run are in ${TESTENV}" diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index 23255fa..81d2c0d 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-10-g9bfc27a ############ # NOTE: you MUST run install-hooks.sh again when updating this file! @@ -68,7 +68,7 @@ fi if command -v codespell &>/dev/null; then echo "Running codespell" echo "............................" - codespell --skip="*.zip,*gz,*.log,*.html,*.txt,.git*,jsonDB-keyboard" -L "ba" + codespell -q 3 --skip="*.zip,*gz,*.log,*.html,*.txt,.git*,jsonDB-keyboard" -L "ba" echo "if there are (to many) typo's shown, consider running:" echo "codespell -i 3 -w --skip=\"*.log,*.html,*.txt,.git*,examples\" -L \"ba\"" else diff --git a/test/e-env-test.sh b/test/e-env-test.sh index aefadef..351645b 100755 --- a/test/e-env-test.sh +++ b/test/e-env-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-10-g9bfc27a # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -31,7 +31,7 @@ set +f # shellcheck disable=SC2086 cp ${TESTDIR}/*commands.sh "${BASHBOT_ETC}" || exit 1 set -f -cp -r "${TESTDIR}/bashbot.sh" "${TESTDIR}/modules" "${BASHBOT_BIN}" || exit 1 +cp -r "${TESTDIR}/bashbot.sh" "${TESTDIR}/modules" "${TESTDIR}/JSON.sh/JSON.sh" "${BASHBOT_BIN}" || exit 1 TESTFILES="${TOKENFILE} ${ACLFILE}" From 21afd4e62e2bffde892c5e92dc3bd7255e70e901 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 14:53:00 +0100 Subject: [PATCH 16/51] no more autoatic download of JSON.sh --- bashbot.sh | 78 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index a46d4c4..cb1f442 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -1,29 +1,33 @@ #!/bin/bash -# file: bashbot.sh -# do not edit, this file will be overwritten on update - -# bashbot, the Telegram bot written in bash. -# Written by Drew (@topkecleon) KayM (@gnadelwartz). -# Also contributed: Daniil Gentili (@danogentili), JuanPotato, BigNerd95, TiagoDanin, iicc1. -# https://github.com/topkecleon/telegram-bot-bash - -# Depends on JSON.sh (http://github.com/dominictarr/JSON.sh) (MIT/Apache), -# This file is public domain in the USA and all free countries. -# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +################################################################## # -#### $$VERSION$$ v1.21-dev-9-g90c5d87 +# File: bashbot.sh +# Note: DO NOT EDIT! this file will be overwritten on update +# +# Description: +# bashbot, the Telegram bot written in bash. +# Written by Drew (@topkecleon) KayM (@gnadelwartz). +# Also contributed: Daniil Gentili (@danogentili), JuanPotato, +# BigNerd95, TiagoDanin, iicc1. +# https://github.com/topkecleon/telegram-bot-bash +# +# This file is public domain in the USA and all free countries. +# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # Exit Codes: -# - 0 success (hopefully) -# - 1 can't change to dir -# - 2 can't write to tmp, count or token -# - 3 user / command / file not found -# - 4 unknown command -# - 5 cannot connect to telegram bot -# - 6 mandatory module not found -# - 7 can't get bottoken -# - 8 curl/wget missing -# - 10 not bash! +# 0 - success (hopefully) +# 1 - can't change to dir +# 2 - can't write to tmp, count or token +# 3 - user / command / file not found +# 4 - unknown command +# 5 - cannot connect to telegram bot +# 6 - mandatory module not found +# 7 - can't get bottoken +# 8 - curl/wget missing +# 10 - not bash! +# +#### $$VERSION$$ v1.21-dev-11-ge817328 +################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 # emmbeded system may claim bash but it is not @@ -96,10 +100,11 @@ log_error(){ printf "%s: %b\n" "$(date)" "$*" >>"${ERRORLOG}"; } log_debug(){ printf "%s: %b\n" "$(date)" "$*" >>"${DEBUGLOG}"; } log_message(){ printf "\n%s: %b\n" "$(date)" "$*" >>"${MESSAGELOG}"; } log_update(){ printf "%s: %b\n" "$(date)" "$*" >>"${UPDATELOG}"; } + # additional tests if we run in debug mode export BASHBOTDEBUG -# debug should always last argument [[ "${BASH_ARGV[0]}" == *"debug"* ]] && BASHBOTDEBUG="yes" + # $1 where $2 command $3 may debug # shellcheck disable=SC2094 debug_checks(){ { @@ -159,7 +164,7 @@ fi ADDONDIR="${BASHBOT_ETC:-.}/addons" RUNUSER="${USER}" # USER is overwritten by bashbot array :-(, save original -# OK everything setup, lets start +# OK, ENVIRONMENT is set up, let's do some additional tests if [[ -z "${SOURCE}" && -z "$BASHBOT_HOME" ]] && ! cd "${RUNDIR}" ; then printf "${RED}ERROR: Can't change to ${RUNDIR} ...${NN}" exit 1 @@ -172,7 +177,17 @@ if [ ! -w "." ]; then ls -ld . fi -# Setup and check environment if BOTTOKEN is NOT set +# check if JSON.sh is available +JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}" +[[ "${JSONSHFILE}" != *"/JSON.sh" ]] &&\ + printf "${RED}ERROR:${NC} ${JSONSHFILE} ${RED}does not end with${NC} JSONS.sh\n" &&\ + exit 3 +[ ! -x "${JSONSHFILE}" ] &&\ + printf "${RED}ERROR:${NC} ${JSONSHFILE} ${RED}seems not to exist, are we in dev environment?${NN}${GREY}%s${NN}"\ + "JSONSHFILE environment variable points to wrong file or bashbot is not installed correct, see doc/2_install.md\n" &&\ + exit 3 + +# file locations based on ENVIRONMENT BOTCONFIG="${BASHBOT_ETC:-.}/botconfig" TOKENFILE="${BASHBOT_ETC:-.}/token" BOTADMIN="${BASHBOT_ETC:-.}/botadmin" @@ -183,6 +198,7 @@ COUNTFILE="${BASHBOT_VAR:-.}/count" LOGDIR="${RUNDIR:-.}/logs" +# CREATE botconfig if not exist # assume everything already set up correctly if TOKEN is set if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then # BOTCONFIG does not exist, create @@ -1181,18 +1197,6 @@ if ! _is_function send_message ; then exit 1 fi -# get location of JSON.sh, download if not exist -JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}" -[[ "${JSONSHFILE}" != *"/JSON.sh" ]] && printf "${RED}ERROR: \"${JSONSHFILE}\" ends not with \"JSONS.sh\".${NN}" && exit 3 - -if [ ! -f "${JSONSHFILE}" ]; then - printf "Seems to be first run, Downloading ${JSONSHFILE}...\n" - [ "${SCRIPTDIR}/JSON.sh/JSON.sh" = "${JSONSHFILE}" ] &&\ - mkdir "${SCRIPTDIR}/JSON.sh" 2>/dev/null && chmod +w "${SCRIPTDIR}/JSON.sh" - getJson "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" >"${JSONSHFILE}" - chmod +x "${JSONSHFILE}" -fi - # check if JSON.awk exist and has x flag JSONAWKFILE="${JSONSHFILE%.sh}.awk" if [ -x "${JSONAWKFILE}" ] && _exists awk ; then From 0d7fee3bd2e2a070d6c4ad421290bf26c0b35d85 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 14:58:34 +0100 Subject: [PATCH 17/51] no more convert old config --- bashbot.sh | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index cb1f442..4596ea7 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-11-ge817328 +#### $$VERSION$$ v1.21-dev-12-g21afd4e ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -203,11 +203,8 @@ LOGDIR="${RUNDIR:-.}/logs" if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then # BOTCONFIG does not exist, create printf '["bot_config_key"]\t"config_key_value"\n' >>"${BOTCONFIG}.jssh" - # convert old token - if [ -r "${TOKENFILE}" ]; then - token="$(< "${TOKENFILE}")" - # no old token, ask user - elif [ -z "${INTERACTIVE}" ] && [ "$1" != "init" ]; then + # ask user for bot token + if [ -z "${INTERACTIVE}" ] && [ "$1" != "init" ]; then printf "Running headless, set BOTTOKEN or run ${SCRIPT} init first!\n" exit 2 else @@ -220,10 +217,8 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then # no botadmin, setup botadmin if [ -z "$(getConfigKey "botadmin")" ]; then - # convert old admin - if [ -r "${BOTADMIN}" ]; then - admin="$(< "${BOTADMIN}")" - elif [ -z "${INTERACTIVE}" ]; then + # ask user for bot admin + if [ -z "${INTERACTIVE}" ]; then printf "Running headless, set botadmin to AUTO MODE!\n" else printf "${RED}ENTER BOT ADMIN...${NN}" @@ -250,10 +245,6 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then # setup count file if [ ! -f "${COUNTFILE}.jssh" ]; then printf '["counted_user_chat_id"]\t"num_messages_seen"\n' >> "${COUNTFILE}.jssh" - # convert old file on creation - if [ -r "${COUNTFILE}" ];then - sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh" - fi elif [ ! -w "${COUNTFILE}.jssh" ]; then printf "${RED}ERROR: Can't write to ${COUNTFILE}!.${NN}" ls -l "${COUNTFILE}.jssh" From 3f8841b8f187c9795c1b63d3b18a2dbcddcffa8f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 15:12:35 +0100 Subject: [PATCH 18/51] currently no update actions on init --- bashbot.sh | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 4596ea7..0b6f52d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-12-g21afd4e +#### $$VERSION$$ v1.21-dev-13-g0d7fee3 ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -1111,13 +1111,10 @@ bot_init() { [ -n "${BASHBOT_HOME}" ] && cd "${BASHBOT_HOME}" || exit 1 local DEBUG="$1" # upgrade from old version + # currently no action printf "Check for Update actions ...\n" - local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash" - [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${DATADIR}"; rmdir "${OLDTMP}"; } - # no more existing modules - [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" - # load addons on startup printf "Done.\n" + # load addons on startup printf "Initialize modules and addons ...\n" for addons in "${ADDONDIR:-.}"/*.sh ; do # shellcheck source=./modules/aliases.sh @@ -1142,19 +1139,15 @@ bot_init() { printf "${RED}User \"$TOUSER\" not found!${NN}" exit 3 else - # shellcheck disable=SC2009 - oldbot="$(ps -fu "$TOUSER" | grep startbot | grep -v -e 'grep' -e '\-startbot' )" - [ -n "${oldbot}" ] && \ - printf "${ORANGE}Warning: At least one not upgraded TMUX bot is running! You must stop it with kill command:${NN}${oldbot}\n" printf "Adjusting files and permissions for user \"${TOUSER}\" ...\n" [ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc" - chown -R "$TOUSER" . ./* chmod 711 . chmod -R o-w ./* chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTADMIN}" logs "${LOGDIR}/"*.log 2>/dev/null - chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null + chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null # jsshDB must writeable by owner find . -name '*.jssh*' -exec chmod u+w \{\} + + chown -R "$TOUSER" . ./* printf "Done.\n" fi # ask to check bottoken online @@ -1166,10 +1159,10 @@ bot_init() { $0 botname fi fi - # check if botconf if seems valid + # check if botconf seems valid printf "${GREEN}This is your bot config:${NN}" sed 's/^/\t/' "${BOTCONFIG}.jssh" | grep -vF '["bot_config_key"]' - if [[ "$(getConfigKey "bottoken")" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ && "$(getConfigKey "botadmin")" =~ ^[0-9]+$ ]]; then + if check_token "$(getConfigKey "bottoken")" && [[ "$(getConfigKey "botadmin")" =~ ^[0-9]+$ ]]; then printf "Bot config seems to be valid. Should I make a backup copy? (Y/n) Y\b" read -r ANSWER if [[ -z "${ANSWER}" || "${ANSWER}" =~ ^[^Nn] ]]; then @@ -1177,7 +1170,7 @@ bot_init() { cp "${BOTCONFIG}.jssh" "${BOTCONFIG}.jssh.ok" fi else - printf "${ORANGE}Bot config may not complete, pls check.${NN}" + printf "${ORANGE}Bot config may incomplete, pls check.${NN}" fi # show result ls -ld "${DATADIR}" "${LOGDIR}" ./*.jssh* ./*.sh 2>/dev/null From a1f7215aa476984bbc2d5668deb23f5d3d0afd76 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 16:22:38 +0100 Subject: [PATCH 19/51] update bashbot in a nutshell --- README.html | 111 ++++++++++++++++++++++++++++++---------------------- README.md | 46 +++++++++++++--------- README.txt | 68 +++++++++++++++++--------------- 3 files changed, 127 insertions(+), 98 deletions(-) diff --git a/README.html b/README.html index ad13d51..0b64fb6 100644 --- a/README.html +++ b/README.html @@ -287,15 +287,32 @@ Written by Drew (@topkecleon) and Kay M (@gnadelwartz).
  • Examples Dir
  • Your really first bashbot in a nutshell

    -

    To install and run bashbot you need access to a linux/unix command line. If you don't know how to get access to a linux/unix command line you should stop reading here :-(

    -

    In addition you need a Telegram client and a mobile phone to register an account. If you don't want to register for Telegram you should stop reading here ;-)

    -

    After you're registered to Telegram send a message to @botfather, create a new Telegram Bot token and write it down. You need the token to install the bot.

    -

    Now open a terminal and check if bash is installed:

    -
    which bash && echo "bash seems available..."
    -

    Create a new directory, change to it: mkdir tbb; cd tbb and download the latest '*.tar.gz' file from https://github.com/topkecleon/telegram-bot-bash/releases. This can be done with the commands:

    -
    wget -q https://github.com/$(wget -q https://github.com/topkecleon/telegram-bot-bash/releases/latest -O - | egrep '/.*/.*/.*tar.gz' -o)
    -

    Extract the '*.tar.gz' file and change to bashbot directory: tar -xzf *.tar.gz; cd telegram-bot-bash, install bashbot: ./bashbot.sh init and enter your bot token when asked. All other questions can be answered by hitting the <Return> key.

    -

    That's all, now you can start your bot with ./bashbot.sh start and send him messages:

    +

    To install and run bashbot you need access to a linux/unix command line with bash, a Telegram client and a mobile phone with a Telegram account.

    +

    First you need to create a new Telegram Bot token for your bot and write it down.

    +

    Now open a linux/unix terminal with bash, create a new directory, change to it and install telegram-bot-bash:

    +
    # create bot dir
    +mkdir mybot
    +cd mybot
    +
    +# download latest release with wget or from https://github.com/topkecleon/telegram-bot-bash/releases/latest
    +wget "https://github.com/$(wget -q "https://github.com/topkecleon/telegram-bot-bash/releases/latest" -O - | egrep '/.*/download/.*/.*tar.gz' -o)"
    +
    +# Extract the tar archive
    +tar -xzf *.tar.gz
    +cd telegram-bot-bash
    +
    +# initialize your bot
    +# Enter your bot token when asked, all other questions can be answered by hitting the \<Return\> key.
    +./bashbot.sh init
    +
    +# Now start your bot
    +./bashbot.sh start
    +
    +Bottoken is valid ...
    +Bot Name: yourbotname_bot
    +Session Name: yourbotname_bot-startbot
    +Bot started successfully.
    +

    Now open the Telegram App on your mobile phone and start a chatting with your bot (your bot's username is shown after 'Bot Name:'):

    /start
     
     You are Botadmin
    @@ -328,19 +345,19 @@ It features background tasks and interactive chats, and can serve as an interfac
     

    A powerful tool to improve your scripts is shellcheck. You can use it online or install shellcheck locally. Shellcheck is used extensively in bashbot development to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a test suite to check if important functionality is working as expected.

    Use printf whenever possible

    If you're writing a script and it is taking external input (from the user as arguments or file system...), you shouldn't use echo to display it. Use printf whenever possible

    -
      # very simple
    -  echo "text with variables. PWD=$PWD"
    -  printf '%s\n' "text with variables. PWD=$PWD"
    -  printf 'text with variables. PWD=%s\n' "$PWD"
    -  -> text with variables. PWD=/home/xxx
    -
    -  # more advanced
    -  FLOAT="1.2346777892864" INTEGER="12345.123"
    -  echo "float=$FLOAT, integer=$INTEGER, PWD=$PWD"
    -  -> float=1.2346777892864, integer=12345.123, PWD=/home/xxx
    -
    -  printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "$FLOAT" "$INTEGER" "$PWD"
    -  -> float=1.23, integer=12345, PWD=/home/xxx
    +
      # very simple
    +  echo "text with variables. PWD=$PWD"
    +  printf '%s\n' "text with variables. PWD=$PWD"
    +  printf 'text with variables. PWD=%s\n' "$PWD"
    +  -> text with variables. PWD=/home/xxx
    +
    +  # more advanced
    +  FLOAT="1.2346777892864" INTEGER="12345.123"
    +  echo "float=$FLOAT, integer=$INTEGER, PWD=$PWD"
    +  -> float=1.2346777892864, integer=12345.123, PWD=/home/xxx
    +
    +  printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "$FLOAT" "$INTEGER" "$PWD"
    +  -> float=1.23, integer=12345, PWD=/home/xxx

    Do not use #!/usr/bin/env bash

    We stay with /bin/bash shebang, because it's more save from security perspective.

    Use of a fixed path to the system provided bash makes it harder for attackers or users to place alternative versions of bash and avoids using a possibly broken, mangled or compromised bash executable.

    @@ -370,40 +387,40 @@ It features background tasks and interactive chats, and can serve as an interfac

    Can I send messages from CLI and scripts?

    Of course, you can send messages from CLI and scripts, simply install bashbot as described here, send the message '/start' to set yourself as botadmin and stop the bot with ./bashbot.sh stop.

    Run the following commands in your bash shell or script while you are in the installation directory:

    -
    # prepare bash / script to send commands
    -export BASHBOT_HOME="$(pwd)"
    -source ./bashbot.sh source
    -
    -# send me a test message
    -send_message "$(getConfigKey "botadmin")" "test"
    -
    -# send me output of a system command
    -send_message "$(getConfigKey "botadmin")" "$(df -h)"
    +
    # prepare bash / script to send commands
    +export BASHBOT_HOME="$(pwd)"
    +source ./bashbot.sh source
    +
    +# send me a test message
    +send_message "$(getConfigKey "botadmin")" "test"
    +
    +# send me output of a system command
    +send_message "$(getConfigKey "botadmin")" "$(df -h)"

    For more information see Expert Use

    Blocked by telegram?

    This may happen if to many or wrong requests are sent to api.telegram.org, e.g. using a invalid token or not existing API calls. If the block stay for longer time you can ask telegram service to unblock your IP-Adress.

    You can check with curl or wget if you are blocked by Telegram:

    -
    curl -m 10  https://api.telegram.org/bot
    -#curl: (28) Connection timed out after 10001 milliseconds
    -
    -wget -t 1 -T 10 https://api.telegram.org/bot
    -#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.
    -
    -nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram"
    -#your IP seems blocked by telegram
    +
    curl -m 10  https://api.telegram.org/bot
    +#curl: (28) Connection timed out after 10001 milliseconds
    +
    +wget -t 1 -T 10 https://api.telegram.org/bot
    +#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.
    +
    +nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram"
    +#your IP seems blocked by telegram

    Since Version 0.96 bashbot offers the option to recover from broken connections (aka blocked). Therefore you can provide a function named bashbotBlockRecover() in mycommands.sh. If the function exists it is called every time when a broken connection is detected.

    Possible actions are: Check if network is working, change IP-Adress or simply wait some time.

    If everything seems OK return 0 for retry or any non 0 value to give up.

    -
    # called when bashbot sedn command failed because we can not connect to telegram
    -# return 0 to retry, return non 0 to give up
    -bashbotBlockRecover() {
    -    # place your commands to unblock here, e.g. change IP-Adess or simply wait
    -    sleep 60 && return 0 # may be temporary
    -    return 1 
    -    }
    +
    # called when bashbot sedn command failed because we can not connect to telegram
    +# return 0 to retry, return non 0 to give up
    +bashbotBlockRecover() {
    +    # place your commands to unblock here, e.g. change IP-Adess or simply wait
    +    sleep 60 && return 0 # may be temporary
    +    return 1 
    +    }

    @Gnadelwartz

    That's it all guys!

    If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

    -

    $$VERSION$$ v1.20-0-g2ab00a2

    +

    $$VERSION$$ v1.21-dev-14-g3f8841b

    diff --git a/README.md b/README.md index 2adfa68..10eef2f 100644 --- a/README.md +++ b/README.md @@ -67,31 +67,39 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do ### Your really first bashbot in a nutshell -To install and run bashbot you need access to a linux/unix command line. If you don't know how to get access to a linux/unix command line you should stop reading here :-( +To install and run bashbot you need access to a linux/unix command line with bash, a [Telegram client](https://telegram.org) and a mobile phone [with a Telegram account](https://telegramguide.com/create-a-telegram-account/). -In addition you need a [Telegram client](https://telegram.org) and a mobile phone to [register an account](https://telegramguide.com/create-a-telegram-account/). -If you don't want to register for Telegram you should stop reading here ;-) +First you need to [create a new Telegram Bot token](doc/1_firstbot.md) for your bot and write it down. -After you're registered to Telegram send a message to [@botfather](https://telegram.me/botfather), -[create a new Telegram Bot token](doc/1_firstbot.md) and write it down. You need the token to install the bot. +Now open a linux/unix terminal with bash, create a new directory, change to it and install telegram-bot-bash: -Now open a terminal and check if bash is installed: - ``` -which bash && echo "bash seems available..." -``` - - -Create a new directory, change to it: ```mkdir tbb; cd tbb``` and download the latest '*.tar.gz' file from -[https://github.com/topkecleon/telegram-bot-bash/releases](https://github.com/topkecleon/telegram-bot-bash/releases). This can be done with the commands: ```bash -wget -q https://github.com/$(wget -q https://github.com/topkecleon/telegram-bot-bash/releases/latest -O - | egrep '/.*/.*/.*tar.gz' -o) +# create bot dir +mkdir mybot +cd mybot + +# download latest release with wget or from https://github.com/topkecleon/telegram-bot-bash/releases/latest +wget "https://github.com/$(wget -q "https://github.com/topkecleon/telegram-bot-bash/releases/latest" -O - | egrep '/.*/download/.*/.*tar.gz' -o)" + +# Extract the tar archive +tar -xzf *.tar.gz +cd telegram-bot-bash + +# initialize your bot +# Enter your bot token when asked, all other questions can be answered by hitting the \ key. +./bashbot.sh init + +# Now start your bot +./bashbot.sh start + +Bottoken is valid ... +Bot Name: yourbotname_bot +Session Name: yourbotname_bot-startbot +Bot started successfully. ``` -Extract the '*.tar.gz' file and change to bashbot directory: ```tar -xzf *.tar.gz; cd telegram-bot-bash```, -install bashbot: ```./bashbot.sh init``` and enter your bot token when asked. All other questions can be answered -by hitting the \ key. +Now open the Telegram App on your mobile phone and start a chatting with your bot (_your bot's username is shown after 'Bot Name:'_): -That's all, now you can start your bot with ```./bashbot.sh start``` and send him messages: ``` /start @@ -269,4 +277,4 @@ bashbotBlockRecover() { If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-14-g3f8841b diff --git a/README.txt b/README.txt index b0c3382..2acd5d3 100644 --- a/README.txt +++ b/README.txt @@ -81,44 +81,48 @@ available on www.github.com ### Your really first bashbot in a nutshell -To install and run bashbot you need access to a linux/unix command line. If you -don't know how to get access to a linux/unix command line you should stop -reading here :-( +To install and run bashbot you need access to a linux/unix command line with +bash, a [Telegram client](https://telegram.org) and a mobile phone [with a +Telegram account](https://telegramguide.com/create-a-telegram-account/). -In addition you need a [Telegram client](https://telegram.org) and a mobile -phone to [register an -account](https://telegramguide.com/create-a-telegram-account/). -If you don't want to register for Telegram you should stop reading here ;-) +First you need to [create a new Telegram Bot token](doc/1_firstbot.md) for your +bot and write it down. -After you're registered to Telegram send a message to -[@botfather](https://telegram.me/botfather), -[create a new Telegram Bot token](doc/1_firstbot.md) and write it down. You -need the token to install the bot. +Now open a linux/unix terminal with bash, create a new directory, change to it +and install telegram-bot-bash: -Now open a terminal and check if bash is installed: - ``` -which bash && echo "bash seems available..." -``` - - -Create a new directory, change to it: ```mkdir tbb; cd tbb``` and download the -latest '*.tar.gz' file from -[https://github.com/topkecleon/telegram-bot-bash/releases](https://github.com/to -pkecleon/telegram-bot-bash/releases). This can be done with the commands: ```bash -wget -q https://github.com/$(wget -q -https://github.com/topkecleon/telegram-bot-bash/releases/latest -O - | egrep -'/.*/.*/.*tar.gz' -o) +# create bot dir +mkdir mybot +cd mybot + +# download latest release with wget or from +https://github.com/topkecleon/telegram-bot-bash/releases/latest +wget "https://github.com/$(wget -q +"https://github.com/topkecleon/telegram-bot-bash/releases/latest" -O - | egrep +'/.*/download/.*/.*tar.gz' -o)" + +# Extract the tar archive +tar -xzf *.tar.gz +cd telegram-bot-bash + +# initialize your bot +# Enter your bot token when asked, all other questions can be answered by +hitting the \ key. +./bashbot.sh init + +# Now start your bot +./bashbot.sh start + +Bottoken is valid ... +Bot Name: yourbotname_bot +Session Name: yourbotname_bot-startbot +Bot started successfully. ``` -Extract the '*.tar.gz' file and change to bashbot directory: ```tar -xzf -*.tar.gz; cd telegram-bot-bash```, -install bashbot: ```./bashbot.sh init``` and enter your bot token when asked. -All other questions can be answered -by hitting the \ key. +Now open the Telegram App on your mobile phone and start a chatting with your +bot (_your bot's username is shown after 'Bot Name:'_): -That's all, now you can start your bot with ```./bashbot.sh start``` and send -him messages: ``` /start @@ -359,4 +363,4 @@ wait If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-14-g3f8841b From eedd0b3ab3365f6dbf396153f43658c8a3d0515f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 17:13:37 +0100 Subject: [PATCH 20/51] update installation instructions --- README.html | 4 +- README.md | 4 +- README.txt | 4 +- bashbot.sh | 4 +- doc/0_install.md | 133 +++++++++++++---------------------------------- 5 files changed, 45 insertions(+), 104 deletions(-) diff --git a/README.html b/README.html index 0b64fb6..6bfb605 100644 --- a/README.html +++ b/README.html @@ -297,7 +297,7 @@ Written by Drew (@topkecleon) and Kay M (@gnadelwartz). # download latest release with wget or from https://github.com/topkecleon/telegram-bot-bash/releases/latest wget "https://github.com/$(wget -q "https://github.com/topkecleon/telegram-bot-bash/releases/latest" -O - | egrep '/.*/download/.*/.*tar.gz' -o)" -# Extract the tar archive +# Extract the tar archive and go into bot dir tar -xzf *.tar.gz cd telegram-bot-bash @@ -421,6 +421,6 @@ It features background tasks and interactive chats, and can serve as an interfac

    @Gnadelwartz

    That's it all guys!

    If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

    -

    $$VERSION$$ v1.21-dev-14-g3f8841b

    +

    $$VERSION$$ v1.21-dev-15-ga1f7215

    diff --git a/README.md b/README.md index 10eef2f..2b41864 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ cd mybot # download latest release with wget or from https://github.com/topkecleon/telegram-bot-bash/releases/latest wget "https://github.com/$(wget -q "https://github.com/topkecleon/telegram-bot-bash/releases/latest" -O - | egrep '/.*/download/.*/.*tar.gz' -o)" -# Extract the tar archive +# Extract the tar archive and go into bot dir tar -xzf *.tar.gz cd telegram-bot-bash @@ -277,4 +277,4 @@ bashbotBlockRecover() { If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.21-dev-14-g3f8841b +#### $$VERSION$$ v1.21-dev-15-ga1f7215 diff --git a/README.txt b/README.txt index 2acd5d3..c3f5ab3 100644 --- a/README.txt +++ b/README.txt @@ -102,7 +102,7 @@ wget "https://github.com/$(wget -q "https://github.com/topkecleon/telegram-bot-bash/releases/latest" -O - | egrep '/.*/download/.*/.*tar.gz' -o)" -# Extract the tar archive +# Extract the tar archive and go into bot dir tar -xzf *.tar.gz cd telegram-bot-bash @@ -363,4 +363,4 @@ wait If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.21-dev-14-g3f8841b +#### $$VERSION$$ v1.21-dev-15-ga1f7215 diff --git a/bashbot.sh b/bashbot.sh index 0b6f52d..698408b 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-13-g0d7fee3 +#### $$VERSION$$ v1.21-dev-15-ga1f7215 ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -184,7 +184,7 @@ JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}" exit 3 [ ! -x "${JSONSHFILE}" ] &&\ printf "${RED}ERROR:${NC} ${JSONSHFILE} ${RED}seems not to exist, are we in dev environment?${NN}${GREY}%s${NN}"\ - "JSONSHFILE environment variable points to wrong file or bashbot is not installed correct, see doc/2_install.md\n" &&\ + "JSONSHFILE environment variable points to wrong file or bashbot is not installed correct, see doc/0_install.md\n" &&\ exit 3 # file locations based on ENVIRONMENT diff --git a/doc/0_install.md b/doc/0_install.md index 0ef765c..260b033 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -3,6 +3,8 @@ ## Check bash installation There may systems where bash seems to be installed but it is not, e.g. embedded systems, or where bash is to old. +Bashbot has some builtin checks but it may better to check before installing bashbot. + Run the following commands to see if your bash looks ok ... ```bash @@ -13,7 +15,10 @@ if which bash; then echo "bash seems available..."; else echo "NO bash"; fi bash -c 'if eval "a[1]=1"; then echo "Shell support arrays..."; else echo "Shell has NO arrays"; fi' # check for bash version by feature -bash -c 'if [ "$(echo -e "\u1111")" != "\u1111" ]; then echo "Bash version ok ..."; else echo "LBash Version may be to old ..."; fi' +bash -c 'if [ "$(echo -e "\u1111")" != "\u1111" ]; then echo "Bash version ok ..."; else echo "Bash version may to old ..."; fi' + +# display bash version, must be greater thane 4.3 +bash --version | grep "bash" ``` ## Install bashbot @@ -21,12 +26,12 @@ bash -c 'if [ "$(echo -e "\u1111")" != "\u1111" ]; then echo "Bash version ok .. 1. Go to the directory you want to install bashbot, e.g. * your $HOME directory (install and run with your user-ID) * /usr/local if you want to run as service -2. [Download latest release zip / tar archive from github](https://github.com/topkecleon/telegram-bot-bash/releases) and extract all files. -3. Change into the directory ```telegram-bot-bash``` -4. Activate the bot example commands ```cp mycommands.sh.dist mycommands.sh``` -5. Run ```./bashbot.sh init``` to setup the environment and enter your Bots token given by botfather. +2. [Download latest release zip / tar archive from github](https://github.com/topkecleon/telegram-bot-bash/releases/latest) and extract all files. +3. Change into the directory `telegram-bot-bash` +4. Copy `mycommands.sh.dist` or `mycommands.sh.clean` to `mycommands.sh` +5. Run `./bashbot.sh init` to setup the environment and enter your Bots token given by botfather. -Edit ```mycommands.sh``` to your needs. +Edit `mycommands.sh` to fit your needs. Now your Bot is ready to start ... **If you are new to Bot development read [Bots: An introduction for developers](https://core.telegram.org/bots)** @@ -34,25 +39,21 @@ Now your Bot is ready to start ... ### Update bashbot -**Note: all files including ```mycommands.sh``` may overwritten, make a backup!** +**Important: all files including `mycommands.sh` may overwritten, make a backup!** -1. Go to the directory where you had installed bashbot, e.g. +1. Go to the directory where you have installed bashbot, e.g. * your $HOME directory * /usr/local -2. [Download latest release zip / tar archive from github](https://github.com/topkecleon/telegram-bot-bash/releases) -3. Stop all running instances of bashbot +2. [Download latest release zip / tar archive from github](https://github.com/topkecleon/telegram-bot-bash/releases/latest) +3. Stop all running instances of bashbot `./bashbot.sh stop` 4. Extract all files to your existing bashbot dir -5. Run ```sudo ./bashbot.sh init``` to setup your environment after the update +5. Run `sudo ./bashbot.sh init` to setup your environment after the update -If you modified ```commands.sh``` move your changes to ```mycommands.sh```, this avoids overwriting your commands on update. +If you modified `commands.sh` move your changes to `mycommands.sh`, this avoids overwriting your commands on every update. Now you can restart your bashbot instances. -### Get zip / tar archive on command line - -```bash -wget -q https://github.com/$(wget -q https://github.com/topkecleon/telegram-bot-bash/releases/latest -O - | egrep '/.*/.*/.*tar.gz' -o) -``` +*Note*: If you are updating from a pre-1.0 version, update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bash/releases/tags/v1.20) first! ### Use JSON.awk (beta) @@ -71,30 +72,25 @@ Note: If you are not using the zip / tar archive, you must install `JSON.awk` ma bash patch-for-busybox-awk.sh -### Install bashbot from github +### Install bashbot from git repo -The first install can also be done from github, updates should always done from the zip or tar archive to avoid -overwriting your config and ```mycommands.sh``` files. +Installation and Updates should be done using the zip / tar archives provided on github to avoid +problems and overwriting your bot config and `mycommands.sh`. -1. Go to the directory you want to install bashbot, e.g. - * your $HOME directory (install and run with your user-ID) - * /usr/local if you want to run as service -2. Run ```git clone https://github.com/topkecleon/telegram-bot-bash.git``` -3. Change into the directory ```telegram-bot-bash``` -4. Run ``` dev/all-tests.sh``` and if everything finish OK ... -5. Run ``` rm addons/*``` to deactivate the example add on's -5. Run ```sudo ./bashbot.sh init``` to setup the environment and enter your Bots token given by botfather. +Nevertheless you can install or update bashbot from a git repo, see next chapter ... -### Get the latest development updates from github +### Create Installation / Update archives yourself -To update an existing installation to the latest development version from github you must create the update archives yourself. +To install or update bashbot from a git repo you must create the archives yourself. -1. Run ```git clone https://github.com/topkecleon/telegram-bot-bash.git``` -2. Change into the directory ```telegram-bot-bash``` -3. Run ``` git checkout develop``` -4. Run ``` dev/make-distribution.sh``` -5. If no errors occur you will find archive files in DISTRIBUTION +1. Run `git clone https://github.com/topkecleon/telegram-bot-bash.git` +2. Change into the directory `telegram-bot-bash` +3. Run ` git checkout develop` (_otional, for latest dev version_) +4. Run ` dev/make-distribution.sh` +5. If no errors occur you'll find archives and a bashbot installation in directory DIST + +*Note*: You can update the basbot installation in `DIST/telegram-bot-bash` with `git pull; dev/make-distrubition.sh` to get the latest updates. ### Note for BSD and MacOS @@ -113,77 +109,22 @@ Bashbot will stay with /bin/bash shebang, as using a fixed path is more secure t I considered to make bashbot BSD sed compatible, but much of the bashbot "magic" relies on (gnu) sed features, e.g. alternation ```|```, non printables ```\n\t\<``` or repeat ```?+``` pattern, not supported by BSD sed. + BSD/MacOS sed compatibility will result in a rewrite of all grep/sed commands with an uncertain outcome, see [BSD/MacOS vs. GNU sed](https://riptutorial.com/sed/topic/9436/bsd-macos-sed-vs--gnu-sed-vs--the-posix-sed-specification) to get an impression how different they are. -If you can convert the following examples to work correct with gnu and BSD sed, contact me. -```bash - # easy start - sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p' - OUT="$(sed -e ':a;N;$!ba;s/\r\n/ mynewlinestartshere /g' <<<"$1"| iconv -f utf-8 -t utf-8 -c)" +### Notes on Changes - # more complex - address="$(sed <<< "${2}" '/myaddressstartshere /!d;s/.*myaddressstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" +#### Support for update from pre-1.0 removed - # for experts? - source <( printf "$1"'=( %s )' "$(sed -E -n -e ':x /"text"\]/ { N; s/([^"])\n/\1\\n/g ; tx }' -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" ) -``` - -### Notes per Version - -#### Change in storing config values - -Up to version 0.94 bashbot stores config values as values in ```token```, ```botadmin``` and ```count```. Since version 0.96 bashbot -uses jsonDB key/value store. Config is stored in ```botconfig.jssh```, counting of users is done in ```count.jssh```. -The acl file ```botacl``` stay as is. On first run of bashbot.sh after an update bashbot converts -the files to the new config format. Afterwards the files ```token```, ```botadmin``` and ```count``` can be deleted. - -You may notice the new file ```blocked.jssh```, every telegram user or chat id stored here will be blocked from -using your bot. - -#### removal of TMUX -From version 0.80 on TMUX is no longer needed and the bashbot command 'attach' is deleted. Old function 'inproc' -is replaced by 'send_interactive'. send_interactive does checks if an interactive job is running internally. -Pls check if you make use of inproc and remove it including the old checks, e.g. -```bash -if tmux ls | grep -v send | grep -q "$copname"; then inproc; fi -# or -[ checkprog ] && inproc -``` -must be replaced by ```send_interactive "${CHATD[ID]}" "${MESSAGE}"``` - -### Do not edit commands.sh -From version 0.60 on your commands must be placed in ```mycommands.sh```. If you update from a version with your commands -in 'commands.sh' move all your commands and functions to ```mycommands.sh```. - -From version 0.80 on 'commands.sh' will be overwritten on update! - -#### Location of var / tmp / data dirs -From version 0.70 on the tmp dir is renamed to 'data-bot-bash' to reflect the fact that not only temporary files are stored. an existing 'tmp-bot-bash' will be automatically renamed after update. - -From version 0.50 on the temporary files are no more placed in '/tmp'. instead a dedicated tmp dir is used. - -#### Changes to send_keyboard in v0.6 -From Version 0.60 on keyboard format for ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."``` was changed. -Keyboards are now defined in JSON Array notation e.g. "[ \\"yes\\" , \\"no\\" ]". -This has the advantage that you can create any type of keyboard supported by Telegram. -The old format is supported for backward compatibility, but may fail for corner cases. - -*Example Keyboards*: - -- yes no in two rows: - - OLD format: 'yes' 'no' *(two strings)* - - NEW format: '[ "yes" ] , [ "no" ]' *(two arrays with a string)* -- new layouts made easy with NEW format: - - Yes No in one row: '[ "yes" , "no" ]' - - Yes No plus Maybe in 2.row: '[ "yes" , "no" ] , [ "maybe" ]' - - numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' +From Version 1.21 on updating from a pre-1.0 version is no more supported! +You must update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bash/releases/tags/v1.20) first! #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-15-ga1f7215 From 8c9298d54881079330bccba8d6b189c0c346b08a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 17:27:04 +0100 Subject: [PATCH 21/51] skip tests with --notest --- dev/make-distribution.sh | 16 +++++++++++----- doc/0_install.md | 15 ++++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index fa30c2e..ed097a9 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -1,8 +1,14 @@ #!/usr/bin/env bash -# file: make-distribution.sh -# creates files and arcchives to dirtribute bashbot +############################################################## # -#### $$VERSION$$ v1.20-0-g2ab00a2 +# File: make-distribution.sh +# +# Description: creates files and arcchives to distribute bashbot +# +# Options: --notest - skip tests +# +#### $$VERSION$$ v1.21-dev-16-geedd0b3 +############################################################## # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -21,9 +27,9 @@ DISTDIR="./DIST/${DISTNAME}" DISTFILES="bashbot.rc bashbot.sh commands.sh mycommands.sh mycommands.sh.clean bin doc examples scripts modules addons LICENSE README.md README.txt README.html" # run tests first! - -for test in dev/all-test*.sh +for test in $1 dev/all-test*.sh do + [[ "${test}" == "--notest"* ]] && break [ ! -x "${test}" ] && continue if ! "${test}" ; then echo "Test ${test} failed, can't create dist!" diff --git a/doc/0_install.md b/doc/0_install.md index 260b033..13fdb7d 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -17,7 +17,7 @@ bash -c 'if eval "a[1]=1"; then echo "Shell support arrays..."; else echo "Shell # check for bash version by feature bash -c 'if [ "$(echo -e "\u1111")" != "\u1111" ]; then echo "Bash version ok ..."; else echo "Bash version may to old ..."; fi' -# display bash version, must be greater thane 4.3 +# display bash version, must be greater than 4.3 bash --version | grep "bash" ``` @@ -75,22 +75,23 @@ Note: If you are not using the zip / tar archive, you must install `JSON.awk` ma ### Install bashbot from git repo Installation and Updates should be done using the zip / tar archives provided on github to avoid -problems and overwriting your bot config and `mycommands.sh`. +problems and not overwriting your bot config and `mycommands.sh`. Nevertheless you can install or update bashbot from a git repo, see next chapter ... ### Create Installation / Update archives yourself -To install or update bashbot from a git repo you must create the archives yourself. +To install or update bashbot from a git repo you must create the archives yourself. 1. Run `git clone https://github.com/topkecleon/telegram-bot-bash.git` 2. Change into the directory `telegram-bot-bash` 3. Run ` git checkout develop` (_otional, for latest dev version_) -4. Run ` dev/make-distribution.sh` -5. If no errors occur you'll find archives and a bashbot installation in directory DIST +4. Run ` dev/make-distribution.sh` (_add option --notest to skip tests_) +5. You'll find archives and a bashbot installation in directory DIST +6. install or update abshbot using one of the archives (optional) -*Note*: You can update the basbot installation in `DIST/telegram-bot-bash` with `git pull; dev/make-distrubition.sh` to get the latest updates. +*Note*: You can update the basbot installation in `DIST/telegram-bot-bash` with `git pull; dev/make-distrubition.sh --notest` to get the latest updates. ### Note for BSD and MacOS @@ -126,5 +127,5 @@ You must update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bas #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v1.21-dev-15-ga1f7215 +#### $$VERSION$$ v1.21-dev-16-geedd0b3 From 249dbb04716cc77726727b9a3d0ef531412cce1b Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 21:17:39 +0100 Subject: [PATCH 22/51] some minor text changes --- bashbot.sh | 9 ++++----- doc/6_reference.md | 4 ++-- doc/7_develop.md | 7 +++++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 698408b..654d720 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-15-ga1f7215 +#### $$VERSION$$ v1.21-dev-17-g8c9298d ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -222,16 +222,15 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then printf "Running headless, set botadmin to AUTO MODE!\n" else printf "${RED}ENTER BOT ADMIN...${NN}" - printf "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR PRESS ENTER\nTO MAKE FIRST USER TYPING '/start' BOT ADMIN${NN}" + printf "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR PRESS ENTER\nTO MAKE FIRST USER TYPING '/start' BOT ADMIN${NN}?\b" read -r admin - printf "\n" fi [ -z "${admin}" ] && admin='?' printf '["botadmin"]\t"%s"\n' "${admin}" >> "${BOTCONFIG}.jssh" fi # setup botacl file if [ ! -f "${BOTACL}" ]; then - printf "${ORANGE}Create empty ${BOTACL} file.${NC}" + printf "${GREY}Create initial ${BOTACL} file.${NN}" printf '\n' >"${BOTACL}" fi # setup data dir file @@ -285,7 +284,7 @@ fi # BOTTOKEN format checks if ! check_token "${BOTTOKEN}"; then - printf "${ORANGE}Warning: Your bot token is incorrect, it should have the following format:${NC}\n" + printf "\n${ORANGE}Warning: Your bot token is incorrect, it should have the following format:${NC}\n" printf "${RED}:${NC}<35_alphanumeric_characters-hash> ${RED}e.g. =>${NC} 123456789${RED}:${NC}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4\n\n" printf "${GREY}Your bot token: '${NC}${BOTTOKEN//:/${RED}:${NC}}'\n" diff --git a/doc/6_reference.md b/doc/6_reference.md index ff8b502..7d0020a 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -114,7 +114,7 @@ The main use case for send_message is to process the output of interactive chats ##### send_file -send_file allows can send different type's of files, e.g. photos, stickers, audio, media, etc. +send_file can send different type's of files, e.g. photos, stickers, audio, media, etc. [see Telegram API documentation](https://core.telegram.org/bots/api#sending-files). It's recommended to use __absolute path names__ (_starting with `/`_), as relative path names are threated as __relative to UPLOADDIR__ `data-bot-bash/upload`! @@ -1155,5 +1155,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v1.21-dev-3-g79402ec +#### $$VERSION$$ v1.21-dev-17-g8c9298d diff --git a/doc/7_develop.md b/doc/7_develop.md index 9352b47..036760b 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -197,7 +197,10 @@ Now have a look at the directory 'standalone', here you find the files 'bashbot. 5. give your (dev) fork a new version tag: `git tag v1.xx` 6. setup github hooks by running `dev/install-hooks.sh` -Note for Debian: Debian Buster has an old shellcheck version, pls update from [buster-backports](https://backports.debian.org/Instructions/) +Note for Debian: Debian Buster ships older versions of many utilities, pls try to install from [buster-backports](https://backports.debian.org/Instructions/) +```bash +sudo apt-get -t buster-backports install git shellcheck pandoc codespell curl +``` #### Test, Add, Push changes A typical bashbot develop loop looks as follow: @@ -351,5 +354,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v1.20-3-g232a16b +#### $$VERSION$$ v1.21-dev-17-g8c9298d From b5e4f53357db10e63116f8b182653f7211cd8b7e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 27 Dec 2020 21:19:32 +0100 Subject: [PATCH 23/51] factor out inject-json.sh --- dev/inject-json.sh | 34 ++++++++++++++++++++++++++++++++ dev/make-distribution.sh | 21 ++++---------------- dev/make-distribution.sh.exclude | 9 ++++++++- dev/make-standalone.sh | 19 +++++++++++++----- 4 files changed, 60 insertions(+), 23 deletions(-) create mode 100644 dev/inject-json.sh diff --git a/dev/inject-json.sh b/dev/inject-json.sh new file mode 100644 index 0000000..f02a58b --- /dev/null +++ b/dev/inject-json.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +############################################################## +# +# File: inject-json.sh +# +# Description: download and prepare JSON.sh and JSON.awk +# +# Usage: source inject-json.sh +# +#### $$VERSION$$ v1.21-dev-17-g8c9298d +############################################################## + +# download JSON.sh +JSONSHFILE="JSON.sh/JSON.sh" +if [ ! -r "${JSONSHFILE}" ]; then + printf "Inject JSON.sh ... " + mkdir "JSON.sh" 2>/dev/null + curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" + chmod +x "${JSONSHFILE}" + printf "Done!\n" +fi + +# download JSON.awk +JSONSHFILE="JSON.sh/JSON.awk" +if [ ! -r "${JSONSHFILE}" ]; then + printf "Inject JSON.awk ... " + curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/JSON.awk" + curl -sL -o "${JSONSHFILE%/*}/awk-patch.sh" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/tool/patch-for-busybox-awk.sh" + printf "Done!\n" + bash "${JSONSHFILE%/*}/awk-patch.sh" "${JSONSHFILE%/*}/JSON.awk" +fi +# delete backup files +rm -f "${JSONSHFILE%/*}"/*.bak + diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index ed097a9..1e381e8 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -7,7 +7,7 @@ # # Options: --notest - skip tests # -#### $$VERSION$$ v1.21-dev-16-geedd0b3 +#### $$VERSION$$ v1.21-dev-17-g8c9298d ############################################################## # magic to ensure that we're always inside the root of our application, @@ -53,22 +53,9 @@ do mv "${file}" "${file}.dist" done -# dwonload JSON.sh -echo "Inject JSON.sh" -JSONSHFILE="JSON.sh/JSON.sh" -if [ ! -r "${JSONSHFILE}" ]; then - mkdir "JSON.sh" 2>/dev/null - curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" - chmod +x "${JSONSHFILE}" -fi -echo "Inject JSON.awk" -JSONSHFILE="JSON.sh/JSON.awk" -if [ ! -r "${JSONSHFILE}" ]; then - curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/JSON.awk" - curl -sL -o "${JSONSHFILE%/*}/awk-patch.sh" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/tool/patch-for-busybox-awk.sh" - bash "${JSONSHFILE%/*}/awk-patch.sh" "${JSONSHFILE%/*}/JSON.awk" -fi -rm -f "${JSONSHFILE%/*}"/*.bak +# inject JSON.sh into distribution +# shellcheck disable=SC1090 +source "$GIT_DIR/../dev/inject-json.sh" # make html doc echo "Create html doc" diff --git a/dev/make-distribution.sh.exclude b/dev/make-distribution.sh.exclude index 0d04a17..2a35aa6 100644 --- a/dev/make-distribution.sh.exclude +++ b/dev/make-distribution.sh.exclude @@ -1,7 +1,14 @@ data-bot-bash/* -*.jssh +*.jssh* botacl *.flock *.log *.last *.ok +*.bad +*.bak +*.jpg +*.jpeg +*.png +*.zip +*.gz diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 7e532f1..666928c 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -1,11 +1,16 @@ #!/usr/bin/env bash -# file: make-standalone.sh -# even after make-distribution.sh bashbot is not self contained as it was in the past. +################################################################### # -# If you your bot is finished you can use make-standalone.sh to create the -# the old all-in-one bashbot: bashbot.sh and commands.sh only! +# File: make-standalone.sh # -#### $$VERSION$$ v1.20-0-g2ab00a2 +# Description: +# even after make-distribution.sh bashbot is not self contained as it was in the past. +# +# If you your bot is finished you can use make-standalone.sh to create the +# the old all-in-one bashbot: bashbot.sh and commands.sh only! +# +#### $$VERSION$$ v1.21-dev-17-g8c9298d +################################################################### # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -30,6 +35,10 @@ mkdir -p "${DISTDIR}" 2>/dev/null cp -r ${DISTFILES} "${DISTDIR}" 2>/dev/null cd "${DISTDIR}" || exit 1 +# inject JSON.sh into distribution +# shellcheck disable=SC1090 +source "$GIT_DIR/../dev/inject-json.sh" + ####################### # here the magic starts # create all in one bashbot.sh file From a4dce7bb5a1e348647d0dbb402267ee74891a275 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 28 Dec 2020 12:29:38 +0100 Subject: [PATCH 24/51] myfilelocationstartshere -> myfilestartshere --- doc/3_advanced.md | 6 +++--- modules/sendMessage.sh | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/3_advanced.md b/doc/3_advanced.md index ef1e8db..49a3ff8 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -114,7 +114,7 @@ echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No ``` Same goes for files: ```bash -echo "Text that will appear in chat? myfilelocationstartshere /home/user/dog.jpg" +echo "Text that will appear in chat? myfilestartshere /home/user/dog.jpg" ``` *Note*: Use an _absolute path name_ (starting with `/`), a relative path name is relative to `data-bot-bash/upload`! See [send_file documentation](6_reference.md#send_file) for more information. @@ -133,7 +133,7 @@ echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45 myti ``` You can combine them: ```bash -echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No, highly unlikely\" ] myfilelocationstartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45" +echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No, highly unlikely\" ] myfilestartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45" ``` Please note that you can either send a location or a venue, not both. To send a venue add the mytitlestartshere and the myaddressstartshere keywords. @@ -302,5 +302,5 @@ Note: If you disable automatic retry, se above, you disable also connection prob #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v1.21-dev-2-gde31231 +#### $$VERSION$$ v1.21-dev-19-gb5e4f53 diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 9bd8dec..c1ab3f6 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117 -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-19-gb5e4f53 # will be automatically sourced from bashbot @@ -286,7 +286,7 @@ send_message() { keyboard="$(sed <<< "${2}" '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere *//;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" btext="$(sed <<< "${2}" '/mybtextstartshere /!d;s/.*mybtextstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" burl="$(sed <<< "${2}" '/myburlstartshere /!d;s/.*myburlstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//g;s/ *mykeyboardendshere.*//g')" - file="$(sed <<< "${2}" '/myfilelocationstartshere /!d;s/.*myfilelocationstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" + file="$(sed <<< "${2}" '/myfile[^s]*startshere /!d;s/.*myfile[^s]*startshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" lat="$(sed <<< "${2}" '/mylatstartshere /!d;s/.*mylatstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" long="$(sed <<< "${2}" '/mylongstartshere /!d;s/.*mylongstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" title="$(sed <<< "${2}" '/mytitlestartshere /!d;s/.*mytitlestartshere //;s/ *my[kfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" From 8c67bb76c482f5a05cba684f8b531baf1567a838 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 28 Dec 2020 13:09:28 +0100 Subject: [PATCH 25/51] provide send_file error --- modules/sendMessage.sh | 44 ++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index c1ab3f6..f46875e 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117 -#### $$VERSION$$ v1.21-dev-19-gb5e4f53 +#### $$VERSION$$ v1.21-dev-20-ga4dce7b # will be automatically sourced from bashbot @@ -155,14 +155,6 @@ send_button() { } -UPLOADDIR="${BASHBOT_UPLOAD:-${DATADIR}/upload}" - -# for now this can only send local files with curl! -# extend to allow send files by URL or telegram ID -send_file() { - upload_file "${@}" -} - if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then # there are no checks if URL or ID exists # $1 chat $3 ... $n URL or ID @@ -189,17 +181,41 @@ else } fi +UPLOADDIR="${BASHBOT_UPLOAD:-${DATADIR}/upload}" + +# for now this can only send local files with curl! +# extend to allow send files by URL or telegram ID +send_file() { + local err + upload_file "${@}"; err="$?" + # fake Telegram response to provide error + if [ "${err}" != "0" ]; then + BOTSENT=() + BOTSENT[OK]="false" + case "$err" in + 1) BOTSENT[ERROR]="Path to file $2 contains to much '../' or starts with '.'";; + 2) BOTSENT[ERROR]="Path to file $2 does not match regex: ${FILE_REGEX} ";; + 3) if [[ "$2" == "/"* ]];then + BOTSENT[ERROR]="File not found: $2" + else + BOTSENT[ERROR]="File not found: ${UPLOADDIR}/$2" + fi;; + esac + [ -n "${BASHBOTDEBUG}" ] && log_message "Error in upload_file: ${BOTSENT[ERROR]}" + fi +} + upload_file(){ local CUR_URL WHAT STATUS text=$3 file="$2" # file access checks ... - [[ "$file" = *'..'* ]] && return # no directory traversal - [[ "$file" = '.'* ]] && return # no hidden or relative files + [[ "$file" = *'..'* ]] && return 1 # no directory traversal + [[ "$file" = '.'* ]] && return 1 # no hidden or relative files if [[ "$file" = '/'* ]] ; then - [[ ! "$file" =~ $FILE_REGEX ]] && return # absolute must match REGEX + [[ ! "$file" =~ ${FILE_REGEX} ]] && return 2 # absolute must match REGEX else file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # othiers must be in UPLOADDIR fi - [ ! -r "$file" ] && return # and file must exits of course + [ ! -r "$file" ] && return 3 # and file must exits of course local ext="${file##*.}" case $ext in @@ -308,7 +324,7 @@ send_message() { sent=y fi if [ -n "$file" ]; then - upload_file "$1" "$file" "$text" + send_file "$1" "$file" "$text" sent=y fi if [ -n "$lat" ] && [ -n "$long" ]; then From a3efcd2e7fc882551d3abee08704fe80ddc4d9c0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 28 Dec 2020 14:21:29 +0100 Subject: [PATCH 26/51] fix injecting JSON.awk to work on updates --- dev/inject-json.sh | 7 ++++--- doc/0_install.md | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/dev/inject-json.sh b/dev/inject-json.sh index f02a58b..9071d04 100644 --- a/dev/inject-json.sh +++ b/dev/inject-json.sh @@ -7,7 +7,7 @@ # # Usage: source inject-json.sh # -#### $$VERSION$$ v1.21-dev-17-g8c9298d +#### $$VERSION$$ v1.21-dev-21-g8c67bb7 ############################################################## # download JSON.sh @@ -21,13 +21,14 @@ if [ ! -r "${JSONSHFILE}" ]; then fi # download JSON.awk -JSONSHFILE="JSON.sh/JSON.awk" +JSONSHFILE="JSON.sh/JSON.awk.dist" if [ ! -r "${JSONSHFILE}" ]; then printf "Inject JSON.awk ... " curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/JSON.awk" curl -sL -o "${JSONSHFILE%/*}/awk-patch.sh" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/tool/patch-for-busybox-awk.sh" + chmod +x "${JSONSHFILE}" printf "Done!\n" - bash "${JSONSHFILE%/*}/awk-patch.sh" "${JSONSHFILE%/*}/JSON.awk" + bash "${JSONSHFILE%/*}/awk-patch.sh" "${JSONSHFILE}" fi # delete backup files rm -f "${JSONSHFILE%/*}"/*.bak diff --git a/doc/0_install.md b/doc/0_install.md index 13fdb7d..f097e7a 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -63,7 +63,7 @@ Most systems with awk can use `JSON.awk` as drop in replacement BSD and MacOS users must install `gnu awk` and adjust the shebang, see below -After you have checked that 'JSON.awk' is working correct on your system make `JSON.awk` executable and (re)start bashbot. +After you have checked that 'JSON.awk.dist' is working correct on your system copy it to `JSON.awk` and (re)start bashbot. Note: If you are not using the zip / tar archive, you must install `JSON.awk` manually into the same directory as 'JSON.sh`: @@ -127,5 +127,5 @@ You must update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bas #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v1.21-dev-16-geedd0b3 +#### $$VERSION$$ v1.21-dev-21-g8c67bb7 From 1a0b51d2aba6a109e42789a7259011b254909887 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 28 Dec 2020 15:03:53 +0100 Subject: [PATCH 27/51] adjust send_meassge test --- dev/all-tests.sh | 2 +- dev/inject-json.sh | 2 +- doc/0_install.md | 2 +- test/d-send_message-test.sh | 8 ++++---- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/dev/all-tests.sh b/dev/all-tests.sh index c2530d3..996bc6a 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -5,7 +5,7 @@ # # Description: run all tests, exit after failed test # -#### $$VERSION$$ v1.21-dev-10-g9bfc27a +#### $$VERSION$$ v1.21-dev-22-ga3efcd2 ############################################################# # magic to ensure that we're always inside the root of our application, diff --git a/dev/inject-json.sh b/dev/inject-json.sh index 9071d04..7893c85 100644 --- a/dev/inject-json.sh +++ b/dev/inject-json.sh @@ -7,7 +7,7 @@ # # Usage: source inject-json.sh # -#### $$VERSION$$ v1.21-dev-21-g8c67bb7 +#### $$VERSION$$ v1.21-dev-22-ga3efcd2 ############################################################## # download JSON.sh diff --git a/doc/0_install.md b/doc/0_install.md index f097e7a..f7397c1 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -127,5 +127,5 @@ You must update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bas #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v1.21-dev-21-g8c67bb7 +#### $$VERSION$$ v1.21-dev-22-ga3efcd2 diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index 4bd6427..152023e 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-22-ga3efcd2 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -45,10 +45,10 @@ touch "$ALLOW/this_is_my.gif" "$ALLOW/this_is_my.doc" touch "$DATADIR/this_is_my.gif" "$DATADIR/this_is_my.doc" while read -r line ; do - echo -n "." - set -x + set -x; set +e send_message "123456" "$line" >>"${OUTPUTFILE}" - set +x + set +x; set -e + echo -n "." done < "${INPUTFILE}" 2>>"${LOGFILE}" [ -d "$ALLOW" ] && rm -rf "$ALLOW" From 2ee8c9456a302a255ebcba55c1a12668b2be1ed6 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 28 Dec 2020 15:11:28 +0100 Subject: [PATCH 28/51] MISSING mycommands --- commands.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands.sh b/commands.sh index bb7fc5f..4b3356c 100644 --- a/commands.sh +++ b/commands.sh @@ -15,7 +15,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-23-g1a0b51d # # adjust your language setting here, e.g. when run from other user or cron. @@ -114,7 +114,7 @@ if [ -z "${1}" ] || [[ "${1}" == *"debug"* ]];then ;; '/start'*) send_action "${CHAT[ID]}" "typing" - MYCOMMANDS="*Note*: No _mycommands.sh_ detected, copy _mycommands.dist_ or _mycommands.clean_." + MYCOMMANDS="*Note*: MISSING mycommands.sh: copy _mycommands.dist_ or _mycommands.clean_." [ -r "${BASHBOT_ETC:-.}/mycommands.sh" ] && MYCOMMANDS="Place your commands and messages in _mycommands.sh_" user_is_botadmin "${USER[ID]}" &&\ send_markdownv2_message "${CHAT[ID]}" "You are *BOTADMIN*.\n${MYCOMMANDS}" From d496639cbb62bcf6e4cb086c3ec033eadd9e5ec9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 28 Dec 2020 16:42:27 +0100 Subject: [PATCH 29/51] fix gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1e2b176..aee3e35 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,7 @@ *.log *.swp *.swo -/log/* +/logs/ /JSON.sh/ /data-bot-bash/ /DIST/ From be87ba3e2312da8eb002aea91ef12bb89458c700 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 28 Dec 2020 22:35:24 +0100 Subject: [PATCH 30/51] fix init test if BOTTOKEN is set --- bashbot.sh | 2 +- dev/all-tests.sh | 2 +- test/c-init-test.sh | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 654d720..8ea8eb7 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-17-g8c9298d +#### $$VERSION$$ v1.21-dev-25-gd496639 ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 diff --git a/dev/all-tests.sh b/dev/all-tests.sh index 996bc6a..cb0e50e 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -5,7 +5,7 @@ # # Description: run all tests, exit after failed test # -#### $$VERSION$$ v1.21-dev-22-ga3efcd2 +#### $$VERSION$$ v1.21-dev-25-gd496639 ############################################################# # magic to ensure that we're always inside the root of our application, diff --git a/test/c-init-test.sh b/test/c-init-test.sh index 30f1905..6caf8e7 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-25-gd496639 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -10,6 +10,7 @@ TESTFILES="${TOKENFILE} ${ACLFILE} ${COUNTFILE} ${BLOCKEDFILE} ${ADMINFILE}" set -e # run bashbot first time with init +export BOTTOKEN="" "${TESTDIR}/bashbot.sh" init >"${LOGFILE}" < Date: Mon, 28 Dec 2020 22:38:30 +0100 Subject: [PATCH 31/51] fix all test if BOTTOKEN is set --- test/ALL-tests.inc.sh | 5 +++-- test/c-init-test.sh | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index d232243..cc540c0 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-26-gbe87ba3 # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME @@ -34,7 +34,8 @@ export INPUTFILE REFFILE INPUTFILE2 REFFILE2 OUTPUTFILE REFFILE2="${DIRME}/${REFDIR}/${REFDIR}2.result" # do not query telegram when testing -export BASHBOT_URL TESTTOKEN +export BASHBOT_URL TESTTOKEN BOTTOKEN +BOTTOKEN="" BASHBOT_URL="https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?" TESTTOKEN="123456789:BASHBOTTESTSCRIPTbashbottestscript_" diff --git a/test/c-init-test.sh b/test/c-init-test.sh index 6caf8e7..bd8290d 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v1.21-dev-25-gd496639 +#### $$VERSION$$ v1.21-dev-26-gbe87ba3 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -10,7 +10,6 @@ TESTFILES="${TOKENFILE} ${ACLFILE} ${COUNTFILE} ${BLOCKEDFILE} ${ADMINFILE}" set -e # run bashbot first time with init -export BOTTOKEN="" "${TESTDIR}/bashbot.sh" init >"${LOGFILE}" < Date: Mon, 28 Dec 2020 22:40:23 +0100 Subject: [PATCH 32/51] better log_message string format --- bashbot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 8ea8eb7..d4faf49 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-25-gd496639 +#### $$VERSION$$ v1.21-dev-27-gd0f8716 ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -98,7 +98,7 @@ check_token(){ # log $1 to ERRORLOG with date log_error(){ printf "%s: %b\n" "$(date)" "$*" >>"${ERRORLOG}"; } log_debug(){ printf "%s: %b\n" "$(date)" "$*" >>"${DEBUGLOG}"; } -log_message(){ printf "\n%s: %b\n" "$(date)" "$*" >>"${MESSAGELOG}"; } +log_message(){ printf "\n%s: %s\n" "$(date)" "$*" >>"${MESSAGELOG}"; } log_update(){ printf "%s: %b\n" "$(date)" "$*" >>"${UPDATELOG}"; } # additional tests if we run in debug mode From 13d15f453ccf64041663c65429204e9e87de5a35 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 29 Dec 2020 10:18:41 +0100 Subject: [PATCH 33/51] doc: remove old stuff --- README.html | 67 ++++----------- README.md | 68 +++------------ README.txt | 84 ++++--------------- doc/2_usage.md | 201 +++++++++++++++++++++++---------------------- doc/3_advanced.md | 36 ++++---- doc/4_expert.md | 42 +++++----- doc/5_practice.md | 20 +++-- doc/6_reference.md | 30 +++---- doc/7_develop.md | 75 ++++++++--------- 9 files changed, 252 insertions(+), 371 deletions(-) diff --git a/README.html b/README.html index 6bfb605..35c0535 100644 --- a/README.html +++ b/README.html @@ -327,7 +327,7 @@ This is bashbot, the Telegram bot written entirely in bash. It features background tasks and interactive chats, and can serve as an interface for CLI programs.

    For more Information on how to install, customize and use your new bot, read the Documentation

    Log files

    -

    Since version 0.96 bashbot log commands received/send and connection errors. If you start bashbot in debug mode bash stdout, stderr and all send/received telegram message are logged also.

    +

    Bashbot actions are logged in BASHBOT.log, Telegram send/receive errors are logged to ERROR.log. Start bashbot in debug mode to get all messages send to / received from Telegram and error messages of bash commands also.

    To enable debug mode start bashbot with debug as third argument: bashbot start debug

    ├── logs 
     │   ├── BASHBOT.log      # log what your bot is doing ...
    @@ -340,28 +340,10 @@ It features background tasks and interactive chats, and can serve as an interfac
     

    Running a Telegram Bot means it is connected to the public and you never know what's send to your Bot.

    Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. Bash programmers often struggle with 'quoting hell' and globbing, see Implications of wrong quoting

    Whenever you are processing input from untrusted sources (messages, files, network) you must be as careful as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands.

    -

    Note: Up to version v0.941 (mai/22/2020) telegram-bot-bash had a remote code execution (RCE) bug, please update if you use an older version! see Issue #125

    One of the most powerful features of unix shells is variable and command substitution using ${} and $(), but as they are expanded in double quotes, this can lead to RCE and information disclosing bugs in complex scripts like bashbot. So it's more secure to escape or remove '$' in input from user, files or network.

    A powerful tool to improve your scripts is shellcheck. You can use it online or install shellcheck locally. Shellcheck is used extensively in bashbot development to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a test suite to check if important functionality is working as expected.

    Use printf whenever possible

    If you're writing a script and it is taking external input (from the user as arguments or file system...), you shouldn't use echo to display it. Use printf whenever possible

    -
      # very simple
    -  echo "text with variables. PWD=$PWD"
    -  printf '%s\n' "text with variables. PWD=$PWD"
    -  printf 'text with variables. PWD=%s\n' "$PWD"
    -  -> text with variables. PWD=/home/xxx
    -
    -  # more advanced
    -  FLOAT="1.2346777892864" INTEGER="12345.123"
    -  echo "float=$FLOAT, integer=$INTEGER, PWD=$PWD"
    -  -> float=1.2346777892864, integer=12345.123, PWD=/home/xxx
    -
    -  printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "$FLOAT" "$INTEGER" "$PWD"
    -  -> float=1.23, integer=12345, PWD=/home/xxx
    -

    Do not use #!/usr/bin/env bash

    -

    We stay with /bin/bash shebang, because it's more save from security perspective.

    -

    Use of a fixed path to the system provided bash makes it harder for attackers or users to place alternative versions of bash and avoids using a possibly broken, mangled or compromised bash executable.

    -

    If you are a BSD / MacOS user or must to use an other bash location, see Install Bashbot

    Run your Bot as a restricted user

    I recommend to run your bot as a user, with almost no access rights. All files your Bot have write access to are in danger to be overwritten/deleted if your bot is hacked. For the same reason every file your Bot can read is in danger to be disclosed. Restrict your Bots access rights to the absolute minimum.

    Never run your Bot as root, this is the most dangerous you can do! Usually the user 'nobody' has almost no rights on unix/linux systems. See Expert use on how to run your Bot as an other user.

    @@ -386,41 +368,28 @@ It features background tasks and interactive chats, and can serve as an interfac

    Hey no Problem, if you are finished with your cool bot run dev/make-standalone.sh to create a stripped down Version of your bot containing only 'bashbot.sh' and 'commands.sh'! For more information see Create a stripped down Version of your Bot

    Can I send messages from CLI and scripts?

    Of course, you can send messages from CLI and scripts, simply install bashbot as described here, send the message '/start' to set yourself as botadmin and stop the bot with ./bashbot.sh stop.

    -

    Run the following commands in your bash shell or script while you are in the installation directory:

    -
    # prepare bash / script to send commands
    -export BASHBOT_HOME="$(pwd)"
    -source ./bashbot.sh source
    -
    -# send me a test message
    -send_message "$(getConfigKey "botadmin")" "test"
    -
    -# send me output of a system command
    -send_message "$(getConfigKey "botadmin")" "$(df -h)"
    -

    For more information see Expert Use

    +

    Bashbot provides some ready to use scripts ro send messages from command line in bin/ dir, e.g. send_message.sh.

    +
    bin/send_message.sh BOTADMIN "This is my first message send from CLI"
    +
    +bin/send_message.sh --help
    +

    You can also source bashbot for use in your scripts, for more information see Expert Use

    Blocked by telegram?

    This may happen if to many or wrong requests are sent to api.telegram.org, e.g. using a invalid token or not existing API calls. If the block stay for longer time you can ask telegram service to unblock your IP-Adress.

    You can check with curl or wget if you are blocked by Telegram:

    -
    curl -m 10  https://api.telegram.org/bot
    -#curl: (28) Connection timed out after 10001 milliseconds
    -
    -wget -t 1 -T 10 https://api.telegram.org/bot
    -#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.
    -
    -nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram"
    -#your IP seems blocked by telegram
    -

    Since Version 0.96 bashbot offers the option to recover from broken connections (aka blocked). Therefore you can provide a function named bashbotBlockRecover() in mycommands.sh. If the function exists it is called every time when a broken connection is detected.

    -

    Possible actions are: Check if network is working, change IP-Adress or simply wait some time.

    -

    If everything seems OK return 0 for retry or any non 0 value to give up.

    -
    # called when bashbot sedn command failed because we can not connect to telegram
    -# return 0 to retry, return non 0 to give up
    -bashbotBlockRecover() {
    -    # place your commands to unblock here, e.g. change IP-Adess or simply wait
    -    sleep 60 && return 0 # may be temporary
    -    return 1 
    -    }
    +
    curl -m 10  https://api.telegram.org/bot
    +#curl: (28) Connection timed out after 10001 milliseconds
    +
    +wget -t 1 -T 10 https://api.telegram.org/bot
    +#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.
    +
    +nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram"
    +#your IP seems blocked by telegram
    +

    Bashbot offers the option to recover from broken connections (blocked). Therefore you can provide a function named bashbotBlockRecover() in mycommands.sh, the function is called every time when a broken connection is detected.

    +

    Possible actions are: Check if network is working, change IP-Adress or simply wait some time. See mycommnds.sh.dist for an example.

    +

    @Gnadelwartz

    That's it all guys!

    If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

    -

    $$VERSION$$ v1.21-dev-15-ga1f7215

    +

    $$VERSION$$ v1.21-dev-28-g43f5536

    diff --git a/README.md b/README.md index 2b41864..0d45cb7 100644 --- a/README.md +++ b/README.md @@ -118,8 +118,8 @@ For more Information on how to install, customize and use your new bot, read the ### Log files -Since version 0.96 bashbot log commands received/send and connection errors. If you start bashbot in debug mode -bash stdout, stderr and all send/received telegram message are logged also. +Bashbot actions are logged in BASHBOT.log, Telegram send/receive errors are logged to ERROR.log. +Start bashbot in debug mode to get all messages send to / received from Telegram and error messages of bash commands also. To enable debug mode start bashbot with debug as third argument: `bashbot start debug` @@ -141,9 +141,6 @@ Bash scripts in general are not designed to be bullet proof, so consider this Bo Whenever you are processing input from untrusted sources (messages, files, network) you must be as careful as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands. -**Note:** Up to version v0.941 (mai/22/2020) telegram-bot-bash had a remote code execution (RCE) bug, please update if you use an older version! -see [Issue #125](https://github.com/topkecleon/telegram-bot-bash/issues/125) - One of the most powerful features of unix shells is variable and command substitution using ```${}``` and ```$()```, but as they are expanded in double quotes, this can lead to RCE and information disclosing bugs in complex scripts like bashbot. So it's more secure to escape or remove '$' in input from user, files or network. @@ -156,31 +153,6 @@ In addition bashbot has a [test suite](doc/7_develop.md) to check if important f If you're writing a script and it is taking external input (from the user as arguments or file system...), you shouldn't use echo to display it. [Use printf whenever possible](https://unix.stackexchange.com/a/6581) -```bash - # very simple - echo "text with variables. PWD=$PWD" - printf '%s\n' "text with variables. PWD=$PWD" - printf 'text with variables. PWD=%s\n' "$PWD" - -> text with variables. PWD=/home/xxx - - # more advanced - FLOAT="1.2346777892864" INTEGER="12345.123" - echo "float=$FLOAT, integer=$INTEGER, PWD=$PWD" - -> float=1.2346777892864, integer=12345.123, PWD=/home/xxx - - printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "$FLOAT" "$INTEGER" "$PWD" - -> float=1.23, integer=12345, PWD=/home/xxx -``` - -### Do not use #!/usr/bin/env bash - -**We stay with /bin/bash shebang, because it's more save from security perspective.** - -Use of a fixed path to the system provided bash makes it harder for attackers or users to place alternative versions of bash -and avoids using a possibly broken, mangled or compromised bash executable. - -If you are a BSD / MacOS user or must to use an other bash location, see [Install Bashbot](doc/0_install.md) - ### Run your Bot as a restricted user **I recommend to run your bot as a user, with almost no access rights.** All files your Bot have write access to are in danger to be overwritten/deleted if your bot is hacked. @@ -220,20 +192,15 @@ Hey no Problem, if you are finished with your cool bot run ```dev/make-standalon Of course, you can send messages from CLI and scripts, simply install bashbot as [described here](#Your-really-first-bashbot-in-a-nutshell), send the message '/start' to set yourself as botadmin and stop the bot with ```./bashbot.sh stop```. -Run the following commands in your bash shell or script while you are in the installation directory: +Bashbot provides some ready to use scripts ro send messages from command line in `bin/` dir, e.g. `send_message.sh`. ```bash -# prepare bash / script to send commands -export BASHBOT_HOME="$(pwd)" -source ./bashbot.sh source +bin/send_message.sh BOTADMIN "This is my first message send from CLI" -# send me a test message -send_message "$(getConfigKey "botadmin")" "test" - -# send me output of a system command -send_message "$(getConfigKey "botadmin")" "$(df -h)" +bin/send_message.sh --help ``` -For more information see [Expert Use](doc/8_custom.md) + +You can also source bashbot for use in your scripts, for more information see [Expert Use](doc/8_custom.md) ### Blocked by telegram? @@ -252,24 +219,13 @@ nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram" #your IP seems blocked by telegram ``` -Since Version 0.96 bashbot offers the option to recover from broken connections (aka blocked). Therefore you can provide a function -named `bashbotBlockRecover()` in `mycommands.sh`. If the function exists it is called every time when a broken connection is detected. +Bashbot offers the option to recover from broken connections (blocked). Therefore you can provide a function +named `bashbotBlockRecover()` in `mycommands.sh`, the function is called every time when a broken connection is detected. Possible actions are: Check if network is working, change IP-Adress or simply wait some time. +See `mycommnds.sh.dist` for an example. -If everything seems OK return 0 for retry or any non 0 value to give up. - -```bash -# called when bashbot sedn command failed because we can not connect to telegram -# return 0 to retry, return non 0 to give up -bashbotBlockRecover() { - # place your commands to unblock here, e.g. change IP-Adess or simply wait - sleep 60 && return 0 # may be temporary - return 1 - } - -``` - +--- @Gnadelwartz @@ -277,4 +233,4 @@ bashbotBlockRecover() { If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.21-dev-15-ga1f7215 +#### $$VERSION$$ v1.21-dev-28-g43f5536 diff --git a/README.txt b/README.txt index c3f5ab3..41c52a4 100644 --- a/README.txt +++ b/README.txt @@ -143,9 +143,10 @@ the [Documentation](#Documentation) ### Log files -Since version 0.96 bashbot log commands received/send and connection errors. If -you start bashbot in debug mode -bash stdout, stderr and all send/received telegram message are logged also. +Bashbot actions are logged in BASHBOT.log, Telegram send/receive errors are +logged to ERROR.log. +Start bashbot in debug mode to get all messages send to / received from +Telegram and error messages of bash commands also. To enable debug mode start bashbot with debug as third argument: `bashbot start debug` @@ -178,10 +179,6 @@ globbing (set -f) and quote everything. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands. -**Note:** Up to version v0.941 (mai/22/2020) telegram-bot-bash had a remote -code execution (RCE) bug, please update if you use an older version! -see [Issue #125](https://github.com/topkecleon/telegram-bot-bash/issues/125) - One of the most powerful features of unix shells is variable and command substitution using ```${}``` and ```$()```, but as they are expanded in double quotes, this can lead to RCE and information @@ -204,35 +201,6 @@ arguments or file system...), you shouldn't use echo to display it. [Use printf whenever possible](https://unix.stackexchange.com/a/6581) -```bash - # very simple - echo "text with variables. PWD=$PWD" - printf '%s\n' "text with variables. PWD=$PWD" - printf 'text with variables. PWD=%s\n' "$PWD" - -> text with variables. PWD=/home/xxx - - # more advanced - FLOAT="1.2346777892864" INTEGER="12345.123" - echo "float=$FLOAT, integer=$INTEGER, PWD=$PWD" - -> float=1.2346777892864, integer=12345.123, PWD=/home/xxx - - printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "$FLOAT" -"$INTEGER" "$PWD" - -> float=1.23, integer=12345, PWD=/home/xxx -``` - -### Do not use #!/usr/bin/env bash - -**We stay with /bin/bash shebang, because it's more save from security -perspective.** - -Use of a fixed path to the system provided bash makes it harder for attackers -or users to place alternative versions of bash -and avoids using a possibly broken, mangled or compromised bash executable. - -If you are a BSD / MacOS user or must to use an other bash location, see -[Install Bashbot](doc/0_install.md) - ### Run your Bot as a restricted user **I recommend to run your bot as a user, with almost no access rights.** All files your Bot have write access to are in danger to be overwritten/deleted @@ -297,21 +265,17 @@ as [described here](#Your-really-first-bashbot-in-a-nutshell), send the message '/start' to set yourself as botadmin and stop the bot with ```./bashbot.sh stop```. -Run the following commands in your bash shell or script while you are in the -installation directory: +Bashbot provides some ready to use scripts ro send messages from command line +in `bin/` dir, e.g. `send_message.sh`. ```bash -# prepare bash / script to send commands -export BASHBOT_HOME="$(pwd)" -source ./bashbot.sh source +bin/send_message.sh BOTADMIN "This is my first message send from CLI" -# send me a test message -send_message "$(getConfigKey "botadmin")" "test" - -# send me output of a system command -send_message "$(getConfigKey "botadmin")" "$(df -h)" +bin/send_message.sh --help ``` -For more information see [Expert Use](doc/8_custom.md) + +You can also source bashbot for use in your scripts, for more information see +[Expert Use](doc/8_custom.md) ### Blocked by telegram? @@ -333,28 +297,16 @@ nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram" #your IP seems blocked by telegram ``` -Since Version 0.96 bashbot offers the option to recover from broken connections -(aka blocked). Therefore you can provide a function -named `bashbotBlockRecover()` in `mycommands.sh`. If the function exists it is -called every time when a broken connection is detected. +Bashbot offers the option to recover from broken connections (blocked). +Therefore you can provide a function +named `bashbotBlockRecover()` in `mycommands.sh`, the function is called every +time when a broken connection is detected. Possible actions are: Check if network is working, change IP-Adress or simply wait some time. +See `mycommnds.sh.dist` for an example. -If everything seems OK return 0 for retry or any non 0 value to give up. - -```bash -# called when bashbot sedn command failed because we can not connect to telegram -# return 0 to retry, return non 0 to give up -bashbotBlockRecover() { - # place your commands to unblock here, e.g. change IP-Adess or simply -wait - sleep 60 && return 0 # may be temporary - return 1 - } - -``` - +--- @Gnadelwartz @@ -363,4 +315,4 @@ wait If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.21-dev-15-ga1f7215 +#### $$VERSION$$ v1.21-dev-28-g43f5536 diff --git a/doc/2_usage.md b/doc/2_usage.md index 8b7057e..a8d0490 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -1,14 +1,15 @@ #### [Home](../README.md) ## Getting Started -The Bots standard commands are in the commands dispatcher ```commands.sh```, Do not edit this file! Add your commands and functions to ```mycommands.sh```. In 'mycommands.sh.dist' you find examples how to add own commands and overwrite existing ones. See [Best practices](5_practice.md) for more information. +The Bots default commands are in `commands.sh`. Do not edit this file! Instead copy `mycommands.sh.clean` to `mycommands.sh` and place you commands there. +Have a look at `mycommands.sh.dist` for examples on how to write commands or overwrite existing ones. See [Best practices](5_practice.md) for more information. -Once you're done with editing start the Bot with ```./bashbot.sh start```. To stop the Bot run ```./bashbot.sh stop``` +Once you're done with editing run the Bot with `./bashbot.sh start`. To stop running the Bot use `./bashbot.sh stop` -If something doesn't work as expected, debug with ```./bashbot.sh startbot DEBUG &```, where DEBUG can be 'debug', 'xdebug' or 'xdebugx'. +If something doesn't work as expected, 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 source```. see [Expert Use](8_expert.md#Expert-use) +To use the functions provided in this script in other scripts simply source bashbot: `source bashbot.sh source`. see [Expert Use](8_expert.md#Expert-use) Have FUN! @@ -143,60 +144,60 @@ Evertime a Message is received, you can read incoming data using the following v These Variables are always present in regular messages: -* ```${MESSAGE}```: Current message text -* ```${MESSAGE[ID]}```: ID of current message -* ```$USER```: This array contains the First name, last name, username and user id of the sender of the current message. - * ```${USER[ID]}```: User id - * ```${USER[FIRST_NAME]}```: User's first name - * ```${USER[LAST_NAME]}```: User's last name - * ```${USER[USERNAME]}```: Username -* ```$CHAT```: This array contains the First name, last name, username, title and user id of the chat of the current message. - * ```${CHAT[ID]}```: Chat id - * ```${CHAT[FIRST_NAME]}```: Chat's first name - * ```${CHAT[LAST_NAME]}```: Chat's last name - * ```${CHAT[USERNAME]}```: Username - * ```${CHAT[TITLE]}```: Title - * ```${CHAT[TYPE]}```: Type - * ```${CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]}```: All members are administrators (true if true) +* `${MESSAGE}`: Current message text +* `${MESSAGE[ID]}`: ID of current message +* `$USER`: This array contains the First name, last name, username and user id of the sender of the current message. + * `${USER[ID]}`: User id + * `${USER[FIRST_NAME]}`: User's first name + * `${USER[LAST_NAME]}`: User's last name + * `${USER[USERNAME]}`: Username +* `$CHAT`: This array contains the First name, last name, username, title and user id of the chat of the current message. + * `${CHAT[ID]}`: Chat id + * `${CHAT[FIRST_NAME]}`: Chat's first name + * `${CHAT[LAST_NAME]}`: Chat's last name + * `${CHAT[USERNAME]}`: Username + * `${CHAT[TITLE]}`: Title + * `${CHAT[TYPE]}`: Type + * `${CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]}`: All members are administrators (true if true) The following variables are set if the message contains optional parts: -* ```$REPLYTO```: Original message which was replied to -* ```$REPLYTO```: This array contains the First name, last name, username and user id of the ORIGINAL sender of the message REPLIED to. - * ```${REPLYTO[ID]}```: ID of message which was replied to - * ```${REPLYTO[UID]}```: Original user's id - * ```${REPLYTO[FIRST_NAME]}```: Original user's first name - * ```${REPLYTO[LAST_NAME]}```: Original user's' last name - * ```${REPLYTO[USERNAME]}```: Original user's username -* ```$FORWARD```: This array contains the First name, last name, username and user id of the ORIGINAL sender of the FORWARDED message. - * ```${FORWARD[ID]}```: Same as MESSAGE[ID] if message is forwarded - * ```${FORWARD[UID]}```: Original user's id - * ```${FORWARD[FIRST_NAME]}```: Original user's first name - * ```${FORWARD[LAST_NAME]}```: Original user's' last name - * ```${FORWARD[USERNAME]}```: Original user's username -* ```$CAPTION```: Picture, Audio, Video, File Captions -* ```$URLS```: This array contains documents, audio files, voice recordings and stickers as URL. - * ```${URLS[AUDIO]}```: Audio files - * ```${URLS[VIDEO]}```: Videos - * ```${URLS[PHOTO]}```: Photos (maximum quality) - * ```${URLS[VOICE]}```: Voice recordings - * ```${URLS[STICKER]}```: Stickers - * ```${URLS[DOCUMENT]}```: Any other file -* ```$CONTACT```: This array contains info about contacts sent in a chat. - * ```${CONTACT[ID]}```: User id - * ```${CONTACT[NUMBER]}```: Phone number - * ```${CONTACT[FIRST_NAME]}```: First name - * ```${CONTACT[LAST_NAME]}```: Last name - * ```${CONTACT[VCARD]}```: User's complete Vcard -* ```$LOCATION```: This array contains info about locations sent in a chat. - * ```${LOCATION[LONGITUDE]}```: Longitude - * ```${LOCATION[LATITUDE]}```: Latitude -* ```$VENUE```: This array contains info about venue (a place) sent in a chat. - * ```${VENUE[TITLE]}```: Name of the place - * ```${VENUE[ADDRESS]}```: Address of the place - * ```${VENUE[LONGITUDE]}```: Longitude - * ```${VENUE[LATITUDE]}```: Latitude - * ```${VENUE[FOURSQUARE]}```: Fouresquare ID +* `$REPLYTO`: Original message which was replied to +* `$REPLYTO`: This array contains the First name, last name, username and user id of the ORIGINAL sender of the message REPLIED to. + * `${REPLYTO[ID]}`: ID of message which was replied to + * `${REPLYTO[UID]}`: Original user's id + * `${REPLYTO[FIRST_NAME]}`: Original user's first name + * `${REPLYTO[LAST_NAME]}`: Original user's' last name + * `${REPLYTO[USERNAME]}`: Original user's username +* `$FORWARD`: This array contains the First name, last name, username and user id of the ORIGINAL sender of the FORWARDED message. + * `${FORWARD[ID]}`: Same as MESSAGE[ID] if message is forwarded + * `${FORWARD[UID]}`: Original user's id + * `${FORWARD[FIRST_NAME]}`: Original user's first name + * `${FORWARD[LAST_NAME]}`: Original user's' last name + * `${FORWARD[USERNAME]}`: Original user's username +* `$CAPTION`: Picture, Audio, Video, File Captions +* `$URLS`: This array contains documents, audio files, voice recordings and stickers as URL. + * `${URLS[AUDIO]}`: Audio files + * `${URLS[VIDEO]}`: Videos + * `${URLS[PHOTO]}`: Photos (maximum quality) + * `${URLS[VOICE]}`: Voice recordings + * `${URLS[STICKER]}`: Stickers + * `${URLS[DOCUMENT]}`: Any other file +* `$CONTACT`: This array contains info about contacts sent in a chat. + * `${CONTACT[ID]}`: User id + * `${CONTACT[NUMBER]}`: Phone number + * `${CONTACT[FIRST_NAME]}`: First name + * `${CONTACT[LAST_NAME]}`: Last name + * `${CONTACT[VCARD]}`: User's complete Vcard +* `$LOCATION`: This array contains info about locations sent in a chat. + * `${LOCATION[LONGITUDE]}`: Longitude + * `${LOCATION[LATITUDE]}`: Latitude +* `$VENUE`: This array contains info about venue (a place) sent in a chat. + * `${VENUE[TITLE]}`: Name of the place + * `${VENUE[ADDRESS]}`: Address of the place + * `${VENUE[LONGITUDE]}`: Longitude + * `${VENUE[LATITUDE]}`: Latitude + * `${VENUE[FOURSQUARE]}`: Fouresquare ID ### Service Messages @@ -207,34 +208,34 @@ client, e.g. new users. If a service message is received bashbot sets MESSAGE to the service message type as a command, e.g. if a new user joins a chat MESSAGE is set to "/_new_chat_user". -* ```$SERVICE```: This array contains info about received service messages. - * ```${SERVICE}```: "yes" if service message is received - * ```${SERVICE[NEWMEMBER]}}```: New user's id - * ```${MESSAGE}```: /_new_chat_member ID NAME - * ```${NEWMEMBER[ID]}```: New user's id - * ```${NEWMEMBER[FIRST_NAME]}```: New user's first name - * ```${NEWMEMBER[LAST_NAME]}```: New user's last name - * ```${NEWMEMBER[USERNAME]}```: New user's username - * ```${NEWMEMBER[ISBOT]}```: New user is a bot - * ```${SERVICE[LEFTMEMBER]}```: Id of user left - * ```${MESSAGE}```: /_left_chat_member ID NAME - * ```${LEFTMEMBER[ID]}```: Left user's id - * ```${LEFTMEMBER[FIRST_NAME]}```: Left user's first name - * ```${LEFTMEMBER[LAST_NAME]}```: Left user's last name - * ```${LEFTMEMBER[USERNAME]}```: Left user's username - * ```${LEFTMEMBER[ISBOT]}```: Left user is a bot - * ```${SERVICE[NEWTITLE]}```: Text of new title - * ```${MESSAGE}```: /_new_chat_title SENDER TEXT - * ```${SERVICE[NEWPHOTO]}```: New Chat Picture - * ```${MESSAGE}```: /_new_chat_picture SENDER URL - * ```${SERVICE[PINNED]}```: Pinned MESSAGE ID - * ```${MESSAGE}```: /_new_pinned_message SENDER ID - * ```${PINNED[ID]}```: Id of pinned message - * ```${PINNED[MESSAGE]}```: Message text of pinned message - * ```${SERVICE[MIGRATE]}```: Old and new group id - * ```${MESSAGE}```: /_migrate_group MIGRATE_FROM MIGRATE_TO - * ```${MIGRATE[FROM]}```: Old group id - * ```${MIGRATE[TO]}```: New group id +* `$SERVICE`: This array contains info about received service messages. + * `${SERVICE}`: "yes" if service message is received + * `${SERVICE[NEWMEMBER]}}`: New user's id + * `${MESSAGE}`: /_new_chat_member ID NAME + * `${NEWMEMBER[ID]}`: New user's id + * `${NEWMEMBER[FIRST_NAME]}`: New user's first name + * `${NEWMEMBER[LAST_NAME]}`: New user's last name + * `${NEWMEMBER[USERNAME]}`: New user's username + * `${NEWMEMBER[ISBOT]}`: New user is a bot + * `${SERVICE[LEFTMEMBER]}`: Id of user left + * `${MESSAGE}`: /_left_chat_member ID NAME + * `${LEFTMEMBER[ID]}`: Left user's id + * `${LEFTMEMBER[FIRST_NAME]}`: Left user's first name + * `${LEFTMEMBER[LAST_NAME]}`: Left user's last name + * `${LEFTMEMBER[USERNAME]}`: Left user's username + * `${LEFTMEMBER[ISBOT]}`: Left user is a bot + * `${SERVICE[NEWTITLE]}`: Text of new title + * `${MESSAGE}`: /_new_chat_title SENDER TEXT + * `${SERVICE[NEWPHOTO]}`: New Chat Picture + * `${MESSAGE}`: /_new_chat_picture SENDER URL + * `${SERVICE[PINNED]}`: Pinned MESSAGE ID + * `${MESSAGE}`: /_new_pinned_message SENDER ID + * `${PINNED[ID]}`: Id of pinned message + * `${PINNED[MESSAGE]}`: Message text of pinned message + * `${SERVICE[MIGRATE]}`: Old and new group id + * `${MESSAGE}`: /_migrate_group MIGRATE_FROM MIGRATE_TO + * `${MIGRATE[FROM]}`: Old group id + * `${MIGRATE[TO]}`: New group id @@ -243,12 +244,12 @@ e.g. if a new user joins a chat MESSAGE is set to "/_new_chat_user". Inline query messages are small, non regular messages used for interaction with the user, they contain the following variables only: -* ```${iQUERY}```: Current inline query -* ```$iQUERY```: This array contains the ID, First name, last name, username and user id of the sender of the current inline query. - * ```${iQUERY[ID]}```: Inline query ID - * ```${iQUERY[USER_ID]}```: User's id - * ```${iQUERY[FIRST_NAME]}```: User's first name - * ```${iQUERY[LAST_NAME]}```: User's last name +* `${iQUERY}`: Current inline query +* `$iQUERY`: This array contains the ID, First name, last name, username and user id of the sender of the current inline query. + * `${iQUERY[ID]}`: Inline query ID + * `${iQUERY[USER_ID]}`: User's id + * `${iQUERY[FIRST_NAME]}`: User's first name + * `${iQUERY[LAST_NAME]}`: User's last name ### Send Message Results @@ -256,15 +257,15 @@ they contain the following variables only: BOTSENT is set on every send_xxx action and only valid until next send action. For more on message results see. [Advanced Usage](3_advanced.md) -* ```$BOTSENT```: This array contains the parsed results from the last transmission to telegram. - * ```${BOTSENT[OK]}```: contains the string ```true```: after a successful transmission - * ```${BOTSENT[ID]}```: Message ID of sent message, image, file etc., if OK is true +* `$BOTSENT`: This array contains the parsed results from the last transmission to telegram. + * `${BOTSENT[OK]}`: contains the string `true`: after a successful transmission + * `${BOTSENT[ID]}`: Message ID of sent message, image, file etc., if OK is true ## Usage of bashbot functions #### sending messages -To send messages use the ```send_xxx_message``` functions. +To send messages use the `send_xxx_message`functions. To insert line brakes in a message place `\n` in the text. To send regular text without any markdown use: @@ -280,7 +281,7 @@ To send text with html: send_html_message "${CHAT[ID]}" "lol bold" ``` -To forward messages use the ```forward``` function: +To forward messages use the `forward`function: ```bash forward "${CHAT[ID]}" "from_chat_id" "message_id" ``` @@ -313,24 +314,24 @@ send_message "${CHAT[ID]}" "lol" "safe" #### Send files, locations, keyboards. -To send images, videos, voice files, photos etc. use the ```send_photo``` function (remember to change the safety Regex @ line 14 of command.sh to allow sending files only from certain directories): +To send images, videos, voice files, photos etc. use the `send_photo`function (remember to change the safety Regex @ line 14 of command.sh to allow sending files only from certain directories): ```bash send_file "${CHAT[ID]}" "/home/user/doge.jpg" "Lool" ``` -To send custom keyboards use the ```send_keyboard``` function: +To send custom keyboards use the `send_keyboard`function: ```bash send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" '[ "Yep" , "No" ]' # note the single quotes! send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" "[ \\"Yep\\" , \\"No\\" ]" # within double quotes you must escape the inside double quots ``` -To send locations use the ```send_location``` function: +To send locations use the `send_location`function: ```bash send_location "${CHAT[ID]}" "Latitude" "Longitude" ``` -To send venues use the ```send_venue``` function: +To send venues use the `send_venue`function: ```bash send_venue "${CHAT[ID]}" "Latitude" "Longitude" "Title" "Address" "optional foursquare id" ``` -To send a chat action use the ```send_action``` function. +To send a chat action use the `send_action`function. Allowed values: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for locations. ```bash send_action "${CHAT[ID]}" "action" @@ -340,5 +341,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-28-g43f5536 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 49a3ff8..ab46446 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -2,7 +2,7 @@ ## Advanced Features ### Access control -Bashbot offers functions to check what Telegram capabilities like 'chat admin' or 'chat creator' the given user has: +Bashbot offers functions to check what Telegram capabilities like `chat admin` or `chat creator` the given user has: ```bash # return true if user is admin/owner of the bot @@ -21,7 +21,7 @@ user_is_admin "${CHAT[ID]}" "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" ``` -In addition you can check individual capabilities of users as you must define in the file ```./botacl```: +In addition you can check individual capabilities of users as you must define in the file `./botacl`: ```bash # file: botacl @@ -54,7 +54,7 @@ ALL:*:* ``` -You must use the function ```user_is_allowed``` to check if a user has the capability to do something. Example: Check if user has capability to start bot. +You must use the function `user_is_allowed`to check if a user has the capability to do something. Example: Check if user has capability to start bot. ```bash case "$MESSAGE" in @@ -143,8 +143,8 @@ echo "Text that will appear in one message mynewlinestartshere with this text echo "Other text message\nwith a newline" # \n instead of mynewlinestartshere ``` -New in v0.7: In case you must extend a message already containing a location, a file, a keyboard etc., -with additionial text simply add ``` mytextstartshere additional text``` at the end of the string: +In case you want extend a message already containing a location, a file, a keyboard etc., +with an additionial text simply add ` mytextstartshere additional text`at the end of the string: ```bash out="Text that will appear mylatstartshere 45 mylongstartshere 45" [[ "$out" != *'in chat'* ]] && out="$out mytextstartshere in chat." @@ -183,14 +183,14 @@ If you want to kill all background jobs permanently run: ./bashbot.sh killback ``` -Note: Background jobs run independent from main bot and continue running until your script exits or you stop it. Background jobs will continue running if your Bot is stopped and must be terminated separately e.g. by ```bashbot.sh killback``` +Note: Background jobs run independent from main bot and continue running until your script exits or you stop it. Background jobs will continue running if your Bot is stopped and must be terminated separately e.g. by `bashbot.sh killback` ### Inline queries **Inline queries** allow users to send commands to your bot from every chat without going to a private chat. An inline query is started if the user type the bots name, e.g. @myBot. Everything after @myBot is immediately send to the bot. In order to enable **inline mode**, send `/setinline` command to [@BotFather](https://telegram.me/botfather) and provide the placeholder text that the user will see in the input field after typing your bot’s name. -The following commands allows you to send ansers to *inline queries*. To enable bashbot to process inline queries set ```INLINE="1"``` in 'mycommands.sh'. +The following commands allows you to send ansers to *inline queries*. To enable bashbot to process inline queries set `INLINE="1"`in 'mycommands.sh'. To send messages or links through an *inline query*: ```bash @@ -246,19 +246,19 @@ is received. **Note**: the values of the variables contains always the result of the LAST transmission to telegram, every send action will overwrite them! -* ```$BOTSENT```: This array contains the parsed results from the last transmission to telegram. - * ```${BOTSENT[OK]}```: contains the string ```true```: after a successful transmission - * ```${BOTSENT[ID]}```: Message ID if OK is true - * ```${BOTSENT[ERROR]}```: Error code if an error occurred - * ```${BOTSENT[DESC]}```: Description text for error - * ```${BOTSENT[RETRY]}```: Seconds to wait if telegram requests throtteling. -* ```$res```: temporary variable containing the full transmission result, may be overwritten by any bashbot function. +* `$BOTSENT`: This array contains the parsed results from the last transmission to telegram. + * `${BOTSENT[OK]}`: contains the string `true`: after a successful transmission + * `${BOTSENT[ID]}`: Message ID if OK is true + * `${BOTSENT[ERROR]}`: Error code if an error occurred + * `${BOTSENT[DESC]}`: Description text for error + * `${BOTSENT[RETRY]}`: Seconds to wait if telegram requests throtteling. +* `$res`: temporary variable containing the full transmission result, may be overwritten by any bashbot function. By default you don't have to care about retry, as bashbot resend the message after the requested time automatically. Only if the retry fails also an error is returned. The downside is that send_message functions will wait until resend is done. If you want to disable automatic error processing and handle all errors manually (or don't care) -set ```BASHBOT_RETRY``` to any no zero value. +set `BASHBOT_RETRY`to any no zero value. [Telegram Bot API error codes](https://github.com/TelegramBotAPI/errors) @@ -266,9 +266,9 @@ set ```BASHBOT_RETRY``` to any no zero value. #### Detect bot blocked If the we can't connect to telegram, e.g. blocked from telegram server but also any other reason, -bashbot set ```BOTSENT[ERROR]``` to ```999```. +bashbot set `BOTSENT[ERROR]`to `999`. -To get a notification on every connection problem create a function named ```bashbotBlockRecover``` and handle blocks there. +To get a notification on every connection problem create a function named `bashbotBlockRecover`and handle blocks there. If the function returns true (0 or no value) bashbot will retry once and then return to the calling function. In case you return any non 0 value bashbot will return to the calling function without retry. @@ -302,5 +302,5 @@ Note: If you disable automatic retry, se above, you disable also connection prob #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v1.21-dev-19-gb5e4f53 +#### $$VERSION$$ v1.21-dev-28-g43f5536 diff --git a/doc/4_expert.md b/doc/4_expert.md index 9b66ab0..5e2f0ec 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -5,17 +5,17 @@ UTF-8 is a variable length encoding of Unicode. UTF-8 is recommended as the default encoding in JSON, XML and HTML, also Telegram make use of it. The first 128 characters are regular ASCII, so it's a superset of and compatible with ASCII environments. The next 1,920 characters need -two bytes for encoding and covers almost all ```Latin``` alphabets, also ```Greek```, ```Cyrillic```, -```Hebrew```, ```Arabic``` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details. +two bytes for encoding and covers almost all `Latin` alphabets, also `Greek```, `Cyrillic```, +```Hebrew```, `Arabic` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details. #### Setting up your Environment -In general ```bash``` and ```GNU``` utitities are UTF-8 aware if you to setup your environment +In general `bash` and `GNU` utitities are UTF-8 aware if you to setup your environment and your scripts accordingly: 1. Your Terminal and Editor must support UTF-8: - Set Terminal and Editor locale to UTF-8, eg. in ```Settings/Configuration``` select UTF-8 (Unicode) as Charset. + Set Terminal and Editor locale to UTF-8, eg. in `Settings/Configuration` select UTF-8 (Unicode) as Charset. -2. Set ```Shell``` environment to UTF-8 in your ```.profile``` and your scripts. The usual settings are: +2. Set `Shell` environment to UTF-8 in your `.profile` and your scripts. The usual settings are: ```bash export 'LC_ALL=C.UTF-8' @@ -35,15 +35,15 @@ export 'LANGUAGE=den_US.UTF-8' ``` 3. make sure your bot scripts use the correct settings, eg. include the lines above at the beginning of your scripts -To display all available locales on your system run ```locale -a | more```. [Gentoo Wiki](https://wiki.gentoo.org/wiki/UTF-8) +To display all available locales on your system run `locale -a | more`. [Gentoo Wiki](https://wiki.gentoo.org/wiki/UTF-8) #### Bashbot UTF-8 Support Bashbot handles all messages transparently, regardless of the charset in use. The only exception is when converting from JSON data to strings. -Telegram use JSON to send / receive data. JSON encodes strings as follow: Characters not ASCII *(>127)* are escaped as sequences of ```\uxxxx``` to be regular ASCII. In addition multibyte characters, *e.g. Emoticons or Arabic characters*, are send in double byte UTF-16 notation. -The Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ``` +Telegram use JSON to send / receive data. JSON encodes strings as follow: Characters not ASCII *(>127)* are escaped as sequences of `\uxxxx` to be regular ASCII. In addition multibyte characters, *e.g. Emoticons or Arabic characters*, are send in double byte UTF-16 notation. +The Emoticons ` 😁 😘 ❤️ 😊 👍 ` are encoded as: ` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ` -**This "mixed" JSON encoding needs special handling and can not decoded from** ```echo -e``` or ```printf '%s\\n'``` +**This "mixed" JSON encoding needs special handling and can not decoded from** `echo -e` or `printf '%s\\n'` Bbashbot uses an internal, pure bash implementation which is well tested now, even there may some corner cases*. @@ -56,7 +56,7 @@ Setup the environment for the user you want to run bashbot and enter desired use sudo ./bashbot.sh init ``` -Edit the file ```bashbot.rc``` and change the following lines to fit your configuration: +Edit the file `bashbot.rc` and change the following lines to fit your configuration: ```bash ####################### # Configuration Section @@ -79,7 +79,7 @@ From now on use 'bashbot.rc' to manage your bot: ```bash sudo ./bashbot.rc start ``` -Type ```ps -ef | grep bashbot``` to verify your Bot is running as the desired user. +Type `ps -ef | grep bashbot` to verify your Bot is running as the desired user. If your Bot is started by 'bashbot.rc', you must use 'bashbot.rc' also to manage your Bot! The following commands are available: ```bash @@ -90,15 +90,15 @@ sudo ./bashbot.rc suspendback sudo ./bashbot.rc resumeback sudo ./bashbot.rc killback ``` -To change back the environment to your user-ID run ```sudo ./bashbot.sh init``` again and enter your user name. +To change back the environment to your user-ID run `sudo ./bashbot.sh init` again and enter your user name. -To use bashbot as a system service include a working ```bashbot.rc``` in your init system (systemd, /etc/init.d). +To use bashbot as a system service include a working `bashbot.rc` in your init system (systemd, /etc/init.d). ### Schedule bashbot from Cron -An example crontab is provided in ```examples/bashbot.cron```. +An example crontab is provided in `examples/bashbot.cron`. -- If you are running bashbot with your user-ID, copy the examples lines to your crontab and remove username ```nobody```. -- if you run bashbot as an other user or a system service edit ```examples/bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot``` +- If you are running bashbot with your user-ID, copy the examples lines to your crontab and remove username `nobody`. +- if you run bashbot as an other user or a system service edit `examples/bashbot.cron` to fit your needs and replace username `nobody` with the username you want to run bashbot. copy the modified file to `/etc/cron.d/bashbot` ### Use bashbot from CLI and scripts @@ -169,7 +169,7 @@ echo $COMMANDS $MODULEDIR $BOTACL $TMPDIR $COUNTFILE /usr/local/telegram-bot-bash/botacl /usr/local/telegram-bot-bash/data-bot-bash /usr/local/telegram-bot-bash/count -``` +` After sourcing you can use bashbot functions to send Messages, Locations, Pictures etc. to any Telegram User or Chat you are in. See [Send Messages](2_usage.md#sending-messages). @@ -209,7 +209,7 @@ If you want to have other locations for config, data etc, define and export the **Note: all specified directories and files must exist or running 'bashbot.sh' will fail.** ##### BASHBOT_ETC -Location of the files ```commands.sh```, ```mycommands.sh```, ```botconfig.jssh```, ```botacl``` ... +Location of the files `commands.sh```, `mycommands.sh```, `botconfig.jssh```, `botacl` ... ```bash unset BASHBOT_ETC # keep in telegram-bot-bash (default) export BASHBOT_ETC "" # keep in telegram-bot-bash @@ -223,7 +223,7 @@ Location of the files ```commands.sh```, ```mycommands.sh```, ```botconfig.jssh` e.g. /etc/bashbot ##### BASHBOT_VAR -Location of runtime data ```data-bot-bash```, ```count.jssh``` +Location of runtime data `data-bot-bash```, `count.jssh` ```bash unset BASHBOT_VAR # keep in telegram-bot-bash (default) export BASHBOT_VAR "" # keep in telegram-bot-bash @@ -296,7 +296,7 @@ set BASHBOT_CURL to point to it. ``` ##### BASHBOT_WGET -Bashbot uses ```curl``` to communicate with telegram server. if ```curl``` is not available ```wget``` is used. +Bashbot uses `curl` to communicate with telegram server. if `curl` is not available `wget` is used. If 'BASHBOT_WGET' is set to any value (not undefined or not empty) wget is used even is curl is available. ```bash unset BASHBOT_WGET # use curl (default) @@ -378,5 +378,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Advanced Use](3_advanced.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-28-g43f5536 diff --git a/doc/5_practice.md b/doc/5_practice.md index db7a77d..e96c390 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -10,9 +10,9 @@ In addition you should know about [BotFather, the one bot to rule them all](http If you don't have a github account, it may time to [setup a free account now](https://github.com/pricing) ### Add commands to mycommands.sh only -Do not change ```bashbot.sh``` and ```commands.sh```, instead place your commands in to ```mycommands.sh```. -To start with a clean/minimal bot copy ```mycommands.sh.clean``` to ```mycommands.sh``` and start editing -the message strings and place commands in the```case ... esac``` block of the function mycommands(): +Do not change `bashbot.sh` and `commands.sh```, instead place your commands in to `mycommands.sh```. +To start with a clean/minimal bot copy `mycommands.sh.clean` to `mycommands.sh` and start editing +the message strings and place commands in the```case ... esac` block of the function mycommands(): ```bash # file: mycommands.sh # your additional bashbot commands @@ -39,13 +39,13 @@ mycommands() { ### DIsable, replace and extend global commands -Global bashbot command processing, e.g. /start, /info etc. is disabled if you return a non zero value from ```mycommands.sh```, +Global bashbot command processing, e.g. /start, /info etc. is disabled if you return a non zero value from `mycommands.sh```, see /start example below. -To replace a global bashbot command add the same command to ```mycommands.sh``` and place ```return 1``` at the end of +To replace a global bashbot command add the same command to `mycommands.sh` and place `return 1` at the end of the case block, see /kickme example below. -If a command is available as a global command and in ```mycommands.sh```, plus you return a zero value (nothing or 0) +If a command is available as a global command and in `mycommands.sh```, plus you return a zero value (nothing or 0) both command sections are processed. Thus you can extend global commands with additional actions, see /info example below **Learn more about [Bot commands](https://core.telegram.org/bots#commands).** @@ -75,7 +75,7 @@ both command sections are processed. Thus you can extend global commands with ad ### Separate logic from commands -If a command need more than 2-3 lines of code, you should use a function to separate logic from command. Place your functions in ```mycommands.sh``` and call the from your command. Example: +If a command need more than 2-3 lines of code, you should use a function to separate logic from command. Place your functions in `mycommands.sh` and call the from your command. Example: ```bash # file: mycommands.sh # your additional bashbot commands @@ -133,7 +133,9 @@ Line 17: ^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'. ``` -As you can see my ```mybotcommands.inc.sh``` contains an useless echo command in 'TEXT=' assignment and can be replaced by ```TEXT="${TEXT}${WORD}"``` + +As you can see my `mybotcommands.inc.sh` contains an useless echo command in 'TEXT=' assignment and can be replaced by `TEXT="${TEXT}${WORD}"` + ```bash $ shellcheck -x examples/notify OK @@ -158,5 +160,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-28-g43f5536 diff --git a/doc/6_reference.md b/doc/6_reference.md index 7d0020a..896d1d3 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -6,11 +6,11 @@ To insert line brakes in a message or caption you can place `\n` in the text. ##### send_action -```send_action``` shows users what your bot is currently doing. +`send_action` shows users what your bot is currently doing. *usage:* send_action "${CHAT[ID]}" "action" -*"action":* ```typing```, ```upload_photo```, ```record_video```, ```upload_video```, ```record_audio```, ```upload_audio```, ```upload_document```, ```find_location```. +*"action":* `typing`, `upload_photo`, `record_video`, `upload_video`, `record_audio`, `upload_audio`, `upload_document`, `find_location`. *alias:* _action "action" @@ -21,7 +21,7 @@ send_action "${CHAT[ID]}" "record_audio" ``` ##### send_normal_message -```send_normal_message``` sends text only messages to the given chat. +`send_normal_message` sends text only messages to the given chat. *usage:* send_normal_message "${CHAT[ID]}" "message" @@ -34,7 +34,7 @@ send_normal_message "${CHAT[ID]}" "this is a text message" ##### send_markdownv2_message -```send_markdownv2_message``` sends markdown v2 style messages to the given chat. +`send_markdownv2_message` sends markdown v2 style messages to the given chat. Telegram supports a new [Markdown V2 Style](https://core.telegram.org/bots/api#markdownv2-style) which has more formatting codes and is more robust, but incompatible with old telegram markdown style. @@ -48,7 +48,7 @@ send_markdownv2_message "${CHAT[ID]}" "*bold* __underlined__ [text](link)" ##### send_markdown_message -```send_markdown_message``` sends markdown style messages to the given chat. +`send_markdown_message` sends markdown style messages to the given chat. This is the old, legacy Telegram markdown style, retained for backward compatibility. It supports a [reduced set of Markdown](https://core.telegram.org/bots/api#markdown-style) only @@ -64,7 +64,7 @@ send_markdown_message "${CHAT[ID]}" "*bold* _italic_ [text](link)" ##### send_html_message -```send_html_message``` sends HTML style messages to the given chat. +`send_html_message` sends HTML style messages to the given chat. Telegram supports a [reduced set of HTML](https://core.telegram.org/bots/api#html-style) only *usage:* send_html_message "${CHAT[ID]}" "html message" @@ -78,7 +78,7 @@ send_normal_message "${CHAT[ID]}" "bold italic> italic>/em> v0.x-prex -> v0.x-rc -> v0.x ... 0.x+1-dev ... ``` -If you release a new Version run ```dev/make-distribution.sh``` to create the zip and tar.gz archives in the dist directory and attach them to the github release. Do not forget to delete directory dist afterwards. +If you release a new Version run `dev/make-distribution.sh` to create the zip and tar.gz archives in the dist directory and attach them to the github release. Do not forget to delete directory dist afterwards. #### Versioning Bashbot is tagged with version numbers. If you start a new development cycle you can tag your fork with a version higher than the current version. -E.g. if you fork 'v0.60' the next develop version should tagged as ```git tag "v0.61-dev"``` for fixes or ```git tag "v0.70-dev"``` for new features. +E.g. if you fork 'v0.60' the next develop version should tagged as `git tag "v0.61-dev"` for fixes or `git tag "v0.70-dev"` for new features. -To get the current version name of your develepment fork run ```git describe --tags```. The output looks like ```v0.70-dev-6-g3fb7796``` where your version tag is followed by the number of commits since you tag your branch and followed by the latest commit hash. see also [comments in version.sh](../dev/version.sh) +To get the current version name of your develepment fork run `git describe --tags`. The output looks like `v0.70-dev-6-g3fb7796` where your version tag is followed by the number of commits since you tag your branch and followed by the latest commit hash. see also [comments in version.sh](../dev/version.sh) -To update the Version Number in files run ```dev/version.sh files```, it will update the line '#### $$VERSION$$ ###' in all files to the current version name. +To update the Version Number in files run `dev/version.sh files`, it will update the line '#### $$VERSION$$ ###' in all files to the current version name. To update version in all files run 'dev/version.sh' without parameter. @@ -287,27 +288,27 @@ To update version in all files run 'dev/version.sh' without parameter. For a shell script running as a service it's important to be paranoid about quoting, globbing and other common problems. So it's a must to run shellchek on all shell scripts before you commit a change. this is automated by a git hook activated in Setup step 6. -To run shellcheck for a single script run ```shellcheck -x script.sh```, to check all schripts run ```dev/hooks/pre-commit.sh```. +To run shellcheck for a single script run `shellcheck -x script.sh`, to check all schripts run `dev/hooks/pre-commit.sh`. ### bashbot test suite -Starting with version 0.70 bashbot has a test suite. To start testsuite run ```dev/all-tests.sh```. all-tests.sh will return 'SUCCESS' only if all tests pass. +Starting with version 0.70 bashbot has a test suite. To start testsuite run `dev/all-tests.sh`. all-tests.sh will return 'SUCCESS' only if all tests pass. #### enabling / disabling tests -All tests are placed in the directory ```test```. To disable a test remove the execute flag from the '*-test.sh' script, to (re)enable a test make the script executable again. +All tests are placed in the directory `test`. To disable a test remove the execute flag from the '*-test.sh' script, to (re)enable a test make the script executable again. #### creating new tests -To create a new test run ```test/ADD-test-new.sh``` and answer the questions, it will create the usually needed files and dirs: +To create a new test run `test/ADD-test-new.sh` and answer the questions, it will create the usually needed files and dirs: -Each test consists of a script script named after ```p-name-test.sh``` *(where p is test pass 'a-z' and name the name -of your test)* and an optional dir ```p-name-test/``` *(script name minus '.sh')* for additional files. +Each test consists of a script script named after `p-name-test.sh` *(where p is test pass 'a-z' and name the name +of your test)* and an optional dir `p-name-test/` *(script name minus '.sh')* for additional files. Tests with no dependency to other tests will run in pass 'a', tests which need an initialized bashbot environment must run in pass 'd' or later. A temporary test environment is created when 'ALL-tests.sh' starts and deleted after all tests are finished. -The file ```ALL-tests.inc.sh``` must be included from all tests and provide the test environment as shell variables: +The file `ALL-tests.inc.sh` must be included from all tests and provide the test environment as shell variables: ```bash # Test Environment TESTME="$(basename "$0")" @@ -354,5 +355,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v1.21-dev-17-g8c9298d +#### $$VERSION$$ v1.21-dev-28-g43f5536 From cf6f3eedd7064af5f6472aee57683aa3fc6f2572 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 29 Dec 2020 11:02:28 +0100 Subject: [PATCH 34/51] doc: remove old stuff part 2 --- README.md | 22 +++++++++++----------- doc/3_advanced.md | 19 +++++++++---------- doc/4_expert.md | 10 +++++----- doc/5_practice.md | 10 +++++----- doc/6_reference.md | 42 ++++++++++++++++++------------------------ modules/sendMessage.sh | 6 +++--- 6 files changed, 51 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 0d45cb7..24b3664 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ For more Information on how to install, customize and use your new bot, read the ### Log files -Bashbot actions are logged in BASHBOT.log, Telegram send/receive errors are logged to ERROR.log. +Bashbot actions are logged to `BASHBOT.log`, Telegram send/receive errors are logged to `ERROR.log`. Start bashbot in debug mode to get all messages send to / received from Telegram and error messages of bash commands also. To enable debug mode start bashbot with debug as third argument: `bashbot start debug` @@ -141,11 +141,11 @@ Bash scripts in general are not designed to be bullet proof, so consider this Bo Whenever you are processing input from untrusted sources (messages, files, network) you must be as careful as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands. -One of the most powerful features of unix shells is variable and command substitution using ```${}``` and ```$()```, +One of the most powerful features of unix shells is variable and command substitution using`${}` and`$()```, but as they are expanded in double quotes, this can lead to RCE and information disclosing bugs in complex scripts like bashbot. So it's more secure to escape or remove '$' in input from user, files or network. -A powerful tool to improve your scripts is ```shellcheck```. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used extensively in bashbot development to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. +A powerful tool to improve your scripts is`shellcheck`. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used extensively in bashbot development to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a [test suite](doc/7_develop.md) to check if important functionality is working as expected. ### Use printf whenever possible @@ -163,9 +163,9 @@ For the same reason every file your Bot can read is in danger to be disclosed. R ### Secure your Bot installation **Your Bot configuration must no be readable from other users.** Everyone who can read your Bots token is able to act as your Bot and has access to all chats the Bot is in! -Everyone with read access to your Bot files can extract your Bots data. Especially your Bot config in ```config.jssh``` must be protected against other users. No one except you should have write access to the Bot files. The Bot should be restricted to have write access to ```count.jssh``` and ```data-bot-bash``` only, all other files must be write protected. +Everyone with read access to your Bot files can extract your Bots data. Especially your Bot config in`config.jssh` must be protected against other users. No one except you should have write access to the Bot files. The Bot should be restricted to have write access to`count.jssh` and `data-bot-bash` only, all other files must be write protected. -To set access rights for your bashbot installation to a reasonable default run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. +To set access rights for your bashbot installation to a reasonable default run`sudo ./bashbot.sh init` after every update or change to your installation directory. ## FAQ @@ -183,16 +183,16 @@ Well, that's a damn good question ... may be because I'm an unix admin from ston - no database, not event driven, not object oriented ... ### Can I have the single bashbot.sh file back? -At the beginning bashbot was simply the file ```bashbot.sh``` you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more. +At the beginning bashbot was simply the file`bashbot.sh` you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more. -Hey no Problem, if you are finished with your cool bot run ```dev/make-standalone.sh``` to create a stripped down Version of your bot containing only +Hey no Problem, if you are finished with your cool bot run`dev/make-standalone.sh` to create a stripped down Version of your bot containing only 'bashbot.sh' and 'commands.sh'! For more information see [Create a stripped down Version of your Bot](doc/7_develop.md) ### Can I send messages from CLI and scripts? -Of course, you can send messages from CLI and scripts, simply install bashbot as [described here](#Your-really-first-bashbot-in-a-nutshell), -send the message '/start' to set yourself as botadmin and stop the bot with ```./bashbot.sh stop```. +Of course, you can send messages from command line and scripts, simply install bashbot as [described here](#Your-really-first-bashbot-in-a-nutshell), +send the message '/start' to set yourself as botadmin and then stop the bot with `./bashbot.sh stop`. -Bashbot provides some ready to use scripts ro send messages from command line in `bin/` dir, e.g. `send_message.sh`. +Bashbot provides some ready to use scripts for sending messages from command line in `bin/` dir, e.g. `send_message.sh`. ```bash bin/send_message.sh BOTADMIN "This is my first message send from CLI" @@ -233,4 +233,4 @@ See `mycommnds.sh.dist` for an example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.21-dev-28-g43f5536 +#### $$VERSION$$ v1.21-dev-29-g13d15f4 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index ab46446..45aee2f 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -54,7 +54,7 @@ ALL:*:* ``` -You must use the function `user_is_allowed`to check if a user has the capability to do something. Example: Check if user has capability to start bot. +You must use the function `user_is_allowed` to check if a user has the capability to do something. Example: Check if user has capability to start bot. ```bash case "$MESSAGE" in @@ -74,8 +74,8 @@ You must use the function `user_is_allowed`to check if a user has the capability ### Interactive Chats Interactive chats are short running scripts, reading user input and echo data to the user. -To create a new interactive chat script copy 'scripts/interactive.sh.clean' to e.g. 'scripts/mynewinteractive.sh', make it executable -and then use 'start_proc' function from your bot, it's possible to pass two arguments. You find more examples for interactive scripts in 'examples' +To create a new interactive chat script copy `scripts/interactive.sh.clean` to e.g. `scripts/mynewinteractive.sh`, make it executable +and then use `start_proc` function from your bot, it's possible to pass two arguments. You find more examples for interactive scripts in 'examples' *usage*: start_proc chat_id script arg1 arg2 @@ -83,7 +83,7 @@ and then use 'start_proc' function from your bot, it's possible to pass two argu *usage*: check_prog chat_id -**Note:** From Version 0.96 on scripts must read user input from '$3' instead of stdin! +**IMPORTANT:** Scripts must read user input from '$3' instead of stdin! ```bash #!/bin/bash @@ -105,8 +105,8 @@ echo -e "Your Message: ${test}\nbye!" #### message formatting and keyboards -The output of the script will be processed by 'send_messages', so you can not only send text, but also keyboards, files, locations and more. -Each newline in the output will start an new message to the user. To have line breaks in your message you must insert ' mynewlinestartshere ' or '\n' instead. +The output of the script will be processed by `send_messages`, so you can not only send text, but also keyboards, files, locations and more. +Each newline in the output will start an new message to the user. To have line breaks in your message you must insert `\n` instead. To open up a keyboard in an interactive script, print out the keyboard layout in the following way: ```bash @@ -137,10 +137,9 @@ echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No ``` Please note that you can either send a location or a venue, not both. To send a venue add the mytitlestartshere and the myaddressstartshere keywords. -To insert a line break in your message you can insert ` mynewlinestartshere ` or `\n` in your echo command: +To insert a line break in your message you can insert `\n` in your echo command: ```bash -echo "Text that will appear in one message mynewlinestartshere with this text on a new line" -echo "Other text message\nwith a newline" # \n instead of mynewlinestartshere +echo "Text that will appear in one message \nwith this text on a new line" ``` In case you want extend a message already containing a location, a file, a keyboard etc., @@ -302,5 +301,5 @@ Note: If you disable automatic retry, se above, you disable also connection prob #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v1.21-dev-28-g43f5536 +#### $$VERSION$$ v1.21-dev-29-g13d15f4 diff --git a/doc/4_expert.md b/doc/4_expert.md index 5e2f0ec..3b2b4ea 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -5,8 +5,8 @@ UTF-8 is a variable length encoding of Unicode. UTF-8 is recommended as the default encoding in JSON, XML and HTML, also Telegram make use of it. The first 128 characters are regular ASCII, so it's a superset of and compatible with ASCII environments. The next 1,920 characters need -two bytes for encoding and covers almost all `Latin` alphabets, also `Greek```, `Cyrillic```, -```Hebrew```, `Arabic` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details. +two bytes for encoding and covers almost all `Latin` alphabets, also `Greek`, `Cyrillic`, +`Hebrew`, `Arabic` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details. #### Setting up your Environment In general `bash` and `GNU` utitities are UTF-8 aware if you to setup your environment @@ -209,7 +209,7 @@ If you want to have other locations for config, data etc, define and export the **Note: all specified directories and files must exist or running 'bashbot.sh' will fail.** ##### BASHBOT_ETC -Location of the files `commands.sh```, `mycommands.sh```, `botconfig.jssh```, `botacl` ... +Location of the files `commands.sh`, `mycommands.sh`, `botconfig.jssh`, `botacl` ... ```bash unset BASHBOT_ETC # keep in telegram-bot-bash (default) export BASHBOT_ETC "" # keep in telegram-bot-bash @@ -223,7 +223,7 @@ Location of the files `commands.sh```, `mycommands.sh```, `botconfig.jssh```, `b e.g. /etc/bashbot ##### BASHBOT_VAR -Location of runtime data `data-bot-bash```, `count.jssh` +Location of runtime data `data-bot-bash`, `count.jssh` ```bash unset BASHBOT_VAR # keep in telegram-bot-bash (default) export BASHBOT_VAR "" # keep in telegram-bot-bash @@ -378,5 +378,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Advanced Use](3_advanced.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v1.21-dev-28-g43f5536 +#### $$VERSION$$ v1.21-dev-29-g13d15f4 diff --git a/doc/5_practice.md b/doc/5_practice.md index e96c390..c0bd3cf 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -10,9 +10,9 @@ In addition you should know about [BotFather, the one bot to rule them all](http If you don't have a github account, it may time to [setup a free account now](https://github.com/pricing) ### Add commands to mycommands.sh only -Do not change `bashbot.sh` and `commands.sh```, instead place your commands in to `mycommands.sh```. +Do not change `bashbot.sh` and `commands.sh`, instead place your commands in to `mycommands.sh`. To start with a clean/minimal bot copy `mycommands.sh.clean` to `mycommands.sh` and start editing -the message strings and place commands in the```case ... esac` block of the function mycommands(): +the message strings and place commands in the`case ... esac` block of the function mycommands(): ```bash # file: mycommands.sh # your additional bashbot commands @@ -39,13 +39,13 @@ mycommands() { ### DIsable, replace and extend global commands -Global bashbot command processing, e.g. /start, /info etc. is disabled if you return a non zero value from `mycommands.sh```, +Global bashbot command processing, e.g. /start, /info etc. is disabled if you return a non zero value from `mycommands.sh`, see /start example below. To replace a global bashbot command add the same command to `mycommands.sh` and place `return 1` at the end of the case block, see /kickme example below. -If a command is available as a global command and in `mycommands.sh```, plus you return a zero value (nothing or 0) +If a command is available as a global command and in `mycommands.sh`, plus you return a zero value (nothing or 0) both command sections are processed. Thus you can extend global commands with additional actions, see /info example below **Learn more about [Bot commands](https://core.telegram.org/bots#commands).** @@ -160,5 +160,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ v1.21-dev-28-g43f5536 +#### $$VERSION$$ v1.21-dev-29-g13d15f4 diff --git a/doc/6_reference.md b/doc/6_reference.md index 896d1d3..2270aab 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -325,13 +325,13 @@ Your Bot will leave the chat. *alias:* _leave ```bash -if _is_admin ; then +if bot_is_admin ; then send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*" leave_chat "${CHAT[ID]}" fi ``` -'See also [kick Chat Member](https://core.telegram.org/bots/api/#kickchatmember)* +See also [kick Chat Member](https://core.telegram.org/bots/api/#kickchatmember)* ---- @@ -572,13 +572,11 @@ fi ---- ##### send_interactive -Form version 0.80 on forward_message is used to forward messages to interactive job. It replaces the old 'inproc' commands used for TMUX. -Usually a message is automatically forwarded in 'commands.sh', but you can forward messages while processing also or send your own messages. +`send_interactive` is used to forward messages to interactive jobs. +Usually a message is automatically forwarded from within `commands.sh`, but you can send messages yourself. *usage:* send_interactive "${CHAT[ID]}" "message" -*replaces:* incproc - ---- ### jsshDB @@ -612,18 +610,17 @@ slow functions: #### File naming and locking -A jssh fileDB consists of two files which must reside inside BASHBOT_ETC or BASHBOT_DATA. +A jssh fileDB consists of two files which must reside inside `BASHBOT_ETC` or `BASHBOT_DATA`. - `filename.jssh` is a text file containing the key/value data in json.sh format. - `filename.jssh.flock` is used for read/write locking with flock -Path names containing `..` or not located in BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions with an error. +Path names containing `..` or not located in `BASHBOT_ETC` or `BASHBOT_DATA` are refused by jsshDB functions with an error. -Since version 0.94 jsshDB functions support file locking with flock. Write/update operations are serialised with flock to wait until -previous operations are finished, see "man flock" for information. To avoid deadlocks we use a timeout of 10s for write and 5s for read operations. +jsshDB functions use file locking with flock, if flock is availible. Write/update operations are serialised to wait until +previous operations are finished, see "man flock". To avoid deadlocks bashbot use a timeout of 10s for write and 5s for read operations. -Every jssh_*DB function exist as jssj_*DB_async also. -In case flock is not availibe or you don't want locking, jssh_*DB_async functions without file locking will be used. +For every `jssh_...DB` function a `jsshj_...DB_async` function exsist also. In case don't want locking, use `jssh_...DB_async` functions. *Example:* for allowed file names: ```bash @@ -676,7 +673,7 @@ Something wrong with data-bot-bash/../../../somevalues ``` ##### jssh_writeDB -Write content of an ARRAY into jsshDB file. ARRAY name must be declared with "declare -A ARRAY" before calling writeDB. +Write content of an ARRAY into jsshDB file. ARRAY name must be declared with `declare -A ARRAY` before calling writeDB. "DB" file MUST exist or nothing is written. Note: Existing content is overwritten. @@ -712,7 +709,7 @@ cat "${DATADIR:-}/myvalues.jssh" ``` ##### jssh_printDB -Print content of an ARRAY to STDOUT. ARRAY name must be declared with "declare -A ARRAY" before calling printDB.. +Print content of an ARRAY to STDOUT. ARRAY name must be declared with `declare -A ARRAY` before calling printDB.. *usage:* jssh_printDB "ARRAY" @@ -734,7 +731,7 @@ jssh_printDB READVALUES ``` ##### jssh_updateDB -Update/Add content of an ARRAY into a jsshDB file. ARRAY name must be declared with "declare -A ARRAY" before calling updateDB. +Update/Add content of an ARRAY into a jsshDB file. ARRAY name must be declared with `declare -A ARRAY` before calling updateDB. "DB" file MUST exist or nothing is written. Note: Existing content not in ARRAY is kept in file. @@ -769,7 +766,7 @@ cat "$DBfile" ``` ##### jssh_readDB -Read content of a file in JSON.sh format into given ARRAY. ARRAY name must be declared with "declare -A ARRAY" upfront, +Read content of a file in JSON.sh format into given ARRAY. ARRAY name must be declared with `declare -A ARRAY` upfront, *usage:* jssh_readDB "ARRAY" "filename" @@ -875,8 +872,8 @@ https://linuxconfig.org/how-to-use-arrays-in-bash-script ---- ### Aliases - shortcuts for often used functions -Aliases are handy shortcuts for using in 'mycommands.sh', they avoid error prone typing of "${CHAT[ID]}" "${USER[ID]}" as much as possible. -Do not use them in bashbot.sh, modules and addons. +Aliases are handy shortcuts for use in `mycommands.sh` *only*, they avoid error prone typing of "${CHAT[ID]}" "${USER[ID]}" as much as possible. +Do not use them in other files e.g. `bashbot.sh`, modules, addons etc. ##### _is_botadmin @@ -974,7 +971,7 @@ Do not use them in bashbot.sh, modules and addons. ### Helper functions ##### download -Download the fiven URL and returns the final filename in TMPDIR. If the given filename exists,the filename is prefixed with a +Download the given URL and returns the final filename in TMPDIR. If the given filename exists,the filename is prefixed with a random number. Filename is not allowed to contain '/' or '..'. *usage:* download URL filename @@ -1084,9 +1081,6 @@ killallproc ##### get_file *usage:* url="$(get_file "${CHAT[ID]}" "message")" -##### send_text -*usage:* send_text "${CHAT[ID]}" "message" - ---- ##### JsonDecode @@ -1107,7 +1101,7 @@ Reads JSON from STDIN and Outputs found Value to STDOUT ##### Json2Array Read JSON.sh style data from STDIN and assign to given ARRAY -ARRAY name must be declared with "declare -A ARRAY" before calling +ARRAY name must be declared with `declare -A ARRAY` before calling *usage:* Json2Array "ARRAY" @@ -1155,5 +1149,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v1.21-dev-28-g43f5536 +#### $$VERSION$$ v1.21-dev-29-g13d15f4 diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index f46875e..f8d4baa 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117 -#### $$VERSION$$ v1.21-dev-20-ga4dce7b +#### $$VERSION$$ v1.21-dev-29-g13d15f4 # will be automatically sourced from bashbot @@ -336,14 +336,14 @@ send_message() { sent=y fi if [ "$sent" != "y" ];then - send_text "$1" "$text" + send_text_mode "$1" "$text" fi } # $1 CHAT $2 message starting possibly with html_parse_mode or markdown_parse_mode # not working, fix or remove after 1.0!! -send_text() { +send_text_mode() { case "$2" in 'html_parse_mode'*) send_html_message "$1" "${2//html_parse_mode}" From fee0615c28d31ae089881de406c95c0318b02d4c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 29 Dec 2020 11:16:14 +0100 Subject: [PATCH 35/51] doc: improve jsshDB description --- doc/6_reference.md | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 2270aab..8773f65 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -580,28 +580,25 @@ Usually a message is automatically forwarded from within `commands.sh`, but you ---- ### jsshDB -Since output generated by JSON.sh is so handy to use in bash, we use the format for a simple keys/value file store. -The file extensions is '.jssh' and for security reasons location of jssh files is restricted to BASHBOT_ETC and BASHBOT_DATA.. + +Since output generated by `JSON.sh` is so easy to use in bash, bashbot uses the format for a simple keys/value file store also. #### fast and slow operations -jsshDB files are simple text files and if you append a new Key/value pairs to the end of the file it overwrites -an existing key/value pair. We use this behavior for "fast" file operations. +jsshDB files are text files, key/value pairs appearing later in the file overwrites earlier key/value pairs in the file. +Bashbot use this behavior to implement "fast" file operations. -"fast functions" add a new key/value pair to the end of the file without deleting an existing one, this is fast but over (long) -time the file grows infinity. +"fast functions" add a new key/value pair to the end of a file without deleting an existing one, this is fast but over time the file grows to infinity. -"slow functions" in contrast modify the key/value pairs in place and write the whole file back, -this is slower but clean up the file. All previously added key/value pairs are replaced -and only the last one is written back to the file. +"slow functions" read the file, modify the key/value pairs in memory and write the whole file back, this is slower but removes duplicate keys from the file. -fast functions: +Fast functions: ``` jssh_insertKeyDB , jssh_addKeyDB , jssh_countKeyDB ``` -slow functions: +Slow functions: ``` jssh_writeDB, jssh_updateDB , jssh_deleteKeyDB, jssh_clearDB @@ -610,17 +607,17 @@ slow functions: #### File naming and locking -A jssh fileDB consists of two files which must reside inside `BASHBOT_ETC` or `BASHBOT_DATA`. +A jssh fileDB consists of two files and must reside inside `BASHBOT_ETC` or `BASHBOT_DATA`. -- `filename.jssh` is a text file containing the key/value data in json.sh format. -- `filename.jssh.flock` is used for read/write locking with flock +- `filename.jssh` is the file containing the key/value pairs in json.sh format. +- `filename.jssh.flock` is used to provide read/write locking with flock Path names containing `..` or not located in `BASHBOT_ETC` or `BASHBOT_DATA` are refused by jsshDB functions with an error. -jsshDB functions use file locking with flock, if flock is availible. Write/update operations are serialised to wait until +jsshDB functions use file locking with flock if flock is available. Write/update operations are serialised to wait until previous operations are finished, see "man flock". To avoid deadlocks bashbot use a timeout of 10s for write and 5s for read operations. -For every `jssh_...DB` function a `jsshj_...DB_async` function exsist also. In case don't want locking, use `jssh_...DB_async` functions. +For every `jssh_...DB` function a `jsshj_...DB_async` function exists also. In case don't want locking, use `jssh_...DB_async` functions. *Example:* for allowed file names: ```bash @@ -1149,5 +1146,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v1.21-dev-29-g13d15f4 +#### $$VERSION$$ v1.21-dev-30-gcf6f3ee From 2aa5c75fe0deedaa5df97895d3a4902c21800dbc Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 29 Dec 2020 11:22:49 +0100 Subject: [PATCH 36/51] doc: improve jsshDB description again --- doc/6_reference.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 8773f65..4e7f959 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -585,8 +585,8 @@ Since output generated by `JSON.sh` is so easy to use in bash, bashbot uses the #### fast and slow operations -jsshDB files are text files, key/value pairs appearing later in the file overwrites earlier key/value pairs in the file. -Bashbot use this behavior to implement "fast" file operations. +jsshDB files are flat text files containing key/value pairs in the `JSON.sh` format. +Key/value pairs appearing later in the file overwrites earlier key/value pairs, Bashbot use this behavior to implement "fast replace" file operations. "fast functions" add a new key/value pair to the end of a file without deleting an existing one, this is fast but over time the file grows to infinity. @@ -1146,5 +1146,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v1.21-dev-30-gcf6f3ee +#### $$VERSION$$ v1.21-dev-31-gfee0615 From d083390dcbeabf8d6c449d3490db23dd7299722f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 29 Dec 2020 11:27:22 +0100 Subject: [PATCH 37/51] doc: improve jsshDB flock description --- doc/6_reference.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 4e7f959..62de4b1 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -609,12 +609,12 @@ Slow functions: A jssh fileDB consists of two files and must reside inside `BASHBOT_ETC` or `BASHBOT_DATA`. -- `filename.jssh` is the file containing the key/value pairs in json.sh format. +- `filename.jssh` is the file containing the key/value pairs in JSON.sh format. - `filename.jssh.flock` is used to provide read/write locking with flock Path names containing `..` or not located in `BASHBOT_ETC` or `BASHBOT_DATA` are refused by jsshDB functions with an error. -jsshDB functions use file locking with flock if flock is available. Write/update operations are serialised to wait until +jsshDB functions use file locking if `flock is available, read/write operations are serialised to wait until previous operations are finished, see "man flock". To avoid deadlocks bashbot use a timeout of 10s for write and 5s for read operations. For every `jssh_...DB` function a `jsshj_...DB_async` function exists also. In case don't want locking, use `jssh_...DB_async` functions. @@ -1146,5 +1146,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v1.21-dev-31-gfee0615 +#### $$VERSION$$ v1.21-dev-32-g2aa5c75 From a5307e3a9452ebce348c6eab52f767b7b9692740 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 29 Dec 2020 13:03:46 +0100 Subject: [PATCH 38/51] doc: better explanation of $ RCE --- README.html | 20 +++++++------- README.md | 20 +++++++++----- README.txt | 70 +++++++++++++++++++++++++---------------------- modules/jsonDB.sh | 5 ++-- 4 files changed, 63 insertions(+), 52 deletions(-) diff --git a/README.html b/README.html index 35c0535..c8f5e59 100644 --- a/README.html +++ b/README.html @@ -327,7 +327,7 @@ This is bashbot, the Telegram bot written entirely in bash. It features background tasks and interactive chats, and can serve as an interface for CLI programs.

    For more Information on how to install, customize and use your new bot, read the Documentation

    Log files

    -

    Bashbot actions are logged in BASHBOT.log, Telegram send/receive errors are logged to ERROR.log. Start bashbot in debug mode to get all messages send to / received from Telegram and error messages of bash commands also.

    +

    Bashbot actions are logged to BASHBOT.log, Telegram send/receive errors are logged to ERROR.log. Start bashbot in debug mode to get all messages send to / received from Telegram and error messages of bash commands also.

    To enable debug mode start bashbot with debug as third argument: bashbot start debug

    ├── logs 
     │   ├── BASHBOT.log      # log what your bot is doing ...
    @@ -339,8 +339,8 @@ It features background tasks and interactive chats, and can serve as an interfac
     

    Security Considerations

    Running a Telegram Bot means it is connected to the public and you never know what's send to your Bot.

    Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. Bash programmers often struggle with 'quoting hell' and globbing, see Implications of wrong quoting

    -

    Whenever you are processing input from untrusted sources (messages, files, network) you must be as careful as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands.

    -

    One of the most powerful features of unix shells is variable and command substitution using ${} and $(), but as they are expanded in double quotes, this can lead to RCE and information disclosing bugs in complex scripts like bashbot. So it's more secure to escape or remove '$' in input from user, files or network.

    +

    Whenever you are processing input from untrusted sources (messages, files, network) you must be as careful as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In addition remove unused scripts and examples from your Bot, e.g. everything in example/ and disable/remove all not needed bot commands.

    +

    It's important to escape or remove $ in input from user, files or network (as bashbot does) One of the powerful features of unix shells are variable and command substitution using ${} and$(), this can lead to remove code execution (RCE) or information disclosing bugs if unescaped $ is included in untrusted input, e.g. $$ or $(rm -rf /*)

    A powerful tool to improve your scripts is shellcheck. You can use it online or install shellcheck locally. Shellcheck is used extensively in bashbot development to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a test suite to check if important functionality is working as expected.

    Use printf whenever possible

    If you're writing a script and it is taking external input (from the user as arguments or file system...), you shouldn't use echo to display it. Use printf whenever possible

    @@ -349,8 +349,8 @@ It features background tasks and interactive chats, and can serve as an interfac

    Never run your Bot as root, this is the most dangerous you can do! Usually the user 'nobody' has almost no rights on unix/linux systems. See Expert use on how to run your Bot as an other user.

    Secure your Bot installation

    Your Bot configuration must no be readable from other users. Everyone who can read your Bots token is able to act as your Bot and has access to all chats the Bot is in!

    -

    Everyone with read access to your Bot files can extract your Bots data. Especially your Bot config in config.jssh must be protected against other users. No one except you should have write access to the Bot files. The Bot should be restricted to have write access to count.jssh and data-bot-bash only, all other files must be write protected.

    -

    To set access rights for your bashbot installation to a reasonable default run sudo ./bashbot.sh init after every update or change to your installation directory.

    +

    Everyone with read access to your Bot files can extract your Bots data. Especially your Bot config inconfig.jssh must be protected against other users. No one except you should have write access to the Bot files. The Bot should be restricted to have write access tocount.jssh and data-bot-bash only, all other files must be write protected.

    +

    To set access rights for your bashbot installation to a reasonable default runsudo ./bashbot.sh init after every update or change to your installation directory.

    FAQ

    Is this Bot insecure?

    Bashbot is not more (in)secure as any Bot written in an other language, we have done our best to make it as secure as possible. But YOU are responsible for the bot commands you wrote and you should know about the risks ...

    @@ -364,11 +364,11 @@ It features background tasks and interactive chats, and can serve as an interfac
  • no database, not event driven, not object oriented ...
  • Can I have the single bashbot.sh file back?

    -

    At the beginning bashbot was simply the file bashbot.sh you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more.

    -

    Hey no Problem, if you are finished with your cool bot run dev/make-standalone.sh to create a stripped down Version of your bot containing only 'bashbot.sh' and 'commands.sh'! For more information see Create a stripped down Version of your Bot

    +

    At the beginning bashbot was simply the filebashbot.sh you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more.

    +

    Hey no Problem, if you are finished with your cool bot rundev/make-standalone.sh to create a stripped down Version of your bot containing only 'bashbot.sh' and 'commands.sh'! For more information see Create a stripped down Version of your Bot

    Can I send messages from CLI and scripts?

    -

    Of course, you can send messages from CLI and scripts, simply install bashbot as described here, send the message '/start' to set yourself as botadmin and stop the bot with ./bashbot.sh stop.

    -

    Bashbot provides some ready to use scripts ro send messages from command line in bin/ dir, e.g. send_message.sh.

    +

    Of course, you can send messages from command line and scripts, simply install bashbot as described here, send the message '/start' to set yourself as botadmin and then stop the bot with ./bashbot.sh stop.

    +

    Bashbot provides some ready to use scripts for sending messages from command line in bin/ dir, e.g. send_message.sh.

    bin/send_message.sh BOTADMIN "This is my first message send from CLI"
     
     bin/send_message.sh --help
    @@ -390,6 +390,6 @@ It features background tasks and interactive chats, and can serve as an interfac

    @Gnadelwartz

    That's it all guys!

    If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

    -

    $$VERSION$$ v1.21-dev-28-g43f5536

    +

    $$VERSION$$ v1.21-dev-33-gd083390

    diff --git a/README.md b/README.md index 24b3664..ded812f 100644 --- a/README.md +++ b/README.md @@ -137,15 +137,21 @@ To enable debug mode start bashbot with debug as third argument: `bashbot start ## Security Considerations Running a Telegram Bot means it is connected to the public and you never know what's send to your Bot. -Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. Bash programmers often struggle with 'quoting hell' and globbing, see [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) +Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. +Bash programmers often struggle with 'quoting hell' and globbing, +see [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) -Whenever you are processing input from untrusted sources (messages, files, network) you must be as careful as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands. +Whenever you are processing input from untrusted sources (messages, files, network) you must be as careful as possible, +e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In addition remove unused scripts and examples +from your Bot, e.g. everything in `example/` and disable/remove all not needed bot commands. -One of the most powerful features of unix shells is variable and command substitution using`${}` and`$()```, -but as they are expanded in double quotes, this can lead to RCE and information disclosing bugs in complex scripts like bashbot. -So it's more secure to escape or remove '$' in input from user, files or network. +It's important to escape or remove `$` in input from user, files or network (_as bashbot does_) +One of the powerful features of unix shells are variable and command substitution using `${}` and`$()`, +this can lead to remove code execution (RCE) or information disclosing bugs if unescaped `$` is included in untrusted input, e.g. `$$` or `$(rm -rf /*)` -A powerful tool to improve your scripts is`shellcheck`. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used extensively in bashbot development to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. +A powerful tool to improve your scripts is `shellcheck`. You can [use it online](https://www.shellcheck.net/) or +[install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used extensively in bashbot development +to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a [test suite](doc/7_develop.md) to check if important functionality is working as expected. ### Use printf whenever possible @@ -233,4 +239,4 @@ See `mycommnds.sh.dist` for an example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.21-dev-29-g13d15f4 +#### $$VERSION$$ v1.21-dev-33-gd083390 diff --git a/README.txt b/README.txt index 41c52a4..ebd8979 100644 --- a/README.txt +++ b/README.txt @@ -143,8 +143,8 @@ the [Documentation](#Documentation) ### Log files -Bashbot actions are logged in BASHBOT.log, Telegram send/receive errors are -logged to ERROR.log. +Bashbot actions are logged to `BASHBOT.log`, Telegram send/receive errors are +logged to `ERROR.log`. Start bashbot in debug mode to get all messages send to / received from Telegram and error messages of bash commands also. @@ -168,29 +168,33 @@ Running a Telegram Bot means it is connected to the public and you never know what's send to your Bot. Bash scripts in general are not designed to be bullet proof, so consider this -Bot as a proof of concept. Bash programmers often struggle with 'quoting hell' -and globbing, see [Implications of wrong +Bot as a proof of concept. +Bash programmers often struggle with 'quoting hell' and globbing, +see [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-o f-forgetting-to-quote-a-variable-in-bash-posix-shells) Whenever you are processing input from untrusted sources (messages, files, -network) you must be as careful as possible, e.g. set IFS appropriate, disable -globbing (set -f) and quote everything. In addition delete unused scripts and -examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable -all not used commands. +network) you must be as careful as possible, +e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In +addition remove unused scripts and examples +from your Bot, e.g. everything in `example/` and disable/remove all not needed +bot commands. -One of the most powerful features of unix shells is variable and command -substitution using ```${}``` and ```$()```, -but as they are expanded in double quotes, this can lead to RCE and information -disclosing bugs in complex scripts like bashbot. -So it's more secure to escape or remove '$' in input from user, files or -network. +It's important to escape or remove `$` in input from user, files or network +(_as bashbot does_) +One of the powerful features of unix shells are variable and command +substitution using `${}` and`$()`, +this can lead to remove code execution (RCE) or information disclosing bugs if +unescaped `$` is included in untrusted input, e.g. `$$` or `$(rm -rf /*)` -A powerful tool to improve your scripts is ```shellcheck```. You can [use it -online](https://www.shellcheck.net/) or [install shellcheck +A powerful tool to improve your scripts is `shellcheck`. You can [use it +online](https://www.shellcheck.net/) or +[install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used -extensively in bashbot development to ensure a high code quality, e.g. it's not -allowed to push changes without passing all shellcheck tests. +extensively in bashbot development +to ensure a high code quality, e.g. it's not allowed to push changes without +passing all shellcheck tests. In addition bashbot has a [test suite](doc/7_develop.md) to check if important functionality is working as expected. @@ -218,13 +222,13 @@ can read your Bots token is able to act as your Bot and has access to all chats the Bot is in! Everyone with read access to your Bot files can extract your Bots data. -Especially your Bot config in ```config.jssh``` must be protected against other +Especially your Bot config in`config.jssh` must be protected against other users. No one except you should have write access to the Bot files. The Bot -should be restricted to have write access to ```count.jssh``` and -```data-bot-bash``` only, all other files must be write protected. +should be restricted to have write access to`count.jssh` and `data-bot-bash` +only, all other files must be write protected. -To set access rights for your bashbot installation to a reasonable default run -```sudo ./bashbot.sh init``` after every update or change to your installation +To set access rights for your bashbot installation to a reasonable default +run`sudo ./bashbot.sh init` after every update or change to your installation directory. ## FAQ @@ -249,24 +253,24 @@ health status - no database, not event driven, not object oriented ... ### Can I have the single bashbot.sh file back? -At the beginning bashbot was simply the file ```bashbot.sh``` you can copy +At the beginning bashbot was simply the file`bashbot.sh` you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more. -Hey no Problem, if you are finished with your cool bot run -```dev/make-standalone.sh``` to create a stripped down Version of your bot +Hey no Problem, if you are finished with your cool bot +run`dev/make-standalone.sh` to create a stripped down Version of your bot containing only 'bashbot.sh' and 'commands.sh'! For more information see [Create a stripped down Version of your Bot](doc/7_develop.md) ### Can I send messages from CLI and scripts? -Of course, you can send messages from CLI and scripts, simply install bashbot -as [described here](#Your-really-first-bashbot-in-a-nutshell), -send the message '/start' to set yourself as botadmin and stop the bot with -```./bashbot.sh stop```. +Of course, you can send messages from command line and scripts, simply install +bashbot as [described here](#Your-really-first-bashbot-in-a-nutshell), +send the message '/start' to set yourself as botadmin and then stop the bot +with `./bashbot.sh stop`. -Bashbot provides some ready to use scripts ro send messages from command line -in `bin/` dir, e.g. `send_message.sh`. +Bashbot provides some ready to use scripts for sending messages from command +line in `bin/` dir, e.g. `send_message.sh`. ```bash bin/send_message.sh BOTADMIN "This is my first message send from CLI" @@ -315,4 +319,4 @@ See `mycommnds.sh.dist` for an example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.21-dev-28-g43f5536 +#### $$VERSION$$ v1.21-dev-33-gd083390 diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 63efb58..4e060f6 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-33-gd083390 # # source from commands.sh to use jsonDB functions # @@ -331,8 +331,9 @@ function jssh_updateArray_async() { # read JSON.sh style data and asssign to an ARRAY # $1 ARRAY name, must be declared with "declare -A ARRAY" before calling Json2Array() { + # match ["....."]\t and replace \t with = and print quote true false escape not escaped $ # shellcheck disable=SC1091,SC1090 - [ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" ) + [ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/p' -e 's/=(true|false)/="\1"/' -e 's/([^\]|^)\$/\1\\$/g')" ) } # get Config Key from jssh file without jsshDB # output ARRAY as JSON.sh style data From 7ecb836c6dcd050140f835f3927f0898d41f9072 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 29 Dec 2020 13:46:48 +0100 Subject: [PATCH 39/51] doc: fix missing ``` --- README.md | 4 ++-- doc/4_expert.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ded812f..b8648e7 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ from your Bot, e.g. everything in `example/` and disable/remove all not needed b It's important to escape or remove `$` in input from user, files or network (_as bashbot does_) One of the powerful features of unix shells are variable and command substitution using `${}` and`$()`, -this can lead to remove code execution (RCE) or information disclosing bugs if unescaped `$` is included in untrusted input, e.g. `$$` or `$(rm -rf /*)` +this can lead to remote code execution (RCE) or remote information disclosure (RID) bugs if unescaped `$` is included in untrusted input, e.g. `$$` or `$(rm -rf /*)` A powerful tool to improve your scripts is `shellcheck`. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used extensively in bashbot development @@ -239,4 +239,4 @@ See `mycommnds.sh.dist` for an example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.21-dev-33-gd083390 +#### $$VERSION$$ v1.21-dev-34-ga5307e3 diff --git a/doc/4_expert.md b/doc/4_expert.md index 3b2b4ea..4d137f3 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -169,7 +169,7 @@ echo $COMMANDS $MODULEDIR $BOTACL $TMPDIR $COUNTFILE /usr/local/telegram-bot-bash/botacl /usr/local/telegram-bot-bash/data-bot-bash /usr/local/telegram-bot-bash/count -` +``` After sourcing you can use bashbot functions to send Messages, Locations, Pictures etc. to any Telegram User or Chat you are in. See [Send Messages](2_usage.md#sending-messages). @@ -378,5 +378,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Advanced Use](3_advanced.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v1.21-dev-29-g13d15f4 +#### $$VERSION$$ v1.21-dev-34-ga5307e3 From c6001c200eee248156eb7ac246293c904a3425aa Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 29 Dec 2020 13:59:31 +0100 Subject: [PATCH 40/51] create dirs on make-dist --- dev/make-distribution.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 1e381e8..6acb574 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -7,7 +7,7 @@ # # Options: --notest - skip tests # -#### $$VERSION$$ v1.21-dev-17-g8c9298d +#### $$VERSION$$ v1.21-dev-35-g7ecb836 ############################################################## # magic to ensure that we're always inside the root of our application, @@ -25,6 +25,7 @@ VERSION="$(git describe --tags | sed -e 's/-[0-9].*//' -e 's/v//')" DISTNAME="telegram-bot-bash" DISTDIR="./DIST/${DISTNAME}" DISTFILES="bashbot.rc bashbot.sh commands.sh mycommands.sh mycommands.sh.clean bin doc examples scripts modules addons LICENSE README.md README.txt README.html" +DISTMKDIR="data-bot-bash logs" # run tests first! for test in $1 dev/all-test*.sh @@ -39,12 +40,18 @@ done # create dir for distribution and copy files mkdir -p "${DISTDIR}" 2>/dev/null -# shellcheck disable=SC2086 + echo "Copy files" # shellcheck disable=SC2086 cp -r ${DISTFILES} "${DISTDIR}" cd "${DISTDIR}" || exit 1 +echo "Create directories" +for dir in $DISTMKDIR +do + [ ! -d "${dir}" ] && mkdir "${dir}" +done + # do not overwrite on update echo "Create .dist files" for file in mycommands.sh bashbot.rc addons/*.sh From 91419a26918a7243ef42c972c6e1a26b9bf96408 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 29 Dec 2020 14:02:11 +0100 Subject: [PATCH 41/51] do not move old logfiles anymore --- README.html | 4 ++-- README.txt | 7 ++++--- bashbot.sh | 11 +---------- dev/make-distribution.sh | 2 +- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/README.html b/README.html index c8f5e59..de61564 100644 --- a/README.html +++ b/README.html @@ -340,7 +340,7 @@ It features background tasks and interactive chats, and can serve as an interfac

    Running a Telegram Bot means it is connected to the public and you never know what's send to your Bot.

    Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. Bash programmers often struggle with 'quoting hell' and globbing, see Implications of wrong quoting

    Whenever you are processing input from untrusted sources (messages, files, network) you must be as careful as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In addition remove unused scripts and examples from your Bot, e.g. everything in example/ and disable/remove all not needed bot commands.

    -

    It's important to escape or remove $ in input from user, files or network (as bashbot does) One of the powerful features of unix shells are variable and command substitution using ${} and$(), this can lead to remove code execution (RCE) or information disclosing bugs if unescaped $ is included in untrusted input, e.g. $$ or $(rm -rf /*)

    +

    It's important to escape or remove $ in input from user, files or network (as bashbot does) One of the powerful features of unix shells are variable and command substitution using ${} and$(), this can lead to remote code execution (RCE) or remote information disclosure (RID) bugs if unescaped $ is included in untrusted input, e.g. $$ or $(rm -rf /*)

    A powerful tool to improve your scripts is shellcheck. You can use it online or install shellcheck locally. Shellcheck is used extensively in bashbot development to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a test suite to check if important functionality is working as expected.

    Use printf whenever possible

    If you're writing a script and it is taking external input (from the user as arguments or file system...), you shouldn't use echo to display it. Use printf whenever possible

    @@ -390,6 +390,6 @@ It features background tasks and interactive chats, and can serve as an interfac

    @Gnadelwartz

    That's it all guys!

    If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

    -

    $$VERSION$$ v1.21-dev-33-gd083390

    +

    $$VERSION$$ v1.21-dev-34-ga5307e3

    diff --git a/README.txt b/README.txt index ebd8979..bdb7a11 100644 --- a/README.txt +++ b/README.txt @@ -185,8 +185,9 @@ It's important to escape or remove `$` in input from user, files or network (_as bashbot does_) One of the powerful features of unix shells are variable and command substitution using `${}` and`$()`, -this can lead to remove code execution (RCE) or information disclosing bugs if -unescaped `$` is included in untrusted input, e.g. `$$` or `$(rm -rf /*)` +this can lead to remote code execution (RCE) or remote information disclosure +(RID) bugs if unescaped `$` is included in untrusted input, e.g. `$$` or `$(rm +-rf /*)` A powerful tool to improve your scripts is `shellcheck`. You can [use it online](https://www.shellcheck.net/) or @@ -319,4 +320,4 @@ See `mycommnds.sh.dist` for an example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v1.21-dev-33-gd083390 +#### $$VERSION$$ v1.21-dev-34-ga5307e3 diff --git a/bashbot.sh b/bashbot.sh index d4faf49..275a067 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-27-gd0f8716 +#### $$VERSION$$ v1.21-dev-36-gc6001c2 ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -1120,15 +1120,6 @@ bot_init() { [ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}" done printf "Done.\n" - if [[ ! -d "logs" ]]; then - printf "Move Logfiles ...\n" - mkdir logs 2>/dev/null - for MVLOG in DEBUG.log MESSAGE.log ERROR.log BASHBOT.log - do - [ -f "${MVLOG}" ] && mv "${MVLOG}" logs 2>/dev/null - done - printf "Done.\n" - fi # setup bashbot [[ "${UID}" -eq "0" ]] && RUNUSER="nobody" printf "Enter User to run bashbot [$RUNUSER]: " diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 6acb574..1632236 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -7,7 +7,7 @@ # # Options: --notest - skip tests # -#### $$VERSION$$ v1.21-dev-35-g7ecb836 +#### $$VERSION$$ v1.21-dev-36-gc6001c2 ############################################################## # magic to ensure that we're always inside the root of our application, From 29f2ce84188188e67a9e200351591d1eee665879 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 30 Dec 2020 10:03:23 +0100 Subject: [PATCH 42/51] fix log_message, log_updates do not interpret \n\t etc --- bashbot.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 275a067..ccc31f3 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-36-gc6001c2 +#### $$VERSION$$ v1.21-dev-37-g91419a2 ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -95,11 +95,12 @@ check_token(){ [[ "${1}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]] && return 0 return 1 } -# log $1 to ERRORLOG with date +# log $1 with date, interpret \n \t ... log_error(){ printf "%s: %b\n" "$(date)" "$*" >>"${ERRORLOG}"; } log_debug(){ printf "%s: %b\n" "$(date)" "$*" >>"${DEBUGLOG}"; } -log_message(){ printf "\n%s: %s\n" "$(date)" "$*" >>"${MESSAGELOG}"; } -log_update(){ printf "%s: %b\n" "$(date)" "$*" >>"${UPDATELOG}"; } +# log $1 with date +log_update(){ printf "%s: %s\n" "$(date)" "$*" >>"${UPDATELOG}"; } +log_message(){ printf "\n%s: %s\n" "$(date)" "$*" >>"${MESSAGELOG/\\n/$'\n'}"; } # additional tests if we run in debug mode export BASHBOTDEBUG From bddd52829e4c257c74b350995b35626497464743 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 30 Dec 2020 10:07:10 +0100 Subject: [PATCH 43/51] fix log_error, log_update do not interpret \n\t etc --- bashbot.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index ccc31f3..8d8010c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-37-g91419a2 +#### $$VERSION$$ v1.21-dev-38-g29f2ce8 ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -95,11 +95,11 @@ check_token(){ [[ "${1}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]] && return 0 return 1 } -# log $1 with date, interpret \n \t ... -log_error(){ printf "%s: %b\n" "$(date)" "$*" >>"${ERRORLOG}"; } -log_debug(){ printf "%s: %b\n" "$(date)" "$*" >>"${DEBUGLOG}"; } # log $1 with date +log_error(){ printf "%s: %s\n" "$(date)" "$*" >>"${ERRORLOG}"; } +log_debug(){ printf "%s: %s\n" "$(date)" "$*" >>"${DEBUGLOG}"; } log_update(){ printf "%s: %s\n" "$(date)" "$*" >>"${UPDATELOG}"; } +# log $1 with date, special first \n log_message(){ printf "\n%s: %s\n" "$(date)" "$*" >>"${MESSAGELOG/\\n/$'\n'}"; } # additional tests if we run in debug mode From 889fe8e8656ef17f3bbc13ae8bcdfcedae8db70a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 30 Dec 2020 10:58:25 +0100 Subject: [PATCH 44/51] doc: better description for git installation --- doc/0_install.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/doc/0_install.md b/doc/0_install.md index f7397c1..6eaeb7a 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -80,18 +80,22 @@ problems and not overwriting your bot config and `mycommands.sh`. Nevertheless you can install or update bashbot from a git repo, see next chapter ... -### Create Installation / Update archives yourself +### Create Installation / Update archives -To install or update bashbot from a git repo you must create the archives yourself. +To install or update bashbot from git repo execute `dev/make-distribution.sh`, this creates the archives and set up bashbot to run in `DIST/telegram.bot-bash`. + +*Note:* You should be familiar with `git`. 1. Run `git clone https://github.com/topkecleon/telegram-bot-bash.git` 2. Change into the directory `telegram-bot-bash` -3. Run ` git checkout develop` (_otional, for latest dev version_) -4. Run ` dev/make-distribution.sh` (_add option --notest to skip tests_) -5. You'll find archives and a bashbot installation in directory DIST -6. install or update abshbot using one of the archives (optional) +3. Optional: Run ` git checkout develop` for latest develop version +4. Run ` dev/make-distribution.sh` (_add --notest to skip tests_) +5. Change to dir `DIST/` -*Note*: You can update the basbot installation in `DIST/telegram-bot-bash` with `git pull; dev/make-distrubition.sh --notest` to get the latest updates. +Use the archives created in `DIST/` to install or update bashbot as described above. + +To run a test bot, e.g. while development or testing latest changes, you can use the bashbot installation provided in `dist/telegram-bot-bash`. +To update the test installation, e.g. after git pull, local changes or switch master/develop, run `dev/make-distrubition.sh` again. ### Note for BSD and MacOS @@ -127,5 +131,5 @@ You must update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bas #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v1.21-dev-22-ga3efcd2 +#### $$VERSION$$ v1.21-dev-39-gbddd528 From 41378f87a93e91b3d8e7f2059fc85d0ca3917022 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 30 Dec 2020 11:06:32 +0100 Subject: [PATCH 45/51] doc: 7_develop: explain test installation --- doc/7_develop.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/7_develop.md b/doc/7_develop.md index 58f8d9d..8212f0d 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -198,6 +198,9 @@ Now have a look at the directory `standalone`, here you find the files `bashbot. 5. give your (dev) fork a new version tag: `git tag v1.xx` 6. setup github hooks by running `dev/install-hooks.sh` +Run `dev/make-distrubition.sh` to create installation archives and a test installation in `DIST/`. +To update the test installation, e.g. after git pull, local changes or switch master/develop, run `dev/make-distrubition.sh` again. + Note for Debian: Debian Buster ships older versions of many utilities, pls try to install from [buster-backports](https://backports.debian.org/Instructions/) ```bash sudo apt-get -t buster-backports install git shellcheck pandoc codespell curl @@ -355,5 +358,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v1.21-dev-28-g43f5536 +#### $$VERSION$$ v1.21-dev-40-g889fe8e From 3407b0500be491852e43b7f46577cdaa9957ad4b Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 30 Dec 2020 11:18:48 +0100 Subject: [PATCH 46/51] doc: fix typo --- doc/0_install.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/0_install.md b/doc/0_install.md index 6eaeb7a..100daa4 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -94,7 +94,7 @@ To install or update bashbot from git repo execute `dev/make-distribution.sh`, t Use the archives created in `DIST/` to install or update bashbot as described above. -To run a test bot, e.g. while development or testing latest changes, you can use the bashbot installation provided in `dist/telegram-bot-bash`. +To run a test bot, e.g. while development or testing latest changes, you can use the bashbot installation provided in `DIST/telegram-bot-bash`. To update the test installation, e.g. after git pull, local changes or switch master/develop, run `dev/make-distrubition.sh` again. @@ -131,5 +131,5 @@ You must update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bas #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v1.21-dev-39-gbddd528 +#### $$VERSION$$ v1.21-dev-41-g41378f8 From 79f58cd7fd796a2c1f158967ae0a2530c7e80a0a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 31 Dec 2020 18:02:31 +0100 Subject: [PATCH 47/51] remove unneeded BOTADMIN TOKENFILE variables --- bashbot.sh | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 8d8010c..6fd05d8 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-38-g29f2ce8 +#### $$VERSION$$ v1.21-dev-42-g3407b05 ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -190,8 +190,6 @@ JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}" # file locations based on ENVIRONMENT BOTCONFIG="${BASHBOT_ETC:-.}/botconfig" -TOKENFILE="${BASHBOT_ETC:-.}/token" -BOTADMIN="${BASHBOT_ETC:-.}/botadmin" BOTACL="${BASHBOT_ETC:-.}/botacl" DATADIR="${BASHBOT_VAR:-.}/data-bot-bash" BLOCKEDFILE="${BASHBOT_VAR:-.}/blocked" @@ -312,7 +310,7 @@ GETFILE_URL=$URL'/getFile' ################# # BASHBOT COMMON functions -declare -rx SCRIPT SCRIPTDIR MODULEDIR RUNDIR ADDONDIR TOKENFILE BOTADMIN BOTACL DATADIR COUNTFILE +declare -rx SCRIPT SCRIPTDIR MODULEDIR RUNDIR ADDONDIR BOTACL DATADIR COUNTFILE declare -rx BOTTOKEN URL ME_URL UPD_URL GETFILE_URL declare -ax CMD @@ -1134,8 +1132,8 @@ bot_init() { [ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc" chmod 711 . chmod -R o-w ./* - chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTADMIN}" logs "${LOGDIR}/"*.log 2>/dev/null - chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null + chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" logs "${LOGDIR}/"*.log 2>/dev/null + chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTACL}" 2>/dev/null # jsshDB must writeable by owner find . -name '*.jssh*' -exec chmod u+w \{\} + chown -R "$TOUSER" . ./* From b04ee84b1d51211ab6c51a63e2b9fac3ea33d90e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 31 Dec 2020 18:11:37 +0100 Subject: [PATCH 48/51] remove unneeded variable ADIM --- bashbot.sh | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 6fd05d8..eff430e 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,7 +26,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-dev-42-g3407b05 +#### $$VERSION$$ v1.21-dev-43-g79f58cd ################################################################## # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059 @@ -1023,7 +1023,7 @@ process_message() { declare -A BASHBOTBLOCKED export BASHBOT_UPDATELOG="${BASHBOT_UPDATELOG-nolog}" # allow to be "" start_bot() { - local DEBUGMSG ADMIN OFFSET=0 + local DEBUGMSG OFFSET=0 # adaptive sleep defaults local nextsleep="100" local stepsleep="${BASHBOT_SLEEP_STEP:-100}" @@ -1062,8 +1062,7 @@ start_bot() { # read blocked users jssh_readDB_async "BASHBOTBLOCKED" "${BLOCKEDFILE}" # inform botadmin about start - ADMIN="$(getConfigKey "botadmin")" - [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") started ..." & + send_normal_message "$(getConfigKey "botadmin")" "Bot $(getConfigKey "botname") started ..." & ########## # bot is ready, start processing updates ... while true; do @@ -1284,8 +1283,7 @@ if [ -z "${SOURCE}" ]; then # shellcheck disable=SC2086 if kill ${BOTPID}; then # inform botadmin about stop - ADMIN="$(getConfigKey "botadmin")" - [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot ${ME} stopped ..." & + send_normal_message "$(getConfigKey "botadmin")" "Bot ${ME} stopped ..." & printf "${GREEN}OK. Bot stopped successfully.${NN}" else printf "${RED}An error occurred while stopping bot.${NN}" From ddb2785f8b3368277afcca285158ced923c9be78 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 31 Dec 2020 18:20:32 +0100 Subject: [PATCH 49/51] remove use of BOTDAMIN to et bot admin --- addons/antiFlood.sh | 4 ++-- addons/example.sh | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/addons/antiFlood.sh b/addons/antiFlood.sh index 123bd18..0abdcb8 100644 --- a/addons/antiFlood.sh +++ b/addons/antiFlood.sh @@ -4,7 +4,7 @@ # this addon counts how many files, e.g. stickers, are sent to # a chat and takes actions if threshold is reached # -#### $$VERSION$$ v1.20-0-g2ab00a2 +#### $$VERSION$$ v1.21-dev-44-gb04ee84 # used events: # @@ -44,7 +44,7 @@ fi # register on startbot if [[ "$1" = "start"* ]]; then - ANTIFL_ADMIN="$(< "${BOTADMIN}")" + ANTIFL_ADMIN="$(getConfigKey "botadmin")" #load existing chat settings on start jssh_readDB "ANTIFL_CHATS" "addons/$ANTIFL_ME" diff --git a/addons/example.sh b/addons/example.sh index a1e2632..01ab6da 100644 --- a/addons/example.sh +++ b/addons/example.sh @@ -4,7 +4,7 @@ # Addons can register to bashbot events at startup # by providing their name and a callback per event # -#### $$VERSION$$ v1.21-dev-1-g03bdecb +#### $$VERSION$$ v1.21-dev-44-gb04ee84 # # If an event occurs each registered event function is called. # @@ -60,6 +60,7 @@ fi if [[ "$1" = "start"* ]]; then # register to reply BASHBOT_EVENT_REPLY["${EXAMPLE_ME}"]="${EXAMPLE_ME}_reply" + EXAMPLE_ADMIN="$(getConfigKey "botadmin")" # any function defined by addons MUST be prefixed by addon name # function local variables can have any name, but must be LOCAL @@ -87,7 +88,7 @@ if [[ "$1" = "start"* ]]; then # any function defined by addons MUST be prefixed by addon name # function local variables can have any name, but must be LOCAL example_after5min(){ - send_markdown_message "$(< "${BOTADMIN}")" "This is a one time event after 5 Minutes!" & + send_markdown_message "${EXAMPLE_ADMIN}" "This is a one time event after 5 Minutes!" & } BASHBOT_EVENT_TIMER["${EXAMPLE_ME}every2min","2"]="${EXAMPLE_ME}_every2min" @@ -95,7 +96,7 @@ if [[ "$1" = "start"* ]]; then # any function defined by addons MUST be prefixed by addon name # function local variables can have any name, but must be LOCAL example_every2min(){ - send_markdown_message "$(< "${BOTADMIN}")" "This a a every 2 minute event ..." & + send_markdown_message "${EXAMPLE_ADMIN}" "This a a every 2 minute event ..." & } # register to send From d13e95ada9ef832c67128528d0e476476061bdc9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 31 Dec 2020 22:43:33 +0100 Subject: [PATCH 50/51] reset ENV before tests --- dev/all-tests.sh | 6 +++--- test/ALL-tests.inc.sh | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/dev/all-tests.sh b/dev/all-tests.sh index cb0e50e..6d404fa 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -5,7 +5,7 @@ # # Description: run all tests, exit after failed test # -#### $$VERSION$$ v1.21-dev-25-gd496639 +#### $$VERSION$$ v1.21-dev-45-gddb2785 ############################################################# # magic to ensure that we're always inside the root of our application, @@ -67,8 +67,8 @@ if [ "$fail" -eq 0 ]; then else /bin/echo -n 'FAILURE ' exitcode=1 - rm -rf "${TESTENV}/test" - find "${TESTENV}/"* ! -name '[a-z]-*' -delete + #rm -rf "${TESTENV}/test" + #find "${TESTENV}/"* ! -name '[a-z]-*' -delete fi ######################### diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index cc540c0..fb7e88b 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v1.21-dev-26-gbe87ba3 +#### $$VERSION$$ v1.21-dev-45-gddb2785 # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME @@ -33,9 +33,13 @@ export INPUTFILE REFFILE INPUTFILE2 REFFILE2 OUTPUTFILE INPUTFILE2="${DIRME}/${REFDIR}/${REFDIR}2.input" REFFILE2="${DIRME}/${REFDIR}/${REFDIR}2.result" -# do not query telegram when testing -export BASHBOT_URL TESTTOKEN BOTTOKEN +# reset ENVIRONMENT +export BASHBOT_URL TESTTOKEN BOTTOKEN BASHBOT_HOME BASHBOT_VAR BASHBOT_ETC BOTTOKEN="" +BASHBOT_HOME="" +BASHBOT_VAR="" +BASHBOT_ETC="" +# do not query telegram when testing BASHBOT_URL="https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?" TESTTOKEN="123456789:BASHBOTTESTSCRIPTbashbottestscript_" From cb8ae247568732c53c10c6a03d5af951b95ade94 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 31 Dec 2020 22:56:58 +0100 Subject: [PATCH 51/51] cleanup test dir --- dev/all-tests.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dev/all-tests.sh b/dev/all-tests.sh index 6d404fa..7d74588 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -5,7 +5,7 @@ # # Description: run all tests, exit after failed test # -#### $$VERSION$$ v1.21-dev-45-gddb2785 +#### $$VERSION$$ v1.21-dev-46-gd13e95a ############################################################# # magic to ensure that we're always inside the root of our application, @@ -67,8 +67,8 @@ if [ "$fail" -eq 0 ]; then else /bin/echo -n 'FAILURE ' exitcode=1 - #rm -rf "${TESTENV}/test" - #find "${TESTENV}/"* ! -name '[a-z]-*' -delete + rm -rf "${TESTENV}/test" + find "${TESTENV}/"* ! -name '[a-z]-*' -delete fi #########################