From f463a1f000a89d7d150c4d551cb98a899d4f8e9c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Apr 2019 17:44:55 +0200 Subject: [PATCH 01/17] recommend shellcheck --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c31e743..9f88fc7 100644 --- a/README.md +++ b/README.md @@ -428,6 +428,8 @@ Bash scripts in general are not designed to be bullet proof, so consider this Bo Whenever you are processing input from outside your bot you should disable globbing (set -f) and carefully quote everthing. +To improve you scripts we recommend to lint them with [shellcheck](https://www.shellcheck.net/). This can be done online or you can install ```shellchek``` from your distributions package manager. bashbot itself is also linted by shellcheck. + ### Run your Bot as a restricted user Every file your bot can write is in danger to be overwritten/deleted, In case of bad handling of user input every file your Bot can read is in danger of being disclosed. @@ -445,4 +447,4 @@ No - its not less (in)secure as any other Bot written in any other language. But If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.5-rc-4-g92e9e9c +#### $$VERSION$$ v0.6-dev-0-g352c64f From da5e96d49e82b167c7b0d8b5b4a727ab2216b376 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Apr 2019 17:50:03 +0200 Subject: [PATCH 02/17] shellcheck installation --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9f88fc7..e50c693 100644 --- a/README.md +++ b/README.md @@ -428,7 +428,7 @@ Bash scripts in general are not designed to be bullet proof, so consider this Bo Whenever you are processing input from outside your bot you should disable globbing (set -f) and carefully quote everthing. -To improve you scripts we recommend to lint them with [shellcheck](https://www.shellcheck.net/). This can be done online or you can install ```shellchek``` from your distributions package manager. bashbot itself is also linted by shellcheck. +To improve you scripts we recommend to lint them with [shellcheck](https://www.shellcheck.net/). This can be done online or you can [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). bashbot itself is also linted by shellcheck. ### Run your Bot as a restricted user Every file your bot can write is in danger to be overwritten/deleted, In case of bad handling of user input every file your Bot can read is in danger of being disclosed. @@ -447,4 +447,4 @@ No - its not less (in)secure as any other Bot written in any other language. But If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.6-dev-0-g352c64f +#### $$VERSION$$ v0.6-dev-1-gf463a1f From 614eae85c74c526ad071fe9e827e96275284776d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Apr 2019 18:24:05 +0200 Subject: [PATCH 03/17] start using shellcheck --- bashbot.rc | 5 ++++- commands.sh | 19 +++++++++++-------- notify | 2 +- question | 6 +++--- version | 11 ++++++----- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index 631f20e..83a4acc 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,10 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.5-rc-4-g92e9e9c +#### $$VERSION$$ v0.6-dev-2-gda5e96d +# shellcheck disable=SC2009 +# shellcheck disable=SC2181 + # ### BEGIN INIT INFO # Provides: bashbot diff --git a/commands.sh b/commands.sh index cd3eadb..406441b 100755 --- a/commands.sh +++ b/commands.sh @@ -4,7 +4,10 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.5-rc-3-gf67503c +#### $$VERSION$$ v0.6-dev-2-gda5e96d +# +# shellcheck disable=SC2154 +# shellcheck disable=SC2034 # adjust your language setting here, e.g.when run from other user or cron. # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -26,11 +29,11 @@ if [ "$1" = "source" ];then else if ! tmux ls | grep -v send | grep -q "$copname"; then [ ! -z "${URLS[*]}" ] && { - curl -s ${URLS[*]} -o $NAME + curl -s "${URLS[*]}" -o "$NAME" send_file "${CHAT[ID]}" "$NAME" "$CAPTION" rm -f "$NAME" } - [ ! -z ${LOCATION[*]} ] && send_location "${CHAT[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}" + [ ! -z "${LOCATION[*]}" ] && send_location "${CHAT[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}" # Inline if [ $INLINE == 1 ]; then @@ -61,7 +64,7 @@ else case "$MESSAGE" in '/question') checkproc - if [ $res -gt 0 ] ; then + if [ "$res" -gt 0 ] ; then startproc "./question" else send_normal_message "${CHAT[ID]}" "$MESSAGE already running ..." @@ -70,7 +73,7 @@ else '/run-notify') myback="notify"; checkback "$myback" - if [ $res -gt 0 ] ; then + if [ "$res" -gt 0 ] ; then background "./notify 60" "$myback" # notify every 60 seconds else send_normal_message "${CHAT[ID]}" "Background command $myback already running ..." @@ -78,7 +81,7 @@ else ;; '/stop-notify') myback="notify"; checkback "$myback" - if [ $res -eq 0 ] ; then + if [ "$res" -eq 0 ] ; then killback "$myback" send_normal_message "${CHAT[ID]}" "Background command $myback canceled." else @@ -120,10 +123,10 @@ Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash) '/cancel') checkprog - if [ $res -eq 0 ] ; then killproc && send_message "${CHAT[ID]}" "Command canceled.";else send_message "${CHAT[ID]}" "No command is currently running.";fi + if [ "$res" -eq 0 ] ; then killproc && send_message "${CHAT[ID]}" "Command canceled.";else send_message "${CHAT[ID]}" "No command is currently running.";fi ;; *) - if tmux ls | grep -v send | grep -q $copname;then inproc; else send_message "${CHAT[ID]}" "$MESSAGE" "safe";fi + if tmux ls | grep -v send | grep -q "$copname";then inproc; else send_message "${CHAT[ID]}" "$MESSAGE" "safe";fi ;; esac fi diff --git a/notify b/notify index 9e09814..6f990bf 100755 --- a/notify +++ b/notify @@ -2,7 +2,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.5-rc-3-gf67503c +#### $$VERSION$$ v0.6-dev-2-gda5e96d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 96cd161..8fcf0fd 100755 --- a/question +++ b/question @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.5-rc-3-gf67503c +#### $$VERSION$$ v0.6-dev-2-gda5e96d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -16,11 +16,11 @@ unset IFS echo "Why hello there. Would you like some tea (y/n)?" -read answer +read -r answer [[ $answer =~ ^([yY][eE][sS]|[yY])$ ]] && echo "OK then, here you go: http://www.rivertea.com/blog/wp-content/uploads/2013/12/Green-Tea.jpg" || echo "OK then." until [ "$SUCCESS" = "y" ] ;do echo 'Do you like Music? mykeyboardstartshere "Yass!" "No"' - read answer + read -r answer case $answer in 'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;; 'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;; diff --git a/version b/version index 79e21fe..e1695fd 100755 --- a/version +++ b/version @@ -1,6 +1,7 @@ -#!/bin/sh +#!/bin/bash # -#### $$VERSION$$ v0.5-rc-3-gf67503c +#### $$VERSION$$ v0.6-dev-2-gda5e96d +# shellcheck disable=SC2016 # # Easy Versioning in git: # @@ -36,15 +37,15 @@ unset IFS # set -f # if you are paranoid use set -f to disable globbing -VERSION="`git describe --tags --long`" +VERSION="$(git describe --tags --long)" echo "Update files to version $VERSION ..." -for file in `ls` +for file in * do [ ! -f "$file" ] && continue #[ "$file" == "version" ] && continue echo -n " $file" - sed -i 's/^#### $$VERSION$$.*/#### \$\$VERSION\$\$ '$VERSION'/' $file + sed -i 's/^#### $$VERSION$$.*/#### \$\$VERSION\$\$ '"$VERSION"'/' "$file" done echo " done." From b4e13bd3542da0cfacea8e80c4f800269f53f11c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Apr 2019 19:13:13 +0200 Subject: [PATCH 04/17] first shellcheck iteration for bashbot.sh --- README.md | 2 +- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 105 ++++++++++++++++++++++++++------------------------- commands.sh | 2 +- notify | 2 +- question | 2 +- version | 2 +- 8 files changed, 60 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index e50c693..5b2dfd6 100644 --- a/README.md +++ b/README.md @@ -447,4 +447,4 @@ No - its not less (in)secure as any other Bot written in any other language. But If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.6-dev-1-gf463a1f +#### $$VERSION$$ v0.6-dev-3-g614eae8 diff --git a/bashbot.cron b/bashbot.cron index bff1aca..18f9853 100644 --- a/bashbot.cron +++ b/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$$ v0.5-rc-4-g92e9e9c +#### $$VERSION$$ v0.6-dev-3-g614eae8 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 83a4acc..e1005f5 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.6-dev-2-gda5e96d +#### $$VERSION$$ v0.6-dev-3-g614eae8 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index cb56652..2dcc84e 100755 --- a/bashbot.sh +++ b/bashbot.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$$ v0.5-rc-4-g92e9e9c +#### $$VERSION$$ v0.6-dev-3-g614eae8 # # Exit Codes: # - 0 sucess (hopefully) @@ -29,8 +29,8 @@ if [ -t 1 ] && [ "$TERM" != "" ]; then fi # get location of bashbot.sh an change to bashbot dir -SCRIPT="./$(basename $0)" -SCRIPTDIR="$(dirname $0)" +SCRIPT="./$(basename "$0")" +SCRIPTDIR="$(dirname "$0")" if ! cd "${SCRIPTDIR}" ; then echo -e "${RED}ERROR: Can't change to ${SCRIPTDIR} ...${NC}" @@ -52,7 +52,7 @@ if [ ! -f "token" ]; then $CLEAR echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE${NC}" - read token + read -r token echo "$token" >> "token" fi @@ -98,7 +98,7 @@ FORWARD_URL=$URL'/forwardMessage' INLINE_QUERY=$URL'/answerInlineQuery' ME_URL=$URL'/getMe' DELETE_URL=$URL'/deleteMessage' -ME=$(curl -s $ME_URL | ./JSON.sh/JSON.sh -s | egrep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2) +ME=$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | grep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2) FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/' @@ -113,47 +113,48 @@ urlencode() { send_message() { + local chat, text, arg, keyboard, file, lat long, title, adress, sent [ "$2" = "" ] && return 1 - local chat="$1" - local text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" - local arg="$3" - [ "$3" != "safe" ] && { + chat="$1" + text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" + arg="$3" + [ "$arg" != "safe" ] && { text="$(echo "$text" | sed 's/ mynewlinestartshere /\r\n/g')" # hack for linebreaks in startproc scripts - local no_keyboard="$(echo $2 | sed '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" + no_keyboard="$(echo "$2" | sed '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" - local keyboard="$(echo "$2" | sed '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere //g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" + keyboard="$(echo "$2" | sed '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere //g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" - local file="$(echo "$2" | sed '/myfilelocationstartshere /!d;s/.*myfilelocationstartshere //g;s/ mykeyboardstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" + file="$(echo "$2" | sed '/myfilelocationstartshere /!d;s/.*myfilelocationstartshere //g;s/ mykeyboardstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" - local lat="$(echo "$2" | sed '/mylatstartshere /!d;s/.*mylatstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" + lat="$(echo "$2" | sed '/mylatstartshere /!d;s/.*mylatstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" - local long="$(echo "$2" | sed '/mylongstartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" + long="$(echo "$2" | sed '/mylongstartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" - local title="$(echo "$2" | sed '/mytitlestartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" + title="$(echo "$2" | sed '/mytitlestartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" - local address="$(echo "$2" | sed '/myaddressstartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mytitlestartshere.*//g;s/ mykeyboardendshere.*//g')" + address="$(echo "$2" | sed '/myaddressstartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mytitlestartshere.*//g;s/ mykeyboardendshere.*//g')" } if [ "$no_keyboard" != "" ]; then echo "remove_keyboard $chat $text" > $TMPDIR/prova remove_keyboard "$chat" "$text" - local sent=y + sent=y fi if [ "$keyboard" != "" ]; then send_keyboard "$chat" "$text" "$keyboard" - local sent=y + sent=y fi if [ "$file" != "" ]; then send_file "$chat" "$file" "$text" - local sent=y + sent=y fi - if [ "$lat" != "" -a "$long" != "" -a "$address" = "" -a "$title" = "" ]; then + if [ "$lat" != "" ] && [ "$long" != "" ] && [ "$address" = "" ] && [ "$title" = "" ]; then send_location "$chat" "$lat" "$long" - local sent=y + sent=y fi - if [ "$lat" != "" -a "$long" != "" -a "$address" != "" -a "$title" != "" ]; then + if [ "$lat" != "" ] && [ "$long" != "" ] && [ "$address" != "" ] && [ "$title" != "" ]; then send_venue "$chat" "$lat" "$long" "$title" "$address" - local sent=y + sent=y fi if [ "$sent" != "y" ];then send_text "$chat" "$text" @@ -291,7 +292,7 @@ send_keyboard() { local keyboard=init OLDIFS=$IFS IFS=$(echo -en "\"") - for f in $*;do [ "$f" != " " ] && local keyboard="$keyboard, [\"$f\"]";done + for f in "$@" ;do [ "$f" != " " ] && local keyboard="$keyboard, [\"$f\"]";done IFS=$OLDIFS local keyboard=${keyboard/init, /} res="$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"keyboard\": [$keyboard],\"one_time_keyboard\": true}")" @@ -305,14 +306,14 @@ remove_keyboard() { } get_file() { - [ "$1" != "" ] && echo $FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | egrep '\["result","file_path"\]' | cut -f 2 | cut -d '"' -f 2) + [ "$1" != "" ] && echo "$FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | grep '\["result","file_path"\]' | cut -f 2 | cut -d '"' -f 2)" } send_file() { [ "$2" = "" ] && return local chat_id=$1 local file=$2 - echo "$file" | grep -qE $FILE_REGEX || return + echo "$file" | grep -qE "$FILE_REGEX" || return local ext="${file##*.}" case $ext in mp3|flac) @@ -351,7 +352,7 @@ send_file() { local CAPTION="$3" ;; esac - send_action $chat_id $STATUS + send_action "$chat_id" "$STATUS" res="$(curl -s "$CUR_URL" -F "chat_id=$chat_id" -F "$WHAT=@$file" -F "caption=$CAPTION")" } @@ -370,7 +371,7 @@ send_location() { send_venue() { [ "$5" = "" ] && return [ "$6" != "" ] add="-F \"foursquare_id=$6\"" - res="$(curl -s "$VENUE_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3" -F "title=$4" -F "address=$5" $add)" + res="$(curl -s "$VENUE_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3" -F "title=$4" -F "address=$5" $add)" # what is add for? } @@ -409,11 +410,11 @@ killback() { killproc() { local fifo="$1${copname}" - (tmux kill-session -t "${fifo}"; echo imprettydarnsuredatdisisdaendofdacmd>$TMPDIR/${fifo}; tmux kill-session -t sendprocess_${fifo}; rm -f -r $TMPDIR/${fifo})2>/dev/null + (tmux kill-session -t "${fifo}"; echo imprettydarnsuredatdisisdaendofdacmd>"$TMPDIR/${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "$TMPDIR/${fifo}")2>/dev/null } inproc() { - tmux send-keys -t $copname "$MESSAGE ${URLS[*]} + tmux send-keys -t "$copname" "${MESSAGE[0]} ${URLS[*]} " } process_updates() { @@ -492,7 +493,7 @@ process_client() { # Location LOCATION[LONGITUDE]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","location","longitude"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" LOCATION[LATITUDE]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","location","latitude"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - NAME="$(echo ${URLS[*]} | sed 's/.*\///g')" + NAME="$(echo "${URLS[*]}" | sed 's/.*\///g')" rm "$TMP" # Tmux @@ -508,10 +509,10 @@ process_client() { # source the script with source as param to use functions in other scripts while [ "$1" == "startbot" ]; do { - UPDATE="$(curl -s $UPD_URL$OFFSET | ./JSON.sh/JSON.sh)" + UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./JSON.sh/JSON.sh)" # Offset - OFFSET="$(echo "$UPDATE" | egrep '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" + OFFSET="$(echo "$UPDATE" | grep '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" OFFSET=$((OFFSET+1)) if [ "$OFFSET" != "1" ]; then @@ -529,24 +530,24 @@ case "$1" in "outproc") until [ "$line" = "imprettydarnsuredatdisisdaendofdacmd" ];do line="" - read -t 10 line - [ "$line" != "" -a "$line" != "imprettydarnsuredatdisisdaendofdacmd" ] && send_message "$2" "$line" - done <$TMPDIR/$3 - rm -f -r $TMPDIR/$3 + read -r -t 10 line + [ "$line" != "" ] && [ "$line" != "imprettydarnsuredatdisisdaendofdacmd" ] && send_message "$2" "$line" + done <"$TMPDIR/$3" + rm -f -r "$TMPDIR/$3" ;; "count") - echo "A total of $(wc -l ${COUNT} | sed 's/count//g')users used me." + echo "A total of $(wc -l "${COUNT}" | sed 's/count//g')users used me." ;; "broadcast") - echo "Sending the broadcast $* to $(wc -l ${COUNT} | sed 's/count//g')users." - [ "$(wc -l ${COUNT} | sed 's/ count//g')" -gt "300" ] && sleep="sleep 0.5" + echo "Sending the broadcast $* to $(wc -l "${COUNT}" | sed 's/count//g')users." + [ "$(wc -l "${COUNT}" | sed 's/ count//g')" -gt "300" ] && sleep="sleep 0.5" shift - for f in "$(cat ${COUNT})";do send_message ${f//COUNT} "$*"; $sleep;done + for f in $(cat "${COUNT}"); do send_message "${f//COUNT}" "$*"; $sleep;done ;; "start") $CLEAR - tmux kill-session -t $ME&>/dev/null - tmux new-session -d -s $ME "bash $SCRIPT startbot" && echo -e "${GREEN}Bot started successfully.${NC}" + tmux kill-session -t "$ME" &>/dev/null + tmux new-session -d -s "$ME" "bash $SCRIPT startbot" && echo -e "${GREEN}Bot started successfully.${NC}" echo "Tmux session name $ME" || echo -e "${RED}An error occurred while starting the bot. ${NC}" send_markdown_message "${CHAT[ID]}" "*Bot started*" ;; @@ -554,19 +555,19 @@ case "$1" in MYUSER="$USER" [[ "$(id -u)" -eq "0" ]] && MYUSER="nobody" echo -n "Enter User to run basbot [$MYUSER]: " - read TOUSER + read -r TOUSER [ "$TOUSER" = "" ] && TOUSER="$MYUSER" - if ! compgen -u "$TOUSER" 2>&1 >/dev/null; then + if ! compgen -u "$TOUSER" >/dev/null 2&>1; then echo -e "${RED}User \"$TOUSER\" not found!${NC}" exit 3 else echo "Adjusting user in bashbot.rc ..." sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' bashbot.rc echo "Adjusting Owner and Permissions ..." - chown -R "$TOUSER" . * + chown -R "$TOUSER" . ./* chmod 711 . - chmod -R a-w * - chmod -R u+w "$COUNT" "$TMPDIR" *.log 2>/dev/null + chmod -R a-w ./* + chmod -R u+w "$COUNT" "$TMPDIR" ./*.log 2>/dev/null chmod -R o-r,o-w "$COUNT" "$TMPDIR" token 2>/dev/null ls -la exit @@ -586,10 +587,10 @@ case "$1" in JOB="${JOB%:*}" fifo="back-${JOB}-${ME}_${CHAT[ID]}" # compose fifo from jobname, $ME (botname) and CHAT[ID] echo "restartbackground ${PROG} ${fifo}" - ( tmux kill-session -t "${fifo}"; tmux kill-session -t sendprocess_${fifo}; rm -f -r $TMPDIR/${fifo}) 2>/dev/null + ( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "$TMPDIR/${fifo}") 2>/dev/null mkfifo "$TMPDIR/${fifo}" TMUX= tmux new-session -d -s "${fifo}" "${PROG} &>$TMPDIR/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>$TMPDIR/${fifo}" - TMUX= tmux new-session -d -s sendprocess_${fifo} "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" + TMUX= tmux new-session -d -s "sendprocess_${fifo}" "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" fi done ;; @@ -611,7 +612,7 @@ case "$1" in fifo="back-${JOB%:*}-${ME}_${REMOVE%%:*}" echo "killbackground ${fifo}" [ "$1" == "killback" ] && rm -f "$FILE" # remove job - ( tmux kill-session -t "${fifo}"; tmux kill-session -t sendprocess_${fifo}; rm -f -r $TMPDIR/${fifo}) 2>/dev/null + ( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "$TMPDIR/${fifo}") 2>/dev/null fi done ;; @@ -636,7 +637,7 @@ esac # warn if root if [[ "$(id -u)" -eq "0" ]] ; then - echo -e "\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}" + echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}" echo -e "${ORANGE}You are at HIGH RISK when processing user input with root privilegs!${NC}" fi diff --git a/commands.sh b/commands.sh index 406441b..79c4ff2 100755 --- a/commands.sh +++ b/commands.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$$ v0.6-dev-2-gda5e96d +#### $$VERSION$$ v0.6-dev-3-g614eae8 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/notify b/notify index 6f990bf..6802ca2 100755 --- a/notify +++ b/notify @@ -2,7 +2,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.6-dev-2-gda5e96d +#### $$VERSION$$ v0.6-dev-3-g614eae8 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 8fcf0fd..bae87eb 100755 --- a/question +++ b/question @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.6-dev-2-gda5e96d +#### $$VERSION$$ v0.6-dev-3-g614eae8 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index e1695fd..0b57f5c 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.6-dev-2-gda5e96d +#### $$VERSION$$ v0.6-dev-3-g614eae8 # shellcheck disable=SC2016 # # Easy Versioning in git: From 18b3b51ec7093e1da86eea88eb39bf7b3e681201 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 3 Apr 2019 18:14:22 +0200 Subject: [PATCH 05/17] fix multiple local --- bashbot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 2dcc84e..1baa93e 100755 --- a/bashbot.sh +++ b/bashbot.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$$ v0.6-dev-3-g614eae8 +#### $$VERSION$$ v0.6-dev-4-gb4e13bd # # Exit Codes: # - 0 sucess (hopefully) @@ -113,7 +113,7 @@ urlencode() { send_message() { - local chat, text, arg, keyboard, file, lat long, title, adress, sent + local chat text arg keyboard file lat long title adress sent [ "$2" = "" ] && return 1 chat="$1" text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" From 34d2e3de5cb7e95085993ca6aae6d4e167a1be20 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Apr 2019 11:58:55 +0200 Subject: [PATCH 06/17] finish bashbot.sh shellcheck --- bashbot.sh | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 1baa93e..d847361 100755 --- a/bashbot.sh +++ b/bashbot.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$$ v0.6-dev-4-gb4e13bd +#### $$VERSION$$ v0.6-dev-5-g18b3b51 # # Exit Codes: # - 0 sucess (hopefully) @@ -113,9 +113,9 @@ urlencode() { send_message() { - local chat text arg keyboard file lat long title adress sent + local text arg keyboard file lat long title address sent [ "$2" = "" ] && return 1 - chat="$1" + local chat="$1" text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" arg="$3" [ "$arg" != "safe" ] && { @@ -292,9 +292,9 @@ send_keyboard() { local keyboard=init OLDIFS=$IFS IFS=$(echo -en "\"") - for f in "$@" ;do [ "$f" != " " ] && local keyboard="$keyboard, [\"$f\"]";done + for f in "$@" ;do [ "$f" != " " ] && keyboard="$keyboard, [\"$f\"]";done IFS=$OLDIFS - local keyboard=${keyboard/init, /} + keyboard=${keyboard/init, /} res="$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"keyboard\": [$keyboard],\"one_time_keyboard\": true}")" } @@ -311,6 +311,7 @@ get_file() { send_file() { [ "$2" = "" ] && return + local CAPTION local chat_id=$1 local file=$2 echo "$file" | grep -qE "$FILE_REGEX" || return @@ -320,13 +321,13 @@ send_file() { CUR_URL=$AUDIO_URL WHAT=audio STATUS=upload_audio - local CAPTION="$3" + CAPTION="$3" ;; png|jpg|jpeg|gif) CUR_URL=$PHO_URL WHAT=photo STATUS=upload_photo - local CAPTION="$3" + CAPTION="$3" ;; webp) CUR_URL=$STICKER_URL @@ -337,7 +338,7 @@ send_file() { CUR_URL=$VIDEO_URL WHAT=video STATUS=upload_video - local CAPTION="$3" + CAPTION="$3" ;; ogg) @@ -349,7 +350,7 @@ send_file() { CUR_URL=$DOCUMENT_URL WHAT=document STATUS=upload_document - local CAPTION="$3" + CAPTION="$3" ;; esac send_action "$chat_id" "$STATUS" @@ -557,7 +558,7 @@ case "$1" in echo -n "Enter User to run basbot [$MYUSER]: " read -r TOUSER [ "$TOUSER" = "" ] && TOUSER="$MYUSER" - if ! compgen -u "$TOUSER" >/dev/null 2&>1; then + if ! compgen -u "$TOUSER" >/dev/null 2>&1; then echo -e "${RED}User \"$TOUSER\" not found!${NC}" exit 3 else From e936e6fe39d3c791aaf2f031efdfe381e87078b0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Apr 2019 12:11:17 +0200 Subject: [PATCH 07/17] fix if TMPDIR is empty and path may point to / --- bashbot.sh | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index d847361..56f66d7 100755 --- a/bashbot.sh +++ b/bashbot.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$$ v0.6-dev-5-g18b3b51 +#### $$VERSION$$ v0.6-dev-6-g34d2e3d # # Exit Codes: # - 0 sucess (hopefully) @@ -136,7 +136,7 @@ send_message() { } if [ "$no_keyboard" != "" ]; then - echo "remove_keyboard $chat $text" > $TMPDIR/prova + echo "remove_keyboard $chat $text" > ${TMPDIR:-.}/prova remove_keyboard "$chat" "$text" sent=y fi @@ -383,15 +383,15 @@ forward() { background() { - echo "${CHAT[ID]}:$2:$1" >"$TMPDIR/${copname}$2-back.cmd" + echo "${CHAT[ID]}:$2:$1" >"${TMPDIR:-.}/${copname}$2-back.cmd" startproc "$1" "back-$2-" } startproc() { killproc "$2" local fifo="$2${copname}" # add $1 to copname, so we can have more than one running script per chat - mkfifo "$TMPDIR/${fifo}" - TMUX= tmux new-session -d -s "${fifo}" "$1 &>$TMPDIR/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>$TMPDIR/${fifo}" + mkfifo "${TMPDIR:-.}/${fifo}" + TMUX= tmux new-session -d -s "${fifo}" "$1 &>${TMPDIR:-.}/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>${TMPDIR:-.}/${fifo}" TMUX= tmux new-session -d -s sendprocess_${fifo} "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" } @@ -406,12 +406,12 @@ checkproc() { killback() { killproc "back-$1-" - rm -f "$TMPDIR/${copname}$1-back.cmd" + rm -f "${TMPDIR:-.}/${copname}$1-back.cmd" } killproc() { local fifo="$1${copname}" - (tmux kill-session -t "${fifo}"; echo imprettydarnsuredatdisisdaendofdacmd>"$TMPDIR/${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "$TMPDIR/${fifo}")2>/dev/null + (tmux kill-session -t "${fifo}"; echo imprettydarnsuredatdisisdaendofdacmd>"${TMPDIR:-.}/${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "${TMPDIR:-.}/${fifo}")2>/dev/null } inproc() { @@ -429,7 +429,7 @@ process_updates() { done } process_client() { - local TMP="$TMPDIR/$RANDOM$RANDOM-MESSAGE" + local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE" echo "$UPDATE" >"$TMP" # Message MESSAGE[0]="$(echo -e "$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","text"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" | sed 's#\\/#/#g')" @@ -533,8 +533,8 @@ case "$1" in line="" read -r -t 10 line [ "$line" != "" ] && [ "$line" != "imprettydarnsuredatdisisdaendofdacmd" ] && send_message "$2" "$line" - done <"$TMPDIR/$3" - rm -f -r "$TMPDIR/$3" + done <"${TMPDIR:-.}/$3" + rm -f -r "${TMPDIR:-.}/$3" ;; "count") echo "A total of $(wc -l "${COUNT}" | sed 's/count//g')users used me." @@ -577,8 +577,8 @@ case "$1" in "background" | "resumeback") $CLEAR echo -e "${GREEN}Restart background processes ...${NC}" - for FILE in "${TMPDIR}/"*-back.cmd; do - if [ "$FILE" == "${TMPDIR}/*-back.cmd" ]; then + for FILE in "${TMPDIR:-.}/"*-back.cmd; do + if [ "$FILE" == "${TMPDIR:-.}/*-back.cmd" ]; then echo -e "${RED}No background processes to start.${NC}"; break else RESTART="$(cat "$FILE")" @@ -588,9 +588,9 @@ case "$1" in JOB="${JOB%:*}" fifo="back-${JOB}-${ME}_${CHAT[ID]}" # compose fifo from jobname, $ME (botname) and CHAT[ID] echo "restartbackground ${PROG} ${fifo}" - ( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "$TMPDIR/${fifo}") 2>/dev/null - mkfifo "$TMPDIR/${fifo}" - TMUX= tmux new-session -d -s "${fifo}" "${PROG} &>$TMPDIR/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>$TMPDIR/${fifo}" + ( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "${TMPDIR:-.}/${fifo}") 2>/dev/null + mkfifo "${TMPDIR:-.}/${fifo}" + TMUX= tmux new-session -d -s "${fifo}" "${PROG} &>${TMPDIR:-.}/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>${TMPDIR:-.}/${fifo}" TMUX= tmux new-session -d -s "sendprocess_${fifo}" "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" fi done @@ -604,8 +604,8 @@ case "$1" in "killback" | "suspendback") $CLEAR echo -e "${GREEN}Stopping background processes ...${NC}" - for FILE in "${TMPDIR}/"*-back.cmd; do - if [ "$FILE" == "${TMPDIR}/*-back.cmd" ]; then + for FILE in "${TMPDIR:-.}/"*-back.cmd; do + if [ "$FILE" == "${TMPDIR:-.}/*-back.cmd" ]; then echo -e "${RED}No background processes.${NC}"; break else REMOVE="$(cat "$FILE")" @@ -613,7 +613,7 @@ case "$1" in fifo="back-${JOB%:*}-${ME}_${REMOVE%%:*}" echo "killbackground ${fifo}" [ "$1" == "killback" ] && rm -f "$FILE" # remove job - ( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "$TMPDIR/${fifo}") 2>/dev/null + ( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "${TMPDIR:-.}/${fifo}") 2>/dev/null fi done ;; From 863f09a52bdd184529b54153740208c2f3b3f7d1 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Apr 2019 12:23:31 +0200 Subject: [PATCH 08/17] current USER is redefind as ARRAY, use RUNUSER as workaround --- bashbot.sh | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 56f66d7..d78960c 100755 --- a/bashbot.sh +++ b/bashbot.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$$ v0.6-dev-6-g34d2e3d +#### $$VERSION$$ v0.6-dev-7-ge936e6f # # Exit Codes: # - 0 sucess (hopefully) @@ -31,6 +31,7 @@ fi # get location of bashbot.sh an change to bashbot dir SCRIPT="./$(basename "$0")" SCRIPTDIR="$(dirname "$0")" +RUNUSER="$USER" # USER is overwritten as array, $USER may not work later on... if ! cd "${SCRIPTDIR}" ; then echo -e "${RED}ERROR: Can't change to ${SCRIPTDIR} ...${NC}" @@ -372,7 +373,7 @@ send_location() { send_venue() { [ "$5" = "" ] && return [ "$6" != "" ] add="-F \"foursquare_id=$6\"" - res="$(curl -s "$VENUE_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3" -F "title=$4" -F "address=$5" $add)" # what is add for? + res="$(curl -s "$VENUE_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3" -F "title=$4" -F "address=$5")" } @@ -391,8 +392,8 @@ startproc() { killproc "$2" local fifo="$2${copname}" # add $1 to copname, so we can have more than one running script per chat mkfifo "${TMPDIR:-.}/${fifo}" - TMUX= tmux new-session -d -s "${fifo}" "$1 &>${TMPDIR:-.}/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>${TMPDIR:-.}/${fifo}" - TMUX= tmux new-session -d -s sendprocess_${fifo} "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" + tmux new-session -d -s "${fifo}" "$1 &>${TMPDIR:-.}/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>${TMPDIR:-.}/${fifo}" + tmux new-session -d -s "sendprocess_${fifo}" "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" } @@ -503,7 +504,7 @@ process_client() { source commands.sh tmpcount="COUNT${CHAT[ID]}" - cat ${COUNT} | grep -q "$tmpcount" || echo "$tmpcount">>${COUNT} + grep -q "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT} # To get user count execute bash bashbot.sh count } @@ -553,11 +554,10 @@ case "$1" in send_markdown_message "${CHAT[ID]}" "*Bot started*" ;; "init") # adjust users and permissions - MYUSER="$USER" - [[ "$(id -u)" -eq "0" ]] && MYUSER="nobody" - echo -n "Enter User to run basbot [$MYUSER]: " + [[ "$(id -u)" -eq "0" ]] && RUNUSER="nobody" + echo -n "Enter User to run basbot [$RUNUSER]: " read -r TOUSER - [ "$TOUSER" = "" ] && TOUSER="$MYUSER" + [ "$TOUSER" = "" ] && TOUSER="$RUNUSER" if ! compgen -u "$TOUSER" >/dev/null 2>&1; then echo -e "${RED}User \"$TOUSER\" not found!${NC}" exit 3 @@ -590,8 +590,8 @@ case "$1" in echo "restartbackground ${PROG} ${fifo}" ( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "${TMPDIR:-.}/${fifo}") 2>/dev/null mkfifo "${TMPDIR:-.}/${fifo}" - TMUX= tmux new-session -d -s "${fifo}" "${PROG} &>${TMPDIR:-.}/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>${TMPDIR:-.}/${fifo}" - TMUX= tmux new-session -d -s "sendprocess_${fifo}" "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" + tmux new-session -d -s "${fifo}" "${PROG} &>${TMPDIR:-.}/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>${TMPDIR:-.}/${fifo}" + tmux new-session -d -s "sendprocess_${fifo}" "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" fi done ;; From f55571306ae99ca64910fa64276e1b6da308b792 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Apr 2019 12:40:07 +0200 Subject: [PATCH 09/17] use while loop for broadcast --- bashbot.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index d78960c..ff4fb83 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -538,13 +538,14 @@ case "$1" in rm -f -r "${TMPDIR:-.}/$3" ;; "count") - echo "A total of $(wc -l "${COUNT}" | sed 's/count//g')users used me." + echo "A total of $(wc -l <"${COUNT}") users used me." ;; "broadcast") - echo "Sending the broadcast $* to $(wc -l "${COUNT}" | sed 's/count//g')users." - [ "$(wc -l "${COUNT}" | sed 's/ count//g')" -gt "300" ] && sleep="sleep 0.5" + NUMCOUNT="$(wc -l <"${COUNT}")" + echo "Sending the broadcast $* to $NUMCOUNT users." + [ "$NUMCOUNT" -gt "300" ] && sleep="sleep 0.5" shift - for f in $(cat "${COUNT}"); do send_message "${f//COUNT}" "$*"; $sleep;done + while read -r f; do send_message "${f//COUNT}" "$*"; $sleep; done <"${COUNT}" ;; "start") $CLEAR From 9fd2f5ca0dda46c0d64f7336b448317fdda9ad5e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 9 Apr 2019 12:29:25 +0200 Subject: [PATCH 10/17] split docs in sections --- README.md | 425 ++-------------------------------------------- doc/1_firstbot.md | 75 ++++++++ doc/2_usage.md | 152 +++++++++++++++++ doc/3_advanced.md | 99 +++++++++++ doc/4_expert.md | 103 +++++++++++ 5 files changed, 445 insertions(+), 409 deletions(-) create mode 100644 doc/1_firstbot.md create mode 100644 doc/2_usage.md create mode 100644 doc/3_advanced.md create mode 100644 doc/4_expert.md diff --git a/README.md b/README.md index 127d79a..59da263 100644 --- a/README.md +++ b/README.md @@ -14,67 +14,8 @@ Released to the public domain wherever applicable. Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). -## Instructions -### Create your first bot -1. Message @botfather https://telegram.me/botfather with the following -text: `/newbot` - If you don't know how to message by username, click the search -field on your Telegram app and type `@botfather`, you should be able -to initiate a conversation. Be careful not to send it to the wrong -contact, because some users has similar usernames to `botfather`. - - ![botfather initial conversation](http://i.imgur.com/aI26ixR.png) - -2. @botfather replies with `Alright, a new bot. How are we going to -call it? Please choose a name for your bot.` - -3. Type whatever name you want for your bot. - -4. @botfather replies with `Good. Now let's choose a username for your -bot. It must end in bot. Like this, for example: TetrisBot or -tetris_bot.` - -5. Type whatever username you want for your bot, minimum 5 characters, -and must end with `bot`. For example: `telesample_bot` - -6. @botfather replies with: - - Done! Congratulations on your new bot. You will find it at -telegram.me/telesample_bot. You can now add a description, about -section and profile picture for your bot, see /help for a list of -commands. - - Use this token to access the HTTP API: - 123456789:AAG90e14-0f8-40183D-18491dDE - - For a description of the Bot API, see this page: -https://core.telegram.org/bots/api - -7. Note down the 'token' mentioned above. - -8. Type `/setprivacy` to @botfather. - - ![botfather later conversation](http://i.imgur.com/tWDVvh4.png) - -9. @botfather replies with `Choose a bot to change group messages settings.` - -10. Type `@telesample_bot` (change to the username you set at step 5 -above, but start it with `@`) - -11. @botfather replies with - - 'Enable' - your bot will only receive messages that either start -with the '/' symbol or mention the bot by username. - 'Disable' - your bot will receive all messages that people send to groups. - Current status is: ENABLED - -12. Type `Disable` to let your bot receive all messages sent to a -group. This step is up to you actually. - -13. @botfather replies with `Success! The new status is: DISABLED. /help` - -### Install bashbot +## Install bashbot 1. Go to the directory you want to install bashbot, e.g. - your $HOME directory (install and run with your user-ID) @@ -87,355 +28,21 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash 3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather. -## Creating your own Bot -All Commands for the Bot are in the ```commands.sh``` file (this should ease upgrades of the bot core). Here you find some examples how to process messages and send out text. - -Once you're done editing start the Bot with ```./bashbot.sh start```. -If some thing doesn't work as it should, debug with ```bash -x bashbot.sh```. To stop the Bot run ```./bashbot.sh kill``` - -To use the functions provided in this script in other scripts simply source bashbot: ```source bashbot.sh``` - -Have FUN! - - -### Recieve data -Evertime a Message is recieved, you can read incoming data using the following variables: - -* ```$MESSAGE```: Incoming messages -* ```${MESSAGE[ID]}```: ID of incoming message -* ```$CAPTION```: Captions -* ```$REPLYTO```: Original message wich was replied to -* ```$USER```: This array contains the First name, last name, username and user id of the sender of the current message. - - ```${USER[ID]}```: User id - - ```${USER[FIRST_NAME]}```: User's first name - - ```${USER[LAST_NAME]}```: User's last name - - ```${USER[USERNAME]}```: Username -* ```$CHAT```: This array contains the First name, last name, username, title and user id of the chat of the current message. - - ```${CHAT[ID]}```: Chat id - - ```${CHAT[FIRST_NAME]}```: Chat's first name - - ```${CHAT[LAST_NAME]}```: Chat's last name - - ```${CHAT[USERNAME]}```: Username - - ```${CHAT[TITLE]}```: Title - - ```${CHAT[TYPE]}```: Type - - ```${CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]}```: All members are administrators (true if true) -* ```$REPLYTO```: This array contains the First name, last name, username and user id of the ORIGINAL sender of the message REPLIED to. - - ```${REPLYTO[ID]}```: ID of message wich was replied to - - ```${REPLYTO[UID]}```: Original user's id - - ```${REPLYTO[FIRST_NAME]}```: Original user's first name - - ```${REPLYTO[LAST_NAME]}```: Original user's' last name - - ```${REPLYTO[USERNAME]}```: Original user's username -* ```$FORWARD```: This array contains the First name, last name, username and user id of the ORIGINAL sender of the FORWARDED message. - - ```${FORWARD[ID]}```: Same as MESSAGE[ID] if message is forwarded - - ```${FORWARD[UID]}```: Original user's id - - ```${FORWARD[FIRST_NAME]}```: Original user's first name - - ```${FORWARD[LAST_NAME]}```: Original user's' last name - - ```${FORWARD[USERNAME]}```: Original user's username -* ```$URLS```: This array contains documents, audio files, stickers, voice recordings and stickers stored in the form of URLs. - - ```${URLS[AUDIO]}```: Audio files - - ```${URLS[VIDEO]}```: Videos - - ```${URLS[PHOTO]}```: Photos (maximum quality) - - ```${URLS[VOICE]}```: Voice recordings - - ```${URLS[STICKER]}```: Stickers - - ```${URLS[DOCUMENT]}```: Any other file -* ```$CONTACT```: This array contains info about contacts sent in a chat. - - ```${CONTACT[NUMBER]}```: Phone number - - ```${CONTACT[FIRST_NAME]}```: First name - - ```${CONTACT[LAST_NAME]}```: Last name - - ```${CONTACT[ID]}```: User id -* ```$LOCATION```: This array contains info about locations sent in a chat. - - ```${LOCATION[LONGITUDE]}```: Longitude - - ```${LOCATION[LATITUDE]}```: Latitude - -### Usage of bashbot functions - -#### send_message -To send messages use the ```send_message``` function: -```bash -send_message "${CHAT[ID]}" "lol" -``` -To send html or markdown put the following strings before the text, depending on the parsing mode you want to enable: -```bash -send_message "${CHAT[ID]}" "markdown_parse_mode lol *bold*" -``` -```bash -send_message "${CHAT[ID]}" "html_parse_mode lol bold" -``` -This function also allows a third parameter that disables additional function parsing (for safety use this when reprinting user input): -```bash -send_message "${CHAT[ID]}" "lol" "safe" -``` -To forward messages use the ```forward``` function: -```bash -forward "${CHAT[ID]}" "from_chat_id" "message_id" -``` - -#### For safety and performance reasoms I recommend to use send_xxxx_message direct and not the universal send_message function. -To send regular text without any markdown use: -```bash -send_text_message "${CHAT[ID]}" "lol" -``` -To send text with markdown: -```bash -send_markdown_message "${CHAT[ID]}" "lol *bold*" -``` -To send text with html: -```bash -send_html_message "${CHAT[ID]}" "lol bold" -``` - -If your Bot is Admin in a Chat you can delete every message, if not you can delete only your messages. -To delete a message with a known ${MESSAGE[ID]} you can simple use: -```bash -delete_message "${CHAT[ID]}" "${MESSAGE[ID]}" -``` - -#### Send files, location etc. -To send images, videos, voice files, photos etc. use the ```send_photo``` function (remember to change the safety Regex @ line 14 of command.sh to allow sending files only from certain directories): -```bash -send_file "${CHAT[ID]}" "/home/user/doge.jpg" "Lool" -``` -To send custom keyboards use the ```send_keyboard``` function: -```bash -send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" "Yep" "No" -``` -To send locations use the ```send_location``` function: -```bash -send_location "${CHAT[ID]}" "Latitude" "Longitude" -``` -To send venues use the ```send_venue``` function: -```bash -send_venue "${CHAT[ID]}" "Latitude" "Longitude" "Title" "Address" "optional foursquare id" -``` -To send a chat action use the ```send_action``` function. -Allowed values: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for locations. -```bash -send_action "${CHAT[ID]}" "action" -``` - -#### Interactive Chats -To create interactive chats, write (or edit the question script) a normal bash (or C or python) script, chmod +x it and then change the argument of the startproc function to match the command you usually use to start the script. -The text that the script will output will be sent in real time to the user, and all user input will be sent to the script (as long as it's running or until the user kills it with /cancel). -To open up a keyboard in an interactive script, print out the keyboard layout in the following way: -```bash -echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\"" -``` -Same goes for files: -```bash -echo "Text that will appear in chat? myfilelocationstartshere /home/user/doge.jpg" -``` -And locations: -```bash -echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45" -``` -And venues: -```bash -echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45 mytitlestartshere my home myaddressstartshere Diagon Alley N. 37" -``` -You can combine them: -```bash -echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\" myfilelocationstartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45" -``` -Please note that you can either send a location or a venue, not both. To send a venue add the mytitlestartshere and the myaddressstartshere keywords. - -To insert a linebreak in your message you can insert ```mynewlinestartshere``` in your echo command: -```bash -echo "Text that will appear in one message mynewlinestartshere with this text on a new line" -``` -Note: Interactive Chats run independent from main bot and continue running until your script exits or you /cancel if from your Bot. - -#### Background Jobs - -A background job is similar to an interactive chat, but runs in the background and does only output massages instead of processing input from the user. In contrast to interactive chats it's possible to run multiple background jobs. To create a background job write a script or edit the notify script and use the funtion ```background``` to start it: -```bash -background "./notify" "jobname" -``` -All output of the script will be sent to the user or chat. To stop a background job use: -```bash -killback "jobname" -``` -You can also suspend and resume the last running background jobs from outside bashbot, e.g. in your startup schripts: -```bash -./bashbot.sh suspendback -./bashbot.sh resumeback -``` - -If you want to kill all background jobs permantly run: -```bash -./bashbot.sh killback - -``` -Note: Background Jobs run independent from main bot and continue running until your script exits or you stop if from your Bot. Backgound Jobs will continue running if your Bot is stoped (kill)!. - -#### Inline queries -The following commands allows users to interact with your bot via *inline queries*. -In order to enable **inline mode**, send `/setinline` command to [@BotFather](https://telegram.me/botfather) and provide the placeholder text that the user will see in the input field after typing your bot’s name. -Also, edit line 12 from `commands.sh` putting a "1". -Note that you can't modify the first two parameters of the function `answer_inline_query`, only the ones after them. - -To send messsages or links through an *inline query*: -```bash -answer_inline_query "$iQUERY_ID" "article" "Title of the result" "Content of the message to be sent" -``` -To send photos in jpeg format and less than 5MB, from a website through an *inline query*: -```bash -answer_inline_query "$iQUERY_ID" "photo" "A valid URL of the photo" "URL of the thumbnail" -``` -To send standard gifs from a website (less than 1MB) through an *inline query*: -```bash -answer_inline_query "$iQUERY_ID" "gif" "gif url" -``` -To send mpeg4 gifs from a website (less than 1MB) through an *inline query*: -```bash -answer_inline_query "$iQUERY_ID" "mpeg4_gif" "mpeg4 gif url" -``` -To send videos from a website through an *inline query*: -```bash -answer_inline_query "$iQUERY_ID" "video" "valid video url" "Select one mime type: text/html or video/mp4" "URL of the thumbnail" "Title for the result" -``` -To send photos stored in Telegram servers through an *inline query*: -```bash -answer_inline_query "$iQUERY_ID" "cached_photo" "identifier for the photo" -``` -To send gifs stored in Telegram servers through an *inline query*: -```bash -answer_inline_query "$iQUERY_ID" "cached_gif" "identifier for the gif" -``` -To send mpeg4 gifs stored in Telegram servers through an *inline query*: -```bash -answer_inline_query "$iQUERY_ID" "cached_mpeg4_gif" "identifier for the gif" -``` -To send stickers through an *inline query*: -```bash -answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" -``` - - -## Managing your Bot -#### Note: running bashbot as root is highly danger and not recommended. See Expert use below. - -### Start / Stop -Start or Stop your Bot use the following commands: -```bash -./bashbot.sh start -``` -```bash -./bashbot.sh kill -``` - -### User count -To count the total number of users that ever used the bot run the following command: -```bash -./bashbot.sh count -``` - -### Sending broadcasts to all users -To send a broadcast to all of users that ever used the bot run the following command: -```bash -./bashbot.sh broadcast "Hey! I just wanted to let you know that the bot's been updated!" -``` - -## Handling UTF-8 character sets - -### Setting up your Environment -In general ```bash``` and ```GNU``` utitities are UTF-8 aware, but you have to setup your environment -and your scripts accordingly: - -1. Your Terminal and Editor must support UTF-8: - Set Terminal and Editor locale to UTF-8, eg. in ```Settings/Configuration``` select UTF-8 (Unicode) as Charset. - -2. Set ```Shell``` environment to UTF-8 in your ```.profile``` and your scripts. The usual settings are: - -```bash -export 'LC_ALL=C.UTF-8' -export 'LANG=C.UTF-8' -export 'LANGUAGE=C.UTF-8' -``` - If you use other languages, eg. german or US english, change the shell settings to: -```bash -export 'LC_ALL=de_DE.UTF-8' -export 'LANG=de_DE.UTF-8' -export 'LANGUAGE=de_DE.UTF-8' -``` -```bash -export 'LC_ALL=en_US.UTF-8' -export 'LANG=de_en_US.UTF-8' -export 'LANGUAGE=den_US.UTF-8' -``` -3. make shure your bot scripts use the correct settings, eg. include the lines above at the beginning of your scripts - -To display all availible locales on your system run ```locale -a | more```. [Gentoo Wiki](https://wiki.gentoo.org/wiki/UTF-8) - -### UTF-8 in Telegram -```UTF-8``` is a variable length encoding of Unicode. UTF-8 is recommended as the default encoding in JSON, XML and HTML, also Telegram make use of it. - -The first 128 characters are regular ASCII, so it's a superset of and compatible with ASCII environments. The next 1,920 characters need -two bytes for encoding and covers almost all ```Latin``` alphabets, also ```Greek```, ```Cyrillic```, -```Hebrew```, ```Arabic``` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more deatils. - -Telegram send Messages with all characters not fitting in one byte (256 bit) escaped as sequences of ```\uxxxx``` to be regular one byte ASCII (incl. iso-xxx-x), e.g. Emoticons and Arabic characters. -E.g. the Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: -``` -\uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D -``` - -'\uXXXX' and '\UXXXXXXXX' escaped endocings are supported by zsh, bash, ksh93, mksh and FreeBSD sh, GNU 'printf' and GNU 'echo -e', see [this Stackexchange Answer](https://unix.stackexchange.com/questions/252286/how-to-convert-an-emoticon-specified-by-a-uxxxxx-code-to-utf-8/252295#252295) for more information. - - -## Expert Use -Bashbot is desingned to run manually by the user who installed it. Nevertheless it's possible to run it by an other user-ID, as a system service or sceduled from cron. This is onyl recommended for experiend linux users. - -### Run as other user or system service -Running bashbot as an other user is only possible with sudo rigths. - -Setup the environment for the user you want to run bashbot and enter desired username, e.g. nobody : -```bash -sudo ./bashbot.sh init -``` - -Edit the file ```bashbot.rc``` and edit the following lines to fit your configuration: -```bash -####################### -# Configuration Section - -# edit the next line to fit the user you want to run bashbot, e.g. nobody: -runas="nobody" - -# uncomment one of the following lines -# runcmd="su $runas -s /bin/bash -c " # runasuser with su -# runcmd="runuser $runas -s /bin/bash -c " # runasuser with runuser - -# edit the values of the following lines to fit your config: -start="/usr/local/telegram-bot-bash/bashbot.sh" # location of your bashbot.sh script -name='' # your bot name as given to botfather, e.g. mysomething_bot - -# END Configuration -####################### -``` -From now on always use bashbot.rc to start/stop your bot: -```bash -sudo ./bashbot.rc start -``` -Type ```ps -ef | grep bashbot``` to verify your Bot is running as the desired user. - -If you started bashbot by bashbot.rc you must use bashbot.rc also to manage your Bot! The following commands are availible: -```bash -sudo ./bashbot.rc start -sudo ./bashbot.rc stop -sudo ./bashbot.rc status -sudo ./bashbot.rc suspendback -sudo ./bashbot.rc resumeback -sudo ./bashbot.rc killback -``` -To change back the environment to your user-ID run ```sudo ./bashbot.sh init``` again and enter your user name. - -To use bashbot as a system servive include a working ```bashbot.rc``` in your init system (systemd, /etc/init.d). - -### Scedule bashbot from Cron -An example crontab is provided in ```bashbot.cron```. - -- If you are running bashbot with your user-ID, copy the examples lines to your crontab and remove username ```nobody```. -- if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot``` +## Getting started + (Create your first telegram bot)[doc/1_firstbot.md] + (Make your own Bot)[doc/2_usage.md] + Managing your own Bot + Recieve data + Send Messages + (Advatage Features)[doc/3_advanced.md] + Interactive Chats + Background Jobs + Inline queries + (Expert Use)[doc/4_expert.md] + Handling UTF-8 + Run as other user or system service + Scedule bashbot from Cron ## Security Considerations @@ -464,4 +71,4 @@ No - its not less (in)secure as any other Bot written in any other language. But If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.6-dev-3-g614eae8 +#### $$VERSION$$ v0.50-11-g4ce19b1 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md new file mode 100644 index 0000000..1905f75 --- /dev/null +++ b/doc/1_firstbot.md @@ -0,0 +1,75 @@ + +## Instructions +### Create your first bot + +1. Message @botfather https://telegram.me/botfather with the following +text: `/newbot` + If you don't know how to message by username, click the search +field on your Telegram app and type `@botfather`, you should be able +to initiate a conversation. Be careful not to send it to the wrong +contact, because some users has similar usernames to `botfather`. + + ![botfather initial conversation](http://i.imgur.com/aI26ixR.png) + +2. @botfather replies with `Alright, a new bot. How are we going to +call it? Please choose a name for your bot.` + +3. Type whatever name you want for your bot. + +4. @botfather replies with `Good. Now let's choose a username for your +bot. It must end in bot. Like this, for example: TetrisBot or +tetris_bot.` + +5. Type whatever username you want for your bot, minimum 5 characters, +and must end with `bot`. For example: `telesample_bot` + +6. @botfather replies with: + + Done! Congratulations on your new bot. You will find it at +telegram.me/telesample_bot. You can now add a description, about +section and profile picture for your bot, see /help for a list of +commands. + + Use this token to access the HTTP API: + 123456789:AAG90e14-0f8-40183D-18491dDE + + For a description of the Bot API, see this page: +https://core.telegram.org/bots/api + +7. Note down the 'token' mentioned above. + +8. Type `/setprivacy` to @botfather. + + ![botfather later conversation](http://i.imgur.com/tWDVvh4.png) + +9. @botfather replies with `Choose a bot to change group messages settings.` + +10. Type `@telesample_bot` (change to the username you set at step 5 +above, but start it with `@`) + +11. @botfather replies with + + 'Enable' - your bot will only receive messages that either start +with the '/' symbol or mention the bot by username. + 'Disable' - your bot will receive all messages that people send to groups. + Current status is: ENABLED + +12. Type `Disable` to let your bot receive all messages sent to a +group. This step is up to you actually. + +13. @botfather replies with `Success! The new status is: DISABLED. /help` + +### Install bashbot +1. Go to the directory you want to install bashbot, e.g. + + - your $HOME directory (install and run with your user-ID) + - /usr/local if you want to run as service + +2. Clone the repository: +``` +git clone --recursive https://github.com/topkecleon/telegram-bot-bash +``` +3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather. + +#### $$VERSION$$ v0.50-11-g4ce19b1 + diff --git a/doc/2_usage.md b/doc/2_usage.md new file mode 100644 index 0000000..dc47f3f --- /dev/null +++ b/doc/2_usage.md @@ -0,0 +1,152 @@ +## Creating your own Bot + +All Commands for the Bot are in the ```commands.sh``` file (this should ease upgrades of the bot core). Here you find some examples how to process messages and send out text. + +Once you're done editing start the Bot with ```./bashbot.sh start```. +If some thing doesn't work as it should, debug with ```bash -x bashbot.sh```. To stop the Bot run ```./bashbot.sh kill``` + +To use the functions provided in this script in other scripts simply source bashbot: ```source bashbot.sh``` + +Have FUN! + +## Managing your Bot +#### Note: running bashbot as root is highly danger and not recommended. See Expert use. + +### Start / Stop +Start or Stop your Bot use the following commands: +```bash +./bashbot.sh start +``` +```bash +./bashbot.sh kill +``` + +### User count +To count the total number of users that ever used the bot run the following command: +```bash +./bashbot.sh count +``` + +### Sending broadcasts to all users +To send a broadcast to all of users that ever used the bot run the following command: +```bash +./bashbot.sh broadcast "Hey! I just wanted to let you know that the bot's been updated!" +``` + +### Recieve data +Evertime a Message is recieved, you can read incoming data using the following variables: + +* ```$MESSAGE```: Incoming messages +* ```${MESSAGE[ID]}```: ID of incoming message +* ```$CAPTION```: Captions +* ```$REPLYTO```: Original message wich was replied to +* ```$USER```: This array contains the First name, last name, username and user id of the sender of the current message. + - ```${USER[ID]}```: User id + - ```${USER[FIRST_NAME]}```: User's first name + - ```${USER[LAST_NAME]}```: User's last name + - ```${USER[USERNAME]}```: Username +* ```$CHAT```: This array contains the First name, last name, username, title and user id of the chat of the current message. + - ```${CHAT[ID]}```: Chat id + - ```${CHAT[FIRST_NAME]}```: Chat's first name + - ```${CHAT[LAST_NAME]}```: Chat's last name + - ```${CHAT[USERNAME]}```: Username + - ```${CHAT[TITLE]}```: Title + - ```${CHAT[TYPE]}```: Type + - ```${CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]}```: All members are administrators (true if true) +* ```$REPLYTO```: This array contains the First name, last name, username and user id of the ORIGINAL sender of the message REPLIED to. + - ```${REPLYTO[ID]}```: ID of message wich was replied to + - ```${REPLYTO[UID]}```: Original user's id + - ```${REPLYTO[FIRST_NAME]}```: Original user's first name + - ```${REPLYTO[LAST_NAME]}```: Original user's' last name + - ```${REPLYTO[USERNAME]}```: Original user's username +* ```$FORWARD```: This array contains the First name, last name, username and user id of the ORIGINAL sender of the FORWARDED message. + - ```${FORWARD[ID]}```: Same as MESSAGE[ID] if message is forwarded + - ```${FORWARD[UID]}```: Original user's id + - ```${FORWARD[FIRST_NAME]}```: Original user's first name + - ```${FORWARD[LAST_NAME]}```: Original user's' last name + - ```${FORWARD[USERNAME]}```: Original user's username +* ```$URLS```: This array contains documents, audio files, stickers, voice recordings and stickers stored in the form of URLs. + - ```${URLS[AUDIO]}```: Audio files + - ```${URLS[VIDEO]}```: Videos + - ```${URLS[PHOTO]}```: Photos (maximum quality) + - ```${URLS[VOICE]}```: Voice recordings + - ```${URLS[STICKER]}```: Stickers + - ```${URLS[DOCUMENT]}```: Any other file +* ```$CONTACT```: This array contains info about contacts sent in a chat. + - ```${CONTACT[NUMBER]}```: Phone number + - ```${CONTACT[FIRST_NAME]}```: First name + - ```${CONTACT[LAST_NAME]}```: Last name + - ```${CONTACT[ID]}```: User id +* ```$LOCATION```: This array contains info about locations sent in a chat. + - ```${LOCATION[LONGITUDE]}```: Longitude + - ```${LOCATION[LATITUDE]}```: Latitude + +### Usage of bashbot functions + +#### send_message +To send messages use the ```send_message``` function: +```bash +send_message "${CHAT[ID]}" "lol" +``` +To send html or markdown put the following strings before the text, depending on the parsing mode you want to enable: +```bash +send_message "${CHAT[ID]}" "markdown_parse_mode lol *bold*" +``` +```bash +send_message "${CHAT[ID]}" "html_parse_mode lol bold" +``` +This function also allows a third parameter that disables additional function parsing (for safety use this when reprinting user input): +```bash +send_message "${CHAT[ID]}" "lol" "safe" +``` +To forward messages use the ```forward``` function: +```bash +forward "${CHAT[ID]}" "from_chat_id" "message_id" +``` + +#### For safety and performance reasoms I recommend to use send_xxxx_message direct and not the universal send_message function. +To send regular text without any markdown use: +```bash +send_text_message "${CHAT[ID]}" "lol" +``` +To send text with markdown: +```bash +send_markdown_message "${CHAT[ID]}" "lol *bold*" +``` +To send text with html: +```bash +send_html_message "${CHAT[ID]}" "lol bold" +``` + +If your Bot is Admin in a Chat you can delete every message, if not you can delete only your messages. +To delete a message with a known ${MESSAGE[ID]} you can simple use: +```bash +delete_message "${CHAT[ID]}" "${MESSAGE[ID]}" +``` + +#### Send files, location etc. +To send images, videos, voice files, photos etc. use the ```send_photo``` function (remember to change the safety Regex @ line 14 of command.sh to allow sending files only from certain directories): +```bash +send_file "${CHAT[ID]}" "/home/user/doge.jpg" "Lool" +``` +To send custom keyboards use the ```send_keyboard``` function: +```bash +send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" "Yep" "No" +``` +To send locations use the ```send_location``` function: +```bash +send_location "${CHAT[ID]}" "Latitude" "Longitude" +``` +To send venues use the ```send_venue``` function: +```bash +send_venue "${CHAT[ID]}" "Latitude" "Longitude" "Title" "Address" "optional foursquare id" +``` +To send a chat action use the ```send_action``` function. +Allowed values: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for locations. +```bash +send_action "${CHAT[ID]}" "action" +``` + +#### $$VERSION$$ v0.50-11-g4ce19b1 + + diff --git a/doc/3_advanced.md b/doc/3_advanced.md new file mode 100644 index 0000000..5353bbc --- /dev/null +++ b/doc/3_advanced.md @@ -0,0 +1,99 @@ +#### Interactive Chats +To create interactive chats, write (or edit the question script) a normal bash (or C or python) script, chmod +x it and then change the argument of the startproc function to match the command you usually use to start the script. +The text that the script will output will be sent in real time to the user, and all user input will be sent to the script (as long as it's running or until the user kills it with /cancel). +To open up a keyboard in an interactive script, print out the keyboard layout in the following way: +```bash +echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\"" +``` +Same goes for files: +```bash +echo "Text that will appear in chat? myfilelocationstartshere /home/user/doge.jpg" +``` +And locations: +```bash +echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45" +``` +And venues: +```bash +echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45 mytitlestartshere my home myaddressstartshere Diagon Alley N. 37" +``` +You can combine them: +```bash +echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\" myfilelocationstartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45" +``` +Please note that you can either send a location or a venue, not both. To send a venue add the mytitlestartshere and the myaddressstartshere keywords. + +To insert a linebreak in your message you can insert ```mynewlinestartshere``` in your echo command: +```bash +echo "Text that will appear in one message mynewlinestartshere with this text on a new line" +``` +Note: Interactive Chats run independent from main bot and continue running until your script exits or you /cancel if from your Bot. + +#### Background Jobs + +A background job is similar to an interactive chat, but runs in the background and does only output massages instead of processing input from the user. In contrast to interactive chats it's possible to run multiple background jobs. To create a background job write a script or edit the notify script and use the funtion ```background``` to start it: +```bash +background "./notify" "jobname" +``` +All output of the script will be sent to the user or chat. To stop a background job use: +```bash +killback "jobname" +``` +You can also suspend and resume the last running background jobs from outside bashbot, e.g. in your startup schripts: +```bash +./bashbot.sh suspendback +./bashbot.sh resumeback +``` + +If you want to kill all background jobs permantly run: +```bash +./bashbot.sh killback + +``` +Note: Background Jobs run independent from main bot and continue running until your script exits or you stop if from your Bot. Backgound Jobs will continue running if your Bot is stoped (kill)!. + +#### Inline queries +The following commands allows users to interact with your bot via *inline queries*. +In order to enable **inline mode**, send `/setinline` command to [@BotFather](https://telegram.me/botfather) and provide the placeholder text that the user will see in the input field after typing your bot’s name. +Also, edit line 12 from `commands.sh` putting a "1". +Note that you can't modify the first two parameters of the function `answer_inline_query`, only the ones after them. + +To send messsages or links through an *inline query*: +```bash +answer_inline_query "$iQUERY_ID" "article" "Title of the result" "Content of the message to be sent" +``` +To send photos in jpeg format and less than 5MB, from a website through an *inline query*: +```bash +answer_inline_query "$iQUERY_ID" "photo" "A valid URL of the photo" "URL of the thumbnail" +``` +To send standard gifs from a website (less than 1MB) through an *inline query*: +```bash +answer_inline_query "$iQUERY_ID" "gif" "gif url" +``` +To send mpeg4 gifs from a website (less than 1MB) through an *inline query*: +```bash +answer_inline_query "$iQUERY_ID" "mpeg4_gif" "mpeg4 gif url" +``` +To send videos from a website through an *inline query*: +```bash +answer_inline_query "$iQUERY_ID" "video" "valid video url" "Select one mime type: text/html or video/mp4" "URL of the thumbnail" "Title for the result" +``` +To send photos stored in Telegram servers through an *inline query*: +```bash +answer_inline_query "$iQUERY_ID" "cached_photo" "identifier for the photo" +``` +To send gifs stored in Telegram servers through an *inline query*: +```bash +answer_inline_query "$iQUERY_ID" "cached_gif" "identifier for the gif" +``` +To send mpeg4 gifs stored in Telegram servers through an *inline query*: +```bash +answer_inline_query "$iQUERY_ID" "cached_mpeg4_gif" "identifier for the gif" +``` +To send stickers through an *inline query*: +```bash +answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" +``` + +#### $$VERSION$$ v0.50-11-g4ce19b1 + diff --git a/doc/4_expert.md b/doc/4_expert.md new file mode 100644 index 0000000..a83085e --- /dev/null +++ b/doc/4_expert.md @@ -0,0 +1,103 @@ +## Handling UTF-8 character sets +### Setting up your Environment +In general ```bash``` and ```GNU``` utitities are UTF-8 aware, but you have to setup your environment +and your scripts accordingly: + +1. Your Terminal and Editor must support UTF-8: + Set Terminal and Editor locale to UTF-8, eg. in ```Settings/Configuration``` select UTF-8 (Unicode) as Charset. + +2. Set ```Shell``` environment to UTF-8 in your ```.profile``` and your scripts. The usual settings are: + +```bash +export 'LC_ALL=C.UTF-8' +export 'LANG=C.UTF-8' +export 'LANGUAGE=C.UTF-8' +``` + If you use other languages, eg. german or US english, change the shell settings to: +```bash +export 'LC_ALL=de_DE.UTF-8' +export 'LANG=de_DE.UTF-8' +export 'LANGUAGE=de_DE.UTF-8' +``` +```bash +export 'LC_ALL=en_US.UTF-8' +export 'LANG=de_en_US.UTF-8' +export 'LANGUAGE=den_US.UTF-8' +``` +3. make shure your bot scripts use the correct settings, eg. include the lines above at the beginning of your scripts + +To display all availible locales on your system run ```locale -a | more```. [Gentoo Wiki](https://wiki.gentoo.org/wiki/UTF-8) + +### UTF-8 in Telegram +```UTF-8``` is a variable length encoding of Unicode. UTF-8 is recommended as the default encoding in JSON, XML and HTML, also Telegram make use of it. + +The first 128 characters are regular ASCII, so it's a superset of and compatible with ASCII environments. The next 1,920 characters need +two bytes for encoding and covers almost all ```Latin``` alphabets, also ```Greek```, ```Cyrillic```, +```Hebrew```, ```Arabic``` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more deatils. + +Telegram send Messages with all characters not fitting in one byte (256 bit) escaped as sequences of ```\uxxxx``` to be regular one byte ASCII (incl. iso-xxx-x), e.g. Emoticons and Arabic characters. +E.g. the Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: +``` +\uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D +``` + +'\uXXXX' and '\UXXXXXXXX' escaped endocings are supported by zsh, bash, ksh93, mksh and FreeBSD sh, GNU 'printf' and GNU 'echo -e', see [this Stackexchange Answer](https://unix.stackexchange.com/questions/252286/how-to-convert-an-emoticon-specified-by-a-uxxxxx-code-to-utf-8/252295#252295) for more information. + + +## Expert Use +Bashbot is desingned to run manually by the user who installed it. Nevertheless it's possible to run it by an other user-ID, as a system service or sceduled from cron. This is onyl recommended for experiend linux users. + +### Run as other user or system service +Running bashbot as an other user is only possible with sudo rigths. + +Setup the environment for the user you want to run bashbot and enter desired username, e.g. nobody : +```bash +sudo ./bashbot.sh init +``` + +Edit the file ```bashbot.rc``` and edit the following lines to fit your configuration: +```bash +####################### +# Configuration Section + +# edit the next line to fit the user you want to run bashbot, e.g. nobody: +runas="nobody" + +# uncomment one of the following lines +# runcmd="su $runas -s /bin/bash -c " # runasuser with su +# runcmd="runuser $runas -s /bin/bash -c " # runasuser with runuser + +# edit the values of the following lines to fit your config: +start="/usr/local/telegram-bot-bash/bashbot.sh" # location of your bashbot.sh script +name='' # your bot name as given to botfather, e.g. mysomething_bot + +# END Configuration +####################### +``` +From now on always use bashbot.rc to start/stop your bot: +```bash +sudo ./bashbot.rc start +``` +Type ```ps -ef | grep bashbot``` to verify your Bot is running as the desired user. + +If you started bashbot by bashbot.rc you must use bashbot.rc also to manage your Bot! The following commands are availible: +```bash +sudo ./bashbot.rc start +sudo ./bashbot.rc stop +sudo ./bashbot.rc status +sudo ./bashbot.rc suspendback +sudo ./bashbot.rc resumeback +sudo ./bashbot.rc killback +``` +To change back the environment to your user-ID run ```sudo ./bashbot.sh init``` again and enter your user name. + +To use bashbot as a system servive include a working ```bashbot.rc``` in your init system (systemd, /etc/init.d). + +### Scedule bashbot from Cron +An example crontab is provided in ```bashbot.cron```. + +- If you are running bashbot with your user-ID, copy the examples lines to your crontab and remove username ```nobody```. +- if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot``` + +#### $$VERSION$$ v0.50-11-g4ce19b1 + From 4d5d38674d3d8d9afcb655277d1f6bfc8bfec9cd Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 9 Apr 2019 12:40:13 +0200 Subject: [PATCH 11/17] split docs in sections final --- README.md | 11 ++++++----- doc/1_firstbot.md | 5 ++--- doc/2_usage.md | 10 +++++----- doc/3_advanced.md | 10 ++++++---- doc/4_expert.md | 16 +++++++++------- 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 59da263..4c8e7ba 100644 --- a/README.md +++ b/README.md @@ -30,16 +30,17 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash ## Getting started - (Create your first telegram bot)[doc/1_firstbot.md] - (Make your own Bot)[doc/2_usage.md] + [Create your first telegram bot](doc/1_firstbot.md) + [Make your own Bot](doc/2_usage.md) Managing your own Bot Recieve data Send Messages - (Advatage Features)[doc/3_advanced.md] + Send files, location etc. + [Advatage Features](doc/3_advanced.md) Interactive Chats Background Jobs Inline queries - (Expert Use)[doc/4_expert.md] + [Expert Use](doc/4_expert.md) Handling UTF-8 Run as other user or system service Scedule bashbot from Cron @@ -71,4 +72,4 @@ No - its not less (in)secure as any other Bot written in any other language. But If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.50-11-g4ce19b1 +#### $$VERSION$$ v0.50-12-g9fd2f5c diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 1905f75..faaf25b 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -1,6 +1,5 @@ -## Instructions -### Create your first bot +## Create your first telegram bot 1. Message @botfather https://telegram.me/botfather with the following text: `/newbot` @@ -71,5 +70,5 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash ``` 3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather. -#### $$VERSION$$ v0.50-11-g4ce19b1 +#### $$VERSION$$ v0.50-12-g9fd2f5c diff --git a/doc/2_usage.md b/doc/2_usage.md index dc47f3f..cdfd759 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -1,4 +1,4 @@ -## Creating your own Bot +## Make your own Bot All Commands for the Bot are in the ```commands.sh``` file (this should ease upgrades of the bot core). Here you find some examples how to process messages and send out text. @@ -9,7 +9,7 @@ To use the functions provided in this script in other scripts simply source bash Have FUN! -## Managing your Bot +## Managing your own Bot #### Note: running bashbot as root is highly danger and not recommended. See Expert use. ### Start / Stop @@ -33,7 +33,7 @@ To send a broadcast to all of users that ever used the bot run the following com ./bashbot.sh broadcast "Hey! I just wanted to let you know that the bot's been updated!" ``` -### Recieve data +## Recieve data Evertime a Message is recieved, you can read incoming data using the following variables: * ```$MESSAGE```: Incoming messages @@ -81,7 +81,7 @@ Evertime a Message is recieved, you can read incoming data using the following v - ```${LOCATION[LONGITUDE]}```: Longitude - ```${LOCATION[LATITUDE]}```: Latitude -### Usage of bashbot functions +## Usage of bashbot functions #### send_message To send messages use the ```send_message``` function: @@ -147,6 +147,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.50-11-g4ce19b1 +#### $$VERSION$$ v0.50-12-g9fd2f5c diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 5353bbc..3db2f72 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -1,4 +1,6 @@ -#### Interactive Chats + +## Advanced Features +### Interactive Chats To create interactive chats, write (or edit the question script) a normal bash (or C or python) script, chmod +x it and then change the argument of the startproc function to match the command you usually use to start the script. The text that the script will output will be sent in real time to the user, and all user input will be sent to the script (as long as it's running or until the user kills it with /cancel). To open up a keyboard in an interactive script, print out the keyboard layout in the following way: @@ -29,7 +31,7 @@ echo "Text that will appear in one message mynewlinestartshere with this text ``` Note: Interactive Chats run independent from main bot and continue running until your script exits or you /cancel if from your Bot. -#### Background Jobs +### Background Jobs A background job is similar to an interactive chat, but runs in the background and does only output massages instead of processing input from the user. In contrast to interactive chats it's possible to run multiple background jobs. To create a background job write a script or edit the notify script and use the funtion ```background``` to start it: ```bash @@ -52,7 +54,7 @@ If you want to kill all background jobs permantly run: ``` Note: Background Jobs run independent from main bot and continue running until your script exits or you stop if from your Bot. Backgound Jobs will continue running if your Bot is stoped (kill)!. -#### Inline queries +### Inline queries The following commands allows users to interact with your bot via *inline queries*. In order to enable **inline mode**, send `/setinline` command to [@BotFather](https://telegram.me/botfather) and provide the placeholder text that the user will see in the input field after typing your bot’s name. Also, edit line 12 from `commands.sh` putting a "1". @@ -95,5 +97,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.50-11-g4ce19b1 +#### $$VERSION$$ v0.50-12-g9fd2f5c diff --git a/doc/4_expert.md b/doc/4_expert.md index a83085e..abd6fae 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -1,5 +1,7 @@ -## Handling UTF-8 character sets -### Setting up your Environment +## Expert Use + +### Handling UTF-8 character sets +#### Setting up your Environment In general ```bash``` and ```GNU``` utitities are UTF-8 aware, but you have to setup your environment and your scripts accordingly: @@ -28,7 +30,7 @@ export 'LANGUAGE=den_US.UTF-8' To display all availible locales on your system run ```locale -a | more```. [Gentoo Wiki](https://wiki.gentoo.org/wiki/UTF-8) -### UTF-8 in Telegram +#### UTF-8 in Telegram ```UTF-8``` is a variable length encoding of Unicode. UTF-8 is recommended as the default encoding in JSON, XML and HTML, also Telegram make use of it. The first 128 characters are regular ASCII, so it's a superset of and compatible with ASCII environments. The next 1,920 characters need @@ -44,11 +46,11 @@ E.g. the Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: '\uXXXX' and '\UXXXXXXXX' escaped endocings are supported by zsh, bash, ksh93, mksh and FreeBSD sh, GNU 'printf' and GNU 'echo -e', see [this Stackexchange Answer](https://unix.stackexchange.com/questions/252286/how-to-convert-an-emoticon-specified-by-a-uxxxxx-code-to-utf-8/252295#252295) for more information. -## Expert Use -Bashbot is desingned to run manually by the user who installed it. Nevertheless it's possible to run it by an other user-ID, as a system service or sceduled from cron. This is onyl recommended for experiend linux users. ### Run as other user or system service -Running bashbot as an other user is only possible with sudo rigths. +Bashbot is desingned to run manually by the user who installed it. Nevertheless it's possible to run it by an other user-ID, as a system service or sceduled from cron. This is onyl recommended for experiend linux users. + +####Running bashbot as an other user is only possible with sudo rigths. Setup the environment for the user you want to run bashbot and enter desired username, e.g. nobody : ```bash @@ -99,5 +101,5 @@ An example crontab is provided in ```bashbot.cron```. - If you are running bashbot with your user-ID, copy the examples lines to your crontab and remove username ```nobody```. - if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot``` -#### $$VERSION$$ v0.50-11-g4ce19b1 +#### $$VERSION$$ v0.50-12-g9fd2f5c From 37eea3562f15d9e257e265ffb30ed0094ebd1483 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 9 Apr 2019 12:50:10 +0200 Subject: [PATCH 12/17] fix TOC --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4c8e7ba..3c35910 100644 --- a/README.md +++ b/README.md @@ -30,20 +30,20 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash ## Getting started - [Create your first telegram bot](doc/1_firstbot.md) - [Make your own Bot](doc/2_usage.md) - Managing your own Bot - Recieve data - Send Messages - Send files, location etc. - [Advatage Features](doc/3_advanced.md) - Interactive Chats - Background Jobs - Inline queries - [Expert Use](doc/4_expert.md) - Handling UTF-8 - Run as other user or system service - Scedule bashbot from Cron + [Create your first telegram bot](doc/1_firstbot.md) + [Make your own Bot](doc/2_usage.md) + Managing your own Bot + Recieve data + Send Messages + Send files, location etc. + [Advatage Features](doc/3_advanced.md) + Interactive Chats + Background Jobs + Inline queries + [Expert Use](doc/4_expert.md) + Handling UTF-8 + Run as other user or system service + Scedule bashbot from Cron ## Security Considerations @@ -72,4 +72,4 @@ No - its not less (in)secure as any other Bot written in any other language. But If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.50-12-g9fd2f5c +#### $$VERSION$$ v0.50-13-g4d5d386 From 275a83b374677444788925d8c303260ccc2cc6fa Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 9 Apr 2019 12:53:15 +0200 Subject: [PATCH 13/17] fix TOC --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 3c35910..97b2ec0 100644 --- a/README.md +++ b/README.md @@ -30,20 +30,20 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash ## Getting started - [Create your first telegram bot](doc/1_firstbot.md) - [Make your own Bot](doc/2_usage.md) - Managing your own Bot - Recieve data - Send Messages - Send files, location etc. - [Advatage Features](doc/3_advanced.md) - Interactive Chats - Background Jobs - Inline queries - [Expert Use](doc/4_expert.md) - Handling UTF-8 - Run as other user or system service - Scedule bashbot from Cron + - [Create your first telegram bot](doc/1_firstbot.md) + - [Make your own Bot](doc/2_usage.md) + - Managing your own Bot + - Recieve data + - Send Messages + - Send files, location etc. + - [Advatage Features](doc/3_advanced.md) + - Interactive Chats + - Background Jobs + - Inline queries + - [Expert Use](doc/4_expert.md) + - Handling UTF-8 + - Run as other user or system service + - Scedule bashbot from Cron ## Security Considerations From 259ad9b358889690174e98ec09dc23388b54e514 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 9 Apr 2019 13:01:02 +0200 Subject: [PATCH 14/17] sync master with dev --- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 2 +- commands.sh | 2 +- question | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bashbot.cron b/bashbot.cron index 18f9853..3986f3a 100644 --- a/bashbot.cron +++ b/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$$ v0.6-dev-3-g614eae8 +#### $$VERSION$$ v0.50-15-g275a83b SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 923dcc8..95f0cda 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.6-dev-3-g614eae8 +#### $$VERSION$$ v0.50-15-g275a83b # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index e4ec567..027efb4 100755 --- a/bashbot.sh +++ b/bashbot.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$$ v0.6-dev-7-ge936e6f +#### $$VERSION$$ v0.50-15-g275a83b # # Exit Codes: # - 0 sucess (hopefully) diff --git a/commands.sh b/commands.sh index 79c4ff2..83e6d21 100755 --- a/commands.sh +++ b/commands.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$$ v0.6-dev-3-g614eae8 +#### $$VERSION$$ v0.50-15-g275a83b # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/question b/question index bae87eb..4cc5d1d 100755 --- a/question +++ b/question @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.6-dev-3-g614eae8 +#### $$VERSION$$ v0.50-15-g275a83b # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment From 143c1222bdfbcb2cb8320775bac3bb34a832aa9f Mon Sep 17 00:00:00 2001 From: Kay Marquardt Date: Tue, 9 Apr 2019 13:09:30 +0200 Subject: [PATCH 15/17] fix version --- notify | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notify b/notify index 6802ca2..809a6cd 100755 --- a/notify +++ b/notify @@ -2,7 +2,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.6-dev-3-g614eae8 +#### $$VERSION$$ v0.50-15-g275a83b # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment From 4ddd1226be08cacee2fbf09fd101b67e6f1f864c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 9 Apr 2019 13:41:38 +0200 Subject: [PATCH 16/17] add README.txt for help command --- README.txt | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++ bashbot.sh | 4 +- commands.sh | 2 +- 3 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 README.txt diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..7f9407b --- /dev/null +++ b/README.txt @@ -0,0 +1,110 @@ +bashbot +------- + +A Telegram bot written in bash. + +Depends on http://github.com/tmux/tmux[tmux]. Uses +http://github.com/dominictarr/JSON.sh[JSON.sh]. + +Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M +(@gnadelwartz). + +Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. + +https://github.com/topkecleon/telegram-bot-bash/releases[Download from +github] + +Released to the public domain wherever applicable. Elsewhere, consider +it released under the http://www.wtfpl.net/txt/copying/[WTFPLv2]. + +Install bashbot +~~~~~~~~~~~~~~~ + +1. Go to the directory you want to install bashbot, e.g. + +* your $HOME directory (install and run with your user-ID) +* /usr/local if you want to run as service + +1. Clone the repository: ++ +.... +git clone --recursive https://github.com/topkecleon/telegram-bot-bash +.... +2. Change to directory `telegram-bot.bash`, run `./bashbot.sh init` and +follow the instructions. At this stage you are asked for your Bots token +given by botfather. + +Getting started +~~~~~~~~~~~~~~~ + +* link:doc/1_firstbot.md[Create your first telegram bot] +* link:doc/2_usage.md[Make your own Bot] +* Managing your own Bot +* Recieve data +* Send Messages +* Send files, location etc. +* link:doc/3_advanced.md[Advatage Features] +* Interactive Chats +* Background Jobs +* Inline queries +* link:doc/4_expert.md[Expert Use] +* Handling UTF-8 +* Run as other user or system service +* Scedule bashbot from Cron + +Security Considerations +~~~~~~~~~~~~~~~~~~~~~~~ + +Running a Telegram Bot means you are conneted to the public, you never +know whats send to your Bot. + +Bash scripts in general are not designed to be bullet proof, so consider +this Bot as a proof of concept. More concret examples of security +problems is bash's 'quoting hell' and globbing. +https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells[Implications +of wrong quoting] + +Whenever you are processing input from outside your bot you should +disable globbing (set -f) and carefully quote everthing. + +To improve you scripts we recommend to lint them with +https://www.shellcheck.net/[shellcheck]. This can be done online or you +can https://github.com/koalaman/shellcheck#installing[install shellcheck +locally]. bashbot itself is also linted by shellcheck. + +Run your Bot as a restricted user +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Every file your bot can write is in danger to be overwritten/deleted, In +case of bad handling of user input every file your Bot can read is in +danger of being disclosed. + +Never run your Bot as root, this is the most dangerous you can do! +Usually the user 'nobody' has almost no rigths on Unix/Linux systems. +See Expert use on how to run your Bot as an other user. + +Secure your Bot installation +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Everyone who can read your Bot files can extract your Bots data. +Especially your Bot Token in `token` must be protected against other +users. No one exept you should have write access to the Bot files. The +Bot itself need write access to `count` and `tmp-bot-bash` only, all +other files should be write protected. + +Runing `./bashbot.sh init` sets the Bot permissions to reasonable +default values as a starting point. + +Is this Bot insecure? +^^^^^^^^^^^^^^^^^^^^^ + +No - its not less (in)secure as any other Bot written in any other +language. But you should know about the implications ... + +That's it! +~~~~~~~~~~ + +If you feel that there's something missing or if you found a bug, feel +free to submit a pull request! + +++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.sh b/bashbot.sh index 027efb4..8dcb499 100755 --- a/bashbot.sh +++ b/bashbot.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$$ v0.50-15-g275a83b +#### $$VERSION$$ v0.50-13-g143c122 # # Exit Codes: # - 0 sucess (hopefully) @@ -621,7 +621,7 @@ case "$1" in ;; "help") $CLEAR - less "README.md" + less "README.txt" exit ;; "attach") diff --git a/commands.sh b/commands.sh index 83e6d21..02a25e7 100755 --- a/commands.sh +++ b/commands.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$$ v0.50-15-g275a83b +#### $$VERSION$$ v0.6-dev2-0-g143c122 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 From 94fa6a05da8206295ca76e74bda097660db395ee Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 10 Apr 2019 11:53:58 +0200 Subject: [PATCH 17/17] Bashbot Version 5.1 shellcheck testet --- README.md | 8 +++----- README.txt | 1 + bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 2 +- commands.sh | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- notify | 2 +- question | 2 +- version | 3 +-- 13 files changed, 15 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 97b2ec0..4308980 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,8 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/cop ## Install bashbot 1. Go to the directory you want to install bashbot, e.g. - - - your $HOME directory (install and run with your user-ID) - - /usr/local if you want to run as service - + - your $HOME directory (install and run with your user-ID) + - /usr/local if you want to run as service 2. Clone the repository: ``` git clone --recursive https://github.com/topkecleon/telegram-bot-bash @@ -72,4 +70,4 @@ No - its not less (in)secure as any other Bot written in any other language. But If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.50-13-g4d5d386 +#### $$VERSION$$ v0.51-0-g4ddd122 diff --git a/README.txt b/README.txt index 7f9407b..a78d11e 100644 --- a/README.txt +++ b/README.txt @@ -107,4 +107,5 @@ That's it! If you feel that there's something missing or if you found a bug, feel free to submit a pull request! +latexmath:[\[VERSION\]] v0.6-dev2-0-g143c122 ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index 3986f3a..d7a371b 100644 --- a/bashbot.cron +++ b/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$$ v0.50-15-g275a83b +#### $$VERSION$$ v0.51-0-g4ddd122 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 95f0cda..9624f9c 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.50-15-g275a83b +#### $$VERSION$$ v0.51-0-g4ddd122 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 8dcb499..56f26d9 100755 --- a/bashbot.sh +++ b/bashbot.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$$ v0.50-13-g143c122 +#### $$VERSION$$ v0.51-0-g4ddd122 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/commands.sh b/commands.sh index 02a25e7..88c3638 100755 --- a/commands.sh +++ b/commands.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$$ v0.6-dev2-0-g143c122 +#### $$VERSION$$ v0.51-0-g4ddd122 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 6c5c614..934f3fe 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -70,5 +70,5 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash ``` 3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather. -#### $$VERSION$$ v0.50-13-g4d5d386 +#### $$VERSION$$ v0.51-0-g4ddd122 diff --git a/doc/2_usage.md b/doc/2_usage.md index 0870a00..acf7b30 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -147,6 +147,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.50-13-g4d5d386 +#### $$VERSION$$ v0.51-0-g4ddd122 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 2322f46..eabd071 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -97,5 +97,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.50-13-g4d5d386 +#### $$VERSION$$ v0.51-0-g4ddd122 diff --git a/doc/4_expert.md b/doc/4_expert.md index c7ca32d..e5d1fee 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -102,5 +102,5 @@ An example crontab is provided in ```bashbot.cron```. - if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot``` -#### $$VERSION$$ v0.50-13-g4d5d386 +#### $$VERSION$$ v0.51-0-g4ddd122 diff --git a/notify b/notify index 809a6cd..bdf25d5 100755 --- a/notify +++ b/notify @@ -2,7 +2,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.50-15-g275a83b +#### $$VERSION$$ v0.51-0-g4ddd122 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 4cc5d1d..701b7dd 100755 --- a/question +++ b/question @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.50-15-g275a83b +#### $$VERSION$$ v0.51-0-g4ddd122 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index db5e8d6..0ef84f3 100755 --- a/version +++ b/version @@ -1,7 +1,6 @@ #!/bin/bash # - -#### $$VERSION$$ v0.50-13-g4d5d386 +#### $$VERSION$$ v0.51-0-g4ddd122 # shellcheck disable=SC2016 # # Easy Versioning in git: