From 473d802aab2183c3ad48cbe3b827d5a95302b753 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Feb 2021 10:20:45 +0100 Subject: [PATCH 01/53] fix checkUploadFile --- bashbot.sh | 7 ++++--- dev/all-tests.sh | 2 +- test/d-send_message-test.sh | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 3633fd4..5c2ab62 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-26-g82a57a7 +#### $$VERSION$$ v1.45-dev-34-gb1e6e0f ################################################################## # are we running in a terminal? @@ -515,8 +515,8 @@ UPLOADDIR="${BASHBOT_UPLOAD:-${DATADIR}/upload}" # return final file name or empty string on error checkUploadFile() { local err file="$2" - [[ "${file}" = *'..'* || "${file}" = '.'* ]] && err=1 # no directory traversal - if [[ "${file}" = '/'* ]] ; then + [[ "${file}" == *'..'* || "${file}" == '.'* ]] && err=1 # no directory traversal + if [[ "${file}" == '/'* ]] ; then [[ ! "${file}" =~ ${FILE_REGEX} ]] && err=2 # absolute must match REGEX else file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # others must be in UPLOADDIR @@ -537,6 +537,7 @@ checkUploadFile() { [ -n "${BASHBOTDEBUG}" ] && log_debug "$3: CHAT=$1 FILE=$2 MSG=${BOTSENT[DESCRIPTION]}" return 1 fi + printf "%s\n" "${file}" } diff --git a/dev/all-tests.sh b/dev/all-tests.sh index 8151a10..92732e2 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.45-dev-21-ge67e43d +#### $$VERSION$$ v1.45-dev-34-gb1e6e0f ############################################################# #shellcheck disable=SC1090 diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index e55b96b..c9ea4ff 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-34-gb1e6e0f #=============================================================================== # include common functions and definitions From f7897fd41be8cf6cc7f70fc5f9acc75d332066f8 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Feb 2021 12:29:41 +0100 Subject: [PATCH 02/53] modules: sendMessage: delete_message: return chat_id also --- modules/sendMessage.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 246ecb4..e0ea811 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.45-dev-24-g785e769 +#### $$VERSION$$ v1.45-dev-35-g473d802 # will be automatically sourced from bashbot @@ -102,6 +102,7 @@ edit_message_caption() { delete_message() { [ -z "$3" ] && log_update "Delete Message CHAT=$1 MSG_ID=$2" sendJson "$1" '"message_id": '"$2"'' "${URL}/deleteMessage" + [ "${BOTSENT[OK]}" = "true" ] && BOTSENT[CHAT]="$1" # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[DESCRIPTION]}" "$2" "$3" } From fdbfcebc7ccbe6ec874f26a0cccc84045e953a26 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Feb 2021 12:54:00 +0100 Subject: [PATCH 03/53] doc: backticks, new set_chat_xxx functions --- README.html | 4 ++-- README.md | 7 ++++--- README.txt | 11 ++++++----- doc/6_reference.md | 18 +++++++++++++++++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/README.html b/README.html index 11c7c50..aeaad79 100644 --- a/README.html +++ b/README.html @@ -341,7 +341,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 bulletproof, 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 appropriately, disable globbing with set -f and quote everything). In addition remove unused scripts and examples from your Bot (e.g. everything in example/) and disable/remove all unused 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 is variable and command substitution using ${} and $() 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 /*)).

+

It's important to escape or remove $ and \`` in input from user, files or network (_as bashbot does_). One of the powerful features of Unix shells is variable and command substitution using ${var}, $(cmd)and`cmd`can lead to remote code execution (RCE) or remote information disclosure (RID) bugs if unescaped$or `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 that accepts external input (e.g. from the user as arguments or the file system), you shouldn't use echo to display it. Use printf whenever possible.

@@ -392,6 +392,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.41-0-gad1b91f

+

$$VERSION$$ v1.45-dev-36-gf7897fd

diff --git a/README.md b/README.md index 7756d42..0201811 100644 --- a/README.md +++ b/README.md @@ -146,8 +146,9 @@ Whenever you are processing input from untrusted sources (messages, files, netwo (e.g. set IFS appropriately, disable globbing with `set -f` and quote everything). In addition remove unused scripts and examples from your Bot (e.g. everything in `example/`) and disable/remove all unused 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 is variable and command substitution using `${}` and `$()` 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 /*)`). +It's important to escape or remove `$` and `\`` in input from user, files or network (_as bashbot does_). +One of the powerful features of Unix shells is variable and command substitution using `${var}`, `$(cmd)` and `\`cmd\`` can lead to remote +code execution (RCE) or remote information disclosure (RID) bugs if unescaped `$` or ` \`` 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 @@ -241,4 +242,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.41-0-gad1b91f +#### $$VERSION$$ v1.45-dev-36-gf7897fd diff --git a/README.txt b/README.txt index dabcc49..f5744f2 100644 --- a/README.txt +++ b/README.txt @@ -200,10 +200,11 @@ Whenever you are processing input from untrusted sources (messages, files, netwo must be as careful as possible (e.g. set IFS appropriately, disable globbing with set - f and quote everything). In addition remove unused scripts and examples from your Bot (e.g. everything in example/) and disable/remove all unused 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 is variable and command substitution -using ${} and $() 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 /*)). +It's important to escape or remove $ and \`` in input from user, files or network (_as +bashbot does_). One of the powerful features of Unix shells is variable and command +substitution using${var},$(cmd)and`cmd`can lead to remote code execution (RCE) or remote +information disclosure (RID) bugs if unescaped$or `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 @@ -318,5 +319,5 @@ 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.41-0-gad1b91f +$$VERSION$$ v1.45-dev-36-gf7897fd diff --git a/doc/6_reference.md b/doc/6_reference.md index 3b68eef..25b605b 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -651,6 +651,15 @@ with description "Bad Request: chat description is not modified" *usage:* set_chat_description "CHAT[ID]" "new chat description" +##### set_chat_photo +`set_chat_photo` sets a new profile photo for the chat, can't be changed for private chat. +Photo must be a local image file in a supported format (_.jpg, .jpeg, .png, .gif, .bmp, .tiff_) + +Same location and naming restrictions as with `send_file` apply. + +*usage:* set_chat_photo "CHAT[ID]" "file" + + ##### new_chat_invite `new_chat_invite` generate a new invite link for a chat; any previously generated link is revoked. Returns the new invite link as String on success. @@ -687,6 +696,13 @@ Returns the new invite link as String on success. *usage:* delete_chat_stickers "CHAT[ID]" +##### set_chatadmin_title +`set_chatadmin_title` set a custom title for an administrator in a supergroup promoted by the bot. + Admin title can be 0-16 characters long, emoji are not allowed. + +*usage:* set_chatadmin_title "CHAT[ID]" "USER[ID]" "admin title" + + ---- ### User Access Control @@ -1641,5 +1657,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.45-dev-9-g62b6b61 +#### $$VERSION$$ v1.45-dev-36-gf7897fd From 882efa8f1ab6d6e548d90b27ad0815f97e61453b Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Feb 2021 13:00:53 +0100 Subject: [PATCH 04/53] doc: fix backticks --- README.html | 4 ++-- README.md | 8 ++++---- README.txt | 12 ++++++------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.html b/README.html index aeaad79..f5cd18f 100644 --- a/README.html +++ b/README.html @@ -341,7 +341,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 bulletproof, 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 appropriately, disable globbing with set -f and quote everything). In addition remove unused scripts and examples from your Bot (e.g. everything in example/) and disable/remove all unused bot commands.

-

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

+

It's important to escape or remove $ and ` in input from user, files or network (as bashbot does). One of the powerful features of Unix shells is variable and command substitution using ${var}, $(cmd) and `cmd` can lead to remote code execution (RCE) or remote information disclosure (RID) bugs if unescaped $ or ` 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 that accepts external input (e.g. from the user as arguments or the file system), you shouldn't use echo to display it. Use printf whenever possible.

@@ -392,6 +392,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.45-dev-36-gf7897fd

+

$$VERSION$$ v1.45-dev-37-gfdbfceb

diff --git a/README.md b/README.md index 0201811..fc7cb94 100644 --- a/README.md +++ b/README.md @@ -146,9 +146,9 @@ Whenever you are processing input from untrusted sources (messages, files, netwo (e.g. set IFS appropriately, disable globbing with `set -f` and quote everything). In addition remove unused scripts and examples from your Bot (e.g. everything in `example/`) and disable/remove all unused bot commands. -It's important to escape or remove `$` and `\`` in input from user, files or network (_as bashbot does_). -One of the powerful features of Unix shells is variable and command substitution using `${var}`, `$(cmd)` and `\`cmd\`` can lead to remote -code execution (RCE) or remote information disclosure (RID) bugs if unescaped `$` or ` \`` is included in untrusted input (e.g. `$$` or `$(rm -rf /*)`). +It's important to escape or remove `$` and \` in input from user, files or network (_as bashbot does_). +One of the powerful features of Unix shells is variable and command substitution using `${var}`, `$(cmd)` and \`cmd\` can lead to remote +code execution (RCE) or remote information disclosure (RID) bugs if unescaped `$` or \` 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 @@ -242,4 +242,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.45-dev-36-gf7897fd +#### $$VERSION$$ v1.45-dev-37-gfdbfceb diff --git a/README.txt b/README.txt index f5744f2..54784c4 100644 --- a/README.txt +++ b/README.txt @@ -200,11 +200,11 @@ Whenever you are processing input from untrusted sources (messages, files, netwo must be as careful as possible (e.g. set IFS appropriately, disable globbing with set - f and quote everything). In addition remove unused scripts and examples from your Bot (e.g. everything in example/) and disable/remove all unused bot commands. -It's important to escape or remove $ and \`` in input from user, files or network (_as -bashbot does_). One of the powerful features of Unix shells is variable and command -substitution using${var},$(cmd)and`cmd`can lead to remote code execution (RCE) or remote -information disclosure (RID) bugs if unescaped$or `is included in untrusted input -(e.g.$$or$(rm -rf /*)`). +It's important to escape or remove $ and ` in input from user, files or network (as +bashbot does). One of the powerful features of Unix shells is variable and command +substitution using ${var}, $(cmd) and `cmd` can lead to remote code execution (RCE) or +remote information disclosure (RID) bugs if unescaped $ or ` 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 @@ -319,5 +319,5 @@ 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.45-dev-36-gf7897fd +$$VERSION$$ v1.45-dev-37-gfdbfceb From 8b18f25c8800a133e5f2471b5dc4f365b6bf78ab Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Feb 2021 18:59:27 +0100 Subject: [PATCH 05/53] bin: process_updates: use process_multi for preprocessing --- bin/process_update.sh | 12 +++--------- modules/processUpdates.sh | 10 ++++++++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/bin/process_update.sh b/bin/process_update.sh index 98cb1f9..ec36da8 100755 --- a/bin/process_update.sh +++ b/bin/process_update.sh @@ -15,12 +15,12 @@ USAGE='process_update.sh [-h|--help] [debug] [/dev/null)" -# escape bash $ expansion bug -UPDATE="${UPDATE//$/\\$}" - -# assign to bashbot ARRAY -Json2Array 'UPD' <<<"${UPDATE}" - # process telegram update -"${COMMAND}" "0" "$1" +"${COMMAND}" "$1" diff --git a/modules/processUpdates.sh b/modules/processUpdates.sh index a7aa58c..9638740 100644 --- a/modules/processUpdates.sh +++ b/modules/processUpdates.sh @@ -4,7 +4,7 @@ # File: processUpdates.sh # Note: DO NOT EDIT! this file will be overwritten on update # -#### $$VERSION$$ v1.45-dev-30-g8efbfca +#### $$VERSION$$ v1.45-dev-38-g882efa8 ################################################################## ############## @@ -43,18 +43,24 @@ delete_webhook() { } ################ -# processing of updates starts here +# processing of array of updates starts here process_multi_updates() { local max num debug="$1" + # get num array elements max="$(grep -F ',"update_id"]' <<< "${UPDATE}" | tail -1 | cut -d , -f 2 )" # escape bash $ expansion bug UPDATE="${UPDATE//$/\\$}" + # convert updates to bash array Json2Array 'UPD' <<<"${UPDATE}" + # iterate over array for ((num=0; num<=max; num++)); do process_update "${num}" "${debug}" done } +################ +# processing of a single array item of update +# $1 array index process_update() { local num="$1" debug="$2" pre_process_message "${num}" From 70724427d953d139a784000ab519a8370471620e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 20 Feb 2021 08:34:42 +0100 Subject: [PATCH 06/53] webhook: stay with simple mode --- examples/webhook/README.md | 52 +++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/examples/webhook/README.md b/examples/webhook/README.md index 7a55688..b07a47d 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -4,9 +4,12 @@ ### Webhooks -Bashbot default mode is to poll Telegram server for updates but Telegram offers also webhook +Bashbot default mode is to poll Telegram server for updates but Telegram offers webhook as a more efficient method to deliver updates. -If your server is reachable from the Internet you can use the webhook method described here. +If your server is reachable from the Internet you can use the methods described here. + +You need a valid SSL certificate or Telegram will refuse to deliever update via webhook. +A self signed certificate will not be sufficient. #### Setup Apache webhook @@ -31,33 +34,20 @@ will append `{"test":"me"}` to the file `data-bot-bash/webhook-fifo-`. Now your Apache is ready to forward data to Bashbot. -#### Simple update processing +#### Enable update processing for Bashbot -To configure `Simple update processing` delete the file `data-bot-bash/webhook-fifo-` after your webhook is working. +To enable update processing delete the file `data-bot-bash/webhook-fifo-` after your webhook is working manually. All webhook calls are now forwarded to `bin/process_update.sh` for processing. -To start `Simple processing ` enable webhook on Telegram (_see below_). - Every incoming Telegram update load Bashbot once for processing one command. Even it seems overkill to load -Bashbot on every incoming update, it's more responsive and create less server load for low traffic bots. +Bashbot on every incoming update, it's more responsive and create less server load than polling Telegram -If your bot uses `addons` or `BASHBOT_EVENTs` you can't use `Simple processing`. +If your bot uses `addons` or `BASHBOT_EVENTs` you can't use webhooks as described here. -*Note:* `Simple processing` works without running `bashbot.sh start`. +*Note:* webhook work without running `bashbot.sh start`. -#### High traffic processing - -#### CURRENTLY NOT IMPLEMENTED - -High traffic processing writes Telegram updates to the named pipe `data-bot-bash/webhook-fifo` -and Bashbot poll them, this is much more efficient than polling Telegram server. - -To switch from `Simple processing` to `High traffic processing` start bashbot as `bashbot.sh start-webhook`. -Stop bashbot with `bashbot.sh stop` to switch back to `Simple processing` - - -#### Enable webhook on Telegram +#### Enable webhook on Telegram side To get updates via webhook your server must be reachable from the internet and you must instruct Telegram where to deliver updates, this is done by calling bashbot function `set_webhook`. @@ -73,8 +63,24 @@ After you enable webhook to deliver Telegram updates it's no more possible to po To stop delivering of Telegram updates via webhook run `bin/any_command.sh delete_webhook`. -**Important**: Only https connections with a valid certificate chain are allowed as endpoint for webhook. +**Important**: Telegram will refuse to deliver updates if your webhook has no valid SSL certificate chain. -#### $$VERSION$$ v1.45-dev-28-g9958b5b +#### Bash webhook + +A pure bash webhook implementaition is not possible without additional software because Telegram deliver +updates only over secure TLS connections and if a valid SSL certificate chain exists. + +`socat` looks like a tool we can use to listen for Telegram updates from bash scripts, let's see ... + + +#### High traffic processing? + +Initially I planned to implement a mode for `High traffic update processing` where Bashbot is started once +and read updates from the named pipe `data-bot-bash/webhook-fifo-`, similar like polling from Telegram. + +But the default webhook method is so convincing and responsive that a special high traffic mode is not necessary. + + +#### $$VERSION$$ v1.45-dev-39-g8b18f25 From 192fae8a3774ec4e9ed1fe96a994445a20fe5ac4 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 20 Feb 2021 16:48:56 +0100 Subject: [PATCH 07/53] modules: jsshDB: do not save unset marked values --- modules/jsonDB.sh | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 7bc2ddf..94d913b 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.45-dev-30-g8efbfca +#### $$VERSION$$ v1.45-dev-40-g7072442 # # source from commands.sh to use jsonDB functions # @@ -25,7 +25,9 @@ eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" # tinybox # lockfile filename.flock is persistent and will be testet with flock for active lock (file open) -export JSSH_LOCKNAME=".flock" +export JSSHDB_LOCKNAME=".flock" +# an array value containing this string will not saveed to DB (unset) +export JSSHDB_UNSET="99999999999999999999_JSSHDB_UNSET_99999999999999999999" # in UTF-8 äöü etc. are part of [:alnum:] and ranges (e.g. a-z), but we want ASCII a-z ranges! # for more information see doc/4_expert.md#Character_classes @@ -64,7 +66,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 # shared lock, many processes can read, max wait 1s - { flock -s -w 1 200; Json2Array "$1" <"${DB}"; } 200>"${DB}${JSSH_LOCKNAME}" + { flock -s -w 1 200; Json2Array "$1" <"${DB}"; } 200>"${DB}${JSSHDB_LOCKNAME}" } # write ARRAY content to a file in JSON.sh format @@ -76,7 +78,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 # exclusive lock, no other process can read or write, maximum wait to get lock is 10s - { flock -e -w 10 200; Array2Json "$1" >"${DB}"; } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 10 200; Array2Json "$1" >"${DB}"; } 200>"${DB}${JSSHDB_LOCKNAME}" } # update/write ARRAY content in file without deleting keys not in ARRAY @@ -88,7 +90,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then [ -z "$2" ] && return 1 local DB="$2.jssh" # check in async [ ! -f "${DB}" ] && return 2 - { flock -e -w 10 200; jssh_updateDB_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 10 200; jssh_updateDB_async "$@"; } 200>"${DB}${JSSHDB_LOCKNAME}" } # insert, update, apped key/value to jsshDB @@ -106,7 +108,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then { flock -e -w 2 200 # it's append, but last one counts, its a simple DB ... printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${DB}" - } 200>"${DB}${JSSH_LOCKNAME}" + } 200>"${DB}${JSSHDB_LOCKNAME}" } @@ -119,7 +121,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then [[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3 local DB="$2.jssh" # start atomic delete here, exclusive max wait 10s - { flock -e -w 10 200; jssh_deleteKeyDB_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 10 200; jssh_deleteKeyDB_async "$@"; } 200>"${DB}${JSSHDB_LOCKNAME}" } # get key/value from jsshDB @@ -133,7 +135,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then # start atomic delete here, exclusive max wait 1s { flock -s -w 1 200 [ -r "${DB}" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' "${DB}" | tail -n 1 - } 200>"${DB}${JSSH_LOCKNAME}" + } 200>"${DB}${JSSHDB_LOCKNAME}" } @@ -148,7 +150,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then [[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3 local DB="$2.jssh" # start atomic delete here, exclusive max wait 5 - { flock -e -w 5 200; jssh_countKeyDB_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 5 200; jssh_countKeyDB_async "$@"; } 200>"${DB}${JSSHDB_LOCKNAME}" } # update key/value in place to jsshDB @@ -169,7 +171,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then jssh_clearDB() { local DB; DB="$(jssh_checkDB "$1")" [ -z "${DB}" ] && return 1 - { flock -e -w 10 200; printf '' >"${DB}"; } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 10 200; printf '' >"${DB}"; } 200>"${DB}${JSSHDB_LOCKNAME}" } # updates Array if DB file has changed since last call @@ -367,7 +369,7 @@ Array2Json() { declare -n ARRAY="$1" for key in "${!ARRAY[@]}" do - [[ "${key}" =~ ^${JSSH_KEYOK}+$ ]] || continue + [[ ! "${key}" =~ ^${JSSH_KEYOK}+$ || "${ARRAY[${key}]}" = "${JSSHDB_UNSET}" ]] && continue # in case value contains newline convert to \n : "${ARRAY[${key}]//$'\n'/\\n}" printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${_//\"/\\\"}" From 1a0642bcce3d1210945ede0b59c6b41df84b780d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 21 Feb 2021 07:45:21 +0100 Subject: [PATCH 08/53] doc: JSSHDB_UNSET value --- doc/6_reference.md | 37 ++++++++++++++++++++++++++++--------- modules/jsonDB.sh | 4 ++-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 25b605b..3981278 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -1042,9 +1042,14 @@ ARRAY["key"]="value" ARRAY["key,subkey"]="value2" ``` -For keys the following charatcsers are allowed: `a-z A-Z 0-9 _ .`, multiple keys must be separated by `,`. +Only the following characters are allowed: `a-z A-Z 0-9 _ .` for keys, multiple keys must be separated by `,`. Keys contaiing other characters will be discarded when written to a file. +To delete (unset) a key/value pair in memory you can `unset ARRAY["abc"]` but this will not delete key/value +piars when using `jssh_updateDB`. Therefore the special value `${JSSHDB_UNSET}` exists to delete a key/value pair +when updateting a file, see `jssh_updateDB` + + ```bash ARRAY["abc"]="abc" # OK ARRAY["abx###"]="abc" # works in bash but will not saved to file @@ -1057,7 +1062,6 @@ cat file.jssh ``` -*Hint*: Try `tr -dc "[:alnum:],.\r\n"` to strip invalid characters from key. ```bash # strip key containing invalid characters KEY="123abcABC,.#?(<>123ÄÖ*%&§" @@ -1201,6 +1205,9 @@ Note: Existing content not in ARRAY is kept in file. *usage:* jssh_updateDB_async "ARRAY" "filename" +*Note:* `jssh_updateDB` updates new or changed keys/value pairs only, it will not delete an existing key/value pair. +If you want to delete a existing key/value pair assign the unset value `${JSSJDB__UNSET}`. + *example:* ```bash # continued example from writeDB @@ -1211,18 +1218,30 @@ MYVALUES["newvalue"]="this is new" jssh_updateDB "MYVALUES" "${DATADIR:-.}/myvalues" # show what's written +cat ${DATADIR:-.}/myvalues".jssh ["value1"] "value1" ["loveit"] "value2" ["whynot"] "value3" ["newvalue"] "this is new" -# now writeDB -cat "$DBfile" -jssh_writeDB "MYVALUES" "${DATADIR:-.}/myvalues" +####### +# update does not delete key/value pairs +# uset in bash and update file +unset MYVALUES["newvalue"] +jssh_updateDB "MYVALUES" "${DATADIR:-.}/myvalues" -# show what's written, ups! -cat "$DBfile" -["newvalue"] "this is new" +["value1"] "value1" +["loveit"] "value2" +["whynot"] "value3" +["newvalue"] "this is new" # value exists! + +# use JSSHDB_UNSET value +MYVALUES["newvalue"]="${JSSHDB_UNSET}" +jssh_updateDB "MYVALUES" "${DATADIR:-.}/myvalues" + +["value1"] "value1" +["loveit"] "value2" +["whynot"] "value3" ``` @@ -1657,5 +1676,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.45-dev-36-gf7897fd +#### $$VERSION$$ v1.45-dev-41-g192fae8 diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 94d913b..c9f1657 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.45-dev-40-g7072442 +#### $$VERSION$$ v1.45-dev-41-g192fae8 # # source from commands.sh to use jsonDB functions # @@ -369,7 +369,7 @@ Array2Json() { declare -n ARRAY="$1" for key in "${!ARRAY[@]}" do - [[ ! "${key}" =~ ^${JSSH_KEYOK}+$ || "${ARRAY[${key}]}" = "${JSSHDB_UNSET}" ]] && continue + [[ ! "${key}" =~ ^${JSSH_KEYOK}+$ || "${ARRAY[${key}]}" == "${JSSHDB_UNSET}" ]] && continue # in case value contains newline convert to \n : "${ARRAY[${key}]//$'\n'/\\n}" printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${_//\"/\\\"}" From ac71000519c15c8b01997e5801df4040ff0dab97 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 21 Feb 2021 07:51:56 +0100 Subject: [PATCH 09/53] doc: small fixes --- doc/6_reference.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 3981278..8dac4c2 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -1042,12 +1042,11 @@ ARRAY["key"]="value" ARRAY["key,subkey"]="value2" ``` -Only the following characters are allowed: `a-z A-Z 0-9 _ .` for keys, multiple keys must be separated by `,`. +Only the following characters are allowed for keys: `a-z A-Z 0-9 _ .`, multiple keys must be separated by `,`. Keys contaiing other characters will be discarded when written to a file. -To delete (unset) a key/value pair in memory you can `unset ARRAY["abc"]` but this will not delete key/value -piars when using `jssh_updateDB`. Therefore the special value `${JSSHDB_UNSET}` exists to delete a key/value pair -when updateting a file, see `jssh_updateDB` +To delete (unset) a key/value pair in memory you can `unset ARRAY["abc"]` but this will not delete the key/value +pair when using `jssh_updateDB` to update a file. Therefore the special value `${JSSHDB_UNSET}` exists, see `jssh_updateDB` ```bash @@ -1205,8 +1204,8 @@ Note: Existing content not in ARRAY is kept in file. *usage:* jssh_updateDB_async "ARRAY" "filename" -*Note:* `jssh_updateDB` updates new or changed keys/value pairs only, it will not delete an existing key/value pair. -If you want to delete a existing key/value pair assign the unset value `${JSSJDB__UNSET}`. +`jssh_updateDB` update new or changed keys/value pairs only, it will not delete an existing key/value pair. +To delete an existing key/value pair you must assign the "unset value" `${JSSJDB_UNSET}` to it instead. *example:* ```bash @@ -1676,5 +1675,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.45-dev-41-g192fae8 +#### $$VERSION$$ v1.45-dev-42-g1a0642b From 17efeeb903dae48ff00597495ca5a6f526a2a3f1 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 21 Feb 2021 08:16:33 +0100 Subject: [PATCH 10/53] doc: rephrase jsshDB intro --- doc/6_reference.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 8dac4c2..1c3623e 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -1007,11 +1007,11 @@ Usually a message is automatically forwarded from within `commands.sh`, but you ### jsshDB -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. +Output generated by `JSON.sh` can easily converted to bash associative arrays. Therefore Bashbot use this format for key/value file store too. #### fast and slow operations -jsshDB files are flat text files containing key/value pairs in the `JSON.sh` format. +jsshDB files are flat text files containing key/value pairs in `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. @@ -1138,7 +1138,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. -"DB" file MUST exist or nothing is written. +if "DB" file does not exist nothing is written. Note: Existing content is overwritten. @@ -1195,10 +1195,8 @@ 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. -"DB" file MUST exist or nothing is written. - -Note: Existing content not in ARRAY is kept in file. +`jssh_updateDB updates key/value pairs of an ARRAY in a jsshDB file. ARRAY name must be declared with `declare -A ARRAY` before calling updateDB. +if "DB" file does not exist nothing is written. *usage:* jssh_updateDB "ARRAY" "filename" @@ -1675,5 +1673,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.45-dev-42-g1a0642b +#### $$VERSION$$ v1.45-dev-43-gac71000 From 6b8ad9783a534975b22fe037efa60676d8b4b35d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 21 Feb 2021 23:34:34 +0100 Subject: [PATCH 11/53] use cleanEscape for more logging --- bashbot.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 5c2ab62..d3cd695 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-34-gb1e6e0f +#### $$VERSION$$ v1.45-dev-44-g17efeeb ################################################################## # are we running in a terminal? @@ -106,7 +106,7 @@ JsonEscape(){ } # clean \ from escaped json string # $1 string, output cleaned string -cleanEscaped(){ # remove " all \ but \n\u \n or \r +cleanEscape(){ # remove " all \ but \n\u \n or \r sed -E -e 's/\\"/+/g' -e 's/\\([^nu])/\1/g' -e 's/(\r|\n)//g' <<<"$1" } # check if $1 seems a valid token @@ -491,9 +491,8 @@ sendJson(){ # compose final json json='{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<"$2")"'}' if [ -n "${BASHBOTDEBUG}" ] ; then - log_update "sendJson (${DETECTED_CURL}) CHAT=${chat#*:} JSON=${2:0:100} URL=${3##*/}" - # mask " and \ , remove newline from json - log_message "DEBUG sendJson ==========\n$("${JSONSHFILE}" -b -n <<<"$(cleanEscaped "${json}")" 2>&1)" + log_update "sendJson (${DETECTED_CURL}) CHAT=${chat#*:} JSON=$(cleanEscape "${json:0:100}") URL=${3##*/}" + log_message "DEBUG sendJson ==========\n$("${JSONSHFILE}" -b -n <<<"$(cleanEscape "${json}")" 2>&1)" fi # chat id not a number if [[ "${chat}" == *"NAN\"," ]]; then From c57e9273fbb4a0699fe4697e5d165ac2f205d150 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 22 Feb 2021 08:26:20 +0100 Subject: [PATCH 12/53] mycommnds: clearer message in errorProcessing examples --- mycommands.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mycommands.sh b/mycommands.sh index 0736616..bd541b4 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -13,7 +13,7 @@ # License: WTFPLv2 http://www.wtfpl.net/txt/copying/ # Author: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-27-g77ffbab +#### $$VERSION$$ v1.45-dev-45-g6b8ad97 ####################################################### # shellcheck disable=SC1117 @@ -317,13 +317,13 @@ else # called when delete Message failed # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args bashbotError_delete_message() { - log_debug "errorProcessing for delete_message failed: ERR=$2 CHAT=$3 MSGID=$6 ERTXT=$5" + log_debug "custom errorProcessing delete_message: ERR=$2 CHAT=$3 MSGID=$6 ERTXT=$5" } # called when error 403 is returned (and no func processing) # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args bashbotError_403() { - log_debug "errorProcessing for error 403 in FUNC=$1 CHAT=$3 USER=${4:-no-user} MSGID=$6 ERTXT=$5" + log_debug "custom errorProcessing error 403: FUNC=$1 CHAT=$3 USER=${4:-no-user} MSGID=$6 ERTXT=$5" } ########################### From f4323e48b5f13a0a71730372e5238b31b8574327 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 24 Feb 2021 16:33:03 +0100 Subject: [PATCH 13/53] doc: no jobs with webhook --- examples/webhook/README.md | 13 +++++++++---- modules/background.sh | 14 ++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/examples/webhook/README.md b/examples/webhook/README.md index b07a47d..c8467dd 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -34,7 +34,7 @@ will append `{"test":"me"}` to the file `data-bot-bash/webhook-fifo-`. Now your Apache is ready to forward data to Bashbot. -#### Enable update processing for Bashbot +#### Webhook update processing for Bashbot To enable update processing delete the file `data-bot-bash/webhook-fifo-` after your webhook is working manually. All webhook calls are now forwarded to `bin/process_update.sh` for processing. @@ -42,9 +42,14 @@ All webhook calls are now forwarded to `bin/process_update.sh` for processing. Every incoming Telegram update load Bashbot once for processing one command. Even it seems overkill to load Bashbot on every incoming update, it's more responsive and create less server load than polling Telegram -If your bot uses `addons` or `BASHBOT_EVENTs` you can't use webhooks as described here. -*Note:* webhook work without running `bashbot.sh start`. +This webhook works without running `bashbot.sh` and thus has the following limitations: + - no startup actions + - no backgroundi* and interactive jobs + - `addons` and `BASHBOT_EVENTs' are not working + +\* workaround for background jobs is to start them in the channel and execute `./bashbot.sh restartback` afterwards. + #### Enable webhook on Telegram side @@ -82,5 +87,5 @@ and read updates from the named pipe `data-bot-bash/webhook-fifo-`, sim But the default webhook method is so convincing and responsive that a special high traffic mode is not necessary. -#### $$VERSION$$ v1.45-dev-39-g8b18f25 +#### $$VERSION$$ v1.45-dev-46-gc57e927 diff --git a/modules/background.sh b/modules/background.sh index 74f5349..bfd0ec9 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117,SC2059 -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-46-gc57e927 # will be automatically sourced from bashbot @@ -46,6 +46,10 @@ start_back() { printf '%s\n' "$1:$3:$2" >"${cmdfile}" restart_back "$@" } +# $1 chatid +# $2 program +# $3 jobname +# $4 $5 parameters restart_back() { local fifo; fifo="${DATADIR:-.}/$(procname "$1" "back-$3-")" log_update "Start background job CHAT=$1 JOB=${fifo##*/} CMD=${2##*/} $4 $5" @@ -62,9 +66,9 @@ start_proc() { [ -z "$2" ] && return [ -x "${2%% *}" ] || return 1 local fifo; fifo="${DATADIR:-.}/$(procname "$1")" - log_update "Start interactive script CHAT=$1 JOB=${fifo##*/} CMD=$2 $3 $4" check_proc "$1" && kill_proc "$1" mkfifo "${fifo}" + log_update "Start interactive script CHAT=$1 JOB=${fifo##*/} CMD=$2 $3 $4" nohup bash -c "{ $2 \"$4\" \"$5\" \"${fifo}\" | \"${SCRIPT}\" outproc \"$1\" \"${fifo}\" rm \"${fifo}\"; [ -s \"${fifo}.log\" ] || rm -f \"${fifo}.log\"; }" &>>"${fifo}.log" & } @@ -99,9 +103,11 @@ kill_proc() { fifo="$(procname "$1" "$2")" prid="$(proclist "${fifo}")" fifo="${DATADIR:-.}/${fifo}" - log_update "Stop interactive / background CHAT=$1 JOB=${fifo##*/}" # shellcheck disable=SC2086 - [ -n "${prid}" ] && kill ${prid} + if [ -n "${prid}" ]; then + log_update "Stop interactive / background CHAT=$1 JOB=${fifo##*/}" + kill ${prid} + fi [ -s "${fifo}.log" ] || rm -f "${fifo}.log" [ -p "${fifo}" ] && rm -f "${fifo}"; } From f4d45d814f70f6b93eb483458bda63a6d67a8780 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 25 Feb 2021 19:20:09 +0100 Subject: [PATCH 14/53] example: webhook limitations --- examples/webhook/README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/webhook/README.md b/examples/webhook/README.md index c8467dd..0f45761 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -43,13 +43,14 @@ Every incoming Telegram update load Bashbot once for processing one command. Eve Bashbot on every incoming update, it's more responsive and create less server load than polling Telegram -This webhook works without running `bashbot.sh` and thus has the following limitations: +Webhook works without running Bashbot and thus has the following limitations: - no startup actions - - no backgroundi* and interactive jobs - - `addons` and `BASHBOT_EVENTs' are not working + - no background and interactive jobs + - `addons` and `TIMER_EVENTS` are not working -\* workaround for background jobs is to start them in the channel and execute `./bashbot.sh restartback` afterwards. +To run startup actions and `TIMER_EVENTS` run Bashbot with `./bashbot start` even not needed with webhook. +Workaround for running new background jobs is to execute `./bashbot.sh restartback` on the command line after starting a new background job. #### Enable webhook on Telegram side @@ -87,5 +88,5 @@ and read updates from the named pipe `data-bot-bash/webhook-fifo-`, sim But the default webhook method is so convincing and responsive that a special high traffic mode is not necessary. -#### $$VERSION$$ v1.45-dev-46-gc57e927 +#### $$VERSION$$ v1.45-dev-47-gf4323e4 From 213191162518638ab99883286a8e4bfa50a81788 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 25 Feb 2021 19:58:00 +0100 Subject: [PATCH 15/53] restart == resume --- bashbot.sh | 4 ++-- examples/webhook/README.md | 4 ++-- modules/background.sh | 13 +++++-------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index d3cd695..e704ac3 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-44-g17efeeb +#### $$VERSION$$ v1.45-dev-48-gf4d45d8 ################################################################## # are we running in a terminal? @@ -912,7 +912,7 @@ if [ -z "${SOURCE}" ]; then exit ;; # suspend, resume or kill background jobs - "suspendb"*|"resumeb"*|"killb"*) + "suspendb"*|"resumeb"*|'restartb'*|"killb"*) _is_function job_control || { printf "${RED}Module background is not available!${NN}"; exit 3; } ME="$(getConfigKey "botname")" job_control "$1" diff --git a/examples/webhook/README.md b/examples/webhook/README.md index 0f45761..7932357 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -50,7 +50,7 @@ Webhook works without running Bashbot and thus has the following limitations: To run startup actions and `TIMER_EVENTS` run Bashbot with `./bashbot start` even not needed with webhook. -Workaround for running new background jobs is to execute `./bashbot.sh restartback` on the command line after starting a new background job. +Workaround for running new background jobs is to execute `./bashbot.sh resumeback` on the command line after starting a new background job. #### Enable webhook on Telegram side @@ -88,5 +88,5 @@ and read updates from the named pipe `data-bot-bash/webhook-fifo-`, sim But the default webhook method is so convincing and responsive that a special high traffic mode is not necessary. -#### $$VERSION$$ v1.45-dev-47-gf4323e4 +#### $$VERSION$$ v1.45-dev-48-gf4d45d8 diff --git a/modules/background.sh b/modules/background.sh index bfd0ec9..c5720be 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117,SC2059 -#### $$VERSION$$ v1.45-dev-46-gc57e927 +#### $$VERSION$$ v1.45-dev-48-gf4d45d8 # will be automatically sourced from bashbot @@ -125,10 +125,7 @@ inproc() { } # start stop all jobs -# $1 command -# killb* -# suspendb* -# resumeb* +# $1 command # kill suspend resume restart job_control() { local BOT ADM content proc CHAT job fifo killall="" BOT="$(getConfigKey "botname")" @@ -144,20 +141,20 @@ job_control() { fifo="$(procname "${CHAT}" "${job}")" debug_checks "Execute job_control" "$1" "${FILE##*/}" case "$1" in - "resumeb"*|"backgr"*) + "resume"*|"restart"*) printf "Restart Job: %s %s\n" "${proc}" " ${fifo##*/}" restart_back "${CHAT}" "${proc}" "${job}" # inform botadmin about stop [ -n "${ADM}" ] && send_normal_message "${ADM}" "Bot ${BOT} restart background jobs ..." & ;; - "suspendb"*) + "suspend"*) printf "Suspend Job: %s %s\n" "${proc}" " ${fifo##*/}" kill_proc "${CHAT}" "${job}" # inform botadmin about stop [ -n "${ADM}" ] && send_normal_message "${ADM}" "Bot ${BOT} suspend background jobs ..." & killall="y" ;; - "killb"*) + "kill"*) printf "Kill Job: %s %s\n" "${proc}" " ${fifo##*/}" kill_proc "${CHAT}" "${job}" rm -f "${FILE}" # remove job From 34923ddabea4833edc0f24ebc4cb455a2d8efee0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 26 Feb 2021 06:45:50 +0100 Subject: [PATCH 16/53] MEONLY: fix and simplify --- commands.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/commands.sh b/commands.sh index f6d45a0..e390cd6 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.45-dev-9-g62b6b61 +#### $$VERSION$$ v1.45-dev-49-g2131911 # # bashbot locale defaults to c.UTF-8, adjust locale in mycommands.sh if needed @@ -96,10 +96,9 @@ if [ -z "$1" ] || [[ "$1" == *"debug"* ]];then ################### # if is bashbot is group admin it get commands sent to other bots # set MEONLY=1 to ignore commands for other bots - if [[ "${MEONLY}" != "0" && "${MESSAGE}" == "/"* && "${MESSAGE%% *}" == *"@"* ]]; then + if [[ "${MEONLY}" != "0" && "${MESSAGE}" == "/"*"@"* ]]; then # here we have a command with @xyz_bot added, check if it's our bot - MYCHECK="${MESSAGE%% *}" - [ "${MYCHECK}" != "${MYCHECK%%@${ME}}" ] && return + [ "${MESSAGE%%@*}" != "${MESSAGE%%@${ME}}" ] && return fi ################### From f7842f4f6007dd7d5ecf6fa2341a18fd48cf24a0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 27 Feb 2021 13:39:59 +0100 Subject: [PATCH 17/53] bin: bashbot_inc: fix full path, add WEBHOOK path --- bin/bashbot_env.inc.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bin/bashbot_env.inc.sh b/bin/bashbot_env.inc.sh index d935e64..f7524ac 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.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-50-g34923dd #=============================================================================== ############ @@ -21,8 +21,8 @@ export BASHBOT_HOME BASHBOT_ETC BASHBOT_VAR FILE_REGEX ME # default: one dir up -BASHBOT_HOME="$(cd "${BASH_SOURCE[0]%/*}" >/dev/null 2>&1 && pwd)/../" -[ "${BASHBOT_HOME}" = "/../" ] && BASHBOT_HOME="../" +BASHBOT_HOME="$(cd "${BASH_SOURCE[0]%/*}/../" >/dev/null 2>&1 && pwd)" +[ "${BASHBOT_HOME}" = "" ] && BASHBOT_HOME="../" # set you own BASHBOT_HOME if different, e.g. # BASHBOT_HOME="/usr/local/telegram-bot-bash" @@ -65,6 +65,10 @@ ME="${BOT_NAME}" [[ -z "${BOT_ADMIN}" || "${BOT_ADMIN}" == "?" ]] && printf "%s\n" "${ORANGE}Warning: Botadmin not set, send bot command${NC} /start" [[ -z "${BOT_NAME}" ]] && printf "%s\n" "${ORANGE}Warning: Botname not set, run bashbot.sh botname" +# default webhook pipe +export WEBHOOK="${DATADIR}/webhook-fifo-${ME}" + + # output command result or Telegram response print_result() { jssh_printDB "BOTSENT" | sort -r; } print_response() { jssh_printDB "UPD"; } From 84ff8cec15c1bc0a5c8cd00c0eedda3a7f7d8547 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 27 Feb 2021 16:46:04 +0100 Subject: [PATCH 18/53] webhook: save json as one line --- examples/webhook/index.php | 4 ++-- examples/webhook/json.txt | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/webhook/index.php b/examples/webhook/index.php index e92e990..02fcfde 100644 --- a/examples/webhook/index.php +++ b/examples/webhook/index.php @@ -11,7 +11,7 @@ * @license http://www.wtfpl.net/txt/copying/ WTFPLv2 * @since 30.01.2021 20:24 * -#### $$VERSION$$ v1.45-dev-28-g9958b5b +#### $$VERSION$$ v1.45-dev-51-gf7842f4 ***********************************************************/ // bashbot home dir @@ -66,7 +66,7 @@ if ($data == '') { $data = implode(" ",$_GET); } } // uncomment to save last received JSON - // file_put_contents($json_file, $data); + // file_put_contents($json_file, str_replace(array("\n", "\r"), '',$data). PHP_EOL)); // prepare for writing if ($data == '') { diff --git a/examples/webhook/json.txt b/examples/webhook/json.txt index 83e85cb..6cf3c52 100644 --- a/examples/webhook/json.txt +++ b/examples/webhook/json.txt @@ -1,2 +1 @@ -{"update_id":665220889, -"message":{"message_id":760,"from":{"id":586928566,"is_bot":false,"first_name":"Kay","last_name":"M","username":"KayM","language_code":"de"},"chat":{"id":589682731,"first_name":"Kay","last_name":"M","username":"KayM","type":"private"},"date":1612029749,"text":"/info","entities":[{"offset":0,"length":5,"type":"bot_command"}]}} +{"update_id":665220889,"message":{"message_id":760,"from":{"id":586928566,"is_bot":false,"first_name":"Kay","last_name":"M","username":"KayM","language_code":"de"},"chat":{"id":589682731,"first_name":"Kay","last_name":"M","username":"KayM","type":"private"},"date":1612029749,"text":"/info","entities":[{"offset":0,"length":5,"type":"bot_command"}]}} From 941598d01f226a56e7de43b5a05ddbc7cd33b3a1 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 27 Feb 2021 16:57:07 +0100 Subject: [PATCH 19/53] bin: add process_batch for processing updates from file --- bin/process_batch.sh | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 bin/process_batch.sh diff --git a/bin/process_batch.sh b/bin/process_batch.sh new file mode 100755 index 0000000..5fa0e1e --- /dev/null +++ b/bin/process_batch.sh @@ -0,0 +1,70 @@ +#!/bin/bash +# shellcheck disable=SC1090,SC2034,SC2059 +#=============================================================================== +# +# FILE: bin/process_batch.sh +# +USAGE='process_update.sh [-h|--help] -w|--watch [-n|--lines n] [file] [debug]' +# +# DESCRIPTION: processes last 10 telegram updates in file, one update per line +# +# -w --watch watch for new updates added to file +# -n --lines read only last "n" lines +# file to read updates from +# empty means read from webhook pipe +# +# -h - display short help +# --help - this help +# +# LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ +# AUTHOR: KayM (gnadelwartz), kay@rrr.de +# CREATED: 27.02.2021 13:14 +# +#### $$VERSION$$ v1.45-dev-52-g84ff8ce +#=============================================================================== + +#### +# parse args +COMMAND="process_multi_updates" +lines="-n 10" + +case "$1" in + "-f"|"--follow") + follow="-f" + shift + ;; + "-n"|"--lines") + lines="-n $2" + shift 2 + ;; +esac + +# set bashbot environment +source "${0%/*}/bashbot_env.inc.sh" "debug" # debug +print_help "${1:-nix}" + + +# empty file is webhook +file="${WEBHOOK}" +[ -n "$1" ] && file="$1" + +if [[ ! -r "${file}" || -d "${file}" ]]; then + printf "%b\n" "File ${GREY}${file}${NC} is not readable or is a directory." + exit 1 +fi + +#### +# ready, do stuff here ----- +trap 'kill $(jobs -p) 2>/dev/null' EXIT HUP QUIT + +# shellcheck disable=SC2086,SC2248 +tail ${follow} ${lines} "${file}" |\ + while IFS="" read -r input + do + # read json from stdin and convert update format + json='{"result": ['"${input}"']}' + UPDATE="$(${JSONSHFILE} -b -n <<<"${json}" 2>/dev/null)" + + # process telegram update + "${COMMAND}" "$1" + done From dda86e3496099ba9c42eb17758338b48b9b44634 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 28 Feb 2021 14:15:26 +0100 Subject: [PATCH 20/53] bin: process_xxx: replace BOTADMIN with id of bot admin --- bin/process_batch.sh | 8 +++++--- bin/process_update.sh | 4 +++- bin/promote_user.sh | 2 +- examples/webhook/json.txt | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/bin/process_batch.sh b/bin/process_batch.sh index 5fa0e1e..dd40d28 100755 --- a/bin/process_batch.sh +++ b/bin/process_batch.sh @@ -20,7 +20,7 @@ USAGE='process_update.sh [-h|--help] -w|--watch [-n|--lines n] [file] [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.02.2021 13:14 # -#### $$VERSION$$ v1.45-dev-52-g84ff8ce +#### $$VERSION$$ v1.45-dev-53-g941598d #=============================================================================== #### @@ -62,9 +62,11 @@ tail ${follow} ${lines} "${file}" |\ while IFS="" read -r input do # read json from stdin and convert update format - json='{"result": ['"${input}"']}' + # replace any ID named BOTADMIN with ID of bot admin + : "${input//\"id\":BOTADMIN,/\"id\":${BOT_ADMIN},}" + json='{"result": ['"${_}"']}' UPDATE="$(${JSONSHFILE} -b -n <<<"${json}" 2>/dev/null)" # process telegram update - "${COMMAND}" "$1" + "${COMMAND}" "$2" done diff --git a/bin/process_update.sh b/bin/process_update.sh index ec36da8..439679d 100755 --- a/bin/process_update.sh +++ b/bin/process_update.sh @@ -15,7 +15,7 @@ USAGE='process_update.sh [-h|--help] [debug] [/dev/null)" # process telegram update diff --git a/bin/promote_user.sh b/bin/promote_user.sh index fdcaad0..a0faa63 100755 --- a/bin/promote_user.sh +++ b/bin/promote_user.sh @@ -25,7 +25,7 @@ USAGE='promote_user.sh [-h|--help] "CHAT[ID]" "USER[ID]" "right[:true|false]" .. # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 25.01.2021 22:34 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-53-g941598d #=============================================================================== #### diff --git a/examples/webhook/json.txt b/examples/webhook/json.txt index 6cf3c52..4defd69 100644 --- a/examples/webhook/json.txt +++ b/examples/webhook/json.txt @@ -1 +1 @@ -{"update_id":665220889,"message":{"message_id":760,"from":{"id":586928566,"is_bot":false,"first_name":"Kay","last_name":"M","username":"KayM","language_code":"de"},"chat":{"id":589682731,"first_name":"Kay","last_name":"M","username":"KayM","type":"private"},"date":1612029749,"text":"/info","entities":[{"offset":0,"length":5,"type":"bot_command"}]}} +{"update_id":665220889,"message":{"message_id":760,"from":{"id":BOTADMIN,"is_bot":false,"first_name":"Kay","last_name":"M","username":"KayM","language_code":"de"},"chat":{"id":BOTADMIN,"first_name":"Kay","last_name":"M","username":"KayM","type":"private"},"date":1612029749,"text":"/info","entities":[{"offset":0,"length":5,"type":"bot_command"}]}} From 5dd24c395808d68d2877406cc27f9601a8f0e6b5 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 28 Feb 2021 16:51:28 +0100 Subject: [PATCH 21/53] bin: process-batch: implement startbot for webhook --- bashbot.sh | 3 ++- bin/process_batch.sh | 22 ++++++++++++++++++---- modules/processUpdates.sh | 5 ++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index e704ac3..b8a8b79 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-48-gf4d45d8 +#### $$VERSION$$ v1.45-dev-54-gdda86e3 ################################################################## # are we running in a terminal? @@ -830,6 +830,7 @@ if [ -z "${SOURCE}" ]; then # finally starts the read update loop, internal use only "startbot" ) _exec_if_function start_bot "$2" + _exec_if_function get_updates "$2" debug_checks "end startbot" "$@" exit ;; diff --git a/bin/process_batch.sh b/bin/process_batch.sh index dd40d28..fcbc2c0 100755 --- a/bin/process_batch.sh +++ b/bin/process_batch.sh @@ -4,10 +4,11 @@ # # FILE: bin/process_batch.sh # -USAGE='process_update.sh [-h|--help] -w|--watch [-n|--lines n] [file] [debug]' +USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] [file] [debug]' # # DESCRIPTION: processes last 10 telegram updates in file, one update per line # +# -s --startbot load addons, start TIMER, trigger startup actions # -w --watch watch for new updates added to file # -n --lines read only last "n" lines # file to read updates from @@ -20,7 +21,7 @@ USAGE='process_update.sh [-h|--help] -w|--watch [-n|--lines n] [file] [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.02.2021 13:14 # -#### $$VERSION$$ v1.45-dev-53-g941598d +#### $$VERSION$$ v1.45-dev-54-gdda86e3 #=============================================================================== #### @@ -29,6 +30,10 @@ COMMAND="process_multi_updates" lines="-n 10" case "$1" in + "-s"|"--startbot") + startbot="yes" + shift + ;; "-f"|"--follow") follow="-f" shift @@ -43,20 +48,29 @@ esac source "${0%/*}/bashbot_env.inc.sh" "debug" # debug print_help "${1:-nix}" - # empty file is webhook file="${WEBHOOK}" [ -n "$1" ] && file="$1" +# start bot +if [ -n "${startbot}" ]; then + # warn when starting bot without pipe + [ -p "${WEBHOOK}" ] || printf "%b\n" "${ORANGE}Warning${NC}: File is not a pipe:${GREY} ${file##*/}${NC}" + start_bot "$2" +fi +# check file exist if [[ ! -r "${file}" || -d "${file}" ]]; then - printf "%b\n" "File ${GREY}${file}${NC} is not readable or is a directory." + printf "%b\n" "${RED}Error${NC}: File not readable:${GREY} ${file}${NC}." exit 1 fi #### # ready, do stuff here ----- + +# kill all sub processes on exit trap 'kill $(jobs -p) 2>/dev/null' EXIT HUP QUIT +# use tail to read appended updates # shellcheck disable=SC2086,SC2248 tail ${follow} ${lines} "${file}" |\ while IFS="" read -r input diff --git a/modules/processUpdates.sh b/modules/processUpdates.sh index 9638740..5c188b2 100644 --- a/modules/processUpdates.sh +++ b/modules/processUpdates.sh @@ -4,7 +4,7 @@ # File: processUpdates.sh # Note: DO NOT EDIT! this file will be overwritten on update # -#### $$VERSION$$ v1.45-dev-38-g882efa8 +#### $$VERSION$$ v1.45-dev-54-gdda86e3 ################################################################## ############## @@ -301,7 +301,7 @@ start_bot() { log_debug "${DEBUGMSG}"; DEBUGMSG="$1" [[ "${DEBUGMSG}" == "xdebug"* ]] && set -x # cleaup old pipes and empty logfiles - find "${DATADIR}" -type p -delete + find "${DATADIR}" -type p -not -name "webhook-fifo-*" -delete find "${DATADIR}" -size 0 -name "*.log" -delete # load addons on startup for addons in "${ADDONDIR:-.}"/*.sh ; do @@ -330,7 +330,6 @@ start_bot() { send_normal_message "$(getConfigKey "botadmin")" "Bot $(getConfigKey "botname") started ..." & ########## # bot is ready, start processing updates ... - get_updates "${DEBUGMSG}" } From 0859354be8d732a936a8083e38439151d2975d6c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 28 Feb 2021 17:13:25 +0100 Subject: [PATCH 22/53] example: webhook: default and full webhook processing --- bashbot.sh | 2 +- bin/process_batch.sh | 2 +- examples/webhook/README.md | 30 ++++++++++++++++-------------- modules/processUpdates.sh | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index b8a8b79..8165f03 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-54-gdda86e3 +#### $$VERSION$$ v1.45-dev-55-g5dd24c3 ################################################################## # are we running in a terminal? diff --git a/bin/process_batch.sh b/bin/process_batch.sh index fcbc2c0..a3e75c5 100755 --- a/bin/process_batch.sh +++ b/bin/process_batch.sh @@ -21,7 +21,7 @@ USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.02.2021 13:14 # -#### $$VERSION$$ v1.45-dev-54-gdda86e3 +#### $$VERSION$$ v1.45-dev-55-g5dd24c3 #=============================================================================== #### diff --git a/examples/webhook/README.md b/examples/webhook/README.md index 7932357..cb6103c 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -27,14 +27,14 @@ Prepare Apache to forward webhook to Bashbot: - execute `php index.php` Every call to webhook `https:///telegram//` will execute -`index.php` and write received JSON to file `data-bot-bash/webhook-fifo-botname`. +`index.php` and write received JSON to file `data-bot-bash/webhook-fifo-`. E.g. the URL `https:///telegram//?json={"test":"me"}` will append `{"test":"me"}` to the file `data-bot-bash/webhook-fifo-`. Now your Apache is ready to forward data to Bashbot. -#### Webhook update processing for Bashbot +#### Default webhook processing To enable update processing delete the file `data-bot-bash/webhook-fifo-` after your webhook is working manually. All webhook calls are now forwarded to `bin/process_update.sh` for processing. @@ -43,15 +43,25 @@ Every incoming Telegram update load Bashbot once for processing one command. Eve Bashbot on every incoming update, it's more responsive and create less server load than polling Telegram -Webhook works without running Bashbot and thus has the following limitations: +Default webhook has the following limitations: - no startup actions - no background and interactive jobs - `addons` and `TIMER_EVENTS` are not working -To run startup actions and `TIMER_EVENTS` run Bashbot with `./bashbot start` even not needed with webhook. +Workaround for running new background jobs is to execute `./bashbot.sh resumeback` after starting a new background job. -Workaround for running new background jobs is to execute `./bashbot.sh resumeback` on the command line after starting a new background job. +#### Full webhook processing +Use this method in case you need addons, TIMER or interactive scripts with webhook. + +1. Default webook method must work first! +2. Create fifo: `mkfifo data-bot-bash/webhook-fifo-botname` +3. Start Bashbot in batch mode: `bin/process-batch.sh --startbot --watch data-bot-bash/webhook-fifo-` + +In batch mode Bashbot read updates from given file instead of Telegram server. `--startbot` loads the addons, start the TIMER and trigger all startup actions. +`--watch` instructs Bashbot to watch for new updates instead of stop on EOF. + +To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-` and kill `bin/process-batch.sh`. #### Enable webhook on Telegram side @@ -80,13 +90,5 @@ updates only over secure TLS connections and if a valid SSL certificate chain ex `socat` looks like a tool we can use to listen for Telegram updates from bash scripts, let's see ... -#### High traffic processing? - -Initially I planned to implement a mode for `High traffic update processing` where Bashbot is started once -and read updates from the named pipe `data-bot-bash/webhook-fifo-`, similar like polling from Telegram. - -But the default webhook method is so convincing and responsive that a special high traffic mode is not necessary. - - -#### $$VERSION$$ v1.45-dev-48-gf4d45d8 +#### $$VERSION$$ v1.45-dev-55-g5dd24c3 diff --git a/modules/processUpdates.sh b/modules/processUpdates.sh index 5c188b2..1bedd8e 100644 --- a/modules/processUpdates.sh +++ b/modules/processUpdates.sh @@ -4,7 +4,7 @@ # File: processUpdates.sh # Note: DO NOT EDIT! this file will be overwritten on update # -#### $$VERSION$$ v1.45-dev-54-gdda86e3 +#### $$VERSION$$ v1.45-dev-55-g5dd24c3 ################################################################## ############## From b6e90af4fa60fd4117f51cbfa2b247b6e46921e0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Mar 2021 08:30:11 +0100 Subject: [PATCH 23/53] examples: webhook: Full webhook processing --- examples/webhook/README.md | 57 +++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/examples/webhook/README.md b/examples/webhook/README.md index cb6103c..50a1fbc 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -2,48 +2,50 @@ ## Bashtbot webhook example -### Webhooks +### Webhook -Bashbot default mode is to poll Telegram server for updates but Telegram offers webhook -as a more efficient method to deliver updates. -If your server is reachable from the Internet you can use the methods described here. +Bashbot default mode is to poll Telegram server for updates but Telegram offers webhook as a more efficient method to deliver updates. +If your server is reachable from the Internet its possible to use the methods described here. -You need a valid SSL certificate or Telegram will refuse to deliever update via webhook. -A self signed certificate will not be sufficient. +Prerequisite for receiving Telegram unpdates with webhook is a valid SSL certificate, a self signed certificate will not be sufficient. + +*Note:* You need at least sudo rights to setup webhook. #### Setup Apache webhook Prerequisite: An Apache webserver with a valid SLL certificate chain and php enabled. +Other webserver should work also but they are not testet. Prepare Apache to forward webhook to Bashbot: - install bashbot as described in [Bashbot Installation](../../doc/0_install.md) -- create file `data-bot-bash/webhook-fifo-` (_ as in `botconfig.jssh`_) -- run `bashbot.sh init` to setup bashbot to run as same user as Apache (_e.g. www_) -- go to apache web root and create directory `telegram/` -- copy all files from `examples/webhook` to new directory and change to it -- write bashbot installation directory as first line to file `BASHBOT_HOME` -- execute `php index.php` +- create file `data-bot-bash/webhook-fifo-` (_\ as in `botconfig.jssh`_) +- run `sudo bashbot.sh init` to setup bashbot to run as same user as Apache (_e.g. www_) +- go to apache web root and create the directory `telegram/` +- change to the new directory and copy all files from `examples/webhook` to it +- edit file `BASHBOT_HOME` to contain Bashbot installation directory as first line +- execute `php index.php` as first test -Every call to webhook `https:///telegram//` will execute -`index.php` and write received JSON to file `data-bot-bash/webhook-fifo-`. +From now on every call to `https:///telegram//` will execute +`index.php` and write received JSON to the file `data-bot-bash/webhook-fifo-`. E.g. the URL `https:///telegram//?json={"test":"me"}` will append `{"test":"me"}` to the file `data-bot-bash/webhook-fifo-`. -Now your Apache is ready to forward data to Bashbot. +Now your Server is ready to receive updates from Telegram. #### Default webhook processing -To enable update processing delete the file `data-bot-bash/webhook-fifo-` after your webhook is working manually. -All webhook calls are now forwarded to `bin/process_update.sh` for processing. +This is the testet and supported default method for receiving and processing Telegram updates over webhook. -Every incoming Telegram update load Bashbot once for processing one command. Even it seems overkill to load -Bashbot on every incoming update, it's more responsive and create less server load than polling Telegram +To enable update processing delete the file `data-bot-bash/webhook-fifo-` after your webhook is working as described above. +Incoming Telegram updates are now forwarded to the script `bin/process_update.sh` for processing. +On every incoming Telegram update the script calls Bashbot once for processing the update. Even it seems overkill to load +Bashbot on every incoming update, it's more responsive and create less server load than polling Telegram. -Default webhook has the following limitations: +Nevertheles this has some limitations compared to run bashbot in background: - no startup actions - no background and interactive jobs - `addons` and `TIMER_EVENTS` are not working @@ -52,14 +54,17 @@ Workaround for running new background jobs is to execute `./bashbot.sh resumebac #### Full webhook processing -Use this method in case you need addons, TIMER or interactive scripts with webhook. +Full webhook processing use an external script to run Bashbot similar like for polling Telegram updates. +There is no support for running as support for running the script in background, as a service or an other user. 1. Default webook method must work first! -2. Create fifo: `mkfifo data-bot-bash/webhook-fifo-botname` -3. Start Bashbot in batch mode: `bin/process-batch.sh --startbot --watch data-bot-bash/webhook-fifo-` +2. run `bashbot.sh` to setup bashbot to run with your user id +2. Create fifo: `mkfifo data-bot-bash/webhook-fifo-botname` and give apache server write access to it +3. Start script for Bashbot batch mode:\ +`bin/process-batch.sh --startbot --watch data-bot-bash/webhook-fifo-` -In batch mode Bashbot read updates from given file instead of Telegram server. `--startbot` loads the addons, start the TIMER and trigger all startup actions. -`--watch` instructs Bashbot to watch for new updates instead of stop on EOF. +In batch mode Bashbot read updates from given file instead from Telegram server. `--startbot` run Bashbot staturaup actionsi +(_e.g. load addons, start TIMER, trigger first run_). `--watch` mean to wait for new updates instead to exit on end of file. To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-` and kill `bin/process-batch.sh`. @@ -90,5 +95,5 @@ updates only over secure TLS connections and if a valid SSL certificate chain ex `socat` looks like a tool we can use to listen for Telegram updates from bash scripts, let's see ... -#### $$VERSION$$ v1.45-dev-55-g5dd24c3 +#### $$VERSION$$ v1.45-dev-56-g0859354 From a473e25dfd99470fdfd9957a03d1b83aa2a252d9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Mar 2021 12:59:06 +0100 Subject: [PATCH 24/53] example: webhook: small doc updates --- examples/webhook/README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/webhook/README.md b/examples/webhook/README.md index 50a1fbc..f26f626 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -1,11 +1,11 @@ #### [Examples](../README.md) -## Bashtbot webhook example +## Bashbot webhook example ### Webhook Bashbot default mode is to poll Telegram server for updates but Telegram offers webhook as a more efficient method to deliver updates. -If your server is reachable from the Internet its possible to use the methods described here. +If your server is reachable from the Internet its possible to use the method described here. Prerequisite for receiving Telegram unpdates with webhook is a valid SSL certificate, a self signed certificate will not be sufficient. @@ -45,7 +45,7 @@ Incoming Telegram updates are now forwarded to the script `bin/process_update.sh On every incoming Telegram update the script calls Bashbot once for processing the update. Even it seems overkill to load Bashbot on every incoming update, it's more responsive and create less server load than polling Telegram. -Nevertheles this has some limitations compared to run bashbot in background: +Nevertheles this has some limitations compared to run bashbot in polling mode: - no startup actions - no background and interactive jobs - `addons` and `TIMER_EVENTS` are not working @@ -54,17 +54,17 @@ Workaround for running new background jobs is to execute `./bashbot.sh resumebac #### Full webhook processing -Full webhook processing use an external script to run Bashbot similar like for polling Telegram updates. -There is no support for running as support for running the script in background, as a service or an other user. +Full webhook processing use an external script to imitate Bashbot polling mode with webhook. +There is no support for running the script in background, as a service or an other user. 1. Default webook method must work first! 2. run `bashbot.sh` to setup bashbot to run with your user id 2. Create fifo: `mkfifo data-bot-bash/webhook-fifo-botname` and give apache server write access to it -3. Start script for Bashbot batch mode:\ +3. Start script to imitate Bashbot polling mode:\ `bin/process-batch.sh --startbot --watch data-bot-bash/webhook-fifo-` -In batch mode Bashbot read updates from given file instead from Telegram server. `--startbot` run Bashbot staturaup actionsi -(_e.g. load addons, start TIMER, trigger first run_). `--watch` mean to wait for new updates instead to exit on end of file. +The script read updates from given file line by line and forward updates to Bashbot update processing. `--startbot` run Bashbot startup actions +(_e.g. load addons, start TIMER, trigger first run_) and `--watch` mean wait for new updates instead of exit on end of file. To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-` and kill `bin/process-batch.sh`. @@ -95,5 +95,5 @@ updates only over secure TLS connections and if a valid SSL certificate chain ex `socat` looks like a tool we can use to listen for Telegram updates from bash scripts, let's see ... -#### $$VERSION$$ v1.45-dev-56-g0859354 +#### $$VERSION$$ v1.45-dev-57-gb6e90af From a9ac7eaedaae95e06786541be7dfd8ed3d1ab578 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Mar 2021 13:22:49 +0100 Subject: [PATCH 25/53] example: webhook: small doc updates --- examples/webhook/README.md | 49 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/examples/webhook/README.md b/examples/webhook/README.md index f26f626..b09072b 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -14,23 +14,22 @@ Prerequisite for receiving Telegram unpdates with webhook is a valid SSL certifi #### Setup Apache webhook -Prerequisite: An Apache webserver with a valid SLL certificate chain and php enabled. -Other webserver should work also but they are not testet. +Prerequisite: An Apache webserver with a valid SLL certificate chain and php enabled.\ +This should work with other webservers also but it's not testet. -Prepare Apache to forward webhook to Bashbot: +Setup webhook with Apache: - install bashbot as described in [Bashbot Installation](../../doc/0_install.md) - create file `data-bot-bash/webhook-fifo-` (_\ as in `botconfig.jssh`_) - run `sudo bashbot.sh init` to setup bashbot to run as same user as Apache (_e.g. www_) -- go to apache web root and create the directory `telegram/` -- change to the new directory and copy all files from `examples/webhook` to it -- edit file `BASHBOT_HOME` to contain Bashbot installation directory as first line -- execute `php index.php` as first test +- go to apache web root and create the directory `telegram/` (_ as `botconfig.jssh`_) +- go into the new directory and copy all files from `examples/webhook` to it +- edit file `BASHBOT_HOME` to contain ithe Bashbot installation directory as first line (_other lines are ignored_) +- execute `php index.php` to test if script has write access to `data-bot-bash/webhook-fifo- -From now on every call to `https:///telegram//` will execute -`index.php` and write received JSON to the file `data-bot-bash/webhook-fifo-`. -E.g. the URL `https:///telegram//?json={"test":"me"}` -will append `{"test":"me"}` to the file `data-bot-bash/webhook-fifo-`. +Calling `https:///telegram//` will execute `index.php` +thus append received data to the file `data-bot-bash/webhook-fifo-`. +E.g. `https:///telegram//?json={"test":"me"}` will append `{"test":"me"}`. Now your Server is ready to receive updates from Telegram. @@ -42,15 +41,15 @@ This is the testet and supported default method for receiving and processing Tel To enable update processing delete the file `data-bot-bash/webhook-fifo-` after your webhook is working as described above. Incoming Telegram updates are now forwarded to the script `bin/process_update.sh` for processing. -On every incoming Telegram update the script calls Bashbot once for processing the update. Even it seems overkill to load -Bashbot on every incoming update, it's more responsive and create less server load than polling Telegram. +On every incoming Telegram update the script is executed, source bashbot.sh and forward the update to Bashbot processing.i +Even it seems overhead to source Bashbot for every update, it's more responsive and create less load than Bashbot polling mode. -Nevertheles this has some limitations compared to run bashbot in polling mode: +Nevertheles there are some limitations compared to polling mode: - no startup actions - - no background and interactive jobs + - no background* and interactive jobs - `addons` and `TIMER_EVENTS` are not working -Workaround for running new background jobs is to execute `./bashbot.sh resumeback` after starting a new background job. +\* Workaround for background jobs is to execute `./bashbot.sh resumeback` if a new background job was started. #### Full webhook processing @@ -58,13 +57,13 @@ Full webhook processing use an external script to imitate Bashbot polling mode w There is no support for running the script in background, as a service or an other user. 1. Default webook method must work first! -2. run `bashbot.sh` to setup bashbot to run with your user id -2. Create fifo: `mkfifo data-bot-bash/webhook-fifo-botname` and give apache server write access to it -3. Start script to imitate Bashbot polling mode:\ +2. run `bashbot.sh init` to setup bashbot to run with your user id +2. Create a named pipe: `mkfifo data-bot-bash/webhook-fifo-botname` and give the web server write access to it +3. Start the script to imitate Bashbot polling mode:\ `bin/process-batch.sh --startbot --watch data-bot-bash/webhook-fifo-` -The script read updates from given file line by line and forward updates to Bashbot update processing. `--startbot` run Bashbot startup actions -(_e.g. load addons, start TIMER, trigger first run_) and `--watch` mean wait for new updates instead of exit on end of file. +The script read updates from given file line by line and forward updates to Bashbot update processing. `--startbot` will run the startup actions +(_e.g. load addons, start TIMER, trigger first run_) and `--watch` will wait for new updates instead of exit on end of file. To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-` and kill `bin/process-batch.sh`. @@ -89,11 +88,11 @@ To stop delivering of Telegram updates via webhook run `bin/any_command.sh delet #### Bash webhook -A pure bash webhook implementaition is not possible without additional software because Telegram deliver -updates only over secure TLS connections and if a valid SSL certificate chain exists. +A pure bash webhook implementation is not possible without extra software because Telegram delivers +webhook updates only over secure TLS connections with a valid SSL certificate chain. -`socat` looks like a tool we can use to listen for Telegram updates from bash scripts, let's see ... +`socat` looks like a tool to listen for Telegram updates from bash scripts, let's see ... -#### $$VERSION$$ v1.45-dev-57-gb6e90af +#### $$VERSION$$ v1.45-dev-58-ga473e25 From 24158142f97647b29db95aa565b83e9e4776097f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Mar 2021 19:40:53 +0100 Subject: [PATCH 26/53] init: bashbot.rc: bashbotdir --- bashbot.rc | 5 +++-- bin/bashbot_init.inc.sh | 4 ++-- examples/webhook/README.md | 13 +++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index 0ff801d..f0f1ad4 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,7 +5,7 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-59-ga9ac7ea # shellcheck disable=SC2009 # shellcheck disable=SC2181 @@ -35,7 +35,8 @@ runas="nobody" # edit the values of the following lines to fit your config: # your bot installation dir -bashbot="cd /usr/local/telegram-bot-bash; /usr/local/telegram-bot-bash/bashbot.sh" +bashbotdir="/usr/local/telegram-bot-bash" +bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh" # your bot name as given to botfather, e.g. mysomething_bot name="" # set additionl parameter, e.g. debug diff --git a/bin/bashbot_init.inc.sh b/bin/bashbot_init.inc.sh index d5ece74..e12fc5a 100644 --- a/bin/bashbot_init.inc.sh +++ b/bin/bashbot_init.inc.sh @@ -11,7 +11,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.01.2021 13:42 # -#### $$VERSION$$ v1.45-dev-3-g429c230 +#### $$VERSION$$ v1.45-dev-59-ga9ac7ea #=============================================================================== # shellcheck disable=SC2059 @@ -116,7 +116,7 @@ bot_init() { if [ -w "bashbot.rc" ]; then printf "Adjust user and botname in bashbot.rc ...\n" sed -i '/^[# ]*runas=/ s|runas=.*$|runas="'"${touser}"'"|' "bashbot.rc" - sed -i '/^[# ]*bashbot=/ s|bashbot=.*$|bashbot="cd '"${PWD}"'; '"${PWD}"'/'"${0##*/}"'"|' "bashbot.rc" + sed -i '/^[# ]*bashbotdir=/ s|bashbotdir=.*$|bashbotdir="'"${PWD}"'"|' "bashbot.rc" botname="$(getConfigKey "botname")" [ -n "${botname}" ] && sed -i '/^[# ]*name=/ s|name=.*$|name="'"${botname}"'"|' "bashbot.rc" printf "Done.\n" diff --git a/examples/webhook/README.md b/examples/webhook/README.md index b09072b..e92e70b 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -7,7 +7,7 @@ Bashbot default mode is to poll Telegram server for updates but Telegram offers webhook as a more efficient method to deliver updates. If your server is reachable from the Internet its possible to use the method described here. -Prerequisite for receiving Telegram unpdates with webhook is a valid SSL certificate, a self signed certificate will not be sufficient. +Prerequisite for receiving Telegram updates with webhook is a valid SSL certificate, a self signed certificate will not be sufficient. *Note:* You need at least sudo rights to setup webhook. @@ -22,7 +22,7 @@ Setup webhook with Apache: - install bashbot as described in [Bashbot Installation](../../doc/0_install.md) - create file `data-bot-bash/webhook-fifo-` (_\ as in `botconfig.jssh`_) - run `sudo bashbot.sh init` to setup bashbot to run as same user as Apache (_e.g. www_) -- go to apache web root and create the directory `telegram/` (_ as `botconfig.jssh`_) +- create a directory in web root: `telegram/` (_ as `botconfig.jssh`_) - go into the new directory and copy all files from `examples/webhook` to it - edit file `BASHBOT_HOME` to contain ithe Bashbot installation directory as first line (_other lines are ignored_) - execute `php index.php` to test if script has write access to `data-bot-bash/webhook-fifo- @@ -36,12 +36,12 @@ Now your Server is ready to receive updates from Telegram. #### Default webhook processing -This is the testet and supported default method for receiving and processing Telegram updates over webhook. +This is the testet and supported default method for processing Telegram updates over webhook. -To enable update processing delete the file `data-bot-bash/webhook-fifo-` after your webhook is working as described above. +To enable update processing delete the file `data-bot-bash/webhook-fifo-` if webhook is working as described above. Incoming Telegram updates are now forwarded to the script `bin/process_update.sh` for processing. -On every incoming Telegram update the script is executed, source bashbot.sh and forward the update to Bashbot processing.i +On incoming Telegram updates the script is executed, it sources bashbot.sh and forward the update to Bashbot for processing. Even it seems overhead to source Bashbot for every update, it's more responsive and create less load than Bashbot polling mode. Nevertheles there are some limitations compared to polling mode: @@ -64,6 +64,7 @@ There is no support for running the script in background, as a service or an oth The script read updates from given file line by line and forward updates to Bashbot update processing. `--startbot` will run the startup actions (_e.g. load addons, start TIMER, trigger first run_) and `--watch` will wait for new updates instead of exit on end of file. +Short form: 'bin/process-batch.sh -s -w' To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-` and kill `bin/process-batch.sh`. @@ -94,5 +95,5 @@ webhook updates only over secure TLS connections with a valid SSL certificate ch `socat` looks like a tool to listen for Telegram updates from bash scripts, let's see ... -#### $$VERSION$$ v1.45-dev-58-ga473e25 +#### $$VERSION$$ v1.45-dev-59-ga9ac7ea From 7b8e39147960bcd0bf32050d3139ba38ff4c6860 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 2 Mar 2021 18:05:14 +0100 Subject: [PATCH 27/53] bin: process_batch: fix options bashbot.rc: support webhook --- bashbot.rc | 41 ++++++++++++++++++++++++++++++++--------- bin/process_batch.sh | 16 +++++++++++----- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index f0f1ad4..3e31c48 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,7 +5,7 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.45-dev-59-ga9ac7ea +#### $$VERSION$$ v1.45-dev-60-g2415814 # shellcheck disable=SC2009 # shellcheck disable=SC2181 @@ -34,11 +34,14 @@ runas="nobody" # runcmd="runuser ${runas} -s /bin/bash -c " # runasuser with *runuser* # edit the values of the following lines to fit your config: -# your bot installation dir -bashbotdir="/usr/local/telegram-bot-bash" -bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh" # your bot name as given to botfather, e.g. mysomething_bot name="" +# your bot installation dir +bashbotdir="/usr/local/telegram-bot-bash" +databotdir="${bashbotdir}/data-bot-bash" +# programs to run +bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh" +webhook="cd ${bashbotdir}; ${bashbotdir}/bin/process_batch.sh --start --watch ${databotdir}/webhook-fifo-${name}" # set additionl parameter, e.g. debug mode="" @@ -53,25 +56,45 @@ case "$1" in $runcmd "$bashbot start $mode" # >/dev/null 2>&1 /dev/null 2>&1 /dev/null' EXIT HUP QUIT +trap 'kill $(jobs -p) 2>/dev/null; printf "Bot in batch mode killed!\n"' EXIT HUP QUIT # use tail to read appended updates # shellcheck disable=SC2086,SC2248 From 4854d0d05153bbc842525b7409079d9fc32ac45d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 2 Mar 2021 18:26:41 +0100 Subject: [PATCH 28/53] bashbot.rc: fix webhook --- bashbot.rc | 29 ++++++++++++++++------------- bin/process_batch.sh | 4 ++-- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index 3e31c48..02f8059 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,9 +5,10 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.45-dev-60-g2415814 +#### $$VERSION$$ v1.45-dev-61-g7b8e391 # shellcheck disable=SC2009 # shellcheck disable=SC2181 +# shellcheck disable=SC2250 # ### BEGIN INIT INFO @@ -27,21 +28,21 @@ runcmd="echo Dry run:" # not activated until you edit lines below # Configuration Section # edit the next line to fit the user you want to run bashbot, e.g. nobody: -runas="nobody" +runas="www" # uncomment one of the example lines to fit your system # runcmd="su ${runas} -s /bin/bash -c " # runasuser with *su* -# runcmd="runuser ${runas} -s /bin/bash -c " # runasuser with *runuser* +runcmd="/usr/sbin/runuser ${runas} -s /bin/bash -c " # runasuser with *runuser* # edit the values of the following lines to fit your config: # your bot name as given to botfather, e.g. mysomething_bot -name="" +name="GnadelTest_bot" # your bot installation dir -bashbotdir="/usr/local/telegram-bot-bash" +bashbotdir="/usr/local/github/telegram-bot-bash-develop/DIST/telegram-bot-bash" databotdir="${bashbotdir}/data-bot-bash" # programs to run bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh" -webhook="cd ${bashbotdir}; ${bashbotdir}/bin/process_batch.sh --start --watch ${databotdir}/webhook-fifo-${name}" +webhook="cd ${bashbotdir}; ${bashbotdir}/bin/process_batch.sh --startbot --watch ${databotdir}/webhook-fifo-${name} &" # set additionl parameter, e.g. debug mode="" @@ -52,32 +53,34 @@ mode="" case "$1" in 'start') - # shellcheck disable=SC2250 $runcmd "$bashbot start $mode" # >/dev/null 2>&1 /dev/null 2>&1 Date: Wed, 3 Mar 2021 08:55:49 +0100 Subject: [PATCH 31/53] bashbot.rc: webhook usage --- bashbot.rc | 5 +++-- examples/webhook/README.md | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index 3371dac..6becc65 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,7 +5,7 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.45-dev-62-g4854d0d +#### $$VERSION$$ v1.45-dev-64-gfff79d6 # shellcheck disable=SC2009 # shellcheck disable=SC2181 # shellcheck disable=SC2250 @@ -113,7 +113,8 @@ case "$1" in fi ;; *) - printf "%s\n" "Usage: $0 { start | stop | starthook | restart | reload | restartback | suspendback | resumeback | killback }" + printf "%s\n" "Usage: $0 [ start | stop | restart | starthook | stophook | restarthook ]" + printf "%s\n" " $0 [ status | restartback | suspendback | resumeback | killback ]" RETVAL=1 ;; esac diff --git a/examples/webhook/README.md b/examples/webhook/README.md index 00ddd04..5e36ddd 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -96,5 +96,5 @@ webhook updates only over secure TLS connections with a valid SSL certificate ch `socat` looks like a tool to listen for Telegram updates from bash scripts, let's see ... -#### $$VERSION$$ v1.45-dev-63-ge098aee +#### $$VERSION$$ v1.45-dev-64-gfff79d6 From ec90ce820df7d077fd9d0f66fc1f801bbc2bd99d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 3 Mar 2021 09:00:41 +0100 Subject: [PATCH 32/53] example: mention bashbot.rc --- bashbot.rc | 2 +- examples/webhook/README.md | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index 6becc65..da73f02 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,7 +5,7 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.45-dev-64-gfff79d6 +#### $$VERSION$$ v1.45-dev-65-g859ce92 # shellcheck disable=SC2009 # shellcheck disable=SC2181 # shellcheck disable=SC2250 diff --git a/examples/webhook/README.md b/examples/webhook/README.md index 5e36ddd..711e58a 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -67,7 +67,9 @@ The script read updates from given file line by line and forward updates to Bash (_e.g. load addons, start TIMER, trigger first run_) and `--watch` will wait for new updates instead of exit on end of file. Short form: 'bin/process-batch.sh -s -w' -To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-` and kill `bin/process-batch.sh`. +To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-` and stop `bin/process-batch.sh`. + +If full processing seems to work well on the command line, you can use `./bachbot.rc starthook/stophook` to run processing in background. #### Enable webhook on Telegram side @@ -96,5 +98,5 @@ webhook updates only over secure TLS connections with a valid SSL certificate ch `socat` looks like a tool to listen for Telegram updates from bash scripts, let's see ... -#### $$VERSION$$ v1.45-dev-64-gfff79d6 +#### $$VERSION$$ v1.45-dev-65-g859ce92 From 53e936ee654c5a8155243120f53930ef98010266 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 3 Mar 2021 09:05:16 +0100 Subject: [PATCH 33/53] bashbot.rc: nohup for webhook --- bashbot.rc | 4 ++-- examples/webhook/README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index da73f02..34c67a6 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,7 +5,7 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.45-dev-65-g859ce92 +#### $$VERSION$$ v1.45-dev-66-gec90ce8 # shellcheck disable=SC2009 # shellcheck disable=SC2181 # shellcheck disable=SC2250 @@ -57,7 +57,7 @@ case "$1" in RETVAL=$? ;; 'starthook') - $runcmd "$webhook $mode" # >/dev/null 2>&1 >${bashbotdir}/logs/WEBHOOK.log" # >/dev/null 2>&1 Date: Wed, 3 Mar 2021 13:27:07 +0100 Subject: [PATCH 34/53] always set BOTADMIN --- bashbot.sh | 8 +++++--- bin/bashbot_env.inc.sh | 5 +++-- modules/background.sh | 4 ++-- modules/chatMember.sh | 9 ++++----- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 8165f03..182cdbc 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-55-g5dd24c3 +#### $$VERSION$$ v1.45-dev-67-g53e936e ################################################################## # are we running in a terminal? @@ -358,7 +358,7 @@ declare -rx BOTTOKEN URL ME_URL declare -ax CMD declare -Ax UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY iBUTTON declare -Ax SERVICE NEWMEMBER LEFTMEMBER PINNED MIGRATE -export res CAPTION ME +export res CAPTION ME BOTADMIN ############### @@ -788,6 +788,8 @@ fi # source the script with source as param to use functions in other scripts # do not execute if read from other scripts +BOTADMIN="$(getConfigKey "botadmin")" + if [ -z "${SOURCE}" ]; then ############## # internal options only for use from bashbot and developers @@ -900,7 +902,7 @@ if [ -z "${SOURCE}" ]; then # shellcheck disable=SC2086 if kill ${BOTPID}; then # inform botadmin about stop - send_normal_message "$(getConfigKey "botadmin")" "Bot ${ME} stopped ..." & + send_normal_message "${BOTADMIN}" "Bot ${ME} stopped ..." & printf "${GREEN}OK. Bot stopped successfully.${NN}" else printf "${RED}An error occurred while stopping bot.${NN}" diff --git a/bin/bashbot_env.inc.sh b/bin/bashbot_env.inc.sh index f7524ac..d4b43b9 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.45-dev-50-g34923dd +#### $$VERSION$$ v1.45-dev-67-g53e936e #=============================================================================== ############ @@ -59,7 +59,8 @@ UPLOADDIR="${BASHBOT_VAR%/bin*}" FILE_REGEX="${UPLOADDIR}/.*" # get and check ADMIN and NAME -BOT_ADMIN="$(getConfigKey "botadmin")" +# shellcheck disable=SC2153 +BOT_ADMIN="${BOTADMIN}" BOT_NAME="$(getConfigKey "botname")" ME="${BOT_NAME}" [[ -z "${BOT_ADMIN}" || "${BOT_ADMIN}" == "?" ]] && printf "%s\n" "${ORANGE}Warning: Botadmin not set, send bot command${NC} /start" diff --git a/modules/background.sh b/modules/background.sh index c5720be..44b655c 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117,SC2059 -#### $$VERSION$$ v1.45-dev-48-gf4d45d8 +#### $$VERSION$$ v1.45-dev-67-g53e936e # will be automatically sourced from bashbot @@ -129,7 +129,7 @@ inproc() { job_control() { local BOT ADM content proc CHAT job fifo killall="" BOT="$(getConfigKey "botname")" - ADM="$(getConfigKey "botadmin")" + ADM="${BOTADMIN}" debug_checks "Enter job_control" "$1" for FILE in "${DATADIR:-.}/"*-back.cmd; do [ "${FILE}" = "${DATADIR:-.}/*-back.cmd" ] && printf "${RED}No background processes.${NN}" && break diff --git a/modules/chatMember.sh b/modules/chatMember.sh index 54462c6..53c6ffa 100644 --- a/modules/chatMember.sh +++ b/modules/chatMember.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.45-dev-25-gb1f6a0b +#### $$VERSION$$ v1.45-dev-67-g53e936e # will be automatically sourced from bashbot @@ -147,10 +147,9 @@ user_is_admin() { # $1 user user_is_botadmin() { [ -z "$1" ] && return 1 - local admin; admin="$(getConfigKey "botadmin")"; [ -z "${admin}" ] && return 1 - [[ "${admin}" == "$1" || "${admin}" == "$2" ]] && return 0 - #[[ "${admin}" = "@*" ]] && [[ "${admin}" = "$2" ]] && return 0 - if [ "${admin}" = "?" ]; then setConfigKey "botadmin" "${1:-?}"; return 0; fi + [ -z "${BOTADMIN}" ] && return 1 + [[ "${BOTADMIN}" == "$1" || "${BOTADMIN}" == "$2" ]] && return 0 + if [ "${BOTADMIN}" = "?" ]; then setConfigKey "botadmin" "${1:-?}"; return 0; fi return 1 } From b454827109e246e8118e8469a9ddaf92296f4ae7 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 3 Mar 2021 13:40:51 +0100 Subject: [PATCH 35/53] bin: use BOTNAME BOTADMIN --- bashbot.sh | 2 +- bin/any_command.sh | 4 +- bin/bashbot_env.inc.sh | 93 ---------------------------------------- bin/bashbot_init.inc.sh | 2 +- bin/bashbot_stats.sh | 4 +- bin/delete_message.sh | 4 +- bin/edit_buttons.sh | 4 +- bin/edit_message.sh | 4 +- bin/kickban_user.sh | 2 +- bin/process_batch.sh | 4 +- bin/process_update.sh | 4 +- bin/promote_user.sh | 2 +- bin/send_broadcast.sh | 4 +- bin/send_buttons.sh | 4 +- bin/send_dice.sh | 4 +- bin/send_edit_message.sh | 1 - bin/send_file.sh | 4 +- bin/send_message.sh | 4 +- modules/background.sh | 2 +- modules/chatMember.sh | 2 +- 20 files changed, 30 insertions(+), 124 deletions(-) delete mode 100644 bin/bashbot_env.inc.sh delete mode 120000 bin/send_edit_message.sh diff --git a/bashbot.sh b/bashbot.sh index 182cdbc..71bcaa4 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-67-g53e936e +#### $$VERSION$$ v1.45-dev-68-ge6838d1 ################################################################## # are we running in a terminal? diff --git a/bin/any_command.sh b/bin/any_command.sh index 2b62f19..1e1373e 100755 --- a/bin/any_command.sh +++ b/bin/any_command.sh @@ -21,7 +21,7 @@ USAGE='any_command.sh [-h|--help] [--force|--reference] bot_command args ...' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 30.01.2021 10:24 # -#### $$VERSION$$ v1.45-dev-7-ga9ed559 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -68,7 +68,7 @@ fi # ready, do stuff here ----- COMMAND="$1" if [ "$2" == "BOTADMIN" ]; then - ARG1="${BOT_ADMIN}" + ARG1="${BOTADMIN}" else ARG1="$2" fi diff --git a/bin/bashbot_env.inc.sh b/bin/bashbot_env.inc.sh deleted file mode 100644 index d4b43b9..0000000 --- a/bin/bashbot_env.inc.sh +++ /dev/null @@ -1,93 +0,0 @@ -#!/bin/bash -#=============================================================================== -# -# FILE: bashbot_env.inc.sh -# -# USAGE: source bashbot_env.inc.sh [debug] -# -# DESCRIPTION: set bashbot environment for all scripts in this directory -# -# OPTIONS: $1 - will be forwarded ro bashbot, e.g. debug -# -# LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ -# AUTHOR: KayM (gnadelwartz), kay@rrr.de -# CREATED: 18.12.2020 12:27 -# -#### $$VERSION$$ v1.45-dev-67-g53e936e -#=============================================================================== - -############ -# set where your bashbot lives -export BASHBOT_HOME BASHBOT_ETC BASHBOT_VAR FILE_REGEX ME - -# default: one dir up -BASHBOT_HOME="$(cd "${BASH_SOURCE[0]%/*}/../" >/dev/null 2>&1 && pwd)" -[ "${BASHBOT_HOME}" = "" ] && BASHBOT_HOME="../" - -# set you own BASHBOT_HOME if different, e.g. -# BASHBOT_HOME="/usr/local/telegram-bot-bash" -BASHBOT_VAR="${BASHBOT_HOME}" -BASHBOT_ETC="${BASHBOT_HOME}" - -##### -# if files are not readable, eviroment is wrong or bashbot is not initialized - -# check for bashbot -if [ ! -r "${BASHBOT_HOME}/bashbot.sh" ]; then - printf "%s\n" "Bashbot.sh not found in \"${BASHBOT_HOME}\"" - exit 4 -fi - -dev=" Are we in dev or did you forget to run init?" -# check for botconfig.jssh readable -if [ ! -r "${BASHBOT_ETC}/botconfig.jssh" ]; then - printf "%s\n" "Bashbot config file in \"${BASHBOT_ETC}\" does not exist or is not readable. ${dev}" - exit 3 -fi -# check for count.jssh readable -if [ ! -r "${BASHBOT_VAR}/count.jssh" ]; then - printf "%s\n" "Bashbot count file in \"${BASHBOT_VAR}\" does not exist or is not readable. ${dev}" - exit 3 -fi - -# shellcheck disable=SC1090 -source "${BASHBOT_HOME}/bashbot.sh" source "$1" - -# overwrite bot FILE regex to BASHBOT_VAR -# change this to the location you want to allow file uploads from -UPLOADDIR="${BASHBOT_VAR%/bin*}" -FILE_REGEX="${UPLOADDIR}/.*" - -# get and check ADMIN and NAME -# shellcheck disable=SC2153 -BOT_ADMIN="${BOTADMIN}" -BOT_NAME="$(getConfigKey "botname")" -ME="${BOT_NAME}" -[[ -z "${BOT_ADMIN}" || "${BOT_ADMIN}" == "?" ]] && printf "%s\n" "${ORANGE}Warning: Botadmin not set, send bot command${NC} /start" -[[ -z "${BOT_NAME}" ]] && printf "%s\n" "${ORANGE}Warning: Botname not set, run bashbot.sh botname" - -# default webhook pipe -export WEBHOOK="${DATADIR}/webhook-fifo-${ME}" - - -# output command result or Telegram response -print_result() { jssh_printDB "BOTSENT" | sort -r; } -print_response() { jssh_printDB "UPD"; } - -# check and output help -print_help() { - case "$1" in - '') - printf "missing arguments\n" - ;& - "-h"*) - printf 'usage: %s\n' "${USAGE}" - exit 1 - ;; - '--h'*) - sed -n '/^#====/,/^#====/p' <"$0" - exit 1 - ;; - esac -} - diff --git a/bin/bashbot_init.inc.sh b/bin/bashbot_init.inc.sh index e12fc5a..7746e6f 100644 --- a/bin/bashbot_init.inc.sh +++ b/bin/bashbot_init.inc.sh @@ -11,7 +11,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.01.2021 13:42 # -#### $$VERSION$$ v1.45-dev-59-ga9ac7ea +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== # shellcheck disable=SC2059 diff --git a/bin/bashbot_stats.sh b/bin/bashbot_stats.sh index df23119..e111bfa 100755 --- a/bin/bashbot_stats.sh +++ b/bin/bashbot_stats.sh @@ -17,7 +17,7 @@ USAGE='bashbot_stats.sh [-h|--help] [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 23.12.2020 20:34 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== # set bashbot environment @@ -27,7 +27,7 @@ source "${0%/*}/bashbot_env.inc.sh" "$1" #### # ready, do stuff here ----- -echo -e "${GREEN}Hi I'm ${BOT_NAME}.${NC}" +echo -e "${GREEN}Hi I'm ${BOTNAME}.${NC}" declare -A STATS jssh_readDB_async "STATS" "${COUNTFILE}" for MSG in ${!STATS[*]} diff --git a/bin/delete_message.sh b/bin/delete_message.sh index 8b26769..e5eab73 100755 --- a/bin/delete_message.sh +++ b/bin/delete_message.sh @@ -20,7 +20,7 @@ USAGE='delete_message.sh [-h|--help] "CHAT[ID]" "MESSAGE[ID]" [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 03.01.2021 15:37 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -34,7 +34,7 @@ print_help "$1" #### # ready, do stuff here ----- if [ "$1" == "BOTADMIN" ]; then - CHAT="${BOT_ADMIN}" + CHAT="${BOTADMIN}" else CHAT="$1" fi diff --git a/bin/edit_buttons.sh b/bin/edit_buttons.sh index 0455d1c..62a63a2 100755 --- a/bin/edit_buttons.sh +++ b/bin/edit_buttons.sh @@ -26,7 +26,7 @@ USAGE='send_message.sh [-h|--help] "CHAT[ID]" "MESSAGE[ID]" "text|url" ...' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 21.01.2021 08:10 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -40,7 +40,7 @@ print_help "$1" #### # ready, do stuff here ----- if [ "$1" == "BOTADMIN" ]; then - CHAT="${BOT_ADMIN}" + CHAT="${BOTADMIN}" else CHAT="$1" fi diff --git a/bin/edit_message.sh b/bin/edit_message.sh index dd9b766..7bd6119 100755 --- a/bin/edit_message.sh +++ b/bin/edit_message.sh @@ -23,7 +23,7 @@ USAGE='send_edit_message.sh [-h|--help] [format|caption] "CHAT[ID]" "MESSAGE[ID] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 23.12.2020 16:52 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -55,7 +55,7 @@ print_help "$1" #### # ready, do stuff here ----- if [ "$1" == "BOTADMIN" ]; then - CHAT="${BOT_ADMIN}" + CHAT="${BOTADMIN}" else CHAT="$1" fi diff --git a/bin/kickban_user.sh b/bin/kickban_user.sh index cb31df1..49b3633 100755 --- a/bin/kickban_user.sh +++ b/bin/kickban_user.sh @@ -20,7 +20,7 @@ USAGE='kickban_user.sh [-h|--help] [-u|--unban] "CHAT[ID]" "USER[ID]" [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 25.01.2021 20:34 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### diff --git a/bin/process_batch.sh b/bin/process_batch.sh index 5c5b726..509d3fb 100755 --- a/bin/process_batch.sh +++ b/bin/process_batch.sh @@ -21,7 +21,7 @@ USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.02.2021 13:14 # -#### $$VERSION$$ v1.45-dev-60-g2415814 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -83,7 +83,7 @@ tail ${follow} ${lines} "${file}" |\ do # read json from stdin and convert update format # replace any ID named BOTADMIN with ID of bot admin - : "${input//\"id\":BOTADMIN,/\"id\":${BOT_ADMIN},}" + : "${input//\"id\":BOTADMIN,/\"id\":${BOTADMIN},}" json='{"result": ['"${_}"']}' UPDATE="$(${JSONSHFILE} -b -n <<<"${json}" 2>/dev/null)" diff --git a/bin/process_update.sh b/bin/process_update.sh index 439679d..61fc5db 100755 --- a/bin/process_update.sh +++ b/bin/process_update.sh @@ -15,7 +15,7 @@ USAGE='process_update.sh [-h|--help] [debug] [/dev/null)" # process telegram update diff --git a/bin/promote_user.sh b/bin/promote_user.sh index a0faa63..9fc9dcd 100755 --- a/bin/promote_user.sh +++ b/bin/promote_user.sh @@ -25,7 +25,7 @@ USAGE='promote_user.sh [-h|--help] "CHAT[ID]" "USER[ID]" "right[:true|false]" .. # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 25.01.2021 22:34 # -#### $$VERSION$$ v1.45-dev-53-g941598d +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### diff --git a/bin/send_broadcast.sh b/bin/send_broadcast.sh index a3390a3..9b3807d 100755 --- a/bin/send_broadcast.sh +++ b/bin/send_broadcast.sh @@ -28,7 +28,7 @@ USAGE='broadcast_message.sh [-h|--help] [--doit] [--groups|--both|--db=file] [fo # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 16.12.2020 16:14 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -91,7 +91,7 @@ if [ -z "${SENDALL[*]}" ]; then fi # loop over users - printf "${GREEN}Sending broadcast message to ${SENDTO}${GROUPSALSO} of ${BOT_NAME} using database:${NC}${GREY} ${database##*/}" + printf "${GREEN}Sending broadcast message to ${SENDTO}${GROUPSALSO} of ${BOTNAME} using database:${NC}${GREY} ${database##*/}" { # dry run [ -z "${DOIT}" ] && printf "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}\n" diff --git a/bin/send_buttons.sh b/bin/send_buttons.sh index f2d2b3e..984a0c1 100755 --- a/bin/send_buttons.sh +++ b/bin/send_buttons.sh @@ -26,7 +26,7 @@ USAGE='send_message.sh [-h|--help] "CHAT[ID]" "message" "text|url" ...' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 18.01.2021 11:34 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -40,7 +40,7 @@ print_help "$1" #### # ready, do stuff here ----- if [ "$1" == "BOTADMIN" ]; then - CHAT="${BOT_ADMIN}" + CHAT="${BOTADMIN}" else CHAT="$1" fi diff --git a/bin/send_dice.sh b/bin/send_dice.sh index dc81c4a..cf95ba2 100755 --- a/bin/send_dice.sh +++ b/bin/send_dice.sh @@ -21,7 +21,7 @@ USAGE='send_dice.sh [-h|--help] "CHAT[ID]" "emoji" [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 07.02.2021 18:45 # -#### $$VERSION$$ v1.45-dev-8-g069570e +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -35,7 +35,7 @@ print_help "$1" #### # ready, do stuff here ----- if [ "$1" == "BOTADMIN" ]; then - CHAT="${BOT_ADMIN}" + CHAT="${BOTADMIN}" else CHAT="$1" fi diff --git a/bin/send_edit_message.sh b/bin/send_edit_message.sh deleted file mode 120000 index e6089f6..0000000 --- a/bin/send_edit_message.sh +++ /dev/null @@ -1 +0,0 @@ -edit_message.sh \ No newline at end of file diff --git a/bin/send_file.sh b/bin/send_file.sh index 1cc2dc2..a77b47d 100755 --- a/bin/send_file.sh +++ b/bin/send_file.sh @@ -25,7 +25,7 @@ USAGE='send_file.sh [-h|--help] "CHAT[ID]" "file|URL" "caption ...." [type] [deb # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 25.12.2020 20:24 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -39,7 +39,7 @@ print_help "$1" #### # ready, do stuff here ----- if [ "$1" == "BOTADMIN" ]; then - CHAT="${BOT_ADMIN}" + CHAT="${BOTADMIN}" else CHAT="$1" fi diff --git a/bin/send_message.sh b/bin/send_message.sh index 10edd37..c0e39f4 100755 --- a/bin/send_message.sh +++ b/bin/send_message.sh @@ -22,7 +22,7 @@ USAGE='send_message.sh [-h|--help] [format] "CHAT[ID]" "message ...." [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 16.12.2020 11:34 # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-68-ge6838d1 #=============================================================================== #### @@ -50,7 +50,7 @@ print_help "$1" #### # ready, do stuff here ----- if [ "$1" == "BOTADMIN" ]; then - CHAT="${BOT_ADMIN}" + CHAT="${BOTADMIN}" else CHAT="$1" fi diff --git a/modules/background.sh b/modules/background.sh index 44b655c..4c775d4 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117,SC2059 -#### $$VERSION$$ v1.45-dev-67-g53e936e +#### $$VERSION$$ v1.45-dev-68-ge6838d1 # will be automatically sourced from bashbot diff --git a/modules/chatMember.sh b/modules/chatMember.sh index 53c6ffa..90f60fd 100644 --- a/modules/chatMember.sh +++ b/modules/chatMember.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.45-dev-67-g53e936e +#### $$VERSION$$ v1.45-dev-68-ge6838d1 # will be automatically sourced from bashbot From b78431eacdf63a31abc87720736cc3c3a0d8994a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 3 Mar 2021 14:09:34 +0100 Subject: [PATCH 36/53] bin: fix missing include --- bashbot.sh | 2 +- bin/any_command.sh | 2 +- bin/bashbot_env.inc.sh | 91 +++++++++++++++++++++++++++++++++++++++++ bin/bashbot_init.inc.sh | 2 +- bin/bashbot_stats.sh | 2 +- bin/delete_message.sh | 2 +- bin/edit_buttons.sh | 2 +- bin/edit_message.sh | 2 +- bin/kickban_user.sh | 2 +- bin/process_batch.sh | 2 +- bin/process_update.sh | 2 +- bin/promote_user.sh | 2 +- bin/send_broadcast.sh | 2 +- bin/send_buttons.sh | 2 +- bin/send_dice.sh | 2 +- bin/send_file.sh | 2 +- bin/send_message.sh | 2 +- modules/background.sh | 2 +- modules/chatMember.sh | 2 +- 19 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 bin/bashbot_env.inc.sh diff --git a/bashbot.sh b/bashbot.sh index 71bcaa4..86bce42 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-68-ge6838d1 +#### $$VERSION$$ v1.45-dev-69-gb454827 ################################################################## # are we running in a terminal? diff --git a/bin/any_command.sh b/bin/any_command.sh index 1e1373e..349356a 100755 --- a/bin/any_command.sh +++ b/bin/any_command.sh @@ -21,7 +21,7 @@ USAGE='any_command.sh [-h|--help] [--force|--reference] bot_command args ...' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 30.01.2021 10:24 # -#### $$VERSION$$ v1.45-dev-68-ge6838d1 +#### $$VERSION$$ v1.45-dev-69-gb454827 #=============================================================================== #### diff --git a/bin/bashbot_env.inc.sh b/bin/bashbot_env.inc.sh new file mode 100644 index 0000000..17c0f5e --- /dev/null +++ b/bin/bashbot_env.inc.sh @@ -0,0 +1,91 @@ +#!/bin/bash +#=============================================================================== +# +# FILE: bashbot_env.inc.sh +# +# USAGE: source bashbot_env.inc.sh [debug] +# +# DESCRIPTION: set bashbot environment for all scripts in this directory +# +# OPTIONS: $1 - will be forwarded ro bashbot, e.g. debug +# +# LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ +# AUTHOR: KayM (gnadelwartz), kay@rrr.de +# CREATED: 18.12.2020 12:27 +# +#### $$VERSION$$ v1.45-dev-69-gb454827 +#=============================================================================== + +############ +# set where your bashbot lives +export BASHBOT_HOME BASHBOT_ETC BASHBOT_VAR FILE_REGEX ME + +# default: one dir up +BASHBOT_HOME="$(cd "${BASH_SOURCE[0]%/*}/../" >/dev/null 2>&1 && pwd)" +[ "${BASHBOT_HOME}" = "" ] && BASHBOT_HOME="../" + +# set you own BASHBOT_HOME if different, e.g. +# BASHBOT_HOME="/usr/local/telegram-bot-bash" +BASHBOT_VAR="${BASHBOT_HOME}" +BASHBOT_ETC="${BASHBOT_HOME}" + +##### +# if files are not readable, eviroment is wrong or bashbot is not initialized + +# check for bashbot +if [ ! -r "${BASHBOT_HOME}/bashbot.sh" ]; then + printf "%s\n" "Bashbot.sh not found in \"${BASHBOT_HOME}\"" + exit 4 +fi + +dev=" Are we in dev or did you forget to run init?" +# check for botconfig.jssh readable +if [ ! -r "${BASHBOT_ETC}/botconfig.jssh" ]; then + printf "%s\n" "Bashbot config file in \"${BASHBOT_ETC}\" does not exist or is not readable. ${dev}" + exit 3 +fi +# check for count.jssh readable +if [ ! -r "${BASHBOT_VAR}/count.jssh" ]; then + printf "%s\n" "Bashbot count file in \"${BASHBOT_VAR}\" does not exist or is not readable. ${dev}" + exit 3 +fi + +# shellcheck disable=SC1090 +source "${BASHBOT_HOME}/bashbot.sh" source "$1" + +# overwrite bot FILE regex to BASHBOT_VAR +# change this to the location you want to allow file uploads from +UPLOADDIR="${BASHBOT_VAR%/bin*}" +FILE_REGEX="${UPLOADDIR}/.*" + +# get and check ADMIN and NAME +BOTNAME="$(getConfigKey "botname")" +ME="${BOTNAME}" +[[ -z "${BOTADMIN}" || "${BOTADMIN}" == "?" ]] && printf "%s\n" "${ORANGE}Warning: Botadmin not set, send bot command${NC} /start" +[[ -z "${BOTNAME}" ]] && printf "%s\n" "${ORANGE}Warning: Botname not set, run bashbot.sh botname" + +# default webhook pipe +export WEBHOOK="${DATADIR}/webhook-fifo-${ME}" + + +# output command result or Telegram response +print_result() { jssh_printDB "BOTSENT" | sort -r; } +print_response() { jssh_printDB "UPD"; } + +# check and output help +print_help() { + case "$1" in + '') + printf "missing arguments\n" + ;& + "-h"*) + printf 'usage: %s\n' "${USAGE}" + exit 1 + ;; + '--h'*) + sed -n '/^#====/,/^#====/p' <"$0" + exit 1 + ;; + esac +} + diff --git a/bin/bashbot_init.inc.sh b/bin/bashbot_init.inc.sh index 7746e6f..b4ca71d 100644 --- a/bin/bashbot_init.inc.sh +++ b/bin/bashbot_init.inc.sh @@ -11,7 +11,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.01.2021 13:42 # -#### $$VERSION$$ v1.45-dev-68-ge6838d1 +#### $$VERSION$$ v1.45-dev-69-gb454827 #=============================================================================== # shellcheck disable=SC2059 diff --git a/bin/bashbot_stats.sh b/bin/bashbot_stats.sh index e111bfa..331033a 100755 --- a/bin/bashbot_stats.sh +++ b/bin/bashbot_stats.sh @@ -17,7 +17,7 @@ USAGE='bashbot_stats.sh [-h|--help] [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 23.12.2020 20:34 # -#### $$VERSION$$ v1.45-dev-68-ge6838d1 +#### $$VERSION$$ v1.45-dev-69-gb454827 #=============================================================================== # set bashbot environment diff --git a/bin/delete_message.sh b/bin/delete_message.sh index e5eab73..5de96fb 100755 --- a/bin/delete_message.sh +++ b/bin/delete_message.sh @@ -20,7 +20,7 @@ USAGE='delete_message.sh [-h|--help] "CHAT[ID]" "MESSAGE[ID]" [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 03.01.2021 15:37 # -#### $$VERSION$$ v1.45-dev-68-ge6838d1 +#### $$VERSION$$ v1.45-dev-69-gb454827 #=============================================================================== #### diff --git a/bin/edit_buttons.sh b/bin/edit_buttons.sh index 62a63a2..8ce27c3 100755 --- a/bin/edit_buttons.sh +++ b/bin/edit_buttons.sh @@ -26,7 +26,7 @@ USAGE='send_message.sh [-h|--help] "CHAT[ID]" "MESSAGE[ID]" "text|url" ...' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 21.01.2021 08:10 # -#### $$VERSION$$ v1.45-dev-68-ge6838d1 +#### $$VERSION$$ v1.45-dev-69-gb454827 #=============================================================================== #### diff --git a/bin/edit_message.sh b/bin/edit_message.sh index 7bd6119..be810da 100755 --- a/bin/edit_message.sh +++ b/bin/edit_message.sh @@ -23,7 +23,7 @@ USAGE='send_edit_message.sh [-h|--help] [format|caption] "CHAT[ID]" "MESSAGE[ID] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 23.12.2020 16:52 # -#### $$VERSION$$ v1.45-dev-68-ge6838d1 +#### $$VERSION$$ v1.45-dev-69-gb454827 #=============================================================================== #### diff --git a/bin/kickban_user.sh b/bin/kickban_user.sh index 49b3633..f119575 100755 --- a/bin/kickban_user.sh +++ b/bin/kickban_user.sh @@ -20,7 +20,7 @@ USAGE='kickban_user.sh [-h|--help] [-u|--unban] "CHAT[ID]" "USER[ID]" [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 25.01.2021 20:34 # -#### $$VERSION$$ v1.45-dev-68-ge6838d1 +#### $$VERSION$$ v1.45-dev-69-gb454827 #=============================================================================== #### diff --git a/bin/process_batch.sh b/bin/process_batch.sh index 509d3fb..e406133 100755 --- a/bin/process_batch.sh +++ b/bin/process_batch.sh @@ -21,7 +21,7 @@ USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.02.2021 13:14 # -#### $$VERSION$$ v1.45-dev-68-ge6838d1 +#### $$VERSION$$ v1.45-dev-69-gb454827 #=============================================================================== #### diff --git a/bin/process_update.sh b/bin/process_update.sh index 61fc5db..7ba74ca 100755 --- a/bin/process_update.sh +++ b/bin/process_update.sh @@ -15,7 +15,7 @@ USAGE='process_update.sh [-h|--help] [debug] [ Date: Wed, 3 Mar 2021 15:33:40 +0100 Subject: [PATCH 37/53] mycommands: use BOTADMIN --- mycommands.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mycommands.sh b/mycommands.sh index bd541b4..17e3ca2 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -13,7 +13,7 @@ # License: WTFPLv2 http://www.wtfpl.net/txt/copying/ # Author: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-45-g6b8ad97 +#### $$VERSION$$ v1.45-dev-70-gb78431e ####################################################### # shellcheck disable=SC1117 @@ -106,12 +106,12 @@ else "${WELCOME_MSG} ${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]} (@${NEWMEMBER[USERNAME]})" MYSENTID="${BOTSENT[ID]}" { sleep 5; delete_message "${CHAT[ID]}" "${MYSENTID}"; } & - [ -n "${REPORT_NEWMEMBER}" ] && send_normal_message "$(getConfigKey "botadmin")"\ + [ -n "${REPORT_NEWMEMBER}" ] && send_normal_message "${BOTADMIN}"\ "New member: ${CHAT[TITLE]} (${CHAT[ID]}): ${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]} (@${NEWMEMBER[USERNAME]})" fi ;; '/_left_chat_member'*) - [ -n "${REPORT_LEFTMEMBER}" ] && send_normal_message "$(getConfigKey "botadmin")"\ + [ -n "${REPORT_LEFTMEMBER}" ] && send_normal_message "${BOTADMIN}"\ "Left member: ${CHAT[TITLE]} (${CHAT[ID]}): ${LEFTMEMBER[FIRST_NAME]} ${LEFTMEMBER[LAST_NAME]} (@${LEFTMEMBER[USERNAME]})" ;; '/_migrate_group'*) From 7500ca0d12179a20bb9caff5ec73b46da4eedb05 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 3 Mar 2021 15:57:58 +0100 Subject: [PATCH 38/53] modules: chatMember: fix user_is_botadmin --- modules/chatMember.sh | 4 ++-- test/d-user_is-test.sh | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/chatMember.sh b/modules/chatMember.sh index 7b5add0..8089f2c 100644 --- a/modules/chatMember.sh +++ b/modules/chatMember.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.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-71-gac16103 # will be automatically sourced from bashbot @@ -149,7 +149,7 @@ user_is_botadmin() { [ -z "$1" ] && return 1 [ -z "${BOTADMIN}" ] && return 1 [[ "${BOTADMIN}" == "$1" || "${BOTADMIN}" == "$2" ]] && return 0 - if [ "${BOTADMIN}" = "?" ]; then setConfigKey "botadmin" "${1:-?}"; return 0; fi + if [ "${BOTADMIN}" = "?" ]; then setConfigKey "botadmin" "${1:-?}"; BOTADMIN="${1:-?}"; return 0; fi return 1 } diff --git a/test/d-user_is-test.sh b/test/d-user_is-test.sh index 8daf09b..5d19501 100755 --- a/test/d-user_is-test.sh +++ b/test/d-user_is-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-71-gac16103 #=============================================================================== # include common functions and definitions @@ -22,6 +22,9 @@ set +f cd "${TESTDIR}" || exit 1 +# reset BOTADMIN +printf '["botadmin"] "?"\n' >>"${ADMINFILE}" # auto mode + # source bashbot.sh function, uncomment if you want to test functions # shellcheck source=./bashbot.sh source "${TESTDIR}/bashbot.sh" source @@ -33,8 +36,6 @@ source "${TESTDIR}/commands.sh" source # first user asking for botadmin will botadmin printf "Check \"user_is_botadmin\" ...\n" -printf '["botadmin"] "?"\n' >>"${ADMINFILE}" # auto mode - printf "BOTADMIN ...\n" user_is_botadmin "BOTADMIN" || exit 1 # should never fail printf "NOBOTADMIN ...\n" From eb0c22761519d0b2665c34b4ac4d04b6d06dc37f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 3 Mar 2021 18:23:09 +0100 Subject: [PATCH 39/53] bashbot.rc: fix background start --- bashbot.rc | 12 ++++++------ bin/process_batch.sh | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index 34c67a6..81691a5 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,7 +5,7 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.45-dev-66-gec90ce8 +#### $$VERSION$$ v1.45-dev-72-g7500ca0 # shellcheck disable=SC2009 # shellcheck disable=SC2181 # shellcheck disable=SC2250 @@ -28,21 +28,21 @@ runcmd="echo Dry run:" # not activated until you edit lines below # Configuration Section # edit the next line to fit the user you want to run bashbot, e.g. nobody: -runas="nobody" +runas="www" # uncomment one of the example lines to fit your system # runcmd="su ${runas} -s /bin/bash -c " # runasuser with *su* -# runcmd="/usr/sbin/runuser ${runas} -s /bin/bash -c " # runasuser with *runuser* +runcmd="/usr/sbin/runuser ${runas} -s /bin/bash -c " # runasuser with *runuser* # edit the values of the following lines to fit your config: # your bot name as given to botfather, e.g. mysomething_bot -name="" +name="GnadelTest_bot" # your bot installation dir bashbotdir="/usr/local/github/telegram-bot-bash-develop/DIST/telegram-bot-bash" databotdir="${bashbotdir}/data-bot-bash" # programs to run bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh" -webhook="cd ${bashbotdir}; ${bashbotdir}/bin/process_batch.sh --startbot --watch ${databotdir}/webhook-fifo-${name} &" +webhook="cd ${bashbotdir}; nohup ${bashbotdir}/bin/process_batch.sh --startbot --watch ${databotdir}/webhook-fifo-${name}" # set additionl parameter, e.g. debug mode="" @@ -57,7 +57,7 @@ case "$1" in RETVAL=$? ;; 'starthook') - $runcmd "nohup $webhook $mode &>>${bashbotdir}/logs/WEBHOOK.log" # >/dev/null 2>&1 >${bashbotdir}/logs/WEBHOOK.log &" # >/dev/null 2>&1 /dev/null; printf "Bot in batch mode killed!\n"' EXIT HUP QUIT +trap 'kill $(jobs -p) 2>/dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME}"' webhook stopped ..."; printf "Bot in batch mode killed!\n"' EXIT HUP QUIT # use tail to read appended updates # shellcheck disable=SC2086,SC2248 From 0a296eaaa15a56577014084801426f11ef6e8c3a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 3 Mar 2021 18:37:57 +0100 Subject: [PATCH 40/53] bashbot polling mode --- bashbot.sh | 6 +++--- modules/processUpdates.sh | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 86bce42..7d2d39c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-73-geb0c227 ################################################################## # are we running in a terminal? @@ -831,7 +831,7 @@ if [ -z "${SOURCE}" ]; then ;; # finally starts the read update loop, internal use only "startbot" ) - _exec_if_function start_bot "$2" + _exec_if_function start_bot "$2" "polling mode" _exec_if_function get_updates "$2" debug_checks "end startbot" "$@" exit @@ -902,7 +902,7 @@ if [ -z "${SOURCE}" ]; then # shellcheck disable=SC2086 if kill ${BOTPID}; then # inform botadmin about stop - send_normal_message "${BOTADMIN}" "Bot ${ME} stopped ..." & + send_normal_message "${BOTADMIN}" "Bot ${ME} polling mode stopped ..." & printf "${GREEN}OK. Bot stopped successfully.${NN}" else printf "${RED}An error occurred while stopping bot.${NN}" diff --git a/modules/processUpdates.sh b/modules/processUpdates.sh index 1bedd8e..d7c7432 100644 --- a/modules/processUpdates.sh +++ b/modules/processUpdates.sh @@ -4,7 +4,7 @@ # File: processUpdates.sh # Note: DO NOT EDIT! this file will be overwritten on update # -#### $$VERSION$$ v1.45-dev-55-g5dd24c3 +#### $$VERSION$$ v1.45-dev-73-geb0c227 ################################################################## ############## @@ -327,7 +327,7 @@ start_bot() { # read blocked users jssh_readDB_async "BASHBOTBLOCKED" "${BLOCKEDFILE}" # inform botadmin about start - send_normal_message "$(getConfigKey "botadmin")" "Bot $(getConfigKey "botname") started ..." & + send_normal_message "$(getConfigKey "botadmin")" "Bot ${ME} $2 started ..." & ########## # bot is ready, start processing updates ... } From fdb2b3ac7b0da28763b6cde45bd36f929082e860 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 3 Mar 2021 18:44:16 +0100 Subject: [PATCH 41/53] bashbot.rc: fix runuser --- bashbot.rc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index 81691a5..a3d6332 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,7 +5,7 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.45-dev-72-g7500ca0 +#### $$VERSION$$ v1.45-dev-74-g0a296ea # shellcheck disable=SC2009 # shellcheck disable=SC2181 # shellcheck disable=SC2250 @@ -28,15 +28,15 @@ runcmd="echo Dry run:" # not activated until you edit lines below # Configuration Section # edit the next line to fit the user you want to run bashbot, e.g. nobody: -runas="www" +runas="nobody" # uncomment one of the example lines to fit your system # runcmd="su ${runas} -s /bin/bash -c " # runasuser with *su* -runcmd="/usr/sbin/runuser ${runas} -s /bin/bash -c " # runasuser with *runuser* +# runcmd="/usr/sbin/runuser ${runas} -s /bin/bash -c " # runasuser with *runuser* # edit the values of the following lines to fit your config: # your bot name as given to botfather, e.g. mysomething_bot -name="GnadelTest_bot" +name="" # your bot installation dir bashbotdir="/usr/local/github/telegram-bot-bash-develop/DIST/telegram-bot-bash" databotdir="${bashbotdir}/data-bot-bash" From 50777ceff7ccbdea5d9531dc90b14a56eab7d919 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 4 Mar 2021 13:58:04 +0100 Subject: [PATCH 42/53] startup: factor out bot_cleanup --- README.html | 397 ------------------ README.md | 2 +- README.txt | 301 ------------- addons/antiFlood.sh | 2 +- addons/example.sh | 2 +- bashbot.rc | 2 +- bashbot.sh | 12 +- bin/any_command.sh | 2 +- bin/bashbot_env.inc.sh | 2 +- bin/bashbot_init.inc.sh | 2 +- bin/bashbot_stats.sh | 2 +- bin/delete_message.sh | 2 +- bin/edit_buttons.sh | 2 +- bin/edit_message.sh | 2 +- bin/kickban_user.sh | 2 +- bin/process_batch.sh | 2 +- bin/process_update.sh | 2 +- bin/promote_user.sh | 2 +- bin/send_broadcast.sh | 2 +- bin/send_buttons.sh | 2 +- bin/send_dice.sh | 2 +- bin/send_file.sh | 2 +- bin/send_message.sh | 2 +- commands.sh | 2 +- dev/all-tests.sh | 2 +- dev/dev.inc.sh | 2 +- dev/git-add.sh | 2 +- dev/hooks/post-commit.sh | 2 +- dev/hooks/pre-commit.sh | 2 +- dev/hooks/pre-push.sh | 2 +- dev/inject-json.sh | 2 +- dev/install-hooks.sh | 2 +- dev/make-distribution.sh | 2 +- dev/make-html.sh | 2 +- dev/make-standalone.sh | 2 +- dev/obfuscate.sh | 2 +- dev/shellcheck.files | 2 +- dev/version.sh | 2 +- doc/0_install.md | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 2 +- doc/7_develop.md | 2 +- examples/README.md | 2 +- examples/background-scripts/run_diskusage.sh | 2 +- .../background-scripts/run_filecontent.sh | 2 +- examples/background-scripts/run_filename.sh | 2 +- examples/background-scripts/run_notify.sh | 2 +- examples/bash2env.sh | 2 +- examples/bashbot-multi.sh | 2 +- examples/bashbot.cron | 2 +- examples/calc.sh | 2 +- examples/jsonDB-keyboard/mycommands.sh | 2 +- examples/notify.sh | 2 +- examples/question.sh | 2 +- examples/send-system-status/botacl | 2 +- examples/send-system-status/mycommands.sh | 2 +- examples/webhook/README.md | 2 +- examples/webhook/index.php | 2 +- modules/aliases.sh | 2 +- modules/answerInline.sh | 2 +- modules/background.sh | 6 +- modules/chatMember.sh | 2 +- modules/jsonDB.sh | 2 +- modules/processUpdates.sh | 16 +- modules/sendMessage.sh | 2 +- mycommands.conf | 2 +- mycommands.sh | 2 +- mycommands.sh.clean | 2 +- scripts/interactive.sh.clean | 2 +- test/ADD-test-new.sh | 2 +- test/ALL-tests.inc.sh | 2 +- test/a-commit-test.sh | 2 +- test/b-example-test.sh | 2 +- test/c-init-test.sh | 2 +- test/d-JSON.sh-test.sh | 2 +- test/d-process_inline-test.sh | 2 +- test/d-process_message-test.sh | 2 +- test/d-send_message-test.sh | 2 +- test/d-user_is-test.sh | 2 +- test/e-env-test.sh | 2 +- 84 files changed, 99 insertions(+), 791 deletions(-) diff --git a/README.html b/README.html index f5cd18f..e69de29 100644 --- a/README.html +++ b/README.html @@ -1,397 +0,0 @@ - - - - - - - Bashbot README - - - - -
-

Bashbot README

-
-

-Bashbot - A Telegram bot written in bash. -

-Written by Drew (@topkecleon) and Kay M (@gnadelwartz). - -

Contributions by Daniil Gentili (@danog), JuanPotato, BigNerd95, TiagoDanin, iicc1 and dcoomber.

-

Released to the public domain wherever applicable. Elsewhere, consider it released under the WTFPLv2.

-

Linted by #ShellCheck

-

Prerequisites

-

Uses JSON.sh/JSON.awk and the magic of sed.

-

Bashbot is written in bash. It depends on commands typically available in a Linux/Unix Environment. For more information on commands provided by recent versions of coreutils, busybox or toybox, see Developer Notes.

-

Note for MacOS and BSD Users: Bashbot will not run without installing additional software as it uses modern bash and (gnu) grep/sed features. See Install Bashbot.

-

Note for embedded systems: You need to install a "real" bash as the vanilla installation of busybox or toybox is not sufficient. See Install Bashbot.

-

Bashbot Documentation and Downloads are available on www.github.com.

-

Documentation

-
    -
  • Introduction to Telegram Bots
  • -
  • Install Bashbot -
      -
    • Install release
    • -
    • Install from github
    • -
    • Update Bashbot
    • -
    • Notes on Updates
    • -
  • -
  • Get Bottoken from Botfather
  • -
  • Getting Started -
      -
    • Managing your Bot
    • -
    • Receive data
    • -
    • Send messages
    • -
    • Send files, locations, keyboards
    • -
  • -
  • Advanced Features -
      -
    • Access Control
    • -
    • Interactive Chats
    • -
    • Background Jobs
    • -
    • Inline queries
    • -
    • Send message errors
    • -
  • -
  • Expert Use -
      -
    • Handling UTF-8 character sets
    • -
    • Run as other user or system service
    • -
    • Schedule bashbot from Cron
    • -
    • Use from CLI and Scripts
    • -
    • Customize Bashbot Environment
    • -
  • -
  • Best Practices -
      -
    • Customize mycommands.sh
    • -
    • Overwrite/disable commands
    • -
    • Separate logic from commands
    • -
    • Test your Bot with shellcheck
    • -
  • -
  • Function Reference -
      -
    • Sending Messages, Files, Keyboards
    • -
    • User Access Control
    • -
    • Inline Queries
    • -
    • jsshDB Bashbot key-value storage
    • -
    • Background and Interactive Jobs
    • -
  • -
  • Developer Notes -
      -
    • Debug bashbot
    • -
    • Modules, addons, events
    • -
    • Setup your environment
    • -
    • Bashbot test suite
    • -
  • -
  • Examples Directory
  • -
  • Webhook Example
  • -
-

Your very first bashbot in a nutshell

-

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 and go into bot dir
-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 chat with your bot (your bot's username is shown after 'Bot Name:'):

-
/start
-
-You are Botadmin
-Available commands:
-  /start: _Start bot and get this message_.
-  /help: _Get this message_.
-  /info: _Get shorter info message about this bot_....
-
-/info
-
-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 to BASHBOT.log. Telegram send/receive errors are logged to ERROR.log. Start bashbot in debug mode to see all messages sent to / received from Telegram, as well as bash command error messages.

-

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

-
|__ logs
-|     |__ BASHBOT.log  # log what your bot is doing ...
-|     |__ ERROR.log    # connection errors from / to Telegram API
-|     |
-|     |__ DEBUG.log    # stdout/stderr of you bot (debug mode enabled)
-|     |__ MESSAGE.log  # full text of all message send/received (debug mode enabled)
-
-

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 bulletproof, 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 appropriately, disable globbing with set -f and quote everything). In addition remove unused scripts and examples from your Bot (e.g. everything in example/) and disable/remove all unused bot commands.

-

It's important to escape or remove $ and ` in input from user, files or network (as bashbot does). One of the powerful features of Unix shells is variable and command substitution using ${var}, $(cmd) and `cmd` can lead to remote code execution (RCE) or remote information disclosure (RID) bugs if unescaped $ or ` 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 that accepts external input (e.g. from the user as arguments or the file system), you shouldn't use echo to display it. Use printf whenever possible.

-

Run your Bot as a restricted user

-

I recommend running your bot as a user with almost no access rights. All files your Bot has write access to are in danger of being overwritten/deleted if your bot is hacked. For the same reason every file your Bot can read is in danger of being 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 Linux/Unix systems. See Expert use on how to run your Bot as an other user.

-

Secure your Bot installation

-

Your Bot configuration must not be readable by 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, data-bot-bash/ and logs/ 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.

-

Note: Keep old log files in a safe place or even better delete them, they are GDPR relevant and may contain information you don't want to be public.

-

FAQ

-

Is this Bot insecure?

-

Bashbot is not more (in)secure than a Bot written in another 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 ...

-

Note: Up to version 0.941 (mai/22/2020) telegram-bot-bash had a remote code execution bug, please update if you use an older version!

-

Why Bash and not the much better xyz?

-

Well, that's a damn good question... maybe because I'm a Unix admin from the stone age. Nevertheless there are more reasons from my side:

-
    -
  • bashbot will run wherever bash and (gnu) sed is available, from embedded Linux to mainframe
  • -
  • easy to integrate with other shell scripts, e.g. for sending system message / health status
  • -
  • no need to install or learn a new programming language, library or framework
  • -
  • 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 that you could 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.

-

Can I send messages from CLI and scripts?

-

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
-

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

-

Blocked by telegram?

-

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

-

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
-

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-Address 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.45-dev-37-gfdbfceb

- - diff --git a/README.md b/README.md index fc7cb94..a5ea3d0 100644 --- a/README.md +++ b/README.md @@ -242,4 +242,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.45-dev-37-gfdbfceb +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a diff --git a/README.txt b/README.txt index 54784c4..b413e56 100644 --- a/README.txt +++ b/README.txt @@ -20,304 +20,3 @@ ******ooo*.............* ***o*........** **...** - - - - - -Bashbot README - - - Bashbot - A Telegram bot written in bash. - -Written by Drew (@topkecleon) and Kay M (@gnadelwartz). -Contributions by Daniil Gentili (@danog), JuanPotato, BigNerd95, TiagoDanin, iicc1 and -dcoomber. -Released to the public domain wherever applicable. Elsewhere, consider it released under -the WTFPLv2 [http://www.wtfpl.net/txt/copying/]. -Linted by #ShellCheck - -Prerequisites - -Uses JSON.sh [http://github.com/dominictarr/JSON.sh]/JSON.awk [https://github.com/step-/ -JSON.awk] and the magic of sed. -Bashbot is written in bash. It depends on commands typically available in a Linux/Unix -Environment. For more information on commands provided by recent versions of coreutils -[https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands], busybox [https:// -en.wikipedia.org/wiki/BusyBox#Commands] or toybox [https://landley.net/toybox/help.html], -see Developer Notes [doc/7_develop.md#common-commands]. -Note for MacOS and BSD Users: Bashbot will not run without installing additional software -as it uses modern bash and (gnu) grep/sed features. See Install Bashbot [doc/ -0_install.md]. -Note for embedded systems: You need to install a "real" bash as the vanilla installation -of busybox or toybox is not sufficient. See Install Bashbot [doc/0_install.md]. -Bashbot Documentation [https://github.com/topkecleon/telegram-bot-bash] and Downloads -[https://github.com/topkecleon/telegram-bot-bash/releases] are available on www.github.com -[https://www.github.com]. - -Documentation - - -* Introduction to Telegram Bots [https://core.telegram.org/bots] -* Install Bashbot [doc/0_install.md] - - o Install release - o Install from github - o Update Bashbot - o Notes on Updates - -* Get Bottoken from Botfather [doc/1_firstbot.md] -* Getting Started [doc/2_usage.md] - - o Managing your Bot - o Receive data - o Send messages - o Send files, locations, keyboards - -* Advanced Features [doc/3_advanced.md] - - o Access Control - o Interactive Chats - o Background Jobs - o Inline queries - o Send message errors - -* Expert Use [doc/4_expert.md] - - o Handling UTF-8 character sets - o Run as other user or system service - o Schedule bashbot from Cron - o Use from CLI and Scripts - o Customize Bashbot Environment - -* Best Practices [doc/5_practice.md] - - o Customize mycommands.sh - o Overwrite/disable commands - o Separate logic from commands - o Test your Bot with shellcheck - -* Function Reference [doc/6_reference.md] - - o Sending Messages, Files, Keyboards - o User Access Control - o Inline Queries - o jsshDB Bashbot key-value storage - o Background and Interactive Jobs - -* Developer Notes [doc/7_develop.md] - - o Debug bashbot - o Modules, addons, events - o Setup your environment - o Bashbot test suite - -* Examples Directory [examples] -* Webhook Example [examples/webhook] - - -Your very first bashbot in a nutshell - -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/]. -First you need to create a new Telegram Bot token [doc/1_firstbot.md] 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 and go into bot dir - 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. - -Now open the Telegram App on your mobile phone and start a chat with your bot (your bot's -username is shown after 'Bot Name:'): - - /start - - You are Botadmin - Available commands: - /start: _Start bot and get this message_. - /help: _Get this message_. - /info: _Get shorter info message about this bot_.... - - /info - - 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 [#Documentation]. - -Log files - -Bashbot actions are logged to BASHBOT.log. Telegram send/receive errors are logged to -ERROR.log. Start bashbot in debug mode to see all messages sent to / received from -Telegram, as well as bash command error messages. -To enable debug mode, start bashbot with debug as third argument: bashbot start debug - - |__ logs - | |__ BASHBOT.log # log what your bot is doing ... - | |__ ERROR.log # connection errors from / to Telegram API - | | - | |__ DEBUG.log # stdout/stderr of you bot (debug mode enabled) - | |__ MESSAGE.log # full text of all message send/received (debug mode enabled) - ------------------------------------------------------------------------------------------- - -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 bulletproof, 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 appropriately, disable globbing with set - -f and quote everything). In addition remove unused scripts and examples from your Bot -(e.g. everything in example/) and disable/remove all unused bot commands. -It's important to escape or remove $ and ` in input from user, files or network (as -bashbot does). One of the powerful features of Unix shells is variable and command -substitution using ${var}, $(cmd) and `cmd` can lead to remote code execution (RCE) or -remote information disclosure (RID) bugs if unescaped $ or ` 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). In addition bashbot has a test suite [doc/7_develop.md] to check if important -functionality is working as expected. - -Use printf whenever possible - -If you're writing a script that accepts external input (e.g. from the user as arguments or -the file system), you shouldn't use echo to display it. Use printf whenever possible -[https://unix.stackexchange.com/a/6581]. - -Run your Bot as a restricted user - -I recommend running your bot as a user with almost no access rights. All files your Bot -has write access to are in danger of being overwritten/deleted if your bot is hacked. For -the same reason every file your Bot can read is in danger of being 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 Linux/Unix systems. See Expert use [doc/4_expert.md] on -how to run your Bot as an other user. - -Secure your Bot installation - -Your Bot configuration must not be readable by 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, data-bot-bash/ and logs/ 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. -Note: Keep old log files in a safe place or even better delete them, they are GDPR -relevant and may contain information [https://github.com/topkecleon/telegram-bot-bash/ -issues/174] you don't want to be public. - -FAQ - - -Is this Bot insecure? - -Bashbot is not more (in)secure than a Bot written in another 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 ... -Note: Up to version 0.941 (mai/22/2020) telegram-bot-bash had a remote code execution bug, -please update if you use an older version! - -Why Bash and not the much better xyz? - -Well, that's a damn good question... maybe because I'm a Unix admin from the stone age. -Nevertheless there are more reasons from my side: - -* bashbot will run wherever bash and (gnu) sed is available, from embedded Linux to - mainframe -* easy to integrate with other shell scripts, e.g. for sending system message / health - status -* no need to install or learn a new programming language, library or framework -* 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 that you could 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 [doc/7_develop.md]. - -Can I send messages from CLI and scripts? - -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 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 - -You can also source bashbot for use in your scripts, for more information see Expert Use -[doc/4_expert.md]. - -Blocked by telegram? - -This may happen if too many or wrong requests are sent to api.telegram.org, e.g. using a -invalid token or invalid API calls. If the block stay for longer time you can ask telegram -service to unblock your IP-Address. -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 - -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-Address 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.45-dev-37-gfdbfceb - diff --git a/addons/antiFlood.sh b/addons/antiFlood.sh index 1fd6ef8..c46cfcc 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.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a # used events: # diff --git a/addons/example.sh b/addons/example.sh index 15cd539..763b67c 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.40-0-gf9dab50 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a # # If an event occurs each registered event function is called. # diff --git a/bashbot.rc b/bashbot.rc index a3d6332..16694a8 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,7 +5,7 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.45-dev-74-g0a296ea +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a # shellcheck disable=SC2009 # shellcheck disable=SC2181 # shellcheck disable=SC2250 diff --git a/bashbot.sh b/bashbot.sh index 7d2d39c..f5a72b8 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-73-geb0c227 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a ################################################################## # are we running in a terminal? @@ -747,6 +747,16 @@ event_send() { done } +# cleanup activities on startup, called from startbot and resume background jobs +# $1 action, timestamp for action is saved in config +bot_cleanup() { + # cleanup countfile on startup + jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" + [ -f "${COUNTFILE}.jssh.flock" ] && rm -f "${COUNTFILE}.jssh.flock" + # store action time and cleanup botconfig on startup + [ -n "$1" ] && jssh_updateKeyDB "$1" "$(_date)" "${BOTCONFIG}" + [ -f "${BOTCONFIG}.jssh.flock" ] && rm -f "${BOTCONFIG}.jssh.flock" +} # fallback version, full version is in bin/bashbot_init.in.sh # initialize bot environment, user and permissions diff --git a/bin/any_command.sh b/bin/any_command.sh index 349356a..d02e8a6 100755 --- a/bin/any_command.sh +++ b/bin/any_command.sh @@ -21,7 +21,7 @@ USAGE='any_command.sh [-h|--help] [--force|--reference] bot_command args ...' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 30.01.2021 10:24 # -#### $$VERSION$$ v1.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a #=============================================================================== #### diff --git a/bin/bashbot_env.inc.sh b/bin/bashbot_env.inc.sh index 17c0f5e..033539f 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.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a #=============================================================================== ############ diff --git a/bin/bashbot_init.inc.sh b/bin/bashbot_init.inc.sh index b4ca71d..8293037 100644 --- a/bin/bashbot_init.inc.sh +++ b/bin/bashbot_init.inc.sh @@ -11,7 +11,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.01.2021 13:42 # -#### $$VERSION$$ v1.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a #=============================================================================== # shellcheck disable=SC2059 diff --git a/bin/bashbot_stats.sh b/bin/bashbot_stats.sh index 331033a..b23fa41 100755 --- a/bin/bashbot_stats.sh +++ b/bin/bashbot_stats.sh @@ -17,7 +17,7 @@ USAGE='bashbot_stats.sh [-h|--help] [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 23.12.2020 20:34 # -#### $$VERSION$$ v1.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a #=============================================================================== # set bashbot environment diff --git a/bin/delete_message.sh b/bin/delete_message.sh index 5de96fb..5a396d5 100755 --- a/bin/delete_message.sh +++ b/bin/delete_message.sh @@ -20,7 +20,7 @@ USAGE='delete_message.sh [-h|--help] "CHAT[ID]" "MESSAGE[ID]" [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 03.01.2021 15:37 # -#### $$VERSION$$ v1.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a #=============================================================================== #### diff --git a/bin/edit_buttons.sh b/bin/edit_buttons.sh index 8ce27c3..014acdc 100755 --- a/bin/edit_buttons.sh +++ b/bin/edit_buttons.sh @@ -26,7 +26,7 @@ USAGE='send_message.sh [-h|--help] "CHAT[ID]" "MESSAGE[ID]" "text|url" ...' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 21.01.2021 08:10 # -#### $$VERSION$$ v1.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a #=============================================================================== #### diff --git a/bin/edit_message.sh b/bin/edit_message.sh index be810da..2d271e6 100755 --- a/bin/edit_message.sh +++ b/bin/edit_message.sh @@ -23,7 +23,7 @@ USAGE='send_edit_message.sh [-h|--help] [format|caption] "CHAT[ID]" "MESSAGE[ID] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 23.12.2020 16:52 # -#### $$VERSION$$ v1.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a #=============================================================================== #### diff --git a/bin/kickban_user.sh b/bin/kickban_user.sh index f119575..55695f3 100755 --- a/bin/kickban_user.sh +++ b/bin/kickban_user.sh @@ -20,7 +20,7 @@ USAGE='kickban_user.sh [-h|--help] [-u|--unban] "CHAT[ID]" "USER[ID]" [debug]' # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 25.01.2021 20:34 # -#### $$VERSION$$ v1.45-dev-69-gb454827 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a #=============================================================================== #### diff --git a/bin/process_batch.sh b/bin/process_batch.sh index 7f2845f..26f0fca 100755 --- a/bin/process_batch.sh +++ b/bin/process_batch.sh @@ -21,7 +21,7 @@ USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.02.2021 13:14 # -#### $$VERSION$$ v1.45-dev-72-g7500ca0 +#### $$VERSION$$ v1.45-dev-75-gfdb2b3a #=============================================================================== #### diff --git a/bin/process_update.sh b/bin/process_update.sh index 7ba74ca..4288832 100755 --- a/bin/process_update.sh +++ b/bin/process_update.sh @@ -15,7 +15,7 @@ USAGE='process_update.sh [-h|--help] [debug] [ Date: Thu, 4 Mar 2021 14:02:45 +0100 Subject: [PATCH 43/53] dev: fix can't get remote version --- README.html | 397 ++++++++++++++++++++++++++++++++++++++++ README.txt | 301 ++++++++++++++++++++++++++++++ dev/hooks/pre-commit.sh | 3 +- 3 files changed, 700 insertions(+), 1 deletion(-) diff --git a/README.html b/README.html index e69de29..fd5e035 100644 --- a/README.html +++ b/README.html @@ -0,0 +1,397 @@ + + + + + + + Bashbot README + + + + +
+

Bashbot README

+
+

+Bashbot - A Telegram bot written in bash. +

+Written by Drew (@topkecleon) and Kay M (@gnadelwartz). + +

Contributions by Daniil Gentili (@danog), JuanPotato, BigNerd95, TiagoDanin, iicc1 and dcoomber.

+

Released to the public domain wherever applicable. Elsewhere, consider it released under the WTFPLv2.

+

Linted by #ShellCheck

+

Prerequisites

+

Uses JSON.sh/JSON.awk and the magic of sed.

+

Bashbot is written in bash. It depends on commands typically available in a Linux/Unix Environment. For more information on commands provided by recent versions of coreutils, busybox or toybox, see Developer Notes.

+

Note for MacOS and BSD Users: Bashbot will not run without installing additional software as it uses modern bash and (gnu) grep/sed features. See Install Bashbot.

+

Note for embedded systems: You need to install a "real" bash as the vanilla installation of busybox or toybox is not sufficient. See Install Bashbot.

+

Bashbot Documentation and Downloads are available on www.github.com.

+

Documentation

+
    +
  • Introduction to Telegram Bots
  • +
  • Install Bashbot +
      +
    • Install release
    • +
    • Install from github
    • +
    • Update Bashbot
    • +
    • Notes on Updates
    • +
  • +
  • Get Bottoken from Botfather
  • +
  • Getting Started +
      +
    • Managing your Bot
    • +
    • Receive data
    • +
    • Send messages
    • +
    • Send files, locations, keyboards
    • +
  • +
  • Advanced Features +
      +
    • Access Control
    • +
    • Interactive Chats
    • +
    • Background Jobs
    • +
    • Inline queries
    • +
    • Send message errors
    • +
  • +
  • Expert Use +
      +
    • Handling UTF-8 character sets
    • +
    • Run as other user or system service
    • +
    • Schedule bashbot from Cron
    • +
    • Use from CLI and Scripts
    • +
    • Customize Bashbot Environment
    • +
  • +
  • Best Practices +
      +
    • Customize mycommands.sh
    • +
    • Overwrite/disable commands
    • +
    • Separate logic from commands
    • +
    • Test your Bot with shellcheck
    • +
  • +
  • Function Reference +
      +
    • Sending Messages, Files, Keyboards
    • +
    • User Access Control
    • +
    • Inline Queries
    • +
    • jsshDB Bashbot key-value storage
    • +
    • Background and Interactive Jobs
    • +
  • +
  • Developer Notes +
      +
    • Debug bashbot
    • +
    • Modules, addons, events
    • +
    • Setup your environment
    • +
    • Bashbot test suite
    • +
  • +
  • Examples Directory
  • +
  • Webhook Example
  • +
+

Your very first bashbot in a nutshell

+

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 and go into bot dir
+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 chat with your bot (your bot's username is shown after 'Bot Name:'):

+
/start
+
+You are Botadmin
+Available commands:
+  /start: _Start bot and get this message_.
+  /help: _Get this message_.
+  /info: _Get shorter info message about this bot_....
+
+/info
+
+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 to BASHBOT.log. Telegram send/receive errors are logged to ERROR.log. Start bashbot in debug mode to see all messages sent to / received from Telegram, as well as bash command error messages.

+

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

+
|__ logs
+|     |__ BASHBOT.log  # log what your bot is doing ...
+|     |__ ERROR.log    # connection errors from / to Telegram API
+|     |
+|     |__ DEBUG.log    # stdout/stderr of you bot (debug mode enabled)
+|     |__ MESSAGE.log  # full text of all message send/received (debug mode enabled)
+
+

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 bulletproof, 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 appropriately, disable globbing with set -f and quote everything). In addition remove unused scripts and examples from your Bot (e.g. everything in example/) and disable/remove all unused bot commands.

+

It's important to escape or remove $ and ` in input from user, files or network (as bashbot does). One of the powerful features of Unix shells is variable and command substitution using ${var}, $(cmd) and `cmd` can lead to remote code execution (RCE) or remote information disclosure (RID) bugs if unescaped $ or ` 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 that accepts external input (e.g. from the user as arguments or the file system), you shouldn't use echo to display it. Use printf whenever possible.

+

Run your Bot as a restricted user

+

I recommend running your bot as a user with almost no access rights. All files your Bot has write access to are in danger of being overwritten/deleted if your bot is hacked. For the same reason every file your Bot can read is in danger of being 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 Linux/Unix systems. See Expert use on how to run your Bot as an other user.

+

Secure your Bot installation

+

Your Bot configuration must not be readable by 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, data-bot-bash/ and logs/ 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.

+

Note: Keep old log files in a safe place or even better delete them, they are GDPR relevant and may contain information you don't want to be public.

+

FAQ

+

Is this Bot insecure?

+

Bashbot is not more (in)secure than a Bot written in another 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 ...

+

Note: Up to version 0.941 (mai/22/2020) telegram-bot-bash had a remote code execution bug, please update if you use an older version!

+

Why Bash and not the much better xyz?

+

Well, that's a damn good question... maybe because I'm a Unix admin from the stone age. Nevertheless there are more reasons from my side:

+
    +
  • bashbot will run wherever bash and (gnu) sed is available, from embedded Linux to mainframe
  • +
  • easy to integrate with other shell scripts, e.g. for sending system message / health status
  • +
  • no need to install or learn a new programming language, library or framework
  • +
  • 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 that you could 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.

+

Can I send messages from CLI and scripts?

+

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
+

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

+

Blocked by telegram?

+

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

+

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
+

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-Address 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.45-dev-75-gfdb2b3a

+ + diff --git a/README.txt b/README.txt index b413e56..b185de9 100644 --- a/README.txt +++ b/README.txt @@ -20,3 +20,304 @@ ******ooo*.............* ***o*........** **...** + + + + + +Bashbot README + + + Bashbot - A Telegram bot written in bash. + +Written by Drew (@topkecleon) and Kay M (@gnadelwartz). +Contributions by Daniil Gentili (@danog), JuanPotato, BigNerd95, TiagoDanin, iicc1 and +dcoomber. +Released to the public domain wherever applicable. Elsewhere, consider it released under +the WTFPLv2 [http://www.wtfpl.net/txt/copying/]. +Linted by #ShellCheck + +Prerequisites + +Uses JSON.sh [http://github.com/dominictarr/JSON.sh]/JSON.awk [https://github.com/step-/ +JSON.awk] and the magic of sed. +Bashbot is written in bash. It depends on commands typically available in a Linux/Unix +Environment. For more information on commands provided by recent versions of coreutils +[https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands], busybox [https:// +en.wikipedia.org/wiki/BusyBox#Commands] or toybox [https://landley.net/toybox/help.html], +see Developer Notes [doc/7_develop.md#common-commands]. +Note for MacOS and BSD Users: Bashbot will not run without installing additional software +as it uses modern bash and (gnu) grep/sed features. See Install Bashbot [doc/ +0_install.md]. +Note for embedded systems: You need to install a "real" bash as the vanilla installation +of busybox or toybox is not sufficient. See Install Bashbot [doc/0_install.md]. +Bashbot Documentation [https://github.com/topkecleon/telegram-bot-bash] and Downloads +[https://github.com/topkecleon/telegram-bot-bash/releases] are available on www.github.com +[https://www.github.com]. + +Documentation + + +* Introduction to Telegram Bots [https://core.telegram.org/bots] +* Install Bashbot [doc/0_install.md] + + o Install release + o Install from github + o Update Bashbot + o Notes on Updates + +* Get Bottoken from Botfather [doc/1_firstbot.md] +* Getting Started [doc/2_usage.md] + + o Managing your Bot + o Receive data + o Send messages + o Send files, locations, keyboards + +* Advanced Features [doc/3_advanced.md] + + o Access Control + o Interactive Chats + o Background Jobs + o Inline queries + o Send message errors + +* Expert Use [doc/4_expert.md] + + o Handling UTF-8 character sets + o Run as other user or system service + o Schedule bashbot from Cron + o Use from CLI and Scripts + o Customize Bashbot Environment + +* Best Practices [doc/5_practice.md] + + o Customize mycommands.sh + o Overwrite/disable commands + o Separate logic from commands + o Test your Bot with shellcheck + +* Function Reference [doc/6_reference.md] + + o Sending Messages, Files, Keyboards + o User Access Control + o Inline Queries + o jsshDB Bashbot key-value storage + o Background and Interactive Jobs + +* Developer Notes [doc/7_develop.md] + + o Debug bashbot + o Modules, addons, events + o Setup your environment + o Bashbot test suite + +* Examples Directory [examples] +* Webhook Example [examples/webhook] + + +Your very first bashbot in a nutshell + +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/]. +First you need to create a new Telegram Bot token [doc/1_firstbot.md] 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 and go into bot dir + 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. + +Now open the Telegram App on your mobile phone and start a chat with your bot (your bot's +username is shown after 'Bot Name:'): + + /start + + You are Botadmin + Available commands: + /start: _Start bot and get this message_. + /help: _Get this message_. + /info: _Get shorter info message about this bot_.... + + /info + + 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 [#Documentation]. + +Log files + +Bashbot actions are logged to BASHBOT.log. Telegram send/receive errors are logged to +ERROR.log. Start bashbot in debug mode to see all messages sent to / received from +Telegram, as well as bash command error messages. +To enable debug mode, start bashbot with debug as third argument: bashbot start debug + + |__ logs + | |__ BASHBOT.log # log what your bot is doing ... + | |__ ERROR.log # connection errors from / to Telegram API + | | + | |__ DEBUG.log # stdout/stderr of you bot (debug mode enabled) + | |__ MESSAGE.log # full text of all message send/received (debug mode enabled) + +------------------------------------------------------------------------------------------ + +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 bulletproof, 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 appropriately, disable globbing with set - +f and quote everything). In addition remove unused scripts and examples from your Bot +(e.g. everything in example/) and disable/remove all unused bot commands. +It's important to escape or remove $ and ` in input from user, files or network (as +bashbot does). One of the powerful features of Unix shells is variable and command +substitution using ${var}, $(cmd) and `cmd` can lead to remote code execution (RCE) or +remote information disclosure (RID) bugs if unescaped $ or ` 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). In addition bashbot has a test suite [doc/7_develop.md] to check if important +functionality is working as expected. + +Use printf whenever possible + +If you're writing a script that accepts external input (e.g. from the user as arguments or +the file system), you shouldn't use echo to display it. Use printf whenever possible +[https://unix.stackexchange.com/a/6581]. + +Run your Bot as a restricted user + +I recommend running your bot as a user with almost no access rights. All files your Bot +has write access to are in danger of being overwritten/deleted if your bot is hacked. For +the same reason every file your Bot can read is in danger of being 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 Linux/Unix systems. See Expert use [doc/4_expert.md] on +how to run your Bot as an other user. + +Secure your Bot installation + +Your Bot configuration must not be readable by 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, data-bot-bash/ and logs/ 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. +Note: Keep old log files in a safe place or even better delete them, they are GDPR +relevant and may contain information [https://github.com/topkecleon/telegram-bot-bash/ +issues/174] you don't want to be public. + +FAQ + + +Is this Bot insecure? + +Bashbot is not more (in)secure than a Bot written in another 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 ... +Note: Up to version 0.941 (mai/22/2020) telegram-bot-bash had a remote code execution bug, +please update if you use an older version! + +Why Bash and not the much better xyz? + +Well, that's a damn good question... maybe because I'm a Unix admin from the stone age. +Nevertheless there are more reasons from my side: + +* bashbot will run wherever bash and (gnu) sed is available, from embedded Linux to + mainframe +* easy to integrate with other shell scripts, e.g. for sending system message / health + status +* no need to install or learn a new programming language, library or framework +* 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 that you could 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 [doc/7_develop.md]. + +Can I send messages from CLI and scripts? + +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 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 + +You can also source bashbot for use in your scripts, for more information see Expert Use +[doc/4_expert.md]. + +Blocked by telegram? + +This may happen if too many or wrong requests are sent to api.telegram.org, e.g. using a +invalid token or invalid API calls. If the block stay for longer time you can ask telegram +service to unblock your IP-Address. +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 + +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-Address 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.45-dev-75-gfdb2b3a + diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index 725a79c..25a28c4 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -41,9 +41,10 @@ else exit 1 fi +# get version strings REMOTEVER="$(git ls-remote -t --refs 2>/dev/null | tail -1 | sed -e 's/.*\/v//' -e 's/-.*//')" VERSION="$(git describe --tags | sed -e 's/-.*//' -e 's/v//' -e 's/,/./')" - +[ -z "${REMOTEVER}" ] && REMOTEVER="${VERSION}" # LOCAL version must greater than latest REMOTE release version printf "Update Version of modified files\n" From c4e2981116f1487353254613ec7e72f601832716 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Mar 2021 11:48:06 +0100 Subject: [PATCH 44/53] example: webhook: improvce doc for full processing --- examples/webhook/README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/webhook/README.md b/examples/webhook/README.md index a6a0a32..37ade9a 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -21,8 +21,9 @@ Setup webhook with Apache: - install bashbot as described in [Bashbot Installation](../../doc/0_install.md) - create file `data-bot-bash/webhook-fifo-` (_\ as in `botconfig.jssh`_) -- run `sudo bashbot.sh init` to setup bashbot to run as same user as Apache (_e.g. www_) +- run `sudo bashbot.sh init` to setup bashbot to run as same user as web server (_e.g. www_) - create a directory in web root: `telegram/` (_ as `botconfig.jssh`_) +- give web server access to directory (_e.g.`chown www:www -R telegram`_) - go into the new directory and copy all files from `examples/webhook` to it - edit file `BASHBOT_HOME` to contain ithe Bashbot installation directory as first line (_other lines are ignored_) - execute `php index.php` with user id of web server to test write access to `data-bot-bash/webhook-fifo- @@ -60,16 +61,16 @@ Full webhook processing use an external script to imitate Bashbot polling mode w 2. run `bashbot.sh init` to setup bashbot to run with your user id 2. Create a named pipe: `mkfifo data-bot-bash/webhook-fifo-botname` and give the web server write access to it 3. execute `php index.php` with user id of web server to test write access to `data-bot-bash/webhook-fifo- -4. Start the script to imitate Bashbot polling mode:\ +4. Start script for Bashbot webhook polling mode:\ `bin/process-batch.sh --startbot --watch data-bot-bash/webhook-fifo-` The script read updates from given file line by line and forward updates to Bashbot update processing. `--startbot` will run the startup actions (_e.g. load addons, start TIMER, trigger first run_) and `--watch` will wait for new updates instead of exit on end of file. Short form: 'bin/process-batch.sh -s -w' -To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-` and stop `bin/process-batch.sh`. +If script works as expected, you may run Bashbot webook polling in background by using `./bachbot.rc starthook/stophook`. -If full processing seems to work well on the command line, you can use `./bachbot.rc starthook/stophook` to run processing in background. +To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-` and stop `bin/process-batch.sh`. #### Enable webhook on Telegram side @@ -98,5 +99,5 @@ webhook updates only over secure TLS connections with a valid SSL certificate ch `socat` looks like a tool to listen for Telegram updates from bash scripts, let's see ... -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.45-dev-77-g235f26a From b1af0f07e7c63fd4c2b1dfd120ce596910acb9f9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Mar 2021 12:01:21 +0100 Subject: [PATCH 45/53] dev: version.sh: ask to update ALL files --- dev/version.sh | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dev/version.sh b/dev/version.sh index d2d53ed..a99818a 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.45-dev-78-gc4e2981 # shellcheck disable=SC2016 # # Easy Versioning in git: @@ -44,8 +44,14 @@ VERSION="$(git describe --tags --long)" printf "Update to version %s ...\n" "${VERSION}" # only regular files, ignore .dot files/dirs, e.g. .git .gitinore in BASEDIR -FILES="$(find ./* -type f)" -[ "$1" != "" ] && FILES="$*" +if [ -n "$1" ]; then + FILES="$*" +else + printf "Update version string in all files? (y/N)\b\b" + read -r answer + [[ "${answer}" != "y" && "${answer}" != "Y" ]] && exit + FILES="$(find ./* -type f)" +fi # autogenerate REMADME.html REMADE.txt if [[ "${FILES}" == *"README.md"* ]]; then From 24638662d3558b81a0525f8079636f811792ebd8 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Mar 2021 12:03:04 +0100 Subject: [PATCH 46/53] bin: process_batch: delay between processing updates --- bin/process_batch.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bin/process_batch.sh b/bin/process_batch.sh index 26f0fca..8ae8e85 100755 --- a/bin/process_batch.sh +++ b/bin/process_batch.sh @@ -21,7 +21,7 @@ USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.02.2021 13:14 # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.45-dev-79-gb1af0f0 #=============================================================================== #### @@ -76,6 +76,8 @@ fi # kill all sub processes on exit trap 'kill $(jobs -p) 2>/dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME}"' webhook stopped ..."; printf "Bot in batch mode killed!\n"' EXIT HUP QUIT +# wait after (first) update to avoid processing to many in parallel +UPDWAIT="0.5" # use tail to read appended updates # shellcheck disable=SC2086,SC2248 tail ${follow} ${lines} "${file}" |\ @@ -89,4 +91,6 @@ tail ${follow} ${lines} "${file}" |\ # process telegram update "${COMMAND}" "$2" + sleep "${UPDWAIT}" + UPDWAIT="0.05" done From a5b230646b8cdfc19081df524e62ed64b5622e52 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Mar 2021 12:33:15 +0100 Subject: [PATCH 47/53] bin: process_batch: output messages with date prefix --- bin/process_batch.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/process_batch.sh b/bin/process_batch.sh index 8ae8e85..38f0bba 100755 --- a/bin/process_batch.sh +++ b/bin/process_batch.sh @@ -21,7 +21,7 @@ USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.02.2021 13:14 # -#### $$VERSION$$ v1.45-dev-79-gb1af0f0 +#### $$VERSION$$ v1.45-dev-80-g2463866 #=============================================================================== #### @@ -60,13 +60,13 @@ file="${WEBHOOK}" # start bot if [ -n "${startbot}" ]; then # warn when starting bot without pipe - [ -p "${file}" ] || printf "%b\n" "${ORANGE}Warning${NC}: File is not a pipe:${GREY} ${file##*/}${NC}" + [ -p "${file}" ] || printf "%(%c)T: %b\n" -1 "${ORANGE}Warning${NC}: File is not a pipe:${GREY} ${file##*/}${NC}" start_bot "$2" "webhook" - printf "${GREEN}Bot start actions done, start reading updates ....${NN}" + printf "%(%c)T: %b\n" -1 "${GREEN}Bot start actions done, start reading updates ....${NN}" fi # check file exist if [[ ! -r "${file}" || -d "${file}" ]]; then - printf "%b\n" "${RED}Error${NC}: File not readable:${GREY} ${file}${NC}." + printf "%(%c)T: %b\n" -1 "${RED}Error${NC}: File not readable:${GREY} ${file}${NC}." exit 1 fi @@ -74,7 +74,7 @@ fi # ready, do stuff here ----- # kill all sub processes on exit -trap 'kill $(jobs -p) 2>/dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME}"' webhook stopped ..."; printf "Bot in batch mode killed!\n"' EXIT HUP QUIT +trap 'kill $(jobs -p) 2>/dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME}"' webhook stopped ..."; printf "%(%c)T: %s\n" -1 "Bot in batch mode killed!\n"' EXIT HUP QUIT # wait after (first) update to avoid processing to many in parallel UPDWAIT="0.5" From 761aa46b66ab7903b4902475e17a902ba801757e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Mar 2021 19:48:08 +0100 Subject: [PATCH 48/53] dev: standalone: fix addons --- dev/make-standalone.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 0672e27..90819eb 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -11,7 +11,7 @@ # 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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.45-dev-81-ga5b2306 ################################################################### #shellcheck disable=SC1090 @@ -21,7 +21,7 @@ source "${0%/*}/dev.inc.sh" #DISTNAME="telegram-bot-bash" DISTDIR="./STANDALONE" DISTMKDIR="data-bot-bash logs bin bin/logs addons" -DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules bin scripts LICENSE README.* doc botacl botconfig.jssh $(echo "addons/"*.sh)" +DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules bin scripts LICENSE README.* doc botacl botconfig.jssh addons" # run pre_commit on files [ "$1" != "--notest" ] && dev/hooks/pre-commit.sh From 47a032d582688c7f36a6f5b6d71ed01b2ea1b083 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Mar 2021 20:12:30 +0100 Subject: [PATCH 49/53] dev: make-standalone: include in distribution --- dev/make-distribution.sh | 6 +++++- dev/make-standalone.sh | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index ab0153b..0a35fa8 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -7,7 +7,7 @@ # # Options: --notest - skip tests # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.45-dev-82-g761aa46 ############################################################## #shellcheck disable=SC1090 @@ -20,6 +20,7 @@ DISTDIR="./DIST/${DISTNAME}" DISTMKDIR="data-bot-bash logs bin bin/logs addons" DISTFILES="bashbot.sh commands.sh mycommands.sh.clean bin doc examples scripts modules LICENSE README.md README.txt README.html" +DISTFILESDEV="dev/make-standalone.sh dev/dev.inc.sh dev/inject-json.sh dev/make-html.sh dev/obfuscate.sh" DISTFILESDIST="mycommands.sh mycommands.conf bashbot.rc $(echo "addons/"*.sh)" # run tests first! @@ -39,6 +40,9 @@ mkdir -p "${DISTDIR}" 2>/dev/null printf "Copy files\n" # shellcheck disable=SC2086 cp -r ${DISTFILES} "${DISTDIR}" +mkdir "${DISTDIR}/dev" +# shellcheck disable=SC2086 +cp ${DISTFILESDEV} "${DISTDIR}/dev" cd "${DISTDIR}" || exit 1 printf "Create directories\n" diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 90819eb..4f48c61 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -11,12 +11,14 @@ # 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.45-dev-81-ga5b2306 +#### $$VERSION$$ v1.45-dev-82-g761aa46 ################################################################### +incfile="${0%/*}/dev.inc.sh" #shellcheck disable=SC1090 -source "${0%/*}/dev.inc.sh" +[ -f "${incfile}" ] && source "${incfile}" [ ! -f "bashbot.sh" ] && printf "bashbot.sh not found in %s\n" " $(pwd)" && exit 1 +[ -z "${BASE_DIR}" ] && BASE_DIR="$(pwd)" #DISTNAME="telegram-bot-bash" DISTDIR="./STANDALONE" @@ -24,7 +26,7 @@ DISTMKDIR="data-bot-bash logs bin bin/logs addons" DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules bin scripts LICENSE README.* doc botacl botconfig.jssh addons" # run pre_commit on files -[ "$1" != "--notest" ] && dev/hooks/pre-commit.sh +[[ -f ""${incfile} && "$1" != "--notest" ]] && dev/hooks/pre-commit.sh # create dir for distribution and copy files printf "Create directories and copy files\n" @@ -41,6 +43,7 @@ do done # inject JSON.sh into distribution + # shellcheck disable=SC1090 source "${BASE_DIR}/dev/inject-json.sh" From ae525c47e0bdc0801ed53ba6fd77c30acd60350b Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 6 Mar 2021 11:45:28 +0100 Subject: [PATCH 50/53] dev: make-standalone: fix standalone outside dev --- dev/make-distribution.sh | 4 ++-- dev/make-standalone.sh | 39 ++++++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 0a35fa8..962944b 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -7,7 +7,7 @@ # # Options: --notest - skip tests # -#### $$VERSION$$ v1.45-dev-82-g761aa46 +#### $$VERSION$$ v1.45-dev-83-g47a032d ############################################################## #shellcheck disable=SC1090 @@ -20,7 +20,7 @@ DISTDIR="./DIST/${DISTNAME}" DISTMKDIR="data-bot-bash logs bin bin/logs addons" DISTFILES="bashbot.sh commands.sh mycommands.sh.clean bin doc examples scripts modules LICENSE README.md README.txt README.html" -DISTFILESDEV="dev/make-standalone.sh dev/dev.inc.sh dev/inject-json.sh dev/make-html.sh dev/obfuscate.sh" +DISTFILESDEV="dev/make-standalone.sh dev/inject-json.sh dev/make-html.sh dev/obfuscate.sh" DISTFILESDIST="mycommands.sh mycommands.conf bashbot.rc $(echo "addons/"*.sh)" # run tests first! diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 4f48c61..27dc15f 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -11,29 +11,43 @@ # 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.45-dev-82-g761aa46 +#### $$VERSION$$ v1.45-dev-83-g47a032d ################################################################### +# include git config and change to base dir incfile="${0%/*}/dev.inc.sh" #shellcheck disable=SC1090 [ -f "${incfile}" ] && source "${incfile}" -[ ! -f "bashbot.sh" ] && printf "bashbot.sh not found in %s\n" " $(pwd)" && exit 1 -[ -z "${BASE_DIR}" ] && BASE_DIR="$(pwd)" +# seems we are not in a dev env +if [ -z "${BASE_DIR}" ]; then + BASE_DIR="$(pwd)" + [[ "${BASE_DIR}" == *"/dev" ]] && BASE_DIR="${BASE_DIR%/*}" + # go to basedir + cd "${BASE_DIR}" || exit 1 +fi + +# see if if bashbot is in base dir +[ ! -f "bashbot.sh" ] && printf "bashbot.sh not found in %s\n" " $(pwd)" && exit 1 + +# run pre_commit if exist +[[ -f "dev/dev.inc.sh" && "$1" != "--notest" ]] && dev/hooks/pre-commit.sh + +# files and dirs to copy #DISTNAME="telegram-bot-bash" DISTDIR="./STANDALONE" -DISTMKDIR="data-bot-bash logs bin bin/logs addons" -DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules bin scripts LICENSE README.* doc botacl botconfig.jssh addons" - -# run pre_commit on files -[[ -f ""${incfile} && "$1" != "--notest" ]] && dev/hooks/pre-commit.sh +DISTMKDIR="data-bot-bash logs bin/logs addons" +DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules scripts LICENSE README.* doc botacl botconfig.jssh addons" +DISTBINFILES="bin/bashbot_env.inc.sh bin/bashbot_stats.sh bin/process_batch.sh bin/process_update.sh bin/send_broadcast.sh bin/send_message.sh" # create dir for distribution and copy files printf "Create directories and copy files\n" -mkdir -p "${DISTDIR}" 2>/dev/null - +mkdir -p "${DISTDIR}/bin" 2>/dev/null # shellcheck disable=SC2086 -cp -r ${DISTFILES} "${DISTDIR}" 2>/dev/null +cp -rp ${DISTFILES} "${DISTDIR}" 2>/dev/null +# shellcheck disable=SC2086 +cp -p ${DISTBINFILES} "${DISTDIR}/bin" 2>/dev/null + cd "${DISTDIR}" || exit 1 # shellcheck disable=SC2250 @@ -43,7 +57,6 @@ do done # inject JSON.sh into distribution - # shellcheck disable=SC1090 source "${BASE_DIR}/dev/inject-json.sh" @@ -70,7 +83,7 @@ printf "OK, now lets do the magic ...\n\t... create unified commands.sh\n" mv $$commands.sh commands.sh rm -f mycommands.sh -printf "\n... create unified bashbot.sh\n" +printf "\t... create unified bashbot.sh\n" { # first head of bashbot.sh From 41e688381762e242852dbf6fac741f6150fce9cc Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Mar 2021 09:09:48 +0100 Subject: [PATCH 51/53] dev: make-standalone: improvments, extra files, make-dist: exclude test --- bin/process_batch.sh | 6 +++--- dev/make-distribution.sh | 6 +++--- dev/make-distribution.sh.exclude | 4 +++- dev/make-standalone.sh | 7 +++++-- dev/make-standalone.sh.include | 1 + 5 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 dev/make-standalone.sh.include diff --git a/bin/process_batch.sh b/bin/process_batch.sh index 38f0bba..7e59c93 100755 --- a/bin/process_batch.sh +++ b/bin/process_batch.sh @@ -21,7 +21,7 @@ USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.02.2021 13:14 # -#### $$VERSION$$ v1.45-dev-80-g2463866 +#### $$VERSION$$ v1.45-dev-84-gae525c4 #=============================================================================== #### @@ -62,7 +62,7 @@ if [ -n "${startbot}" ]; then # warn when starting bot without pipe [ -p "${file}" ] || printf "%(%c)T: %b\n" -1 "${ORANGE}Warning${NC}: File is not a pipe:${GREY} ${file##*/}${NC}" start_bot "$2" "webhook" - printf "%(%c)T: %b\n" -1 "${GREEN}Bot start actions done, start reading updates ....${NN}" + printf "%(%c)T: %b\n" -1 "${GREEN}Bot start actions done, start reading updates ....${NC}" fi # check file exist if [[ ! -r "${file}" || -d "${file}" ]]; then @@ -74,7 +74,7 @@ fi # ready, do stuff here ----- # kill all sub processes on exit -trap 'kill $(jobs -p) 2>/dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME}"' webhook stopped ..."; printf "%(%c)T: %s\n" -1 "Bot in batch mode killed!\n"' EXIT HUP QUIT +trap 'kill $(jobs -p) 2>/dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME}"' webhook stopped ..."; printf "%(%c)T: %s\n" -1 "Bot in batch mode killed!"' EXIT HUP QUIT # wait after (first) update to avoid processing to many in parallel UPDWAIT="0.5" diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 962944b..e5a7c15 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -7,7 +7,7 @@ # # Options: --notest - skip tests # -#### $$VERSION$$ v1.45-dev-83-g47a032d +#### $$VERSION$$ v1.45-dev-84-gae525c4 ############################################################## #shellcheck disable=SC1090 @@ -20,7 +20,7 @@ DISTDIR="./DIST/${DISTNAME}" DISTMKDIR="data-bot-bash logs bin bin/logs addons" DISTFILES="bashbot.sh commands.sh mycommands.sh.clean bin doc examples scripts modules LICENSE README.md README.txt README.html" -DISTFILESDEV="dev/make-standalone.sh dev/inject-json.sh dev/make-html.sh dev/obfuscate.sh" +DISTFILESDEV="dev/make-standalone.sh dev/make-standalone.sh.include dev/inject-json.sh dev/make-html.sh dev/obfuscate.sh" DISTFILESDIST="mycommands.sh mycommands.conf bashbot.rc $(echo "addons/"*.sh)" # run tests first! @@ -40,7 +40,7 @@ mkdir -p "${DISTDIR}" 2>/dev/null printf "Copy files\n" # shellcheck disable=SC2086 cp -r ${DISTFILES} "${DISTDIR}" -mkdir "${DISTDIR}/dev" +mkdir -p "${DISTDIR}/dev" # shellcheck disable=SC2086 cp ${DISTFILESDEV} "${DISTDIR}/dev" cd "${DISTDIR}" || exit 1 diff --git a/dev/make-distribution.sh.exclude b/dev/make-distribution.sh.exclude index c03a2d7..5098716 100644 --- a/dev/make-distribution.sh.exclude +++ b/dev/make-distribution.sh.exclude @@ -1,5 +1,7 @@ +STANDALONE data-bot-bash/* -webhook-fifo +test +webhook-fifo* JSON.awk bashbot.rc mycommands.sh diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 27dc15f..bef39a1 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -11,7 +11,7 @@ # 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.45-dev-83-g47a032d +#### $$VERSION$$ v1.45-dev-84-gae525c4 ################################################################### # include git config and change to base dir @@ -37,9 +37,12 @@ fi #DISTNAME="telegram-bot-bash" DISTDIR="./STANDALONE" DISTMKDIR="data-bot-bash logs bin/logs addons" -DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules scripts LICENSE README.* doc botacl botconfig.jssh addons" +DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh modules scripts LICENSE README.* doc botacl botconfig.jssh addons" DISTBINFILES="bin/bashbot_env.inc.sh bin/bashbot_stats.sh bin/process_batch.sh bin/process_update.sh bin/send_broadcast.sh bin/send_message.sh" +# add extra files +DISTFILES+=" $(cat "${BASE_DIR}/dev/${0##*/}.include")" + # create dir for distribution and copy files printf "Create directories and copy files\n" mkdir -p "${DISTDIR}/bin" 2>/dev/null diff --git a/dev/make-standalone.sh.include b/dev/make-standalone.sh.include new file mode 100644 index 0000000..f92d11b --- /dev/null +++ b/dev/make-standalone.sh.include @@ -0,0 +1 @@ +dev/obfuscate.sh From 8adca9beea3be106d29b9bc3c25fb479e9fa1f72 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Mar 2021 08:06:35 +0100 Subject: [PATCH 52/53] final adjustments for v1.5 --- bin/bashbot_init.inc.sh | 4 ++-- dev/make-distribution.sh | 4 ++-- dev/make-distribution.sh.exclude | 1 + dev/make-standalone.sh | 10 ++++---- dev/make-standalone.sh.include | 4 ++++ doc/2_usage.md | 39 +++++++++++++++++++++----------- doc/6_reference.md | 4 ++-- examples/webhook/README.md | 5 ++-- modules/processUpdates.sh | 11 +++++---- 9 files changed, 52 insertions(+), 30 deletions(-) diff --git a/bin/bashbot_init.inc.sh b/bin/bashbot_init.inc.sh index 8293037..8f8f803 100644 --- a/bin/bashbot_init.inc.sh +++ b/bin/bashbot_init.inc.sh @@ -11,7 +11,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.01.2021 13:42 # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.45-dev-85-g41e6883 #=============================================================================== # shellcheck disable=SC2059 @@ -76,7 +76,7 @@ bot_init() { [ -n "${INTERACTIVE}" ] && read -r runuser fi # check if mycommands exist - if [ ! -r "${BASHBOT_ETC:-.}/mycommands.sh" ]; then + if [[ ! -r "${BASHBOT_ETC:-.}/mycommands.sh" && -r ${BASHBOT_ETC:-.}/mycommands.sh.dist ]]; then printf "Mycommands.sh not found, copy ${GREY}lean file, xamples or one${NC} to mycommands.sh? (c/e/N) N\b" read -r ANSWER [[ "${ANSWER}" =~ ^[cC] ]] && cp -f "${BASHBOT_ETC:-.}/mycommands.sh.clean" "${BASHBOT_ETC:-.}/mycommands.sh" diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index e5a7c15..1872747 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -7,7 +7,7 @@ # # Options: --notest - skip tests # -#### $$VERSION$$ v1.45-dev-84-gae525c4 +#### $$VERSION$$ v1.45-dev-85-g41e6883 ############################################################## #shellcheck disable=SC1090 @@ -20,7 +20,7 @@ DISTDIR="./DIST/${DISTNAME}" DISTMKDIR="data-bot-bash logs bin bin/logs addons" DISTFILES="bashbot.sh commands.sh mycommands.sh.clean bin doc examples scripts modules LICENSE README.md README.txt README.html" -DISTFILESDEV="dev/make-standalone.sh dev/make-standalone.sh.include dev/inject-json.sh dev/make-html.sh dev/obfuscate.sh" +DISTFILESDEV="dev/make-standalone.sh dev/inject-json.sh dev/make-html.sh dev/obfuscate.sh" DISTFILESDIST="mycommands.sh mycommands.conf bashbot.rc $(echo "addons/"*.sh)" # run tests first! diff --git a/dev/make-distribution.sh.exclude b/dev/make-distribution.sh.exclude index 5098716..01e467f 100644 --- a/dev/make-distribution.sh.exclude +++ b/dev/make-distribution.sh.exclude @@ -7,6 +7,7 @@ bashbot.rc mycommands.sh mycommands.conf awk-patch.sh +make-standalone.sh.include *.jssh* botacl *.flock diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index bef39a1..79a4f67 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -11,7 +11,7 @@ # 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.45-dev-84-gae525c4 +#### $$VERSION$$ v1.45-dev-85-g41e6883 ################################################################### # include git config and change to base dir @@ -37,11 +37,13 @@ fi #DISTNAME="telegram-bot-bash" DISTDIR="./STANDALONE" DISTMKDIR="data-bot-bash logs bin/logs addons" -DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh modules scripts LICENSE README.* doc botacl botconfig.jssh addons" +DISTFILES="bashbot.sh commands.sh mycommands.sh modules scripts LICENSE README.* doc addons" DISTBINFILES="bin/bashbot_env.inc.sh bin/bashbot_stats.sh bin/process_batch.sh bin/process_update.sh bin/send_broadcast.sh bin/send_message.sh" -# add extra files -DISTFILES+=" $(cat "${BASE_DIR}/dev/${0##*/}.include")" +# add extra files, minimum mycommands.conf +extrafile="${BASE_DIR}/dev/${0##*/}.include" +[ ! -f "${extrafile}" ] && printf "bashbot.rc\nbotacl\nbotconfig.jssh\nmycommands.conf\ndev/obfuscate.sh\n" >"${extrafile}" +DISTFILES+=" $(<"${extrafile}")" # create dir for distribution and copy files printf "Create directories and copy files\n" diff --git a/dev/make-standalone.sh.include b/dev/make-standalone.sh.include index f92d11b..32bf3d3 100644 --- a/dev/make-standalone.sh.include +++ b/dev/make-standalone.sh.include @@ -1 +1,5 @@ +bashbot.rc +botacl +botconfig.jssh +mycommands.conf dev/obfuscate.sh diff --git a/doc/2_usage.md b/doc/2_usage.md index ea68abd..f6657e1 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -127,7 +127,7 @@ bin/send_message.sh "CHAT[ID]" "Hey, I just wanted to let you know that the bot' To replace a message already sent to one user or chat run the following command: ```bash -bin/send_edit_message.sh "CHAT[ID]" "12345" "Done!" +bin/edit_message.sh "CHAT[ID]" "12345" "Done!" ["OK"] "true" ["ID"] "12345" @@ -150,9 +150,14 @@ Note: to get help about a script in bin/ run `scriptname.sh --help` Evertime a Telegram update is received, you can read incoming data using the following variables: In case you need other update values, the array `UPD` contains complete Telegram response. -### Regular Messages +### Processing Messages -These Variables are always present in regular messages: +If an update is received from Telegram, the message is pre processed by Bashbot and the following bash variables are set for use in `mycommands.sh`. + +These variables are always present if a message is pre processed: + +* `${ME}`: Name of your bot +* `${BOTADMIN}`: User id of bot administrator * `${MESSAGE}`: Current message text * `${MESSAGE[ID]}`: ID of current message @@ -215,11 +220,10 @@ The following variables are set if the message contains optional parts: ### Service Messages -Service Messages are regular messages not itended for end users, instead they signal special events to the -client, e.g. new users. +Service Messages are updates not itended for end users, instead they signal special events in a chat, 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". +If a service message is received bashbot pre processing sets `${MESSAGE}` according to the service message type, +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 @@ -255,10 +259,17 @@ e.g. if a new user joins a chat MESSAGE is set to "/_new_chat_user". ### Inline query messages -Inline query messages are special messages used for interaction with the user, -they contain the following variables only: +Inline query messages are special messages for direct interaction with your bot. +If an user starts an inline conversation an inline query is sent after each user keystroke. -* `${iQUERY}`: Current inline query +To receive inline messages you must set `inline=1` in `mycommands.conf` and in botfather. +THe message contatains all characters so far typed from the user. + +An received inline query must be anserwered with `answer_inline_query`, see also (Inline Query)[6_reference.md#inline-query] + +If an inline query is received only the following variables are available: + +* `${iQUERY}`: Inline message typed so far by user * `$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 @@ -266,9 +277,9 @@ they contain the following variables only: * `${iQUERY[LAST_NAME]}`: User's last name + ### Callback button messages -Callback button messages special messages swedn from callback buttons, -they contain the following variables only: +Callback button messages special messages swend from callback buttons, they contain the following variables only: * `$iBUTTON`: This array contains the ID, First name, last name, username and user id of the user clicked on the button * `${iBUTTON[ID]}`: Callback query ID @@ -287,6 +298,8 @@ they contain the following variables only: After every `send_xxx` `get_xxx` call the array BOTSENT contains the most important values from Telegram response. In case you need other response values , the array `UPD` contains complete Telegram response. +You can use the array values to check if a commands was successful and get returned values from Telegram. + ### BOTSENT array * `$BOTSENT`: This array contains the parsed results from the last transmission to telegram. @@ -377,5 +390,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.45-dev-85-g41e6883 diff --git a/doc/6_reference.md b/doc/6_reference.md index 320a577..476849b 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -826,7 +826,7 @@ fi ---- -### Inline Queries - answer direct queries to bot +### Inline Query Inline Queries allows users to interact with your bot directly without sending extra commands. As an answer to an inline query you can send back one or more results to the Telegram client. The Telegram client will then show the results to the user and let him select one. @@ -1673,5 +1673,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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.45-dev-85-g41e6883 diff --git a/examples/webhook/README.md b/examples/webhook/README.md index 37ade9a..d397c59 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -9,8 +9,7 @@ If your server is reachable from the Internet its possible to use the method des Prerequisite for receiving Telegram updates with webhook is a valid SSL certificate, a self signed certificate will not be sufficient. -*Note:* You need at least sudo rights to setup webhook. - +Webhook processing require special setup on server and Telegram side, therefore it's implemented as separate scripts and you need at least sudo rights to setup. #### Setup Apache webhook @@ -99,5 +98,5 @@ webhook updates only over secure TLS connections with a valid SSL certificate ch `socat` looks like a tool to listen for Telegram updates from bash scripts, let's see ... -#### $$VERSION$$ v1.45-dev-77-g235f26a +#### $$VERSION$$ v1.45-dev-85-g41e6883 diff --git a/modules/processUpdates.sh b/modules/processUpdates.sh index 14e5746..9479914 100644 --- a/modules/processUpdates.sh +++ b/modules/processUpdates.sh @@ -4,7 +4,7 @@ # File: processUpdates.sh # Note: DO NOT EDIT! this file will be overwritten on update # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.45-dev-85-g41e6883 ################################################################## ############## @@ -296,9 +296,12 @@ start_bot() { DEBUGMSG="Start BASHBOT updates in Mode \"${1:-normal}\" ==========" log_update "${DEBUGMSG}" # redirect to Debug.log - # shellcheck disable=SC2153 - [[ "$1" == *"debug" ]] && exec &>>"${DEBUGLOG}" - log_debug "${DEBUGMSG}"; DEBUGMSG="$1" + if [[ "$1" == *"debug" ]]; then + # shellcheck disable=SC2153 + exec &>>"${DEBUGLOG}" + log_debug "${DEBUGMSG}"; + fi + DEBUGMSG="$1" [[ "${DEBUGMSG}" == "xdebug"* ]] && set -x # cleaup old pipes and empty logfiles find "${DATADIR}" -type p -not -name "webhook-fifo-*" -delete From 369124bada186b6b9b0192d065ee383c11ab4ca7 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 10 Mar 2021 08:39:17 +0100 Subject: [PATCH 53/53] Bashbot Version 1.5 --- README.html | 3 ++- README.md | 2 +- README.txt | 2 +- addons/antiFlood.sh | 2 +- addons/example.sh | 2 +- bashbot.rc | 9 ++++++--- bashbot.sh | 2 +- bin/any_command.sh | 2 +- bin/bashbot_env.inc.sh | 2 +- bin/bashbot_init.inc.sh | 2 +- bin/bashbot_stats.sh | 2 +- bin/delete_message.sh | 2 +- bin/edit_buttons.sh | 2 +- bin/edit_message.sh | 2 +- bin/kickban_user.sh | 2 +- bin/process_batch.sh | 4 ++-- bin/process_update.sh | 2 +- bin/promote_user.sh | 2 +- bin/send_broadcast.sh | 2 +- bin/send_buttons.sh | 2 +- bin/send_dice.sh | 2 +- bin/send_file.sh | 2 +- bin/send_message.sh | 2 +- commands.sh | 2 +- dev/all-tests.sh | 2 +- dev/dev.inc.sh | 2 +- dev/git-add.sh | 2 +- dev/hooks/post-commit.sh | 2 +- dev/hooks/pre-commit.sh | 6 +++--- dev/hooks/pre-push.sh | 2 +- dev/inject-json.sh | 2 +- dev/install-hooks.sh | 2 +- dev/make-distribution.sh | 2 +- dev/make-html.sh | 2 +- dev/make-standalone.sh | 5 ++++- dev/obfuscate.sh | 2 +- dev/shellcheck.files | 2 +- dev/version.sh | 4 ++-- doc/0_install.md | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 18 ++++++++++-------- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 2 +- doc/7_develop.md | 2 +- examples/README.md | 2 +- examples/background-scripts/run_diskusage.sh | 2 +- examples/background-scripts/run_filecontent.sh | 2 +- examples/background-scripts/run_filename.sh | 2 +- examples/background-scripts/run_notify.sh | 2 +- examples/bash2env.sh | 2 +- examples/bashbot-multi.sh | 2 +- examples/bashbot.cron | 2 +- examples/calc.sh | 2 +- examples/jsonDB-keyboard/mycommands.sh | 2 +- examples/notify.sh | 2 +- examples/question.sh | 2 +- examples/send-system-status/botacl | 2 +- examples/send-system-status/mycommands.sh | 2 +- examples/webhook/README.md | 2 +- examples/webhook/index.php | 2 +- modules/aliases.sh | 2 +- modules/answerInline.sh | 2 +- modules/background.sh | 2 +- modules/chatMember.sh | 2 +- modules/jsonDB.sh | 4 ++-- modules/processUpdates.sh | 2 +- modules/sendMessage.sh | 2 +- mycommands.conf | 2 +- mycommands.sh | 2 +- mycommands.sh.clean | 2 +- scripts/interactive.sh.clean | 2 +- test/ADD-test-new.sh | 2 +- test/ALL-tests.inc.sh | 2 +- test/a-commit-test.sh | 2 +- test/b-example-test.sh | 2 +- test/c-init-test.sh | 2 +- test/d-JSON.sh-test.sh | 2 +- test/d-process_inline-test.sh | 2 +- test/d-process_message-test.sh | 2 +- test/d-send_message-test.sh | 2 +- test/d-user_is-test.sh | 2 +- test/e-env-test.sh | 2 +- 84 files changed, 107 insertions(+), 98 deletions(-) diff --git a/README.html b/README.html index fd5e035..f681054 100644 --- a/README.html +++ b/README.html @@ -145,6 +145,7 @@ pre > code.sourceCode { white-space: pre; position: relative; } pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } pre > code.sourceCode > span:empty { height: 1.2em; } + .sourceCode { overflow: visible; } code.sourceCode > span { color: inherit; text-decoration: inherit; } div.sourceCode { margin: 1em 0; } pre.sourceCode { margin: 0; } @@ -392,6 +393,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.45-dev-75-gfdb2b3a

+

$$VERSION$$ v1.5-0-g8adca9b

diff --git a/README.md b/README.md index a5ea3d0..47a38ed 100644 --- a/README.md +++ b/README.md @@ -242,4 +242,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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/README.txt b/README.txt index b185de9..025d869 100644 --- a/README.txt +++ b/README.txt @@ -319,5 +319,5 @@ 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.45-dev-75-gfdb2b3a +$$VERSION$$ v1.5-0-g8adca9b diff --git a/addons/antiFlood.sh b/addons/antiFlood.sh index c46cfcc..0161c76 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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # used events: # diff --git a/addons/example.sh b/addons/example.sh index 763b67c..5dfb42a 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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # # If an event occurs each registered event function is called. # diff --git a/bashbot.rc b/bashbot.rc index 16694a8..9334286 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -5,7 +5,7 @@ # # tested on: ubuntu, opensuse, debian # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # shellcheck disable=SC2009 # shellcheck disable=SC2181 # shellcheck disable=SC2250 @@ -38,7 +38,7 @@ runas="nobody" # your bot name as given to botfather, e.g. mysomething_bot name="" # your bot installation dir -bashbotdir="/usr/local/github/telegram-bot-bash-develop/DIST/telegram-bot-bash" +bashbotdir="/usr/local/telegram-bot-bash" databotdir="${bashbotdir}/data-bot-bash" # programs to run bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh" @@ -57,6 +57,7 @@ case "$1" in RETVAL=$? ;; 'starthook') + printf "Starting bashbot in webhook mode ... " $runcmd "$webhook $mode >${bashbotdir}/logs/WEBHOOK.log &" # >/dev/null 2>&1 /dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME}"' webhook stopped ..."; printf "%(%c)T: %s\n" -1 "Bot in batch mode killed!"' EXIT HUP QUIT +trap 'kill $(jobs -p) 2>/dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME}"' webhook stopped ..."; printf "%(%c)T: %s\n" -1 "Bot in batch mode stopped!"' EXIT HUP QUIT # wait after (first) update to avoid processing to many in parallel UPDWAIT="0.5" diff --git a/bin/process_update.sh b/bin/process_update.sh index 4288832..8d55f6c 100755 --- a/bin/process_update.sh +++ b/bin/process_update.sh @@ -15,7 +15,7 @@ USAGE='process_update.sh [-h|--help] [debug] [ /dev/null || (( $(printf "%s\n" "${VERSION} >= ${REMOTEVER}" | bc -l) )); then # update version in bashbot files on push set +f - [ -f "${LASTPUSH}" ] && LASTFILES="$(find ./* -newer "${LASTPUSH}")" + [ -f "${LASTPUSH}" ] && LASTFILES="$(find ./* -newer "${LASTPUSH}" ! -path "./DIST/*" ! -path "./STANDALONE/*")" [ "${LASTFILES}" = "" ] && exit printf " " # shellcheck disable=SC2086 @@ -65,7 +65,7 @@ fi if command -v codespell &>/dev/null; then printf "Running codespell\n............................\n" - codespell -q 3 --skip="*.zip,*gz,*.log,*.html,*.txt,.git*,jsonDB-keyboard" -L "ba" + codespell -q 3 --skip="*.zip,*gz,*.log,*.html,*.txt,.git*,jsonDB-keyboard,DIST,STANDALONE" -L "ba" printf "if there are (to many) typo's shown, consider running:\ncodespell -i 3 -w --skip=\"*.log,*.html,*.txt,.git*,examples\" -L \"ba\"\n" else printf "consider installing codespell: pip install codespell\n" diff --git a/dev/hooks/pre-push.sh b/dev/hooks/pre-push.sh index c23edab..cea01ee 100755 --- a/dev/hooks/pre-push.sh +++ b/dev/hooks/pre-push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/inject-json.sh b/dev/inject-json.sh index fa60f39..b79e6f1 100644 --- a/dev/inject-json.sh +++ b/dev/inject-json.sh @@ -7,7 +7,7 @@ # # Usage: source inject-json.sh # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ############################################################## # download JSON.sh diff --git a/dev/install-hooks.sh b/dev/install-hooks.sh index 9150cfe..34bfcec 100755 --- a/dev/install-hooks.sh +++ b/dev/install-hooks.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #shellcheck disable=SC1090 source "${0%/*}/dev.inc.sh" diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 1872747..a9faf85 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -7,7 +7,7 @@ # # Options: --notest - skip tests # -#### $$VERSION$$ v1.45-dev-85-g41e6883 +#### $$VERSION$$ v1.5-0-g8adca9b ############################################################## #shellcheck disable=SC1090 diff --git a/dev/make-html.sh b/dev/make-html.sh index 0912443..1bad0a1 100644 --- a/dev/make-html.sh +++ b/dev/make-html.sh @@ -7,7 +7,7 @@ # # Usage: source make-hmtl # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ############################################################## # check for correct dir diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 79a4f67..f7746c6 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -11,7 +11,7 @@ # 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.45-dev-85-g41e6883 +#### $$VERSION$$ v1.5-0-g8adca9b ################################################################### # include git config and change to base dir @@ -55,6 +55,9 @@ cp -p ${DISTBINFILES} "${DISTDIR}/bin" 2>/dev/null cd "${DISTDIR}" || exit 1 +# remove log files +find . -name '*.log' -delete + # shellcheck disable=SC2250 for dir in $DISTMKDIR do diff --git a/dev/obfuscate.sh b/dev/obfuscate.sh index a3d1553..671987b 100755 --- a/dev/obfuscate.sh +++ b/dev/obfuscate.sh @@ -2,7 +2,7 @@ # # joke hack to obfuscate bashbot.min.sh # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # shellcheck disable=SC2028,SC2016,SC1117 infile="bashbot.sh" diff --git a/dev/shellcheck.files b/dev/shellcheck.files index c345b4a..c8369ec 100644 --- a/dev/shellcheck.files +++ b/dev/shellcheck.files @@ -1,5 +1,5 @@ # list of additional files to check from shellcheck -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b bashbot.rc mycommands.conf mycommands.sh.clean diff --git a/dev/version.sh b/dev/version.sh index a99818a..8da2c6e 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v1.45-dev-78-gc4e2981 +#### $$VERSION$$ v1.5-0-g8adca9b # shellcheck disable=SC2016 # # Easy Versioning in git: @@ -50,7 +50,7 @@ else printf "Update version string in all files? (y/N)\b\b" read -r answer [[ "${answer}" != "y" && "${answer}" != "Y" ]] && exit - FILES="$(find ./* -type f)" + FILES="$(find ./* -type f ! -path "./DIST/*" ! -path "./STANDALONE/*")" fi # autogenerate REMADME.html REMADE.txt diff --git a/doc/0_install.md b/doc/0_install.md index f49089a..c35342a 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -132,5 +132,5 @@ You must update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bas #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index df45b7d..6e547b9 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -65,5 +65,5 @@ group. This step is up to you actually. #### [Prev Installation](0_install.md) #### [Next Getting started](2_usage.md) -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/doc/2_usage.md b/doc/2_usage.md index f6657e1..c22edb6 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -30,7 +30,9 @@ Have FUN! │ ├── bashbot.sh # main bashbot script - DO NOT EDIT! ├── commands.sh # command dispatcher - DO NOT EDIT! -├── JSON.sh # bashbots JSON parser, see https://github.com/dominictarr/JSON.sh +├── JSON.sh # bashbot JSON parsers +│   ├── JSON.sh # sh implementation, https://github.com/dominictarr/JSON.sh +│   └── JSON.awk.dist # faster awk version, https://github.com/step-/JSON.awk │ ├── bin # ready to use scripts, use `scriptname --help` for help │   ├── bashbot_stats.sh # does what it says ... @@ -44,7 +46,7 @@ Have FUN! │   ├── kickban_user.sh # kick/unban user from given chat │   ├── promote_user.sh # promote/dente user rights in given chat │ │ -│   └── bashbot_env.inc.sh # sourced from scripts, adapt locations if needed +│   ├── bashbot_env.inc.sh # sourced from scripts, adapt locations if needed │   └── bashbot_init.inc.sh # sourced from bashbot.sh init │ ├── scripts # place your bashbot interactive and background scripts here @@ -55,15 +57,15 @@ Have FUN! ├── modules # optional functions, sourced by commands.sh │   ├── aliases.sh # to disable modules rename them xxx.sh.off │   ├── answerInline.sh -│   ├── jsshDB.sh # read and store JSON.sh style JSON, mandatory │   ├── background.sh # interactive and background functions -│   ├── chatMember.sh -│   └── sendMessage.sh # main send message functions, mandatory +│   ├── chatMember.sh # manage chat mambers +│   ├── jsshDB.sh # read and store JSON.sh style JSON, mandatory +│   ├── processUpdates.sh # process updates from telegram, mandatory (run bot) +│   └── sendMessage.sh # send message functions, mandatory │ ├── addons # optional addons, disabled by default │   ├── example.sh # to enable addons change their XXX_ENABLE to true -│   ├── antiFlood.sh # simple addon taking actions based on # files and text sent to chat -│   └── xxxxxage.sh +│   └── antiFlood.sh # simple addon taking actions based on # files and text sent to chat │ ├── bashbot.rc # start/stop script if you run bashbot as service │ @@ -390,5 +392,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v1.45-dev-85-g41e6883 +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 726977e..0c4876c 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/doc/4_expert.md b/doc/4_expert.md index c2abf09..fdc79eb 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -434,5 +434,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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/doc/5_practice.md b/doc/5_practice.md index 4b16a87..0d781e2 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/doc/6_reference.md b/doc/6_reference.md index 476849b..e40033d 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -1673,5 +1673,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.45-dev-85-g41e6883 +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/doc/7_develop.md b/doc/7_develop.md index 2780225..fdb43e6 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -387,5 +387,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/examples/README.md b/examples/README.md index 595c10c..ff92a69 100644 --- a/examples/README.md +++ b/examples/README.md @@ -60,6 +60,6 @@ plus use of keyboards in private chats. It's an extended version of mycommands.s **Webhook** contains instructions on how use webhook API to get updates from telegram instead polling Telegram server. -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/examples/background-scripts/run_diskusage.sh b/examples/background-scripts/run_diskusage.sh index d330112..9c6594f 100755 --- a/examples/background-scripts/run_diskusage.sh +++ b/examples/background-scripts/run_diskusage.sh @@ -4,7 +4,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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ###### # parameters diff --git a/examples/background-scripts/run_filecontent.sh b/examples/background-scripts/run_filecontent.sh index 7616fce..ff409ae 100755 --- a/examples/background-scripts/run_filecontent.sh +++ b/examples/background-scripts/run_filecontent.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display content of all new files in WATCHDIR # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ###### # parameters diff --git a/examples/background-scripts/run_filename.sh b/examples/background-scripts/run_filename.sh index 883f244..0b395ab 100755 --- a/examples/background-scripts/run_filename.sh +++ b/examples/background-scripts/run_filename.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display all new files in WATCHDIR # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ###### # parameters diff --git a/examples/background-scripts/run_notify.sh b/examples/background-scripts/run_notify.sh index 3e79146..62fc99a 100755 --- a/examples/background-scripts/run_notify.sh +++ b/examples/background-scripts/run_notify.sh @@ -4,7 +4,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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ###### # parameters diff --git a/examples/bash2env.sh b/examples/bash2env.sh index c3c20a1..e462bf2 100755 --- a/examples/bash2env.sh +++ b/examples/bash2env.sh @@ -6,7 +6,7 @@ # 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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/bashbot-multi.sh b/examples/bashbot-multi.sh index 53c6afc..f0753ff 100755 --- a/examples/bashbot-multi.sh +++ b/examples/bashbot-multi.sh @@ -2,7 +2,7 @@ # file. multibot.sh # description: run multiple telegram bots from one installation # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b if [ "$2" = "" ] || [ "$2" = "-h" ]; then echo "Usage: $0 botname command" diff --git a/examples/bashbot.cron b/examples/bashbot.cron index f32174f..52191a0 100644 --- a/examples/bashbot.cron +++ b/examples/bashbot.cron @@ -7,7 +7,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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b SHELL=/bin/sh diff --git a/examples/calc.sh b/examples/calc.sh index 4a86d52..3feaa33 100755 --- a/examples/calc.sh +++ b/examples/calc.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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ######################################################################## ###### diff --git a/examples/jsonDB-keyboard/mycommands.sh b/examples/jsonDB-keyboard/mycommands.sh index b73d5c1..c82a4be 100644 --- a/examples/jsonDB-keyboard/mycommands.sh +++ b/examples/jsonDB-keyboard/mycommands.sh @@ -10,7 +10,7 @@ # AUTHOR: KayM (), kay@rrr.de # DATE: 19.12.2020 19:03 # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/examples/notify.sh b/examples/notify.sh index 9ce3f77..bfbb45d 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -13,7 +13,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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ######################################################################## ###### diff --git a/examples/question.sh b/examples/question.sh index 266143d..d20b4ba 100755 --- a/examples/question.sh +++ b/examples/question.sh @@ -10,7 +10,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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ######################################################################## ###### diff --git a/examples/send-system-status/botacl b/examples/send-system-status/botacl index cc8b0d3..4efe4d6 100644 --- a/examples/send-system-status/botacl +++ b/examples/send-system-status/botacl @@ -1,7 +1,7 @@ # file: botacl # a user not listed here, will return false from 'user_is_allowed' # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # Format: # user:resource:chat diff --git a/examples/send-system-status/mycommands.sh b/examples/send-system-status/mycommands.sh index 6db07cd..36e4b7f 100644 --- a/examples/send-system-status/mycommands.sh +++ b/examples/send-system-status/mycommands.sh @@ -5,7 +5,7 @@ # to show how you can customize bashbot by only editing mycommands.sh # NOTE: this is not tested, simply copied from original source and reworked! # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/examples/webhook/README.md b/examples/webhook/README.md index d397c59..440cc3b 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -98,5 +98,5 @@ webhook updates only over secure TLS connections with a valid SSL certificate ch `socat` looks like a tool to listen for Telegram updates from bash scripts, let's see ... -#### $$VERSION$$ v1.45-dev-85-g41e6883 +#### $$VERSION$$ v1.5-0-g8adca9b diff --git a/examples/webhook/index.php b/examples/webhook/index.php index edcee8d..18ff9c6 100644 --- a/examples/webhook/index.php +++ b/examples/webhook/index.php @@ -11,7 +11,7 @@ * @license http://www.wtfpl.net/txt/copying/ WTFPLv2 * @since 30.01.2021 20:24 * -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ***********************************************************/ // bashbot home dir diff --git a/modules/aliases.sh b/modules/aliases.sh index 38ac81d..b834a79 100644 --- a/modules/aliases.sh +++ b/modules/aliases.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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # # will be automatically sourced from bashbot diff --git a/modules/answerInline.sh b/modules/answerInline.sh index aef9893..f8f601a 100644 --- a/modules/answerInline.sh +++ b/modules/answerInline.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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # will be automatically sourced from bashbot diff --git a/modules/background.sh b/modules/background.sh index bc4a471..2804ad1 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117,SC2059 -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # will be automatically sourced from bashbot diff --git a/modules/chatMember.sh b/modules/chatMember.sh index cc6073f..198207a 100644 --- a/modules/chatMember.sh +++ b/modules/chatMember.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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # will be automatically sourced from bashbot diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 96b5d4b..9c2d4be 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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # # source from commands.sh to use jsonDB functions # @@ -358,7 +358,7 @@ Json2Array() { # match ["....."]\t and replace \t with = and print delete ` 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/=/p' -e 's/`//g' -e 's/=(true|false)/="\1"/' -e 's/([^\]|^)\$/\1\\$/g')" ) + "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/p' -e 's/[`´]//g' -e 's/=(true|false)/="\1"/' -e 's/([^\]|^)\$/\1\\$/g')" ) } # get Config Key from jssh file without jsshDB # output ARRAY as JSON.sh style data diff --git a/modules/processUpdates.sh b/modules/processUpdates.sh index 9479914..c5286bb 100644 --- a/modules/processUpdates.sh +++ b/modules/processUpdates.sh @@ -4,7 +4,7 @@ # File: processUpdates.sh # Note: DO NOT EDIT! this file will be overwritten on update # -#### $$VERSION$$ v1.45-dev-85-g41e6883 +#### $$VERSION$$ v1.5-0-g8adca9b ################################################################## ############## diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index da9355a..b6e2df3 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.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # will be automatically sourced from bashbot diff --git a/mycommands.conf b/mycommands.conf index 422fb78..c74f079 100644 --- a/mycommands.conf +++ b/mycommands.conf @@ -12,7 +12,7 @@ # Author: KayM (gnadelwartz), kay@rrr.de # Created: 09.01.2021 07:27 # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ####################################################### ########## diff --git a/mycommands.sh b/mycommands.sh index 4761b4a..d17f1c7 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -13,7 +13,7 @@ # License: WTFPLv2 http://www.wtfpl.net/txt/copying/ # Author: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ####################################################### # shellcheck disable=SC1117 diff --git a/mycommands.sh.clean b/mycommands.sh.clean index 0e8e680..4cb32bf 100644 --- a/mycommands.sh.clean +++ b/mycommands.sh.clean @@ -10,7 +10,7 @@ # License: WTFPLv2 http://www.wtfpl.net/txt/copying/ # Author: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ####################################################### # shellcheck disable=SC1117 diff --git a/scripts/interactive.sh.clean b/scripts/interactive.sh.clean index 63862fc..4274db3 100755 --- a/scripts/interactive.sh.clean +++ b/scripts/interactive.sh.clean @@ -12,7 +12,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b ######################################################################## ###### diff --git a/test/ADD-test-new.sh b/test/ADD-test-new.sh index c453953..a47c93b 100755 --- a/test/ADD-test-new.sh +++ b/test/ADD-test-new.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # magic to ensure that we're always inside the root of our application, diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index 386308e..e45aa67 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -11,7 +11,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # common variables diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh index 9277d3c..944c5e8 100755 --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== ../dev/hooks/pre-commit.sh diff --git a/test/b-example-test.sh b/test/b-example-test.sh index 1d5e4a2..d68c549 100644 --- a/test/b-example-test.sh +++ b/test/b-example-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # include common functions and definitions diff --git a/test/c-init-test.sh b/test/c-init-test.sh index 3dd5961..c77ecf8 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # include common functions and definitions diff --git a/test/d-JSON.sh-test.sh b/test/d-JSON.sh-test.sh index d109226..aef3129 100755 --- a/test/d-JSON.sh-test.sh +++ b/test/d-JSON.sh-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_inline-test.sh b/test/d-process_inline-test.sh index 2c81f7c..55b53e1 100755 --- a/test/d-process_inline-test.sh +++ b/test/d-process_inline-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # include common functions and definitions diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index b6bfe6e..aea5411 100755 --- a/test/d-process_message-test.sh +++ b/test/d-process_message-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # include common functions and definitions diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index 7e55479..7bebb38 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # include common functions and definitions diff --git a/test/d-user_is-test.sh b/test/d-user_is-test.sh index 1d414b2..9c8a984 100755 --- a/test/d-user_is-test.sh +++ b/test/d-user_is-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # include common functions and definitions diff --git a/test/e-env-test.sh b/test/e-env-test.sh index dfc198a..ce0036a 100755 --- a/test/e-env-test.sh +++ b/test/e-env-test.sh @@ -10,7 +10,7 @@ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # -#### $$VERSION$$ v1.45-dev-75-gfdb2b3a +#### $$VERSION$$ v1.5-0-g8adca9b #=============================================================================== # include common functions and definitions