From fe9443fe4dcfbdce344375e7b9835a5997b8d7b9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 12 Apr 2019 17:18:20 +0200 Subject: [PATCH 01/36] start v0.6-dev3 - optimize quoting and grep use --- README.md | 2 +- README.txt | 4 +- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 109 ++++++++++++++++++++++++---------------------- commands.sh | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- notify | 2 +- question | 2 +- version | 7 ++- 14 files changed, 74 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index f77b8cb..641b9bb 100644 --- a/README.md +++ b/README.md @@ -75,4 +75,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.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 diff --git a/README.txt b/README.txt index a44379b..b9c77c4 100644 --- a/README.txt +++ b/README.txt @@ -119,5 +119,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.52-0-gdb7b19f -++++++++++++++++++++++++++++++++++++++++ +latexmath:[\[VERSION\]] v0.6-dev3-0-gae157c4 +++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index ec84aec..af518ff 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.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 9594199..9d58275 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index c290954..18a4868 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -10,13 +10,13 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-1-gae157c4 # # Exit Codes: # - 0 sucess (hopefully) # - 1 can't change to dir # - 2 can't write to tmp, count or token -# - 3 user not found +# - 3 user / command not found # - 4 unkown command # are we runnig in a terminal? @@ -43,12 +43,6 @@ if [ ! -w "." ]; then ls -ld . fi -if [ ! -f "JSON.sh/JSON.sh" ]; then - echo "You did not clone recursively! Downloading JSON.sh..." - git clone http://github.com/dominictarr/JSON.sh - echo "JSON.sh has been downloaded. Proceeding." -fi - TOKEN="./token" if [ ! -f "${TOKEN}" ]; then if [ "${CLEAR}" == "" ]; then @@ -57,12 +51,18 @@ if [ ! -f "${TOKEN}" ]; then else ${CLEAR} echo -e "${RED}TOKEN MISSING.${NC}" - echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE${NC}" + echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" read -r token echo "${token}" > "${TOKEN}" fi fi +if [ ! -f "JSON.sh/JSON.sh" ]; then + echo "You did not clone recursively! Downloading JSON.sh..." + git clone http://github.com/dominictarr/JSON.sh + echo "JSON.sh has been downloaded. Proceeding." +fi + BOTADMIN="./botadmin" if [ ! -f "${BOTADMIN}" ]; then if [ "${CLEAR}" == "" ]; then @@ -104,42 +104,47 @@ elif [ ! -w "${COUNT}" ]; then exit 2 fi +COMMANDS="./commands.sh" +if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then + ${CLEAR} + echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}" + exit 3 + ls -l "${COMMANDS}" +fi -source "commands.sh" "source" -URL='https://api.telegram.org/bot'$TOKEN +# shellcheck source=./commands.sh +source "${COMMANDS}" "source" + +URL='https://api.telegram.org/bot'"$TOKEN" -MSG_URL=$URL'/sendMessage' -LEAVE_URL=$URL'/leaveChat' -KICK_URL=$URL'/kickChatMember' -UNBAN_URL=$URL'/unbanChatMember' -PHO_URL=$URL'/sendPhoto' -AUDIO_URL=$URL'/sendAudio' -DOCUMENT_URL=$URL'/sendDocument' -STICKER_URL=$URL'/sendSticker' -VIDEO_URL=$URL'/sendVideo' -VOICE_URL=$URL'/sendVoice' -LOCATION_URL=$URL'/sendLocation' -VENUE_URL=$URL'/sendVenue' -ACTION_URL=$URL'/sendChatAction' -FORWARD_URL=$URL'/forwardMessage' -INLINE_QUERY=$URL'/answerInlineQuery' -ME_URL=$URL'/getMe' -DELETE_URL=$URL'/deleteMessage' -GETMEMBER_URL=$URL'/getChatMember' -ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | grep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2)" +MSG_URL="$URL"'/sendMessage' +LEAVE_URL="$URL"'/leaveChat' +KICK_URL="$URL"'/kickChatMember' +UNBAN_URL="$URL"'/unbanChatMember' +PHO_URL="$URL"'/sendPhoto' +AUDIO_URL="$URL"'/sendAudio' +DOCUMENT_URL="$URL"'/sendDocument' +STICKER_URL="$URL"'/sendSticker' +VIDEO_URL="$URL"'/sendVideo' +VOICE_URL="$URL"'/sendVoice' +LOCATION_URL="$URL"'/sendLocation' +VENUE_URL="$URL"'/sendVenue' +ACTION_URL="$URL"'/sendChatAction' +FORWARD_URL="$URL"'/forwardMessage' +INLINE_QUERY="$URL"'/answerInlineQuery' +DELETE_URL="$URL"'/deleteMessage' +GETMEMBER_URL="$URL"'/getChatMember' +ME_URL="$URL"'/getMe' +ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | sed -n -e '\["result","username"\] s/.*\][ \t]"\(.*\)"$/\1/p')" -FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/' -UPD_URL=$URL'/getUpdates?offset=' -GET_URL=$URL'/getFile' +FILE_URL='https://api.telegram.org/file/bot'"$TOKEN"'/' +UPD_URL="$URL"'/getUpdates?offset=' +GET_URL="$URL"'/getFile' OFFSET=0 declare -A USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO -urlencode() { - echo "$*" | sed 's:%:%25:g;s: :%20:g;s:<:%3C:g;s:>:%3E:g;s:#:%23:g;s:{:%7B:g;s:}:%7D:g;s:|:%7C:g;s:\\:%5C:g;s:\^:%5E:g;s:~:%7E:g;s:\[:%5B:g;s:\]:%5D:g;s:`:%60:g;s:;:%3B:g;s:/:%2F:g;s:?:%3F:g;s^:^%3A^g;s:@:%40:g;s:=:%3D:g;s:&:%26:g;s:\$:%24:g;s:\!:%21:g;s:\*:%2A:g' -} - send_message() { local text arg keyboard file lat long title address sent @@ -148,7 +153,6 @@ send_message() { 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 text="${text// mynewlinestartshere /$'\r\n'}" no_keyboard="$(echo "$2" | sed '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" @@ -194,10 +198,10 @@ send_message() { send_text() { case "$2" in - html_parse_mode*) + 'html_parse_mode'*) send_html_message "$1" "${2//html_parse_mode}" ;; - markdown_parse_mode*) + 'markdown_parse_mode'*) send_markdown_message "$1" "${2//markdown_parse_mode}" ;; *) @@ -271,7 +275,7 @@ user_is_botadmin() { } user_is_allowed() { - local acl; acl="$1" + local acl="$1" [ "$1" == "" ] && return 1 grep -F -xq "${acl}:*:*" <"${BOTACL}" && return 0 [ "$2" != "" ] && acl="${acl}:$2" @@ -352,7 +356,6 @@ answer_inline_query() { send_keyboard() { local chat="$1" local text="$2" - shift 2 local keyboard=init OLDIFS=$IFS IFS=$(echo -en "\"") @@ -365,12 +368,11 @@ send_keyboard() { remove_keyboard() { local chat="$1" local text="$2" - shift 2 res="$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"remove_keyboard\": true}")" } get_file() { - [ "$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)" + [ "$1" != "" ] && echo "$FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | sed -n -e '\["result","file_path"\] s/.*\][ \t]"\(.*\)"$/\1/p')" } send_file() { @@ -424,24 +426,24 @@ send_file() { # 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 location send_action() { - [ "$2" = "" ] && return + [ "$2" == "" ] && return res="$(curl -s "$ACTION_URL" -F "chat_id=$1" -F "action=$2")" } send_location() { - [ "$3" = "" ] && return + [ "$3" == "" ] && return res="$(curl -s "$LOCATION_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3")" } send_venue() { - [ "$5" = "" ] && return + [ "$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")" } forward() { - [ "$3" = "" ] && return + [ "$3" == "" ] && return res="$(curl -s "$FORWARD_URL" -F "chat_id=$1" -F "from_chat_id=$2" -F "message_id=$3")" } @@ -465,7 +467,7 @@ checkback() { } checkproc() { - tmux ls | grep -q "$1${copname}"; res=$? + tmux ls | grep -qF "$1${copname}"; res=$? } killback() { @@ -562,12 +564,13 @@ process_client() { rm "$TMP" # Tmux - copname="$ME"_"${CHAT[ID]}" + copname="$ME"'_'"${CHAT[ID]}" - source commands.sh + # shellcheck source=./commands.sh + source "${COMMANDS}" tmpcount="COUNT${CHAT[ID]}" - grep -q "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT} + grep -qF "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT} # To get user count execute bash bashbot.sh count } @@ -577,7 +580,7 @@ while [ "$1" == "startbot" ]; do { UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./JSON.sh/JSON.sh)" # Offset - OFFSET="$(echo "$UPDATE" | grep '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" + OFFSET="$(echo "$UPDATE" | sed -n -e '\["result",[0-9]*,"update_id"\] s/.*\][ \t]//p')" OFFSET=$((OFFSET+1)) if [ "$OFFSET" != "1" ]; then diff --git a/commands.sh b/commands.sh index e411a53..d01b6f7 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.52-1-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 3fc862e..2cf1885 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.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 diff --git a/doc/2_usage.md b/doc/2_usage.md index 3a8746c..5b3790c 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.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 0c80975..9685984 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 diff --git a/doc/4_expert.md b/doc/4_expert.md index bbeec62..f2dcb3a 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.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 diff --git a/doc/5_practice.md b/doc/5_practice.md index 0f372f1..fe8b587 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -111,5 +111,5 @@ In bashbot.sh line 490: ``` As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 diff --git a/notify b/notify index 8c2a55b..9cfbfbb 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.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 8d34846..ed413ad 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.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index 6137641..421e430 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.52-0-gdb7b19f +#### $$VERSION$$ v0.6-dev3-0-gae157c4 # shellcheck disable=SC2016 # # Easy Versioning in git: @@ -40,7 +40,10 @@ unset IFS VERSION="$(git describe --tags --long)" echo "Update files to version $VERSION ..." -for file in * doc/* +FILES="* doc/*" +[ "$1" != "" ] && FILES="$*" + +for file in $FILES do [ ! -f "$file" ] && continue #[ "$file" == "version" ] && continue From 121a113703ca4be9144d51791513ff9e99f1a99c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 12 Apr 2019 19:39:21 +0200 Subject: [PATCH 02/36] more portable [ = ] test --- bashbot.sh | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 18a4868..1394011 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-dev3-1-gae157c4 +#### $$VERSION$$ v0.6-dev3-1-gfe9443f # # Exit Codes: # - 0 sucess (hopefully) @@ -45,7 +45,7 @@ fi TOKEN="./token" if [ ! -f "${TOKEN}" ]; then - if [ "${CLEAR}" == "" ]; then + if [ "${CLEAR}" = "" ]; then echo "Running headless, run ${SCRIPT} init first!" exit 2 else @@ -65,9 +65,9 @@ fi BOTADMIN="./botadmin" if [ ! -f "${BOTADMIN}" ]; then - if [ "${CLEAR}" == "" ]; then + if [ "${CLEAR}" = "" ]; then echo "Running headless, set botadmin to AUTO MODE!" - echo "?" > "${BOTADMIN}" + echo '?' > "${BOTADMIN}" else ${CLEAR} echo -e "${RED}BOTADMIN MISSING.${NC}" @@ -256,27 +256,27 @@ leave_chat() { } user_is_creator() { - if [ "${1:--}" == "${2:-+}" ] || [ "$(get_chat_member_status "$1" "$2")" == "creator" ]; then return 0; fi + if [ "${1:--}" = "${2:-+}" ] || [ "$(get_chat_member_status "$1" "$2")" = "creator" ]; then return 0; fi return 1 } user_is_admin() { local me; me="$(get_chat_member_status "$1" "$2")" - if [ "${me}" == "creator" ] || [ "${me}" == "administrator" ]; then return 0; fi + if [ "${me}" = "creator" ] || [ "${me}" = "administrator" ]; then return 0; fi return 1 } user_is_botadmin() { local admin; admin="$(head -n 1 "${BOTADMIN}")" - [ "${admin}" == "${1}" ] && return 0 - [[ "${admin}" == "@*" ]] && [[ "${admin}" == "${2}" ]] && return 0 - if [ "${admin}" == "?" ]; then echo "${1:-?}" >"${BOTADMIN}"; return 0; fi + [ "${admin}" = "${1}" ] && return 0 + [[ "${admin}" = "@*" ]] && [[ "${admin}" = "${2}" ]] && return 0 + if [ "${admin}" = "?" ]; then echo "${1:-?}" >"${BOTADMIN}"; return 0; fi return 1 } user_is_allowed() { local acl="$1" - [ "$1" == "" ] && return 1 + [ "$1" = "" ] && return 1 grep -F -xq "${acl}:*:*" <"${BOTACL}" && return 0 [ "$2" != "" ] && acl="${acl}:$2" grep -F -xq "${acl}:*" <"${BOTACL}" && return 0 @@ -426,24 +426,24 @@ send_file() { # 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 location send_action() { - [ "$2" == "" ] && return + [ "$2" = "" ] && return res="$(curl -s "$ACTION_URL" -F "chat_id=$1" -F "action=$2")" } send_location() { - [ "$3" == "" ] && return + [ "$3" = "" ] && return res="$(curl -s "$LOCATION_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3")" } send_venue() { - [ "$5" == "" ] && return + [ "$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")" } forward() { - [ "$3" == "" ] && return + [ "$3" = "" ] && return res="$(curl -s "$FORWARD_URL" -F "chat_id=$1" -F "from_chat_id=$2" -F "message_id=$3")" } @@ -487,7 +487,7 @@ inproc() { process_updates() { MAX_PROCESS_NUMBER=$(echo "$UPDATE" | sed '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g') for ((PROCESS_NUMBER=0; PROCESS_NUMBER<=MAX_PROCESS_NUMBER; PROCESS_NUMBER++)); do - if [ "$1" == "test" ]; then + if [ "$1" = "test" ]; then process_client "$1" else process_client "$1" & @@ -575,7 +575,7 @@ process_client() { } # source the script with source as param to use functions in other scripts -while [ "$1" == "startbot" ]; do { +while [ "$1" = "startbot" ]; do { UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./JSON.sh/JSON.sh)" @@ -584,7 +584,7 @@ while [ "$1" == "startbot" ]; do { OFFSET=$((OFFSET+1)) if [ "$OFFSET" != "1" ]; then - if [ "$2" == "test" ]; then + if [ "$2" = "test" ]; then process_updates "$2" else process_updates "$2" & @@ -646,7 +646,7 @@ case "$1" in ${CLEAR} echo -e "${GREEN}Restart background processes ...${NC}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do - if [ "${FILE}" == "${TMPDIR:-.}/*-back.cmd" ]; then + if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then echo -e "${RED}No background processes to start.${NC}"; break else RESTART="$(< "${FILE}")" @@ -673,14 +673,14 @@ case "$1" in ${CLEAR} echo -e "${GREEN}Stopping background processes ...${NC}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do - if [ "${FILE}" == "${TMPDIR:-.}/*-back.cmd" ]; then + if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then echo -e "${RED}No background processes.${NC}"; break else REMOVE="$(< "${FILE}")" JOB="${REMOVE#*:}" fifo="back-${JOB%:*}-${ME}_${REMOVE%%:*}" echo "killbackground ${fifo}" - [ "$1" == "killback" ] && rm -f "${FILE}" # remove job + [ "$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 fi done From 03b69296b5e5428ac5b170d7c2a91f96d985eca0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 12 Apr 2019 20:29:21 +0200 Subject: [PATCH 03/36] fix grep to sed conversion --- commands.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.sh b/commands.sh index d01b6f7..e8aaa75 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-dev3-0-gae157c4 +#### $$VERSION$$ v0.6-dev3-2-g121a113 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 From c16345995414a46a434a3915ba25d33a51eae559 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 12 Apr 2019 20:49:58 +0200 Subject: [PATCH 04/36] revert some optimizations --- bashbot.sh | 72 +++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 1394011..0ff5c25 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-dev3-1-gfe9443f +#### $$VERSION$$ v0.6-dev3-3-g03b6929 # # Exit Codes: # - 0 sucess (hopefully) @@ -115,36 +115,40 @@ fi # shellcheck source=./commands.sh source "${COMMANDS}" "source" -URL='https://api.telegram.org/bot'"$TOKEN" +URL='https://api.telegram.org/bot'$TOKEN -MSG_URL="$URL"'/sendMessage' -LEAVE_URL="$URL"'/leaveChat' -KICK_URL="$URL"'/kickChatMember' -UNBAN_URL="$URL"'/unbanChatMember' -PHO_URL="$URL"'/sendPhoto' -AUDIO_URL="$URL"'/sendAudio' -DOCUMENT_URL="$URL"'/sendDocument' -STICKER_URL="$URL"'/sendSticker' -VIDEO_URL="$URL"'/sendVideo' -VOICE_URL="$URL"'/sendVoice' -LOCATION_URL="$URL"'/sendLocation' -VENUE_URL="$URL"'/sendVenue' -ACTION_URL="$URL"'/sendChatAction' -FORWARD_URL="$URL"'/forwardMessage' -INLINE_QUERY="$URL"'/answerInlineQuery' -DELETE_URL="$URL"'/deleteMessage' -GETMEMBER_URL="$URL"'/getChatMember' -ME_URL="$URL"'/getMe' -ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | sed -n -e '\["result","username"\] s/.*\][ \t]"\(.*\)"$/\1/p')" +MSG_URL=$URL'/sendMessage' +LEAVE_URL=$URL'/leaveChat' +KICK_URL=$URL'/kickChatMember' +UNBAN_URL=$URL'/unbanChatMember' +PHO_URL=$URL'/sendPhoto' +AUDIO_URL=$URL'/sendAudio' +DOCUMENT_URL=$URL'/sendDocument' +STICKER_URL=$URL'/sendSticker' +VIDEO_URL=$URL'/sendVideo' +VOICE_URL=$URL'/sendVoice' +LOCATION_URL=$URL'/sendLocation' +VENUE_URL=$URL'/sendVenue' +ACTION_URL=$URL'/sendChatAction' +FORWARD_URL=$URL'/forwardMessage' +INLINE_QUERY=$URL'/answerInlineQuery' +ME_URL=$URL'/getMe' +DELETE_URL=$URL'/deleteMessage' +GETMEMBER_URL=$URL'/getChatMember' +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"'/' -UPD_URL="$URL"'/getUpdates?offset=' -GET_URL="$URL"'/getFile' +FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/' +UPD_URL=$URL'/getUpdates?offset=' +GET_URL=$URL'/getFile' OFFSET=0 declare -A USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO +urlencode() { + echo "$*" | sed 's:%:%25:g;s: :%20:g;s:<:%3C:g;s:>:%3E:g;s:#:%23:g;s:{:%7B:g;s:}:%7D:g;s:|:%7C:g;s:\\:%5C:g;s:\^:%5E:g;s:~:%7E:g;s:\[:%5B:g;s:\]:%5D:g;s:`:%60:g;s:;:%3B:g;s:/:%2F:g;s:?:%3F:g;s^:^%3A^g;s:@:%40:g;s:=:%3D:g;s:&:%26:g;s:\$:%24:g;s:\!:%21:g;s:\*:%2A:g' +} + send_message() { local text arg keyboard file lat long title address sent @@ -153,6 +157,7 @@ send_message() { 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 text="${text// mynewlinestartshere /$'\r\n'}" no_keyboard="$(echo "$2" | sed '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" @@ -198,10 +203,10 @@ send_message() { send_text() { case "$2" in - 'html_parse_mode'*) + html_parse_mode*) send_html_message "$1" "${2//html_parse_mode}" ;; - 'markdown_parse_mode'*) + markdown_parse_mode*) send_markdown_message "$1" "${2//markdown_parse_mode}" ;; *) @@ -356,6 +361,7 @@ answer_inline_query() { send_keyboard() { local chat="$1" local text="$2" + shift 2 local keyboard=init OLDIFS=$IFS IFS=$(echo -en "\"") @@ -368,11 +374,12 @@ send_keyboard() { remove_keyboard() { local chat="$1" local text="$2" + shift 2 res="$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"remove_keyboard\": true}")" } get_file() { - [ "$1" != "" ] && echo "$FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | sed -n -e '\["result","file_path"\] s/.*\][ \t]"\(.*\)"$/\1/p')" + [ "$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() { @@ -467,7 +474,7 @@ checkback() { } checkproc() { - tmux ls | grep -qF "$1${copname}"; res=$? + tmux ls | grep -q "$1${copname}"; res=$? } killback() { @@ -564,13 +571,12 @@ process_client() { rm "$TMP" # Tmux - copname="$ME"'_'"${CHAT[ID]}" + copname="$ME"_"${CHAT[ID]}" - # shellcheck source=./commands.sh - source "${COMMANDS}" + source commands.sh tmpcount="COUNT${CHAT[ID]}" - grep -qF "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT} + grep -q "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT} # To get user count execute bash bashbot.sh count } @@ -580,7 +586,7 @@ while [ "$1" = "startbot" ]; do { UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./JSON.sh/JSON.sh)" # Offset - OFFSET="$(echo "$UPDATE" | sed -n -e '\["result",[0-9]*,"update_id"\] s/.*\][ \t]//p')" + OFFSET="$(echo "$UPDATE" | grep '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" OFFSET=$((OFFSET+1)) if [ "$OFFSET" != "1" ]; then From c944292047267dc67a70e66aefc6fa6bf518c216 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Apr 2019 14:50:53 +0200 Subject: [PATCH 05/36] fix UTF-8 decode of message --- README.md | 4 +++- README.txt | 6 ++++-- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 16 +++++++++++++--- commands.sh | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 11 ++++------- doc/5_practice.md | 2 +- notify | 2 +- question | 2 +- version | 2 +- 14 files changed, 34 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 641b9bb..4ff80dd 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ A Telegram bot written in bash. Depends on [tmux](http://github.com/tmux/tmux). Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). +For full UTF-8 support you need python on your system (optional). + Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadelwartz). Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. @@ -75,4 +77,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-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec diff --git a/README.txt b/README.txt index b9c77c4..fba9ed9 100644 --- a/README.txt +++ b/README.txt @@ -6,6 +6,8 @@ A Telegram bot written in bash. Depends on http://github.com/tmux/tmux[tmux]. Uses http://github.com/dominictarr/JSON.sh[JSON.sh]. +For full UTF-8 support you need python on your system (optional). + Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadelwartz). @@ -119,5 +121,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-dev3-0-gae157c4 -++++++++++++++++++++++++++++++++++++++++++++ +latexmath:[\[VERSION\]] v0.60-dev3-0-g2550aec ++++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index af518ff..a608261 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-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 9d58275..ebf016e 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.6-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 0ff5c25..3aa3b41 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-dev3-3-g03b6929 +#### $$VERSION$$ v0.60-dev3-0-g2550aec # # Exit Codes: # - 0 sucess (hopefully) @@ -149,6 +149,17 @@ urlencode() { echo "$*" | sed 's:%:%25:g;s: :%20:g;s:<:%3C:g;s:>:%3E:g;s:#:%23:g;s:{:%7B:g;s:}:%7D:g;s:|:%7C:g;s:\\:%5C:g;s:\^:%5E:g;s:~:%7E:g;s:\[:%5B:g;s:\]:%5D:g;s:`:%60:g;s:;:%3B:g;s:/:%2F:g;s:?:%3F:g;s^:^%3A^g;s:@:%40:g;s:=:%3D:g;s:&:%26:g;s:\$:%24:g;s:\!:%21:g;s:\*:%2A:g' } +# use phyton to decode UFT-8 JSON, provide error prone echo -e as fallback +if which python >/dev/null 2>&1 || which phyton2 >/dev/null 2>&1; then + jsondecode() { + printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))' + } +else + echo -e "${ORANGE}WARNING: Fallback to non UTF mode, install python to have full UTF-8 support!${NC}" + jsondecode() { + echo -e "$1" + } +fi send_message() { local text arg keyboard file lat long title address sent @@ -157,7 +168,6 @@ send_message() { 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 text="${text// mynewlinestartshere /$'\r\n'}" no_keyboard="$(echo "$2" | sed '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" @@ -505,7 +515,7 @@ process_client() { 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')" + MESSAGE[0]="$(jsondecode "$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","text"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" | sed 's#\\/#/#g')" MESSAGE[ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","message_id"\]/ s/.*\][ \t]//p' <"$TMP" )" # Chat diff --git a/commands.sh b/commands.sh index e8aaa75..ba8b78b 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-dev3-2-g121a113 +#### $$VERSION$$ v0.60-dev3-0-g2550aec # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 2cf1885..2b6f6aa 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.6-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec diff --git a/doc/2_usage.md b/doc/2_usage.md index 5b3790c..188090a 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.6-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 9685984..357405f 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.6-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec diff --git a/doc/4_expert.md b/doc/4_expert.md index f2dcb3a..16abd9f 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -30,21 +30,18 @@ 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 Support for Telegram JSON ```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: +Telegram send JSON 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. - +In theorie you can decode these characters with ```echo -e``` or ```printf '%s\\n'``` in bash, but this works only for single byte characters! To support also multibyte characters in JSON strings you need a working python on your system. If no python is detected on your system, bashbot falls back to echo -e. See [longstanding issue #50](https://github.com/topkecleon/telegram-bot-bash/issues/50) ### Run as other user or system service @@ -102,5 +99,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.6-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec diff --git a/doc/5_practice.md b/doc/5_practice.md index fe8b587..b153323 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -111,5 +111,5 @@ In bashbot.sh line 490: ``` As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.6-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec diff --git a/notify b/notify index 9cfbfbb..16472d8 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-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index ed413ad..a886f0c 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-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index 421e430..57fbae3 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.6-dev3-0-gae157c4 +#### $$VERSION$$ v0.60-dev3-0-g2550aec # shellcheck disable=SC2016 # # Easy Versioning in git: From 9eddea5ce4d43a7efb3ea149522fb27f27fe41f4 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Apr 2019 15:07:49 +0200 Subject: [PATCH 06/36] adjust doc for UTF-8 support --- README.md | 4 ++-- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 4 ++-- doc/5_practice.md | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4ff80dd..268135e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A Telegram bot written in bash. Depends on [tmux](http://github.com/tmux/tmux). Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). -For full UTF-8 support you need python on your system (optional). +For full UTF-8 support you need [python on your system](doc/4_expert.md#UTF-8-Support) (optional). Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadelwartz). @@ -77,4 +77,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.60-dev3-0-g2550aec +#### $$VERSION$$ v0.60-dev3-1-gc944292 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 2b6f6aa..19b464c 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.60-dev3-0-g2550aec +#### $$VERSION$$ v0.60-dev3-1-gc944292 diff --git a/doc/2_usage.md b/doc/2_usage.md index 188090a..d0cb1a6 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.60-dev3-0-g2550aec +#### $$VERSION$$ v0.60-dev3-1-gc944292 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 357405f..5f6d694 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.60-dev3-0-g2550aec +#### $$VERSION$$ v0.60-dev3-1-gc944292 diff --git a/doc/4_expert.md b/doc/4_expert.md index 16abd9f..d41f9b9 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -30,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 Support for Telegram JSON +#### UTF-8 Support ```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 @@ -99,5 +99,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.60-dev3-0-g2550aec +#### $$VERSION$$ v0.60-dev3-1-gc944292 diff --git a/doc/5_practice.md b/doc/5_practice.md index b153323..486e22f 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -111,5 +111,5 @@ In bashbot.sh line 490: ``` As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.60-dev3-0-g2550aec +#### $$VERSION$$ v0.60-dev3-1-gc944292 From ff684b916e1890b7fe7fc89147c2ad57fcd3ffe5 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Apr 2019 15:11:18 +0200 Subject: [PATCH 07/36] fix doc typo --- doc/4_expert.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/4_expert.md b/doc/4_expert.md index d41f9b9..a08d786 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -41,13 +41,14 @@ Telegram send JSON messages with all characters not fitting in one byte (256 bit ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ``` -In theorie you can decode these characters with ```echo -e``` or ```printf '%s\\n'``` in bash, but this works only for single byte characters! To support also multibyte characters in JSON strings you need a working python on your system. If no python is detected on your system, bashbot falls back to echo -e. See [longstanding issue #50](https://github.com/topkecleon/telegram-bot-bash/issues/50) +In theorie you can decode these characters with ```echo -e``` or ```printf '%s\\n'``` in bash, but this works only for single byte characters! To support also multibyte characters in JSON strings you need a working python on your system. +#### If no python is detected on your system, bashbot falls back to echo -e. See [longstanding issue #50](https://github.com/topkecleon/telegram-bot-bash/issues/50) ### Run as other user or system service 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. +#### 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 +100,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.60-dev3-1-gc944292 +#### $$VERSION$$ v0.60-dev3-2-g9eddea5 From 8ec4c1434a709bbc3d70c0413d239437751750fa Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Apr 2019 20:53:59 +0200 Subject: [PATCH 08/36] decode all strings with JsonDecode --- bashbot.sh | 87 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 3aa3b41..b249c0c 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.60-dev3-0-g2550aec +#### $$VERSION$$ v0.60-dev3-3-gff684b9 # # Exit Codes: # - 0 sucess (hopefully) @@ -145,18 +145,15 @@ GET_URL=$URL'/getFile' OFFSET=0 declare -A USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO -urlencode() { - echo "$*" | sed 's:%:%25:g;s: :%20:g;s:<:%3C:g;s:>:%3E:g;s:#:%23:g;s:{:%7B:g;s:}:%7D:g;s:|:%7C:g;s:\\:%5C:g;s:\^:%5E:g;s:~:%7E:g;s:\[:%5B:g;s:\]:%5D:g;s:`:%60:g;s:;:%3B:g;s:/:%2F:g;s:?:%3F:g;s^:^%3A^g;s:@:%40:g;s:=:%3D:g;s:&:%26:g;s:\$:%24:g;s:\!:%21:g;s:\*:%2A:g' -} # use phyton to decode UFT-8 JSON, provide error prone echo -e as fallback if which python >/dev/null 2>&1 || which phyton2 >/dev/null 2>&1; then - jsondecode() { + JsonDecode() { printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))' } else echo -e "${ORANGE}WARNING: Fallback to non UTF mode, install python to have full UTF-8 support!${NC}" - jsondecode() { + JsonDecode() { echo -e "$1" } fi @@ -511,72 +508,78 @@ process_updates() { fi done } +JsonGetString() { + sed -n -e '/\['"$1"'\]/ s/.*\][ \t]"\(.*\)"$/\1/p' +} +JsonGetValue() { + sed -n -e '/\['"$1"'\]/ s/.*\][ \t]//p' +} process_client() { local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE" echo "$UPDATE" >"$TMP" # Message - MESSAGE[0]="$(jsondecode "$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","text"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" | sed 's#\\/#/#g')" - MESSAGE[ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","message_id"\]/ s/.*\][ \t]//p' <"$TMP" )" + MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","text"' <"$TMP")" | sed 's#\\/#/#g')" + MESSAGE[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","message_id"' <"$TMP" )" # Chat - CHAT[ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","chat","id"\]/ s/.*\][ \t]//p' <"$TMP" )" - CHAT[FIRST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","chat","first_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - CHAT[LAST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","chat","last_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - CHAT[USERNAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","chat","username"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - CHAT[TITLE]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","chat","title"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - CHAT[TYPE]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","chat","type"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","chat","all_members_are_administrators"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" + CHAT[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","chat","id"' <"$TMP" )" + CHAT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","first_name"' <"$TMP")")" + CHAT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","last_name"' <"$TMP")")" + CHAT[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","username"' <"$TMP")")" + CHAT[TITLE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","title"' <"$TMP")")" + CHAT[TYPE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","type"' <"$TMP")")" + CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="$(JsonDecode "$(JsonGetString '/\["result",'$PROCESS_NUMBER',"message","chat","all_members_are_administrators"' <"$TMP")")" # User - USER[ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","from","id"\]/ s/.*\][ \t]//p' <"$TMP" )" - USER[FIRST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","from","first_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - USER[LAST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","from","last_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - USER[USERNAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","from","username"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" + USER[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","from","id"' <"$TMP" )" + USER[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","from","first_name"' <"$TMP")")" + USER[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","from","last_name"' <"$TMP")")" + USER[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","from","username"' <"$TMP")")" # in reply to message from - REPLYTO[UID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","reply_to_message","from","id"\]/ s/.*\][ \t]//p' <"$TMP" )" + REPLYTO[UID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","id"' <"$TMP" )" if [ "${REPLYTO[UID]}" != "" ]; then - REPLYTO[0]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","reply_to_message","text"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - REPLYTO[ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","reply_to_message","message_id"\/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - REPLYTO[FIRST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","reply_to_message","from","first_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - REPLYTO[LAST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","reply_to_message","from","last_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - REPLYTO[USERNAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","reply_to_message","from","username"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" + REPLYTO[0]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","text"' <"$TMP")")" + REPLYTO[ID]="$(JsonDecode "$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","reply_to_message","message_id"' <"$TMP")")" + REPLYTO[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","first_name"' <"$TMP")")" + REPLYTO[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","last_name"' <"$TMP")")" + REPLYTO[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","username"' <"$TMP")")" fi # forwarded message from - FORWARD[UID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","forward_from","id"\]/ s/.*\][ \t]//p' <"$TMP" )" + FORWARD[UID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","forward_from","id"' <"$TMP" )" if [ "${FORWARD[UID]}" != "" ]; then FORWARD[ID]="${MESSAGE[ID]}" # same as message ID - FORWARD[FIRST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","forward_from","first_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - FORWARD[LAST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","forward_from","last_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - FORWARD[USERNAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","forward_from","username"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" + FORWARD[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","forward_from","first_name"' <"$TMP")")" + FORWARD[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","forward_from","last_name"' <"$TMP")")" + FORWARD[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","forward_from","username"' <"$TMP")")" fi # Audio - URLS[AUDIO]="$(get_file "$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","audio","file_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")")" + URLS[AUDIO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","audio","file_id"' <"$TMP")")")" # Document - URLS[DOCUMENT]="$(get_file "$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","document","file_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")")" + URLS[DOCUMENT]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","document","file_id"' <"$TMP")")")" # Photo - URLS[PHOTO]="$(get_file "$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","photo",.*,"file_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")")" + URLS[PHOTO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","photo",.*,"file_id"' <"$TMP")")")" # Sticker - URLS[STICKER]="$(get_file "$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","sticker","file_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")")" + URLS[STICKER]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","sticker","file_id"' <"$TMP")")")" # Video - URLS[VIDEO]="$(get_file "$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","video","file_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")")" + URLS[VIDEO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","video","file_id"' <"$TMP")")")" # Voice - URLS[VOICE]="$(get_file "$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","voice","file_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")")" + URLS[VOICE]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","voice","file_id"' <"$TMP")")")" # Contact - CONTACT[NUMBER]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","contact","phone_number"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - CONTACT[FIRST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","contact","first_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - CONTACT[LAST_NAME]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","contact","last_name"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" - CONTACT[USER_ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","contact","user_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" + CONTACT[NUMBER]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","phone_number"' <"$TMP")")" + CONTACT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","first_name"' <"$TMP")")" + CONTACT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","last_name"' <"$TMP")")" + CONTACT[USER_ID]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","user_id"' <"$TMP")")" # Caption - CAPTION="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","caption"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" + CAPTION="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","caption"' <"$TMP")")" # 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")" + LOCATION[LONGITUDE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","location","longitude"' <"$TMP")")" + LOCATION[LATITUDE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","location","latitude"' <"$TMP")")" NAME="$(echo "${URLS[*]}" | sed 's/.*\///g')" rm "$TMP" From aa1404d49c21660299404bd121f1d18cb730fb11 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Apr 2019 21:05:17 +0200 Subject: [PATCH 09/36] get all JSON string with JsonGetStrings --- bashbot.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index b249c0c..eb40e5d 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.60-dev3-3-gff684b9 +#### $$VERSION$$ v0.60-dev3-4-g8ec4c14 # # Exit Codes: # - 0 sucess (hopefully) @@ -136,7 +136,6 @@ INLINE_QUERY=$URL'/answerInlineQuery' ME_URL=$URL'/getMe' DELETE_URL=$URL'/deleteMessage' GETMEMBER_URL=$URL'/getChatMember' -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'/' @@ -386,7 +385,7 @@ remove_keyboard() { } get_file() { - [ "$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)" + [ "$1" != "" ] && echo "$FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | JsonGetString '"result","file_path"')" } send_file() { @@ -593,13 +592,16 @@ process_client() { # To get user count execute bash bashbot.sh count } +# get bot name +ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | JsonGetString '"result","username"')" + # 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)" # Offset - OFFSET="$(echo "$UPDATE" | grep '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" + OFFSET="$(echo "$UPDATE" | JsonGetValue '"result",[0-9]*,"update_id"')" OFFSET=$((OFFSET+1)) if [ "$OFFSET" != "1" ]; then From 5787908d726c13ed486f031ec14d8518d5d68309 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Apr 2019 17:00:58 +0200 Subject: [PATCH 10/36] pure bash implementaion of JsonDecode --- bashbot.sh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index eb40e5d..92c0c3b 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.60-dev3-4-g8ec4c14 +#### $$VERSION$$ v0.60-dev3-5-gaa1404d # # Exit Codes: # - 0 sucess (hopefully) @@ -145,15 +145,26 @@ OFFSET=0 declare -A USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO -# use phyton to decode UFT-8 JSON, provide error prone echo -e as fallback +# use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback if which python >/dev/null 2>&1 || which phyton2 >/dev/null 2>&1; then JsonDecode() { printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))' } else - echo -e "${ORANGE}WARNING: Fallback to non UTF mode, install python to have full UTF-8 support!${NC}" + # pure bash implementaion, done by KayM (@gnadelwartz) + # see https://stackoverflow.com/a/55666449/9381171 JsonDecode() { - echo -e "$1" + local out="$1" + local remain="" + local regexp='(.*)\\ud([0-9a-fA-F]{3})\\ud([0-9a-fA-F]{3})(.*)' + while [[ "${out}" =~ $regexp ]] ; do + local W1="$(( ( 0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ))" + local W2="$(( 0xd${BASH_REMATCH[3]} & 0x3ff ))" + U="$(( (${W1} | ${W2}) + 0x10000 ))" + remain="$(printf '\\U%8.8x' "${U}")${BASH_REMATCH[4]}${remain}" + out="${BASH_REMATCH[1]}" + done + echo -e "${out}${remain}" } fi From f5538eff3ae6d7e13df725e6ac0df549fce4f754 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Apr 2019 17:43:12 +0200 Subject: [PATCH 11/36] update doc, fix minor problem with CHAT[TYPE] --- bashbot.sh | 49 +++++++++++++++++++++++++------------------------ doc/4_expert.md | 21 ++++++++++----------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 92c0c3b..4b2e142 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.60-dev3-5-gaa1404d +#### $$VERSION$$ v0.60-dev3-6-g5787908 # # Exit Codes: # - 0 sucess (hopefully) @@ -145,28 +145,6 @@ OFFSET=0 declare -A USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO -# use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback -if which python >/dev/null 2>&1 || which phyton2 >/dev/null 2>&1; then - JsonDecode() { - printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))' - } -else - # pure bash implementaion, done by KayM (@gnadelwartz) - # see https://stackoverflow.com/a/55666449/9381171 - JsonDecode() { - local out="$1" - local remain="" - local regexp='(.*)\\ud([0-9a-fA-F]{3})\\ud([0-9a-fA-F]{3})(.*)' - while [[ "${out}" =~ $regexp ]] ; do - local W1="$(( ( 0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ))" - local W2="$(( 0xd${BASH_REMATCH[3]} & 0x3ff ))" - U="$(( (${W1} | ${W2}) + 0x10000 ))" - remain="$(printf '\\U%8.8x' "${U}")${BASH_REMATCH[4]}${remain}" - out="${BASH_REMATCH[1]}" - done - echo -e "${out}${remain}" - } -fi send_message() { local text arg keyboard file lat long title address sent @@ -538,7 +516,7 @@ process_client() { CHAT[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","username"' <"$TMP")")" CHAT[TITLE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","title"' <"$TMP")")" CHAT[TYPE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","type"' <"$TMP")")" - CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="$(JsonDecode "$(JsonGetString '/\["result",'$PROCESS_NUMBER',"message","chat","all_members_are_administrators"' <"$TMP")")" + CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","all_members_are_administrators"' <"$TMP")")" # User USER[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","from","id"' <"$TMP" )" @@ -603,6 +581,29 @@ process_client() { # To get user count execute bash bashbot.sh count } +# use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback +if which python >/dev/null 2>&1 || which phyton2 >/dev/null 2>&1; then + JsonDecode() { + printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))' + } +else + # pure bash implementaion, done by KayM (@gnadelwartz) + # see https://stackoverflow.com/a/55666449/9381171 + JsonDecode() { + local out="$1" + local remain="" + local regexp='(.*)\\ud([0-9a-fA-F]{3})\\ud([0-9a-fA-F]{3})(.*)' + while [[ "${out}" =~ $regexp ]] ; do + local W1="$(( ( 0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ))" + local W2="$(( 0xd${BASH_REMATCH[3]} & 0x3ff ))" + U="$(( ( W1 | W2 ) + 0x10000 ))" + remain="$(printf '\\U%8.8x' "${U}")${BASH_REMATCH[4]}${remain}" + out="${BASH_REMATCH[1]}" + done + echo -e "${out}${remain}" + } +fi + # get bot name ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | JsonGetString '"result","username"')" diff --git a/doc/4_expert.md b/doc/4_expert.md index a08d786..31d748b 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -1,6 +1,12 @@ ## Expert Use ### Handling UTF-8 character sets +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. + #### 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: @@ -31,18 +37,11 @@ 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 Support -```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. +Telegram send JSON messages with all characters not fitting in one byte (<256 bit) escaped as sequences of ```\uxxxx``` to be regular one byte ASCII Multibyte UTF-8 characters, e.g. Emoticons and Arabic characters, are send in UTF-16 notation. e.g. the Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ``` -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. +**This mixed JSON encoding can't not decoded from ```echo -e``` or ```printf '%s\\n'```, this works only for single byte characters!** -Telegram send JSON 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 -``` -In theorie you can decode these characters with ```echo -e``` or ```printf '%s\\n'``` in bash, but this works only for single byte characters! To support also multibyte characters in JSON strings you need a working python on your system. -#### If no python is detected on your system, bashbot falls back to echo -e. See [longstanding issue #50](https://github.com/topkecleon/telegram-bot-bash/issues/50) +To to fully support decoding of multibyte characters you need a working python2 instllation on your system. If no python is detected bashbot falls back to a **slow, pure bash solution which may not always work 100% correct**. ### Run as other user or system service @@ -100,5 +99,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.60-dev3-2-g9eddea5 +#### $$VERSION$$ v0.60-dev3-6-g5787908 From dec31508fc118fba73f7d735e4d063a9c9705af6 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Apr 2019 17:50:10 +0200 Subject: [PATCH 12/36] fix doc typos --- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 8 +++++--- doc/5_practice.md | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 19b464c..684cecb 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.60-dev3-1-gc944292 +#### $$VERSION$$ v0.60-dev3-7-gf5538ef diff --git a/doc/2_usage.md b/doc/2_usage.md index d0cb1a6..13ab651 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.60-dev3-1-gc944292 +#### $$VERSION$$ v0.60-dev3-7-gf5538ef diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 5f6d694..93bd97c 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.60-dev3-1-gc944292 +#### $$VERSION$$ v0.60-dev3-7-gf5538ef diff --git a/doc/4_expert.md b/doc/4_expert.md index 31d748b..a397f11 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -37,9 +37,11 @@ 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 Support -Telegram send JSON messages with all characters not fitting in one byte (<256 bit) escaped as sequences of ```\uxxxx``` to be regular one byte ASCII Multibyte UTF-8 characters, e.g. Emoticons and Arabic characters, are send in UTF-16 notation. e.g. the Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ``` +Telegram send JSON messages with all characters not fitting in one byte (<256 bit) escaped as sequences of ```\uxxxx``` to be regular +one byte ASCII. Multibyte UTF-8 characters, e.g. Emoticons and Arabic characters, are send in UTF-16 notation. +The Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ``` -**This mixed JSON encoding can't not decoded from ```echo -e``` or ```printf '%s\\n'```, this works only for single byte characters!** +**This mixed JSON encoding can not decoded from ```echo -e``` or ```printf '%s\\n'```**, this works only for single byte characters! To to fully support decoding of multibyte characters you need a working python2 instllation on your system. If no python is detected bashbot falls back to a **slow, pure bash solution which may not always work 100% correct**. @@ -99,5 +101,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.60-dev3-6-g5787908 +#### $$VERSION$$ v0.60-dev3-7-gf5538ef diff --git a/doc/5_practice.md b/doc/5_practice.md index 486e22f..c06e91c 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -111,5 +111,5 @@ In bashbot.sh line 490: ``` As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.60-dev3-1-gc944292 +#### $$VERSION$$ v0.60-dev3-7-gf5538ef From 7e75b92261ada6d67baa5bf3ac08f106ae8499db Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Apr 2019 20:30:59 +0200 Subject: [PATCH 13/36] new interactive command calc --- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 4 ++-- calc | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100755 calc diff --git a/bashbot.cron b/bashbot.cron index a608261..770c393 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.60-dev3-0-g2550aec +#### $$VERSION$$ v0.60-dev3-8-gdec3150 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index ebf016e..8625dba 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.60-dev3-0-g2550aec +#### $$VERSION$$ v0.60-dev3-8-gdec3150 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 4b2e142..a3e6dfe 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.60-dev3-6-g5787908 +#### $$VERSION$$ v0.60-dev3-8-gdec3150 # # Exit Codes: # - 0 sucess (hopefully) @@ -457,7 +457,7 @@ background() { startproc() { killproc "$2" - local fifo="$2${copname}" # add $1 to copname, so we can have more than one running script per chat + local fifo="$2${copname}" mkfifo "${TMPDIR:-.}/${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}" diff --git a/calc b/calc new file mode 100755 index 0000000..f5f9260 --- /dev/null +++ b/calc @@ -0,0 +1,33 @@ +#!/bin/bash + +# This file is public domain in the USA and all free countries. +# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) + +#### $$VERSION$$ v0.60-dev3-8-gdec3150 + +# adjust your language setting here +# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment +export 'LC_ALL=C.UTF-8' +export 'LANG=C.UTF-8' +export 'LANGUAGE=C.UTF-8' + +unset IFS +# set -f # if you are paranoid use set -f to disable globbing + +echo 'Starting Calculator ...' +echo 'Enter first number.' +read -r A +echo 'Enter second number.' +read -r B +echo 'Select Operation: mykeyboardstartshere "Addition" "Subtraction" "Multiplication" "Division" "Cancel"' +read -r opt +echo -n 'Result: ' +case $opt in + 'add'* | 'Add'* ) expr $A + $B ;; + 'sub'* | 'Sub'* ) expr $A - $B ;; + 'mul'* | 'Mul'* ) expr $A \* $B ;; + 'div'* | 'Div'* ) expr $A / $B ;; + 'can'* | 'Can'* ) echo "abort!" ;; + * ) echo "unknown operator!";; +esac +echo "Bye .." From c001d148f85f4c9414f943b6a776c062e58ec9e9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Apr 2019 23:04:17 +0200 Subject: [PATCH 14/36] adjust doc, fix unneeded JsonDecode use --- bashbot.sh | 4 ++-- doc/5_practice.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index a3e6dfe..42f13aa 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.60-dev3-8-gdec3150 +#### $$VERSION$$ v0.60-dev3-9-g7e75b92 # # Exit Codes: # - 0 sucess (hopefully) @@ -528,7 +528,7 @@ process_client() { REPLYTO[UID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","id"' <"$TMP" )" if [ "${REPLYTO[UID]}" != "" ]; then REPLYTO[0]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","text"' <"$TMP")")" - REPLYTO[ID]="$(JsonDecode "$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","reply_to_message","message_id"' <"$TMP")")" + REPLYTO[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","reply_to_message","message_id"' <"$TMP")" REPLYTO[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","first_name"' <"$TMP")")" REPLYTO[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","last_name"' <"$TMP")")" REPLYTO[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","username"' <"$TMP")")" diff --git a/doc/5_practice.md b/doc/5_practice.md index c06e91c..89bd3c4 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -111,5 +111,5 @@ In bashbot.sh line 490: ``` As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.60-dev3-7-gf5538ef +#### $$VERSION$$ v0.60-dev3-9-g7e75b92 From 87ec24bcebf7c25b13f1243418fa82eaf349d6a3 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Apr 2019 23:05:43 +0200 Subject: [PATCH 15/36] Version 0.60-rc1 --- README.md | 2 +- README.txt | 7 ++++--- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 2 +- calc | 2 +- commands.sh | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 7 ++++--- doc/5_practice.md | 2 +- notify | 2 +- question | 2 +- version | 2 +- 15 files changed, 21 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 268135e..f22a7c8 100644 --- a/README.md +++ b/README.md @@ -77,4 +77,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.60-dev3-1-gc944292 +#### $$VERSION$$ v0.6-rc1-0-gc001d14 diff --git a/README.txt b/README.txt index fba9ed9..244455f 100644 --- a/README.txt +++ b/README.txt @@ -6,7 +6,8 @@ A Telegram bot written in bash. Depends on http://github.com/tmux/tmux[tmux]. Uses http://github.com/dominictarr/JSON.sh[JSON.sh]. -For full UTF-8 support you need python on your system (optional). +For full UTF-8 support you need +link:doc/4_expert.md#UTF-8-Support[python on your system] (optional). Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadelwartz). @@ -121,5 +122,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.60-dev3-0-g2550aec -+++++++++++++++++++++++++++++++++++++++++++++ +latexmath:[\[VERSION\]] v0.6-rc1-0-gc001d14 ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index 770c393..d9c74cc 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.60-dev3-8-gdec3150 +#### $$VERSION$$ v0.6-rc1-0-gc001d14 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 8625dba..3c6804e 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.60-dev3-8-gdec3150 +#### $$VERSION$$ v0.6-rc1-0-gc001d14 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 42f13aa..00b14d2 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.60-dev3-9-g7e75b92 +#### $$VERSION$$ v0.6-rc1-0-gc001d14 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/calc b/calc index f5f9260..a79c376 100755 --- a/calc +++ b/calc @@ -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.60-dev3-8-gdec3150 +#### $$VERSION$$ v0.6-rc1-0-gc001d14 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/commands.sh b/commands.sh index ba8b78b..d698859 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.60-dev3-0-g2550aec +#### $$VERSION$$ v0.6-rc1-0-gc001d14 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 684cecb..6fb9479 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.60-dev3-7-gf5538ef +#### $$VERSION$$ v0.6-rc1-0-gc001d14 diff --git a/doc/2_usage.md b/doc/2_usage.md index 13ab651..d00e7af 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.60-dev3-7-gf5538ef +#### $$VERSION$$ v0.6-rc1-0-gc001d14 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 93bd97c..1ce1014 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.60-dev3-7-gf5538ef +#### $$VERSION$$ v0.6-rc1-0-gc001d14 diff --git a/doc/4_expert.md b/doc/4_expert.md index a397f11..ea263a5 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -41,9 +41,10 @@ Telegram send JSON messages with all characters not fitting in one byte (<256 bi one byte ASCII. Multibyte UTF-8 characters, e.g. Emoticons and Arabic characters, are send in UTF-16 notation. The Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ``` -**This mixed JSON encoding can not decoded from ```echo -e``` or ```printf '%s\\n'```**, this works only for single byte characters! +**This mixed JSON encoding can not decoded from** ```echo -e``` or ```printf '%s\\n'```, this works only for single byte characters! -To to fully support decoding of multibyte characters you need a working python2 instllation on your system. If no python is detected bashbot falls back to a **slow, pure bash solution which may not always work 100% correct**. +To to fully support decoding of multibyte characters you need a working python2 installation on your system. +If no python is detected bashbot falls back to a **slow, pure bash solution which may not work for some corner cases**. ### Run as other user or system service @@ -101,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.60-dev3-7-gf5538ef +#### $$VERSION$$ v0.6-rc1-0-gc001d14 diff --git a/doc/5_practice.md b/doc/5_practice.md index 89bd3c4..49b57f3 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -111,5 +111,5 @@ In bashbot.sh line 490: ``` As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.60-dev3-9-g7e75b92 +#### $$VERSION$$ v0.6-rc1-0-gc001d14 diff --git a/notify b/notify index 16472d8..bb1daff 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.60-dev3-0-g2550aec +#### $$VERSION$$ v0.6-rc1-0-gc001d14 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index a886f0c..036490e 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.60-dev3-0-g2550aec +#### $$VERSION$$ v0.6-rc1-0-gc001d14 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index 57fbae3..da01d03 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.60-dev3-0-g2550aec +#### $$VERSION$$ v0.6-rc1-0-gc001d14 # shellcheck disable=SC2016 # # Easy Versioning in git: From 10415846d951881942a12db5b2dd6987676e6d18 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Apr 2019 23:08:11 +0200 Subject: [PATCH 16/36] update doc --- doc/5_practice.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/doc/5_practice.md b/doc/5_practice.md index 49b57f3..232256b 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -11,7 +11,7 @@ Insert your own Bot commands in the ```case ... esac``` block in commands.sh: ;; ################################################ - # DEFAULT commands start here, edit messages only + # DEFAULT commands start here, do not edit below this! '/info') bashbot_info "${CHAT[ID]}" ;; @@ -19,22 +19,21 @@ Insert your own Bot commands in the ```case ... esac``` block in commands.sh: ``` after editing commands.sh restart Bot. -### Seperate Bot logic from command +### Seperate logic from command block -If a Bot command needs more than 2-3 lines of code I recommend to factor it out to a bash function in a seperate file, e.g. -```mybotcommands.inc.sh``` and source the file from bashbot.sh. ```bashbot_info and bashbot_help``` are examples how to use -bash functions to make customisation easy and keep case block small. ```process_message``` is an example for a complex -processing logic as a bash funtcion in a seperate file. +If your Bot command needs more than 2-3 lines of code I recommend to factor it out to a function to keep the command block small.i +Place the functions in a file, e.g. ```mybotcommands.inc.sh``` and source it in bashbot.sh. +```process_message``` is an example for a function hiding complex logic in a bash funtcion. ```bash source mybotcommands.inc.sh case "$MESSAGE" in - '/report') # report dealz from database and output result + '/report') # logic for /report is done in process_message send_normal_message "${CHAT[ID]}" "$(process_message "$MESSAGE")" ;; ################################################ - # DEFAULT commands start here, edit messages only + # DEFAULT commands start here, do not edit below this! '/info') bashbot_info "${CHAT[ID]}" ;; @@ -44,7 +43,7 @@ processing logic as a bash funtcion in a seperate file. ;; esac ``` -Example ```mybotcommands.inc.sh```: +Example function ```process_message``` in file ```mybotcommands.inc.sh```: ```bash #!/bin/bash # @@ -91,6 +90,7 @@ Line 17: ^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'. ``` +As you can see my ```mybotcommands.inc.sh``` contains an useless echo command in 'TEXT=' assigment and can be replaced by ```TEXT="${TEXT}${WORD}"``` ```bash $ shellcheck -x notify OK @@ -109,7 +109,8 @@ In bashbot.sh line 490: CONTACT[USER_ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","contact","user_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" ^-- SC2034: CONTACT appears unused. Verify it or export it. ``` -As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) +Here are two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is really possible and much faster! +The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.6-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-1-g87ec24b From 6be706b571f4990e6995e4ccd826e462dcbcaeb6 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 10:55:22 +0200 Subject: [PATCH 17/36] fix case problem with bash JsonDecode --- bashbot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 00b14d2..050d328 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-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-2-g1041584 # # Exit Codes: # - 0 sucess (hopefully) @@ -592,7 +592,7 @@ else JsonDecode() { local out="$1" local remain="" - local regexp='(.*)\\ud([0-9a-fA-F]{3})\\ud([0-9a-fA-F]{3})(.*)' + local regexp='(.*)\\u[dD]([0-9a-fA-F]{3})\\u[dD]([0-9a-fA-F]{3})(.*)' while [[ "${out}" =~ $regexp ]] ; do local W1="$(( ( 0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ))" local W2="$(( 0xd${BASH_REMATCH[3]} & 0x3ff ))" From 6e32cd684749b4742057a27bb9ce894462dc706e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 11:48:50 +0200 Subject: [PATCH 18/36] start function reference --- doc/5_practice.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/5_practice.md b/doc/5_practice.md index 232256b..c388593 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -21,8 +21,8 @@ after editing commands.sh restart Bot. ### Seperate logic from command block -If your Bot command needs more than 2-3 lines of code I recommend to factor it out to a function to keep the command block small.i -Place the functions in a file, e.g. ```mybotcommands.inc.sh``` and source it in bashbot.sh. +If your Bot command needs more than 2-3 lines of code I recommend to factor it out to a function to keep the command block small. +Place the functions in a file, e.g. ```mybotcommands.inc.sh``` and source it from bashbot.sh. ```process_message``` is an example for a function hiding complex logic in a bash funtcion. ```bash source mybotcommands.inc.sh @@ -112,5 +112,5 @@ In bashbot.sh line 490: Here are two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is really possible and much faster! The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.6-rc1-1-g87ec24b +#### $$VERSION$$ v0.6-rc1-3-g6be706b From 2ac5e6fdbdfd157b89d249421ab9ccb00ed7d46d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 11:49:13 +0200 Subject: [PATCH 19/36] start function reference --- doc/6_reference.md | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 doc/6_reference.md diff --git a/doc/6_reference.md b/doc/6_reference.md new file mode 100644 index 0000000..e5c7b3f --- /dev/null +++ b/doc/6_reference.md @@ -0,0 +1,106 @@ +## Bashbot functions reference + +### Send, forward, delete Messages + +send_action() + +send_message() + +send_normal_message() + +send_markdown_message() + +send_html_message() + +---- + +forward() + +---- + +delete_message() + +---- + +answer_inline_query() + +---- + +### File, Location, Venu, keyboards + +get_file() + +send_file() + +send_location() + +send_venue() + +---- + +send_keyboard() + +remove_keyboard() + +### Manage users + +kick_chat_member() + +unban_chat_member() + +leave_chat() + +---- + +user_is_creator() + +user_is_admin() + +user_is_botadmin() + +user_is_allowed() + +### Interactive and backgound jobs + +startproc() + +checkproc() + +killproc() + +---- + +background() + +checkback() + +killback() + +### Bashbot internal + +send_text() + +---- + +JsonDecode() + +JsonGetString() + +JsonGetValue() + +---- + +get_chat_member_status() + +---- + +process_client() + +process_updates() + +---- + +inproc() + +#### $$VERSION$$ v0.6-rc1-3-g6be706b + From e18b200fe6c62c80354108345b0f2f9a97716bbe Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 12:19:04 +0200 Subject: [PATCH 20/36] check if we can connect to telegram bot --- bashbot.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 050d328..00b2cc8 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-rc1-2-g1041584 +#### $$VERSION$$ v0.6-rc1-5-g2ac5e6f # # Exit Codes: # - 0 sucess (hopefully) @@ -18,6 +18,7 @@ # - 2 can't write to tmp, count or token # - 3 user / command not found # - 4 unkown command +# - 5 cannot connect to telegram bot # are we runnig in a terminal? if [ -t 1 ] && [ "$TERM" != "" ]; then @@ -580,6 +581,17 @@ process_client() { grep -q "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT} # To get user count execute bash bashbot.sh count } +# get bot name +getBotName() { + res="$(curl -s "$ME_URL")" + echo "$res" | ./JSON.sh/JSON.sh -s | JsonGetString '"result","username"' +} + +ME="$(getBotName)" +if [ "$ME" = "" ]; then + echo -e "${RED}ERROR: Can't connect to Telegram Bot! May be your TOKEN is invalid ...${NC}" + exit 1 +fi # use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback if which python >/dev/null 2>&1 || which phyton2 >/dev/null 2>&1; then @@ -594,6 +606,7 @@ else local remain="" local regexp='(.*)\\u[dD]([0-9a-fA-F]{3})\\u[dD]([0-9a-fA-F]{3})(.*)' while [[ "${out}" =~ $regexp ]] ; do + # match 2 \udxxx hex values, calculate new U, then split and replace local W1="$(( ( 0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ))" local W2="$(( 0xd${BASH_REMATCH[3]} & 0x3ff ))" U="$(( ( W1 | W2 ) + 0x10000 ))" @@ -604,9 +617,6 @@ else } fi -# get bot name -ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | JsonGetString '"result","username"')" - # source the script with source as param to use functions in other scripts while [ "$1" = "startbot" ]; do { @@ -731,7 +741,7 @@ case "$1" in exit ;; *) - echo -e "${RED}BAD REQUEST${NC}" + echo -e "${RED}${ME}: BAD REQUEST${NC}" echo -e "${RED}Available arguments: outproc, count, broadcast, start, suspendback, resumeback, kill, killback, help, attach${NC}" exit 4 ;; From 14eb3520d5bb93293116a9c52e8f805b880adf1b Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 12:52:38 +0200 Subject: [PATCH 21/36] minor doc updates --- doc/1_firstbot.md | 2 +- doc/2_usage.md | 57 ++++++++------ doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 185 ++++++++++++++++++++++++++++++++++++--------- 6 files changed, 185 insertions(+), 65 deletions(-) diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 6fb9479..15247e8 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.6-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-6-ge18b200 diff --git a/doc/2_usage.md b/doc/2_usage.md index d00e7af..b0926d7 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -83,8 +83,37 @@ Evertime a Message is recieved, you can read incoming data using the following v ## Usage of bashbot functions +#### sending messages +To send messages use the ```send_xxx_message``` functions. + +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" +``` + +To forward messages use the ```forward``` function: +```bash +forward "${CHAT[ID]}" "from_chat_id" "message_id" +``` + +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_message -To send messages use the ```send_message``` function: +In addition there is a universal send_massage function which can output any type of message. +This function is used to process output from external scrips like interactive chats or background jobs. +**For safety and performance reasons I recommend to use send_xxxx_message functions above for sending messages** ```bash send_message "${CHAT[ID]}" "lol" ``` @@ -99,30 +128,8 @@ This function also allows a third parameter that disables additional function pa ```bash send_message "${CHAT[ID]}" "lol" "safe" ``` -To forward messages use the ```forward``` function: -```bash -forward "${CHAT[ID]}" "from_chat_id" "message_id" -``` +More examples boutsend_message strings can be found in [Advanced Usage](3_advanced.md#Interactive-Chats) -#### 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): @@ -147,6 +154,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.6-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-6-ge18b200 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 1ce1014..319e4a0 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.6-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-6-ge18b200 diff --git a/doc/4_expert.md b/doc/4_expert.md index ea263a5..b2157fc 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.6-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-6-ge18b200 diff --git a/doc/5_practice.md b/doc/5_practice.md index c388593..d98323f 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ In bashbot.sh line 490: Here are two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is really possible and much faster! The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.6-rc1-3-g6be706b +#### $$VERSION$$ v0.6-rc1-6-ge18b200 diff --git a/doc/6_reference.md b/doc/6_reference.md index e5c7b3f..7e9a60e 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -2,105 +2,218 @@ ### Send, forward, delete Messages -send_action() +##### send_action +*usage:* -send_message() +*example:* -send_normal_message() +##### send_normal_message +*usage:* -send_markdown_message() +*example:* -send_html_message() +##### send_markdown_message +*usage:* + +*example:* + +##### send_html_message +*usage:* + +*example:* + +##### forward +*usage:* + +*example:* ---- -forward() +##### send_message +Send Message is only used to process the output of interactive chats an background jobs. +I reccommend to use the more dedicated send_xxx_message() functions above. + +*usage:* + +*example:* see [Usage](2_usage.md#send_message) and [Advanced Usage](3_advanced.md#Interactive-Chats) ---- -delete_message() +##### delete_message +*usage:* + +*example:* ---- -answer_inline_query() +##### answer_inline_query +*usage:* + +*example:* ---- ### File, Location, Venu, keyboards -get_file() +##### get_file +*usage:* -send_file() +*example:* -send_location() +##### send_file +*usage:* -send_venue() +*example:* + +##### send_location +*usage:* + +*example:* + +##### send_venue +*usage:* + +*example:* ---- -send_keyboard() +##### send_keyboard +*usage:* -remove_keyboard() +*example:* + +##### remove_keyboard +*usage:* + +*example:* ### Manage users -kick_chat_member() +##### kick_chat_member +*usage:* -unban_chat_member() +*example:* -leave_chat() +##### unban_chat_member +*usage:* + +*example:* + +##### leave_chat +*usage:* + +*example:* ---- -user_is_creator() +##### user_is_creator +*usage:* -user_is_admin() +*example:* -user_is_botadmin() +##### user_is_admin +*usage:* -user_is_allowed() +*example:* + +##### user_is_botadmin +*usage:* + +*example:* + +##### user_is_allowed +*usage:* + +*example:* ### Interactive and backgound jobs -startproc() +##### startproc +*usage:* -checkproc() +*example:* -killproc() +##### checkproc +*usage:* + +*example:* + +##### killproc +*usage:* + +*example:* ---- -background() +##### background +*usage:* -checkback() +*example:* -killback() +##### checkback +*usage:* + +*example:* + +##### killback +*usage:* + +*example:* ### Bashbot internal +These function are for internal use only and must not used for your bot commands. -send_text() +##### send_text +*usage:* + +*example:* ---- -JsonDecode() +##### JsonDecode +*usage:* -JsonGetString() +*example:* -JsonGetValue() +##### JsonGetString +*usage:* + +*example:* + +##### JsonGetValue +*usage:* + +*example:* ---- -get_chat_member_status() +##### get_chat_member_status +*usage:* + +*example:* ---- -process_client() +##### process_client +*usage:* -process_updates() +*example:* + +##### process_updates +*usage:* + +*example:* ---- +##### getBotName +*usage:* -inproc() +*example:* -#### $$VERSION$$ v0.6-rc1-3-g6be706b +##### inproc +*usage:* + +*example:* + +#### $$VERSION$$ v0.6-rc1-6-ge18b200 From bf976463610feb682d3c4fa39d716b6553b16b37 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 14:17:18 +0200 Subject: [PATCH 22/36] dev3-rc1 candidate --- README.md | 2 +- README.txt | 2 +- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 2 +- calc | 3 ++- commands.sh | 22 +++++++++++----------- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 36 ++++++++++-------------------------- notify | 2 +- question | 2 +- version | 2 +- 16 files changed, 36 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index f22a7c8..cfb11eb 100644 --- a/README.md +++ b/README.md @@ -77,4 +77,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-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 diff --git a/README.txt b/README.txt index 244455f..6e04c34 100644 --- a/README.txt +++ b/README.txt @@ -122,5 +122,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-rc1-0-gc001d14 +latexmath:[\[VERSION\]] v0.6-rc1-7-g14eb352 +++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index d9c74cc..fc0d5c2 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-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 3c6804e..d5e46cd 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.6-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 00b2cc8..32151d5 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-rc1-5-g2ac5e6f +#### $$VERSION$$ v0.6-rc1-7-g14eb352 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/calc b/calc index a79c376..fcc86f6 100755 --- a/calc +++ b/calc @@ -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-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -20,6 +20,7 @@ read -r A echo 'Enter second number.' read -r B echo 'Select Operation: mykeyboardstartshere "Addition" "Subtraction" "Multiplication" "Division" "Cancel"' +echo 'Your anwser can be: add, sub, mul, div or cancel' read -r opt echo -n 'Result: ' case $opt in diff --git a/commands.sh b/commands.sh index d698859..924bea2 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-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 @@ -71,7 +71,7 @@ else [ ! -z "${LOCATION[*]}" ] && send_location "${CHAT[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}" # Inline - if [ $INLINE == 1 ]; then + if [ "$INLINE" = 1 ]; then # inline query data iUSER[FIRST_NAME]="$(echo "$res" | sed 's/^.*\(first_name.*\)/\1/g' | cut -d '"' -f3 | tail -1)" iUSER[LAST_NAME]="$(echo "$res" | sed 's/^.*\(last_name.*\)/\1/g' | cut -d '"' -f3)" @@ -80,18 +80,18 @@ else iQUERY_MSG="$(echo "$res" | sed 's/^.*\(inline_query.*\)/\1/g' | cut -d '"' -f5 | tail -6 | head -1)" # Inline examples - if [[ "$iQUERY_MSG" == "photo" ]]; then + if [[ "$iQUERY_MSG" = "photo" ]]; then answer_inline_query "$iQUERY_ID" "photo" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg" fi - if [[ "$iQUERY_MSG" == "sticker" ]]; then + if [[ "$iQUERY_MSG" = "sticker" ]]; then answer_inline_query "$iQUERY_ID" "cached_sticker" "BQADBAAD_QEAAiSFLwABWSYyiuj-g4AC" fi - if [[ "$iQUERY_MSG" == "gif" ]]; then + if [[ "$iQUERY_MSG" = "gif" ]]; then answer_inline_query "$iQUERY_ID" "cached_gif" "BQADBAADIwYAAmwsDAABlIia56QGP0YC" fi - if [[ "$iQUERY_MSG" == "web" ]]; then + if [[ "$iQUERY_MSG" = "web" ]]; then answer_inline_query "$iQUERY_ID" "article" "GitHub" "http://github.com/topkecleon/telegram-bot-bash" fi fi & @@ -131,8 +131,8 @@ else ;; '/start') send_action "${CHAT[ID]}" "typing" - user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*." - if user_is_allowed "${USER[ID]}" "start" "${CHAT[ID]}" ; then + _is_botadmin && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*." + if _is_allowed "start" ; then bot_help "${CHAT[ID]}" else send_normal_message "${CHAT[ID]}" "You are not allowed to start Bot." @@ -140,7 +140,7 @@ else ;; '/leavechat') # bot leave chat if user is admin in chat - if user_is_admin "${CHAT[ID]}" "${USER[ID]}"; then + if _is_admin ; then send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*" leave_chat "${CHAT[ID]}" fi @@ -155,8 +155,8 @@ else 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 tmux ls | grep -v send | grep -q "$copname";then inproc; else send_message "${CHAT[ID]}" "$MESSAGE" "safe";fi + *) # forward input to interactive chat if running + if tmux ls | grep -v send | grep -q $copname;then inproc; fi ;; esac fi diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 15247e8..05b1cca 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.6-rc1-6-ge18b200 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 diff --git a/doc/2_usage.md b/doc/2_usage.md index b0926d7..8f4f1da 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -154,6 +154,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.6-rc1-6-ge18b200 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 319e4a0..c4b1f56 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.6-rc1-6-ge18b200 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 diff --git a/doc/4_expert.md b/doc/4_expert.md index b2157fc..5ca2782 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.6-rc1-6-ge18b200 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 diff --git a/doc/5_practice.md b/doc/5_practice.md index d98323f..0ede099 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ In bashbot.sh line 490: Here are two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is really possible and much faster! The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.6-rc1-6-ge18b200 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 diff --git a/doc/6_reference.md b/doc/6_reference.md index 7e9a60e..c68fe6f 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -30,12 +30,12 @@ ---- ##### send_message -Send Message is only used to process the output of interactive chats an background jobs. -I reccommend to use the more dedicated send_xxx_message() functions above. +Send Message must (only) used to process the output of interactive chats and background jobs. +**For your commands I reccommend the more dedicated send_xxx_message() functions above.** *usage:* -*example:* see [Usage](2_usage.md#send_message) and [Advanced Usage](3_advanced.md#Interactive-Chats) +*example:* - see [Usage](2_usage.md#send_message) and [Advanced Usage](3_advanced.md#Interactive-Chats) ---- @@ -161,59 +161,43 @@ I reccommend to use the more dedicated send_xxx_message() functions above. *example:* ### Bashbot internal -These function are for internal use only and must not used for your bot commands. +These functions are for internal use only and must not used in your bot commands. ##### send_text *usage:* -*example:* - ---- ##### JsonDecode *usage:* -*example:* - ##### JsonGetString *usage:* -*example:* - ##### JsonGetValue *usage:* -*example:* - ---- ##### get_chat_member_status *usage:* -*example:* - ---- ##### process_client -*usage:* - -*example:* +Every Message sent to your Bot is processd by this function. It parse the send JSON and assign the found Values to bash variables. ##### process_updates -*usage:* - -*example:* +If new updates are availible, this functions gets the JSON from Telegram and dispatch it. ---- ##### getBotName -*usage:* +The name of your bot is availible as bash variable "$ME", there is no need to call this function if Bot is running. -*example:* +*usage:* ME="$(getBotNiname)" ##### inproc -*usage:* +Send Input from Telegram to waiting Interactive Chat. -*example:* - -#### $$VERSION$$ v0.6-rc1-6-ge18b200 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 diff --git a/notify b/notify index bb1daff..0a38bdd 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-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 036490e..4050fd5 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-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index da01d03..367cf74 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.6-rc1-0-gc001d14 +#### $$VERSION$$ v0.6-rc1-7-g14eb352 # shellcheck disable=SC2016 # # Easy Versioning in git: From 3824137433fb688554cbfeaeedee9bfdbc06e389 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 16:32:56 +0200 Subject: [PATCH 23/36] fix / allow more complex keyboards --- README.md | 14 ++++++++++++++ bashbot.sh | 12 +++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cfb11eb..58a6fbb 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,20 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash - Seperate Bot logic from command - Test your Bot with shellcheck +## Note to Keyboards +To make using Keayboards easier I changed the format of send_keyboard as of send_message "mykeyboardstartshere ...". +Now you must provide the keyboards in Telegram JSON style "[ \"yes\" , \"no\" ]". +The advantage is that you can create every type of keyboard iwithout relying on bashbot ```send_keyboard``` functionality. +**This is incompatible with keyboards in bashbot versions older than 0.6!** + +*Example Keboards*: + +- Yes No in one row: "[ \"yes\" , \"no\" ]" +- Yes No ind two rows: "[ \"yes\" ] , [ \"no\" ]" +- numpad keyboard: "[ \"1\" , \"2\" , \"3\" ] , [ \"4\" , \"5\" , \"6\" ] , [ \"7\" , \"8\" , \"9\" ] , [ \"0\" ]" + + + ## Security Considerations Running a Telegram Bot means you are conneted to the public, you never know whats send to your Bot. diff --git a/bashbot.sh b/bashbot.sh index 32151d5..37ced4f 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -357,14 +357,8 @@ answer_inline_query() { send_keyboard() { local chat="$1" local text="$2" - shift 2 - local keyboard=init - OLDIFS=$IFS - IFS=$(echo -en "\"") - for f in "$@" ;do [ "$f" != " " ] && keyboard="$keyboard, [\"$f\"]";done - IFS=$OLDIFS - 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}")" + local keyboard="$3" + 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}")" } remove_keyboard() { @@ -470,7 +464,7 @@ checkback() { } checkproc() { - tmux ls | grep -q "$1${copname}"; res=$? + tmux ls | grep -q "$1${copname}"; res=$?; return $? } killback() { From 6ff574b9a32fcfb0afe0eb29ddc10ce2427d0940 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 16:44:26 +0200 Subject: [PATCH 24/36] update calc example with new keyboard style --- calc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/calc b/calc index fcc86f6..dc626f5 100755 --- a/calc +++ b/calc @@ -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-rc1-7-g14eb352 +#### $$VERSION$$ v0.60-dev3-7-gf5538ef # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -19,8 +19,7 @@ echo 'Enter first number.' read -r A echo 'Enter second number.' read -r B -echo 'Select Operation: mykeyboardstartshere "Addition" "Subtraction" "Multiplication" "Division" "Cancel"' -echo 'Your anwser can be: add, sub, mul, div or cancel' +echo 'Select Operation: mykeyboardstartshere [ "Addition" , "Subtraction" , "Multiplication" , "Division" , "Cancel" ]' read -r opt echo -n 'Result: ' case $opt in From ba80ec69766f87090216a041f43f2cb84d4c88d8 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 16:51:26 +0200 Subject: [PATCH 25/36] fix showing text 2x times when upload file --- bashbot.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashbot.sh b/bashbot.sh index 37ced4f..8ed4759 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -180,7 +180,7 @@ send_message() { sent=y fi if [ "$file" != "" ]; then - send_file "$chat" "$file" "$text" + send_file "$chat" "$file" "" sent=y fi if [ "$lat" != "" ] && [ "$long" != "" ] && [ "$address" = "" ] && [ "$title" = "" ]; then From 0b27870479d9c52309d4f03ffb2d2a67d0fe1931 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 17:44:04 +0200 Subject: [PATCH 26/36] some minor doc update --- README.md | 13 +++++++------ doc/1_firstbot.md | 2 +- doc/2_usage.md | 4 ++-- doc/3_advanced.md | 6 +++--- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 2 +- 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 58a6fbb..9f01fa8 100644 --- a/README.md +++ b/README.md @@ -50,14 +50,15 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash - Customizing commands.sh - Seperate Bot logic from command - Test your Bot with shellcheck + - [Bashbot functions reference](doc/6_reference.md) -## Note to Keyboards -To make using Keayboards easier I changed the format of send_keyboard as of send_message "mykeyboardstartshere ...". -Now you must provide the keyboards in Telegram JSON style "[ \"yes\" , \"no\" ]". -The advantage is that you can create every type of keyboard iwithout relying on bashbot ```send_keyboard``` functionality. +## Note on Keyboards +To make use of Keyboards easyer we changed the format of ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."```. +Now you must provide the keyboards in Telegram JSON Array style "[ \"yes\" , \"no\" ]". +The advantage is that you can create every type of keyboard without relying on bashbot ```send_keyboard``` functionality. **This is incompatible with keyboards in bashbot versions older than 0.6!** -*Example Keboards*: +*Example Keyboards*: - Yes No in one row: "[ \"yes\" , \"no\" ]" - Yes No ind two rows: "[ \"yes\" ] , [ \"no\" ]" @@ -91,4 +92,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-rc1-7-g14eb352 +#### $$VERSION$$ v0.6-rc1-11-gba80ec6 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 05b1cca..5ebb490 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.6-rc1-7-g14eb352 +#### $$VERSION$$ v0.6-rc1-11-gba80ec6 diff --git a/doc/2_usage.md b/doc/2_usage.md index 8f4f1da..3415480 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -138,7 +138,7 @@ 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" +send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" "[ \"Yep\" , \"No\" ]" ``` To send locations use the ```send_location``` function: ```bash @@ -154,6 +154,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.6-rc1-7-g14eb352 +#### $$VERSION$$ v0.6-rc1-11-gba80ec6 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index c4b1f56..59be5fe 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -61,7 +61,7 @@ To create interactive chats, write (or edit the question script) a normal bash ( 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\"" +echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No, highly unlikely\" ]" ``` Same goes for files: ```bash @@ -77,7 +77,7 @@ echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45 myti ``` You can combine them: ```bash -echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\" myfilelocationstartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45" +echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No, highly unlikely\" ] 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. @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.6-rc1-7-g14eb352 +#### $$VERSION$$ v0.6-rc1-11-gba80ec6 diff --git a/doc/4_expert.md b/doc/4_expert.md index 5ca2782..b9f32e2 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.6-rc1-7-g14eb352 +#### $$VERSION$$ v0.6-rc1-11-gba80ec6 diff --git a/doc/5_practice.md b/doc/5_practice.md index 0ede099..6bdc2f9 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ In bashbot.sh line 490: Here are two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is really possible and much faster! The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.6-rc1-7-g14eb352 +#### $$VERSION$$ v0.6-rc1-11-gba80ec6 diff --git a/doc/6_reference.md b/doc/6_reference.md index c68fe6f..8e122ef 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -199,5 +199,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca ##### inproc Send Input from Telegram to waiting Interactive Chat. -#### $$VERSION$$ v0.6-rc1-7-g14eb352 +#### $$VERSION$$ v0.6-rc1-11-gba80ec6 From c581932d078852294f88bd652257bf420f8d15e1 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 19:13:39 +0200 Subject: [PATCH 27/36] update function reference --- doc/6_reference.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 8e122ef..5478828 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -3,9 +3,16 @@ ### Send, forward, delete Messages ##### send_action -*usage:* +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. + +*usage:* send_action "${CHAT[ID]}" "action" *example:* +```bash +send_action "${CHAT[ID]}" "typing" +send_action "${CHAT[ID]}" "record_audio" +``` + ##### send_normal_message *usage:* @@ -78,9 +85,16 @@ Send Message must (only) used to process the output of interactive chats and bac ---- ##### send_keyboard -*usage:* +Note: since version 0.6 send_keyboard was changed to use native "JSON Array" as used from Telegram. + +*usage:* send_keyboard "chat-id" "keyboard" *example:* +```bash +send_keyboard "${CHAT[ID]}" "[ \"yes\" , \"no\" ]" +send_keyboard "${CHAT[ID]}" "[ \"yes\" ] , [ \"no\" ]" +send_keyboard "${CHAT[ID]}" "[ \"1\" , \"2\" , \"3\" ] , [ \"4\" , \"5\" , \"6\" ] , [ \"7\" , \"8\" , \"9\" ] , [ \"0\" ]" +``` ##### remove_keyboard *usage:* From e29924706d566c33f5b8a19c8fc1862d27894f33 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 21:06:29 +0200 Subject: [PATCH 28/36] provide bashbot function reference --- README.md | 2 +- README.txt | 22 +++++- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 2 +- calc | 2 +- commands.sh | 4 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 192 +++++++++++++++++++++++++++++++++++---------- notify | 2 +- question | 2 +- version | 2 +- 16 files changed, 187 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 9f01fa8..18b4cb4 100644 --- a/README.md +++ b/README.md @@ -92,4 +92,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-rc1-11-gba80ec6 +#### $$VERSION$$ v0.60-rc2-0-gc581932 diff --git a/README.txt b/README.txt index 6e04c34..9f84eef 100644 --- a/README.txt +++ b/README.txt @@ -66,6 +66,24 @@ Getting started * Customizing commands.sh * Seperate Bot logic from command * Test your Bot with shellcheck +* link:doc/6_reference.md[Bashbot functions reference] + +Note on Keyboards +~~~~~~~~~~~~~~~~~ + +To make use of Keyboards easyer we changed the format of `send_keyboard` +and `send_message "mykeyboardstartshere ..."`. Now you must provide the +keyboards in Telegram JSON Array style "[ "yes" , "no" ]". The advantage +is that you can create every type of keyboard without relying on bashbot +`send_keyboard` functionality. *This is incompatible with keyboards in +bashbot versions older than 0.6!* + +_Example Keyboards_: + +* Yes No in one row: "[ "yes" , "no" ]" +* Yes No ind two rows: "[ "yes" ] , [ "no" ]" +* numpad keyboard: "[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , +"8" , "9" ] , [ "0" ]" Security Considerations ~~~~~~~~~~~~~~~~~~~~~~~ @@ -122,5 +140,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-rc1-7-g14eb352 -+++++++++++++++++++++++++++++++++++++++++++ +latexmath:[\[VERSION\]] v0.60-rc2-0-gc581932 +++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index fc0d5c2..e6057f7 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-rc1-7-g14eb352 +#### $$VERSION$$ v0.60-rc2-0-gc581932 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index d5e46cd..ba5d15f 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.6-rc1-7-g14eb352 +#### $$VERSION$$ v0.60-rc2-0-gc581932 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 8ed4759..8a20945 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-rc1-7-g14eb352 +#### $$VERSION$$ v0.60-rc2-0-gc581932 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/calc b/calc index dc626f5..0cada5f 100755 --- a/calc +++ b/calc @@ -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.60-dev3-7-gf5538ef +#### $$VERSION$$ v0.60-rc2-0-gc581932 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/commands.sh b/commands.sh index 924bea2..f934796 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-rc1-7-g14eb352 +#### $$VERSION$$ v0.60-rc2-0-gc581932 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 @@ -156,7 +156,7 @@ else if [ "$res" -eq 0 ] ; then killproc && send_message "${CHAT[ID]}" "Command canceled.";else send_message "${CHAT[ID]}" "No command is currently running.";fi ;; *) # forward input to interactive chat if running - if tmux ls | grep -v send | grep -q $copname;then inproc; fi + if tmux ls | grep -v send | grep -q "$copname"; then inproc; fi ;; esac fi diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 5ebb490..de5cd35 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.6-rc1-11-gba80ec6 +#### $$VERSION$$ v0.60-rc2-0-gc581932 diff --git a/doc/2_usage.md b/doc/2_usage.md index 3415480..607a499 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -154,6 +154,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.6-rc1-11-gba80ec6 +#### $$VERSION$$ v0.60-rc2-0-gc581932 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 59be5fe..7750a94 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.6-rc1-11-gba80ec6 +#### $$VERSION$$ v0.60-rc2-0-gc581932 diff --git a/doc/4_expert.md b/doc/4_expert.md index b9f32e2..f4cbac3 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.6-rc1-11-gba80ec6 +#### $$VERSION$$ v0.60-rc2-0-gc581932 diff --git a/doc/5_practice.md b/doc/5_practice.md index 6bdc2f9..e3f5ad4 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ In bashbot.sh line 490: Here are two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is really possible and much faster! The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.6-rc1-11-gba80ec6 +#### $$VERSION$$ v0.60-rc2-0-gc581932 diff --git a/doc/6_reference.md b/doc/6_reference.md index 5478828..11cf58c 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -1,4 +1,4 @@ -## Bashbot functions reference +## Bashbot function reference ### Send, forward, delete Messages @@ -15,24 +15,41 @@ send_action "${CHAT[ID]}" "record_audio" ##### send_normal_message -*usage:* +sen_normal_message send text only messages to chat. + +*usage:* send_normal_message "${CHAT[ID]}" "message" *example:* +```bash +send_normal_message "${CHAT[ID]}" "this is a text message" +``` + ##### send_markdown_message -*usage:* +Telegram supports onyl a [reduced set of Markdown](https://core.telegram.org/bots/api#markdown-style) + +*usage:* send_markdown_message "${CHAT[ID]}" "message" *example:* +```bash +send_normal_message "${CHAT[ID]}" "this is a markdown message, next word is *bold*" +send_normal_message "${CHAT[ID]}" "*bold* _italic_ [text](link)" +``` ##### send_html_message -*usage:* +Telegram supports onyl a [reduced set of HTML](https://core.telegram.org/bots/api#html-style) + +*usage:* send_html_message "${CHAT[ID]}" "message" *example:* +```bash +send_normal_message "${CHAT[ID]}" "this is a markdown message, next word is bold" +send_normal_message "${CHAT[ID]}" "bold italic> italic>/em> Text" +``` ##### forward -*usage:* +*usage:* forward "${CHAT[ID]}" "${MESSAGE[ID]}" -*example:* ---- @@ -40,23 +57,26 @@ send_action "${CHAT[ID]}" "record_audio" Send Message must (only) used to process the output of interactive chats and background jobs. **For your commands I reccommend the more dedicated send_xxx_message() functions above.** -*usage:* +*usage:* send_message "${CHAT[ID]}" "message" *example:* - see [Usage](2_usage.md#send_message) and [Advanced Usage](3_advanced.md#Interactive-Chats) ---- ##### delete_message -*usage:* +If your Bot is Admin in a Chat you can delete every message, if not you can delete only your Bot messages. -*example:* +*usage:* delete_message "${CHAT[ID]}" "${MESSAGE[ID]}" ---- ##### answer_inline_query -*usage:* +Inline Queries allows users to interact with your bot via directly without sending extra commands. +answer_inline_query provide the result to a users Inline Query -*example:* +*usage:* answer_inline_query "$iQUERY_ID" "type" "type arg 1" ... "type arg n" + +*example:* - see [Advanced Usage](3_advanced.md#Inline-queries) ---- @@ -66,21 +86,27 @@ Send Message must (only) used to process the output of interactive chats and bac *usage:* *example:* +```bash +``` ##### send_file -*usage:* +There are some ways to send files (photos, stickers, audio, media, etc.), [see more](https://core.telegram.org/bots/api#sending-files) + +*usage:* send_file "${CHAT[ID]}" "file" "caption" *example:* +```bash +send_file "${CHAT[ID]}" "/home/user/doge.jpg" "Lool" +send_file "${CHAT[ID]}" "https://www.domain,com/something.gif" "Something" +``` ##### send_location -*usage:* +*usage:* send_location "${CHAT[ID]}" "Latitude" "Longitude" -*example:* ##### send_venue -*usage:* +*usage:* send_venue "${CHAT[ID]}" "Latitude" "Longitude" "Title" "Address" "foursquare id (optional)" -*example:* ---- @@ -99,102 +125,188 @@ send_keyboard "${CHAT[ID]}" "[ \"1\" , \"2\" , \"3\" ] , [ \"4\" , \"5\" , \"6\" ##### remove_keyboard *usage:* -*example:* ### Manage users ##### kick_chat_member -*usage:* +If Bot is Admin you can kick and ban a user. + +*usage:* kick_chat_member "${CHAT[ID]}" "${USER[ID]}" -*example:* ##### unban_chat_member -*usage:* +If Bot is Admin you can unban a kicked user. -*example:* +*usage:* unban_chat_member "${CHAT[ID]}" "${USER[ID]}" ##### leave_chat -*usage:* +Bot will leave chat. -*example:* +*usage:* leave_chat "${CHAT[ID]}" + +```bash +if _is_admin ; then + send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*" + leave_chat "${CHAT[ID]}" +fi +``` ---- ##### user_is_creator -*usage:* +Returns true (0) if user is creator of chat or chat is a one2one / private chat. -*example:* +*usage:* user_is_creator "${CHAT[ID]}" "${USER[ID]}" + +*alias:* _is_creator ##### user_is_admin -*usage:* +Returns true (0) if user is admin or creator of chat. + +*usage:* user_is_admin "${CHAT[ID]}" "${USER[ID]}" + +*alias:* _is_creator *example:* +```bash +if _is_admin ; then + send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*" + leave_chat "${CHAT[ID]}" +fi +``` ##### user_is_botadmin -*usage:* +Returns true (0) if user is owner / admin of bot. +botadmin is stored in file './botadmin' + +*usage:* user_is_botadmin "${CHAT[ID]}" "${USER[ID]}" + +*alias:* _is_botadmin *example:* +```bash + _is_botadmin && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*." +``` ##### user_is_allowed -*usage:* +Bahsbot supports User Access Control, see [Advanced Usage](4_advanced.ma) + +*usage:* user_is_allowed "${USER[ID]}" "what" "${CHAT[ID]}" *example:* +```bash +if ! user_is_allowed "${USER[ID]}" "start" "${CHAT[ID]}" ; then + send_normal_message "${CHAT[ID]}" "You are not allowed to start Bot." +fi +``` ### Interactive and backgound jobs ##### startproc -*usage:* +With ```startproc``` you can start scrips (or C or python program etc.). 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. see [Advanced Usage](3_advanced.md#Interactive-Chats) + +*usage:* startproc "./script" *example:* +```bash +startproc './calc' +``` ##### checkproc -*usage:* +Returns true (0) if an interactive script is running in chat. + +*usage:* checkprog *example:* +```bash +checkproc +if [ "$res" -gt 0 ] ; then + startproc "./calc" +else + send_normal_message "${CHAT[ID]}" "Calc already running ..." +fi +``` ##### killproc -*usage:* +*usage:* killproc *example:* +```bash +checkprog +if [ "$res" -eq 0 ]; then + killproc && send_message "${CHAT[ID]}" "Command canceled." +else + send_message "${CHAT[ID]}" "Command is not running." +fi +``` ---- ##### background -*usage:* +*usage:* background "./script" "jobname" *example:* +```bash +background "./notify" "notify" +``` ##### checkback -*usage:* +Returns true (0) if an background job is running in chat. + +*usage:* checkback "jobname" *example:* +```bash +checkback "notify" +if [ "$res" -gt 0 ] ; then + send_normal_message "${CHAT[ID]}" "Start notify" + background "./notify" "notify" +else + send_normal_message "${CHAT[ID]}" "Process notify already running." +fi +``` ##### killback -*usage:* +*usage:* killback "jobname" *example:* +```bash +checkback "notify" +if [ "$res" -eq 0 ] ; then + send_normal_message "${CHAT[ID]}" "Kill notify" + killback "notify" +else + send_normal_message "${CHAT[ID]}" "Process notify not run." +fi +``` ### Bashbot internal These functions are for internal use only and must not used in your bot commands. ##### send_text -*usage:* +*usage:* send_text "${CHAT[ID]}" "message" ---- ##### JsonDecode -*usage:* +Outputs decoded string to STDOUT + +*usage:* JsonDecode "string" ##### JsonGetString -*usage:* +Reads JSON fro STDIN and Outputs found String to STDOUT + +*usage:* JsonGetString `"path","to","string"` ##### JsonGetValue -*usage:* +Reads JSON fro STDIN and Outputs found Value to STDOUT + +*usage:* JsonGetValue `"path","to","value"` ---- ##### get_chat_member_status -*usage:* +*usage:* get_chat_member_status "${CHAT[ID]}" "${USER[ID]}" ---- @@ -213,5 +325,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca ##### inproc Send Input from Telegram to waiting Interactive Chat. -#### $$VERSION$$ v0.6-rc1-11-gba80ec6 +#### $$VERSION$$ v0.60-rc2-0-gc581932 diff --git a/notify b/notify index 0a38bdd..72056f2 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-rc1-7-g14eb352 +#### $$VERSION$$ v0.60-rc2-0-gc581932 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 4050fd5..8f173e5 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-rc1-7-g14eb352 +#### $$VERSION$$ v0.60-rc2-0-gc581932 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index 367cf74..311b780 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.6-rc1-7-g14eb352 +#### $$VERSION$$ v0.60-rc2-0-gc581932 # shellcheck disable=SC2016 # # Easy Versioning in git: From 772760855da1e13cafc9e5896da903fc6e7e803e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Apr 2019 21:13:08 +0200 Subject: [PATCH 29/36] fix doc functions typos --- doc/6_reference.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 11cf58c..4d50b62 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -26,7 +26,7 @@ send_normal_message "${CHAT[ID]}" "this is a text message" ##### send_markdown_message -Telegram supports onyl a [reduced set of Markdown](https://core.telegram.org/bots/api#markdown-style) +Telegram supports only a [reduced set of Markdown](https://core.telegram.org/bots/api#markdown-style) *usage:* send_markdown_message "${CHAT[ID]}" "message" @@ -37,7 +37,7 @@ send_normal_message "${CHAT[ID]}" "*bold* _italic_ [text](link)" ``` ##### send_html_message -Telegram supports onyl a [reduced set of HTML](https://core.telegram.org/bots/api#html-style) +Telegram supports only a [reduced set of HTML](https://core.telegram.org/bots/api#html-style) *usage:* send_html_message "${CHAT[ID]}" "message" @@ -71,7 +71,7 @@ If your Bot is Admin in a Chat you can delete every message, if not you can dele ---- ##### answer_inline_query -Inline Queries allows users to interact with your bot via directly without sending extra commands. +Inline Queries allows users to interact with your bot directly without sending extra commands. answer_inline_query provide the result to a users Inline Query *usage:* answer_inline_query "$iQUERY_ID" "type" "type arg 1" ... "type arg n" @@ -90,7 +90,7 @@ answer_inline_query provide the result to a users Inline Query ``` ##### send_file -There are some ways to send files (photos, stickers, audio, media, etc.), [see more](https://core.telegram.org/bots/api#sending-files) +send_file allows you to send different type's of files, e.g. photos, stickers, audio, media, etc. [see more](https://core.telegram.org/bots/api#sending-files) *usage:* send_file "${CHAT[ID]}" "file" "caption" @@ -129,13 +129,13 @@ send_keyboard "${CHAT[ID]}" "[ \"1\" , \"2\" , \"3\" ] , [ \"4\" , \"5\" , \"6\" ### Manage users ##### kick_chat_member -If Bot is Admin you can kick and ban a user. +If your Bot is Admin of a chat he can kick and ban a user. *usage:* kick_chat_member "${CHAT[ID]}" "${USER[ID]}" ##### unban_chat_member -If Bot is Admin you can unban a kicked user. +If your Bot is Admin af a chat he can unban a kicked user. *usage:* unban_chat_member "${CHAT[ID]}" "${USER[ID]}" From 4a944d90891b630130e901bf874eb67faaba2507 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 16 Apr 2019 11:46:37 +0200 Subject: [PATCH 30/36] finalze docs for 0.6 --- README.md | 94 ++++++++++++++++++++++++---------------------- doc/1_firstbot.md | 4 +- doc/2_usage.md | 4 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 12 +++--- 7 files changed, 63 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 18b4cb4..110afd8 100644 --- a/README.md +++ b/README.md @@ -19,77 +19,83 @@ 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 -``` + ``` + 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. ## Update bashbot -[Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases) and copy all files to bashbot dir. run ```sudo ./bashbot.sh init```. +[Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases) extract all files and copy them to your bashbot dir. Now run ```sudo ./bashbot.sh init``` to setup your environment to the current release. ## 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. - - [Advanced Features](doc/3_advanced.md) - - Access Control - - 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 - - [Best Practices](doc/5_practice.md) - - Customizing commands.sh - - Seperate Bot logic from command - - Test your Bot with shellcheck - - [Bashbot functions reference](doc/6_reference.md) +* [Create Telegram Bot with botfather](doc/1_firstbot.md) +* [Getting Started](doc/2_usage.md) + * Managing your Bot + * Recieve data + * Send Messages + * Send files, location etc. +* [Advanced Features](doc/3_advanced.md) + * Access Control + * 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 +* [Best Practices](doc/5_practice.md) + * Customizing commands.sh + * Seperate Bot logic from command + * Test your Bot with shellcheck +* [Bashbot functions reference](doc/6_reference.md) ## Note on Keyboards -To make use of Keyboards easyer we changed the format of ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."```. -Now you must provide the keyboards in Telegram JSON Array style "[ \"yes\" , \"no\" ]". -The advantage is that you can create every type of keyboard without relying on bashbot ```send_keyboard``` functionality. -**This is incompatible with keyboards in bashbot versions older than 0.6!** +To make use of Keyboards easier the keybord format for ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."``` was changed. +Keybords are now defined in an JSON Array notation e.g. "[ \\"yes\\" , \\"no\\" ]". +This has the advantage that you can create any type of keyboard supported by Telegram. +**This is incompatible change for keyboards used in older bashbot versions** *Example Keyboards*: -- Yes No in one row: "[ \"yes\" , \"no\" ]" -- Yes No ind two rows: "[ \"yes\" ] , [ \"no\" ]" -- numpad keyboard: "[ \"1\" , \"2\" , \"3\" ] , [ \"4\" , \"5\" , \"6\" ] , [ \"7\" , \"8\" , \"9\" ] , [ \"0\" ]" - - +- yes no in one row + - OLD format: "yes" "no" (two strings) + - NEW format: "[ \\"yes\\" , \\"no\\" ]" (one string with array) +- new keybord layouts, no possible with old format: + - Yes No in two rows: "[ \\"yes\\" ] , [ \\"no\\" ]" + - numpad style keyboard: "[ \\"1\\" , \\"2\\" , \\"3\\" ] , [ \\"4\\" , \\"5\\" , \\"6\\" ] , [ \\"7\\" , \\"8\\" , \\"9\\" ] , [ \\"0\\" ]" ## Security Considerations -Running a Telegram Bot means you are conneted to the public, you never know whats send to your Bot. +Running a Telegram Bot means it is conneted to the public and 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. [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) -Whenever you are processing input from outside your bot you should disable globbing (set -f) and carefully quote everthing. +Whenever you are processing input from from untrusted sources (messages, files, network) you mustbe be as carefull as possible, e.g. disable globbing (set -f) and 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 shellcheck locally](https://github.com/koalaman/shellcheck#installing). bashbot itself is also linted by shellcheck. +A powerful tool to improve your scripts robustness is [shellcheck](https://www.shellcheck.net/). You can use it online or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). All bashbot scripts are checked 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. +It's important to run your bot as a user, with almost no access rights! -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. +All files your Bot can write are in danger to be overwritten/deleted if your bot can be abused. +Every file your Bot can read is in danger of being disclosed for the same reason. + +**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. +You Bot configuration contains data which should not be readable from other users. -Runing ```./bashbot.sh init``` sets the Bot permissions to reasonable default values as a starting point. +Everyone who can read your Bot files can extract your Bots secret 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 can be restricted to have write access to ```count``` and ```tmp-bot-bash``` only, all other files should be write protected. + +To set access rights for your telegram-bot-bash directory to reasonable default values you should run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. ### 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 ... +Bashbot is no more insecure as any other Bot written in any other language. But since YOU change the bot commands and run the Bot, 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! -#### $$VERSION$$ v0.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-2-g7727608 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index de5cd35..81240ef 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -1,5 +1,5 @@ -## Create your first telegram bot +## Create a Telegram Bot with botfather 1. Message @botfather https://telegram.me/botfather with the following text: `/newbot` @@ -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.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-2-g7727608 diff --git a/doc/2_usage.md b/doc/2_usage.md index 607a499..16c4d82 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -1,4 +1,4 @@ -## Make your own Bot +## Gettting Started 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. @@ -154,6 +154,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-2-g7727608 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 7750a94..41a5186 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -153,5 +153,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-2-g7727608 diff --git a/doc/4_expert.md b/doc/4_expert.md index f4cbac3..f2ed9f3 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.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-2-g7727608 diff --git a/doc/5_practice.md b/doc/5_practice.md index e3f5ad4..17469f6 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ In bashbot.sh line 490: Here are two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is really possible and much faster! The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-2-g7727608 diff --git a/doc/6_reference.md b/doc/6_reference.md index 4d50b62..0b2c1cd 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -111,15 +111,15 @@ send_file "${CHAT[ID]}" "https://www.domain,com/something.gif" "Something" ---- ##### send_keyboard -Note: since version 0.6 send_keyboard was changed to use native "JSON Array" as used from Telegram. - +Note: since version 0.6 send_keyboard was changed to use native "JSON Array" notation as used from Telegram. Eexample Keybord Array definitions: + *usage:* send_keyboard "chat-id" "keyboard" *example:* ```bash -send_keyboard "${CHAT[ID]}" "[ \"yes\" , \"no\" ]" -send_keyboard "${CHAT[ID]}" "[ \"yes\" ] , [ \"no\" ]" -send_keyboard "${CHAT[ID]}" "[ \"1\" , \"2\" , \"3\" ] , [ \"4\" , \"5\" , \"6\" ] , [ \"7\" , \"8\" , \"9\" ] , [ \"0\" ]" +send_keyboard "${CHAT[ID]}" "[ \\"yes\" , \\"no\" ]"" +send_keyboard "${CHAT[ID]}" "[ \\"yes\\" ] , [ \\"no\\" ]" +send_keyboard "${CHAT[ID]}" "[ \\"1\\" , \\"2\\" , \\"3\\" ] , [ \\"4\\" , \\"5\\" , \\"6\\" ] , [ \\"7\\" , \\"8\\" , \\"9\\" ] , [ \\"0\\" ]" ``` ##### remove_keyboard @@ -325,5 +325,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca ##### inproc Send Input from Telegram to waiting Interactive Chat. -#### $$VERSION$$ v0.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-2-g7727608 From 1bf26b9caf68e5c2d91acb799358f5c7bf402087 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 16 Apr 2019 13:29:49 +0200 Subject: [PATCH 31/36] finalize features and descriptions --- README.md | 26 +++++----- README.txt | 115 +++++++++++++++++++++++++-------------------- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 8 ++-- calc | 13 ++--- commands.sh | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 72 ++++++++++++++-------------- doc/3_advanced.md | 15 +++--- doc/4_expert.md | 15 +++--- doc/5_practice.md | 30 +++++------- doc/6_reference.md | 59 +++++++++++++++-------- notify | 2 +- question | 2 +- version | 2 +- 16 files changed, 201 insertions(+), 166 deletions(-) diff --git a/README.md b/README.md index 110afd8..1cbaa0e 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/cop 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. ## Update bashbot -[Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases) extract all files and copy them to your bashbot dir. Now run ```sudo ./bashbot.sh init``` to setup your environment to the current release. +[Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases), extract all files and copy them to your bashbot dir. Now run ```sudo ./bashbot.sh init``` to setup your environment for the new release. ## Getting started * [Create Telegram Bot with botfather](doc/1_firstbot.md) @@ -56,13 +56,13 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/cop To make use of Keyboards easier the keybord format for ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."``` was changed. Keybords are now defined in an JSON Array notation e.g. "[ \\"yes\\" , \\"no\\" ]". This has the advantage that you can create any type of keyboard supported by Telegram. -**This is incompatible change for keyboards used in older bashbot versions** +**This is an incompatible change for keyboards used in older bashbot versions.** *Example Keyboards*: - yes no in one row - OLD format: "yes" "no" (two strings) - - NEW format: "[ \\"yes\\" , \\"no\\" ]" (one string with array) + - NEW format: "[ \\"yes\\" , \\"no\\" ]" (string containing an array) - new keybord layouts, no possible with old format: - Yes No in two rows: "[ \\"yes\\" ] , [ \\"no\\" ]" - numpad style keyboard: "[ \\"1\\" , \\"2\\" , \\"3\\" ] , [ \\"4\\" , \\"5\\" , \\"6\\" ] , [ \\"7\\" , \\"8\\" , \\"9\\" ] , [ \\"0\\" ]" @@ -72,30 +72,30 @@ Running a Telegram Bot means it is conneted to the public and you never know wha 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. [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) -Whenever you are processing input from from untrusted sources (messages, files, network) you mustbe be as carefull as possible, e.g. disable globbing (set -f) and quote everthing. +Whenever you are processing input from from untrusted sources (messages, files, network) you must be as carefull as possible, e.g. disable globbing (set -f) and quote everthing. -A powerful tool to improve your scripts robustness is [shellcheck](https://www.shellcheck.net/). You can use it online or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). All bashbot scripts are checked by shellcheck. +A powerful tool to improve your scripts robustness is ```shellcheck```. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). All bashbot scripts are checked by shellcheck. ### Run your Bot as a restricted user -It's important to run your bot as a user, with almost no access rights! +**It's important to run your bot as a user, with almost no access rights.** -All files your Bot can write are in danger to be overwritten/deleted if your bot can be abused. -Every file your Bot can read is in danger of being disclosed for the same reason. +All files your Bot write access to are in danger to be overwritten/deleted if your bot is hacked. +For the same reason ervery file your Bot can read is in danger of being disclosed. So please restict your Bots access rigths to the absolute minimum. **Never run your Bot as root, this is the most dangerous you can do!** Usually the user 'nobody' has almost no rigths on Unix/Linux systems. See Expert use on how to run your Bot as an other user. ### Secure your Bot installation -You Bot configuration contains data which should not be readable from other users. +**Your Bot configuration should not be readable from other users.** If someone can read your Bots token he can act as your Bot and has access to all chats you bot is in! -Everyone who can read your Bot files can extract your Bots secret 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 can be restricted to have write access to ```count``` and ```tmp-bot-bash``` only, all other files should be write protected. +Everyone with read access to 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 must be restricted to have write access to ```count``` and ```tmp-bot-bash``` only, all other files should be write protected. -To set access rights for your telegram-bot-bash directory to reasonable default values you should run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. +To set access rights for your telegram-bot-bash directory to reasonable default values you must run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. ### Is this Bot insecure? -Bashbot is no more insecure as any other Bot written in any other language. But since YOU change the bot commands and run the Bot, you should know about the implications ... +Bashbot is no more (in)secure as any other Bot written in any other language. But since YOU change your bots commands and run the Bot, 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! -#### $$VERSION$$ v0.60-rc2-2-g7727608 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 diff --git a/README.txt b/README.txt index 9f84eef..02040aa 100644 --- a/README.txt +++ b/README.txt @@ -24,16 +24,14 @@ 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: +2. 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 +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. @@ -41,54 +39,58 @@ Update bashbot ~~~~~~~~~~~~~~ https://github.com/topkecleon/telegram-bot-bash/releases[Download latest -update zip from github] and copy all files to bashbot dir. run -`sudo ./bashbot.sh init`. +update zip from github], extract all files and copy them to your bashbot +dir. Now run `sudo ./bashbot.sh init` to setup your environment for the +new release. 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/1_firstbot.md[Create Telegram Bot with botfather] +* link:doc/2_usage.md[Getting Started] +** Managing your Bot +** Recieve data +** Send Messages +** Send files, location etc. * link:doc/3_advanced.md[Advanced Features] -* Access Control -* Interactive Chats -* Background Jobs -* Inline queries +** Access Control +** 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 +** Handling UTF-8 +** Run as other user or system service +** Scedule bashbot from Cron * link:doc/5_practice.md[Best Practices] -* Customizing commands.sh -* Seperate Bot logic from command -* Test your Bot with shellcheck +** Customizing commands.sh +** Seperate Bot logic from command +** Test your Bot with shellcheck * link:doc/6_reference.md[Bashbot functions reference] Note on Keyboards ~~~~~~~~~~~~~~~~~ -To make use of Keyboards easyer we changed the format of `send_keyboard` -and `send_message "mykeyboardstartshere ..."`. Now you must provide the -keyboards in Telegram JSON Array style "[ "yes" , "no" ]". The advantage -is that you can create every type of keyboard without relying on bashbot -`send_keyboard` functionality. *This is incompatible with keyboards in -bashbot versions older than 0.6!* +To make use of Keyboards easier the keybord format for `send_keyboard` +and `send_message "mykeyboardstartshere ..."` was changed. Keybords are +now defined in an JSON Array notation e.g. "[ \"yes\" , \"no\" ]". This +has the advantage that you can create any type of keyboard supported by +Telegram. *This is an incompatible change for keyboards used in older +bashbot versions.* _Example Keyboards_: -* Yes No in one row: "[ "yes" , "no" ]" -* Yes No ind two rows: "[ "yes" ] , [ "no" ]" -* numpad keyboard: "[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , -"8" , "9" ] , [ "0" ]" +* yes no in one row +** OLD format: "yes" "no" (two strings) +** NEW format: "[ \"yes\" , \"no\" ]" (string containing an array) +* new keybord layouts, no possible with old format: +** Yes No in two rows: "[ \"yes\" ] , [ \"no\" ]" +** numpad style keyboard: "[ \"1\" , \"2\" , \"3\" ] , [ \"4\" , \"5\" , +\"6\" ] , [ \"7\" , \"8\" , \"9\" ] , [ \"0\" ]" Security Considerations ~~~~~~~~~~~~~~~~~~~~~~~ -Running a Telegram Bot means you are conneted to the public, you never +Running a Telegram Bot means it is conneted to the public and you never know whats send to your Bot. Bash scripts in general are not designed to be bullet proof, so consider @@ -97,42 +99,53 @@ 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. +Whenever you are processing input from from untrusted sources (messages, +files, network) you must be as carefull as possible, e.g. disable +globbing (set -f) and 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. +A powerful tool to improve your scripts robustness is `shellcheck`. You +can https://www.shellcheck.net/[use it online] or +https://github.com/koalaman/shellcheck#installing[install shellcheck +locally]. All bashbot scripts are checked 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. +*It's important to run your bot as a user, with almost no access +rights.* -Never run your Bot as root, this is the most dangerous you can do! +All files your Bot write access to are in danger to be +overwritten/deleted if your bot is hacked. For the same reason ervery +file your Bot can read is in danger of being disclosed. So please +restict your Bots access rigths to the absolute minimum. + +*Never run your Bot as root, this is the most dangerous you can do!* Usually the user 'nobody' has almost no 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. +*Your Bot configuration should not be readable from other users.* If +someone can read your Bots token he can act as your Bot and has access +to all chats you bot is in! + +Everyone with read access to 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. +Bot must be restricted to have 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. +To set access rights for your telegram-bot-bash directory to reasonable +default values you must run `sudo ./bashbot.sh init` after every update +or change to your installation directory. 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 ... +Bashbot is no more (in)secure as any other Bot written in any other +language. But since YOU change your bots commands and run the Bot, you +should know about the implications ... That's it! ~~~~~~~~~~ @@ -140,5 +153,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.60-rc2-0-gc581932 +latexmath:[\[VERSION\]] v0.60-rc2-3-g4a944d9 ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index e6057f7..630316d 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.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index ba5d15f..9f9f9e8 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 8a20945..dba5e46 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.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 # # Exit Codes: # - 0 sucess (hopefully) @@ -439,11 +439,13 @@ send_venue() { } -forward() { +forward_message() { [ "$3" = "" ] && return res="$(curl -s "$FORWARD_URL" -F "chat_id=$1" -F "from_chat_id=$2" -F "message_id=$3")" } - +forward() { # backward compatibility + forward_message "$@" || return +} background() { echo "${CHAT[ID]}:$2:$1" >"${TMPDIR:-.}/${copname}$2-back.cmd" diff --git a/calc b/calc index 0cada5f..bfa0358 100755 --- a/calc +++ b/calc @@ -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.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -23,11 +23,12 @@ echo 'Select Operation: mykeyboardstartshere [ "Addition" , "Subtraction" , "Mul read -r opt echo -n 'Result: ' case $opt in - 'add'* | 'Add'* ) expr $A + $B ;; - 'sub'* | 'Sub'* ) expr $A - $B ;; - 'mul'* | 'Mul'* ) expr $A \* $B ;; - 'div'* | 'Div'* ) expr $A / $B ;; - 'can'* | 'Can'* ) echo "abort!" ;; + 'add'* | 'Add'* ) res="$(( A + B ))" ;; + 'sub'* | 'Sub'* ) res="$(( A - B ))" ;; + 'mul'* | 'Mul'* ) res="$(( A * B ))" ;; + 'div'* | 'Div'* ) res="$(( A / B ))" ;; + 'can'* | 'Can'* ) res="abort!" ;; * ) echo "unknown operator!";; esac +echo "$res" echo "Bye .." diff --git a/commands.sh b/commands.sh index f934796..0dbbd26 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.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 81240ef..870cbb5 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.60-rc2-2-g7727608 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 diff --git a/doc/2_usage.md b/doc/2_usage.md index 16c4d82..e83c64e 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -36,50 +36,50 @@ To send a broadcast to all of users that ever used the bot run the following com ## 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 +* ```${MESSAGE}```: Current incoming messages +* ```${MESSAGE[ID]}```: ID of current 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 + * ```${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) + * ```${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 + * ```${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 + * ```${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 + * ```${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 + * ```${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 + * ```${LOCATION[LONGITUDE]}```: Longitude + * ```${LOCATION[LATITUDE]}```: Latitude ## Usage of bashbot functions @@ -154,6 +154,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.60-rc2-2-g7727608 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 41a5186..7c6e401 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -1,7 +1,9 @@ ## Advanced Features + ### Access control -Bashbot offers functions to check if a has Telegram capabilities like chat admin or chat creator: +Bashbot offers functions to check what Telegram capabilities like chat admin or chat creator the given user has: + ```bash # return true if user is admin/owner of the bot # -> botadmin is stored in file './botadmin' @@ -19,10 +21,10 @@ user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are * user_is_admin "${CHAT[ID]}" "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *CHATADMIN*." ``` -In addtion you can configure individual capabilities for users in the file ```./botacl```: +In addtion the bot can check individual capabilities of users as defined in the ```./botacl``` file: ```bash -# botacl -# if a user is not listed here, function 'user_is_allowed' will always return false +# file: botacl +# a user not listed here, will return false from 'user_is_allowed' # # Format: # user:ressource:chat @@ -42,8 +44,7 @@ In addtion you can configure individual capabilities for users in the file ```./ *:start:* *:*:98979695 ``` -you have to check yourself if a user is allowed to to something by calling function ```user_is_allowed```. -example to check if a user is allowed to start bot: +you have to use the function ```user_is_allowed``` to check if a user has the capability to do something. Example check if user has capability to start bot: ```bash case "$MESSAGE" in '/start') @@ -153,5 +154,5 @@ To send stickers through an *inline query*: answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### $$VERSION$$ v0.60-rc2-2-g7727608 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 diff --git a/doc/4_expert.md b/doc/4_expert.md index f2ed9f3..fc1baea 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -8,7 +8,7 @@ two bytes for encoding and covers almost all ```Latin``` alphabets, also ```Gree ```Hebrew```, ```Arabic``` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more deatils. #### Setting up your Environment -In general ```bash``` and ```GNU``` utitities are UTF-8 aware, but you have to setup your environment +In general ```bash``` and ```GNU``` utitities are UTF-8 aware if you to setup your environment and your scripts accordingly: 1. Your Terminal and Editor must support UTF-8: @@ -36,15 +36,16 @@ 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 Support -Telegram send JSON messages with all characters not fitting in one byte (<256 bit) escaped as sequences of ```\uxxxx``` to be regular -one byte ASCII. Multibyte UTF-8 characters, e.g. Emoticons and Arabic characters, are send in UTF-16 notation. +#### Bashbot UTF-8 Support +Bashbot handles all messages transparently, regardless what charset is used. One exception is the conversation from JSON data to strings. + +Telegram use JSON to send / recieve data. Characters not ASCII *(>127)* are escaped as sequences of ```\uxxxx``` to be regular ASCII. In addition multibyte characters, *e.g. Emoticons or Arabic characters*, are send in double byte UTF-16 notation. The Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ``` -**This mixed JSON encoding can not decoded from** ```echo -e``` or ```printf '%s\\n'```, this works only for single byte characters! +**This mixed JSON encoding needs special handling and can not decoded from** ```echo -e``` or ```printf '%s\\n'``` To to fully support decoding of multibyte characters you need a working python2 installation on your system. -If no python is detected bashbot falls back to a **slow, pure bash solution which may not work for some corner cases**. +If no python is detected bashbot falls back to a **internal pure bash implementaion which may not work for some corner cases**. ### Run as other user or system service @@ -102,5 +103,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.60-rc2-2-g7727608 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 diff --git a/doc/5_practice.md b/doc/5_practice.md index 17469f6..6491a35 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -1,9 +1,8 @@ ## Best Practices -### Customizing commands.sh +### Customize commands.sh only -To ease Updates never change ```bashbot.sh```, all changes should be done in ```commands.sh``` . -Insert your own Bot commands in the ```case ... esac``` block in commands.sh: +To ease Updates never change ```bashbot.sh```, instead individual commands should go to ```commands.sh``` . Insert your Bot commands in the ```case ... esac``` block: ```bash case "$MESSAGE" in '/echo') # my first own command, echo MESSAGE @@ -17,19 +16,17 @@ Insert your own Bot commands in the ```case ... esac``` block in commands.sh: ;; esac ``` -after editing commands.sh restart Bot. -### Seperate logic from command block +### Seperate logic from commands -If your Bot command needs more than 2-3 lines of code I recommend to factor it out to a function to keep the command block small. -Place the functions in a file, e.g. ```mybotcommands.inc.sh``` and source it from bashbot.sh. -```process_message``` is an example for a function hiding complex logic in a bash funtcion. +If a command need more than 2-3 lines of code, you should use a function to seperate logic from command. Place your functions in a seperate file, e.g. ```mycommands.inc.sh``` and source it from bashbot.sh. Example: ```bash - source mybotcommands.inc.sh + source "mycommands.inc.sh" case "$MESSAGE" in - '/report') # logic for /report is done in process_message - send_normal_message "${CHAT[ID]}" "$(process_message "$MESSAGE")" + '/process') # logic for /report is done in process_message + result="$(process_message "$MESSAGE") + send_normal_message "${CHAT[ID]}" "$result" ;; ################################################ @@ -43,15 +40,15 @@ Place the functions in a file, e.g. ```mybotcommands.inc.sh``` and source it fro ;; esac ``` -Example function ```process_message``` in file ```mybotcommands.inc.sh```: ```bash #!/bin/bash -# +# file: mycommands.inc.sh + process_message() { - local ARGS="${1#/* }" # remove command /* + local ARGS="${1#/* }" # remove command local TEXT OUTPUT="" - # process every word in MESSAGE, avoid globbing from MESSAGE + # process every word in MESSAGE, avoid globbing set -f for WORD in $ARGS do @@ -75,7 +72,6 @@ process_message() { } ``` -Doing it this way keeps commands.sh small and clean, while allowing complex tasks to be done in the included function. ### Test your Bot with shellcheck Shellcheck is a static linter for shell scripts providing excellent tips and hints for shell coding pittfalls. You can [use it online](https://www.shellcheck.net/) or [install it on your system](https://github.com/koalaman/shellcheck#installing). @@ -112,5 +108,5 @@ In bashbot.sh line 490: Here are two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is really possible and much faster! The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.60-rc2-2-g7727608 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 diff --git a/doc/6_reference.md b/doc/6_reference.md index 0b2c1cd..9138b2a 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -1,12 +1,15 @@ ## Bashbot function reference -### Send, forward, delete Messages +### Send, forward, delete messages ##### send_action -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. +```send_action``` shows users what your bot is currently doing. *usage:* send_action "${CHAT[ID]}" "action" +*"action":* ```typing```, ```upload_photo```, ```record_video```, ```upload_video```, ```record_audio```, ```upload_audio```, ```upload_document```, ```find_location```. + + *example:* ```bash send_action "${CHAT[ID]}" "typing" @@ -15,7 +18,7 @@ send_action "${CHAT[ID]}" "record_audio" ##### send_normal_message -sen_normal_message send text only messages to chat. +```send_normal_message``` sends text only messages to the given chat. *usage:* send_normal_message "${CHAT[ID]}" "message" @@ -26,9 +29,10 @@ send_normal_message "${CHAT[ID]}" "this is a text message" ##### send_markdown_message -Telegram supports only a [reduced set of Markdown](https://core.telegram.org/bots/api#markdown-style) +```send_markdown_message``` sends markdown style messages to the given chat. +Telegram supports a [reduced set of Markdown](https://core.telegram.org/bots/api#markdown-style) only -*usage:* send_markdown_message "${CHAT[ID]}" "message" +*usage:* send_markdown_message "${CHAT[ID]}" "markdown message" *example:* ```bash @@ -37,9 +41,10 @@ send_normal_message "${CHAT[ID]}" "*bold* _italic_ [text](link)" ``` ##### send_html_message -Telegram supports only a [reduced set of HTML](https://core.telegram.org/bots/api#html-style) +```send_html_message``` sends HTML style messages to the given chat. +Telegram supports a [reduced set of HTML](https://core.telegram.org/bots/api#html-style) only -*usage:* send_html_message "${CHAT[ID]}" "message" +*usage:* send_html_message "${CHAT[ID]}" "html message" *example:* ```bash @@ -47,15 +52,19 @@ send_normal_message "${CHAT[ID]}" "this is a markdown message, next word is send_normal_message "${CHAT[ID]}" "bold italic> italic>/em> Text" ``` -##### forward -*usage:* forward "${CHAT[ID]}" "${MESSAGE[ID]}" +##### forward_message +```forward_mesage``` forwards a messsage to the given chat. +*usage:* forward_message "chat_to" "chat_from" "${MESSAGE[ID]}" + +*alias:* forward "${CHAT[ID]}" "$FROMCHAT" "${MESSAGE[ID]}" ---- ##### send_message -Send Message must (only) used to process the output of interactive chats and background jobs. -**For your commands I reccommend the more dedicated send_xxx_message() functions above.** +```send_message``` sends any type of message to the given chat. Type of output is steered by keywords within the message. + +The main use case for send_message is to process the output of interactive chats and background jobs. **For regular Bot commands I recommend using of the dedicated send_xxx_message() functions from above.** *usage:* send_message "${CHAT[ID]}" "message" @@ -64,7 +73,7 @@ Send Message must (only) used to process the output of interactive chats and bac ---- ##### delete_message -If your Bot is Admin in a Chat you can delete every message, if not you can delete only your Bot messages. +If your Bot is admin of a Chat he can delete every message, if not he can delete only his messages. *usage:* delete_message "${CHAT[ID]}" "${MESSAGE[ID]}" @@ -111,7 +120,15 @@ send_file "${CHAT[ID]}" "https://www.domain,com/something.gif" "Something" ---- ##### send_keyboard -Note: since version 0.6 send_keyboard was changed to use native "JSON Array" notation as used from Telegram. Eexample Keybord Array definitions: +Note: since version 0.6 send_keyboard was changed to use native "JSON Array" notation as used from Telegram. Example Keybord Array definitions: + +- yes no in one row + - OLD format: "yes" "no" (two strings) + - NEW format: "[ \\"yes\\" , \\"no\\" ]" (string containing an array) +- new keybord layouts, no possible with old format: + - Yes No in two rows: "[ \\"yes\\" ] , [ \\"no\\" ]" + - numpad style keyboard: "[ \\"1\\" , \\"2\\" , \\"3\\" ] , [ \\"4\\" , \\"5\\" , \\"6\\" ] , [ \\"7\\" , \\"8\\" , \\"9\\" ] , [ \\"0\\" ]" + *usage:* send_keyboard "chat-id" "keyboard" @@ -154,7 +171,7 @@ fi ---- ##### user_is_creator -Returns true (0) if user is creator of chat or chat is a one2one / private chat. +Returns true (0) if user is creator of chat or chat is a private chat. *usage:* user_is_creator "${CHAT[ID]}" "${USER[ID]}" @@ -203,7 +220,7 @@ fi ### Interactive and backgound jobs ##### startproc -With ```startproc``` you can start scrips (or C or python program etc.). 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. see [Advanced Usage](3_advanced.md#Interactive-Chats) +```startproc``` tarts a script (or C or python program etc.) running in parallel to your Bot. The text that the script outputs is sent time to the user or chat, user input will be sent back to the script. see [Advanced Usage](3_advanced.md#Interactive-Chats) *usage:* startproc "./script" @@ -213,7 +230,7 @@ startproc './calc' ``` ##### checkproc -Returns true (0) if an interactive script is running in chat. +Returns true (0) if an interactive script active in the given chat. *usage:* checkprog @@ -243,6 +260,10 @@ fi ---- ##### background +```background``` starts a script / programm as a background job and attaches a jobname to it. All output from a background job is sent to the associated chat. + +In contrast to interactive chats, background jobs do not recieve user input and can run forever. In addition you can suspend and restart running jobs, e.g. after reboot. + *usage:* background "./script" "jobname" *example:* @@ -251,7 +272,7 @@ background "./notify" "notify" ``` ##### checkback -Returns true (0) if an background job is running in chat. +Returns true (0) if an background job is active in the given chat. *usage:* checkback "jobname" @@ -280,7 +301,7 @@ else fi ``` -### Bashbot internal +### Bashbot internal functions These functions are for internal use only and must not used in your bot commands. ##### send_text @@ -325,5 +346,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca ##### inproc Send Input from Telegram to waiting Interactive Chat. -#### $$VERSION$$ v0.60-rc2-2-g7727608 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 diff --git a/notify b/notify index 72056f2..2bcc0f9 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.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 8f173e5..b4fbb67 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.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index 311b780..540199a 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.60-rc2-0-gc581932 +#### $$VERSION$$ v0.60-rc2-3-g4a944d9 # shellcheck disable=SC2016 # # Easy Versioning in git: From 591c5834fa89fcdf315e842bec71a84a72cc8353 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 16 Apr 2019 16:45:26 +0200 Subject: [PATCH 32/36] some more doc cleanup for release --- README.md | 14 +++++++------- README.txt | 16 ++++++++-------- doc/1_firstbot.md | 13 ++----------- doc/2_usage.md | 4 +++- doc/3_advanced.md | 6 ++++-- doc/4_expert.md | 20 ++++++++++---------- doc/5_practice.md | 11 +++++++---- doc/6_reference.md | 22 ++++++++++++---------- 8 files changed, 53 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 1cbaa0e..f8a062f 100644 --- a/README.md +++ b/README.md @@ -68,9 +68,9 @@ This has the advantage that you can create any type of keyboard supported by Tel - numpad style keyboard: "[ \\"1\\" , \\"2\\" , \\"3\\" ] , [ \\"4\\" , \\"5\\" , \\"6\\" ] , [ \\"7\\" , \\"8\\" , \\"9\\" ] , [ \\"0\\" ]" ## Security Considerations -Running a Telegram Bot means it is conneted to the public and you never know whats send to your Bot. +Running a Telegram Bot means it is connected to the public and 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. [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) +Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. More concret examples of security problems are bash's 'quoting hell' and globbing. [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) Whenever you are processing input from from untrusted sources (messages, files, network) you must be as carefull as possible, e.g. disable globbing (set -f) and quote everthing. @@ -79,23 +79,23 @@ A powerful tool to improve your scripts robustness is ```shellcheck```. You can ### Run your Bot as a restricted user **It's important to run your bot as a user, with almost no access rights.** -All files your Bot write access to are in danger to be overwritten/deleted if your bot is hacked. +All files your Bot have write access to are in danger to be overwritten/deleted if your bot is hacked. For the same reason ervery file your Bot can read is in danger of being disclosed. So please restict your Bots access rigths to the absolute minimum. -**Never run your Bot as root, this is the most dangerous you can do!** Usually the user 'nobody' has almost no rigths on Unix/Linux systems. See Expert use on how to run your Bot as an other user. +**Never run your Bot as root, this is the most dangerous you can do!** Usually the user 'nobody' has almost no rights on Unix/Linux systems. See Expert use on how to run your Bot as an other user. ### Secure your Bot installation -**Your Bot configuration should not be readable from other users.** If someone can read your Bots token he can act as your Bot and has access to all chats you bot is in! +**Your Bot configuration should not be readable from other users.** If someone can read your Bots token he can act as your Bot and has access to all chats you Bot is in! Everyone with read access to 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 must be restricted to have write access to ```count``` and ```tmp-bot-bash``` only, all other files should be write protected. To set access rights for your telegram-bot-bash directory to reasonable default values you must run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. ### Is this Bot insecure? -Bashbot is no more (in)secure as any other Bot written in any other language. But since YOU change your bots commands and run the Bot, you should know about the implications ... +Bashbot is no more (in)secure as any other Bot written in any other language. But since YOU are responsible for your bots commands and run the Bot, 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! -#### $$VERSION$$ v0.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 diff --git a/README.txt b/README.txt index 02040aa..fba63af 100644 --- a/README.txt +++ b/README.txt @@ -90,12 +90,12 @@ _Example Keyboards_: Security Considerations ~~~~~~~~~~~~~~~~~~~~~~~ -Running a Telegram Bot means it is conneted to the public and you never +Running a Telegram Bot means it is connected to the public and 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. +problems are 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] @@ -114,13 +114,13 @@ Run your Bot as a restricted user *It's important to run your bot as a user, with almost no access rights.* -All files your Bot write access to are in danger to be +All files your Bot have write access to are in danger to be overwritten/deleted if your bot is hacked. For the same reason ervery file your Bot can read is in danger of being disclosed. So please restict your Bots access rigths to the absolute minimum. *Never run your Bot as root, this is the most dangerous you can do!* -Usually the user 'nobody' has almost no rigths on Unix/Linux systems. +Usually the user 'nobody' has almost no rights on Unix/Linux systems. See Expert use on how to run your Bot as an other user. Secure your Bot installation @@ -128,7 +128,7 @@ Secure your Bot installation *Your Bot configuration should not be readable from other users.* If someone can read your Bots token he can act as your Bot and has access -to all chats you bot is in! +to all chats you Bot is in! Everyone with read access to your Bot files can extract your Bots data. Especially your Bot Token in `token` must be protected against other @@ -144,8 +144,8 @@ Is this Bot insecure? ^^^^^^^^^^^^^^^^^^^^^ Bashbot is no more (in)secure as any other Bot written in any other -language. But since YOU change your bots commands and run the Bot, you -should know about the implications ... +language. But since YOU are responsible for your bots commands and run +the Bot, you should know about the implications ... That's it! ~~~~~~~~~~ @@ -153,5 +153,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.60-rc2-3-g4a944d9 +latexmath:[\[VERSION\]] v0.60-rc2-4-g1bf26b9 ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 870cbb5..34061ec 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -58,17 +58,8 @@ 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 +#### [Next Getting started](2_usage.md) -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.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 diff --git a/doc/2_usage.md b/doc/2_usage.md index e83c64e..c18dded 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -154,6 +154,8 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### $$VERSION$$ v0.60-rc2-3-g4a944d9 +#### [Prev Getting started](2_usage.md) +#### [Next Advanced Usage](3_advanced.md) +#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 7c6e401..878a284 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -21,7 +21,7 @@ user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are * user_is_admin "${CHAT[ID]}" "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *CHATADMIN*." ``` -In addtion the bot can check individual capabilities of users as defined in the ```./botacl``` file: +In addition you can check individual capabilities of users as defined in the ```./botacl``` file: ```bash # file: botacl # a user not listed here, will return false from 'user_is_allowed' @@ -153,6 +153,8 @@ To send stickers through an *inline query*: ```bash answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` +#### [prev Advanced Usage](3_advanced.md) +#### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 diff --git a/doc/4_expert.md b/doc/4_expert.md index fc1baea..c6a3da3 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -5,7 +5,7 @@ UTF-8 is a variable length encoding of Unicode. UTF-8 is recommended as the defa 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. +```Hebrew```, ```Arabic``` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details. #### Setting up your Environment In general ```bash``` and ```GNU``` utitities are UTF-8 aware if you to setup your environment @@ -36,13 +36,13 @@ 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) -#### Bashbot UTF-8 Support -Bashbot handles all messages transparently, regardless what charset is used. One exception is the conversation from JSON data to strings. +#### Bashbot's UTF-8 Support +Bashbot handles all messages transparently, regardless of the charset in use. The only exception is when converting from JSON data to strings. -Telegram use JSON to send / recieve data. Characters not ASCII *(>127)* are escaped as sequences of ```\uxxxx``` to be regular ASCII. In addition multibyte characters, *e.g. Emoticons or Arabic characters*, are send in double byte UTF-16 notation. +Telegram use JSON to send / recieve data. JSON encodes strings as follow: Characters not ASCII *(>127)* are escaped as sequences of ```\uxxxx``` to be regular ASCII. In addition multibyte characters, *e.g. Emoticons or Arabic characters*, are send in double byte UTF-16 notation. The Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ``` -**This mixed JSON encoding needs special handling and can not decoded from** ```echo -e``` or ```printf '%s\\n'``` +**This "mixed" JSON encoding needs special handling and can not decoded from** ```echo -e``` or ```printf '%s\\n'``` To to fully support decoding of multibyte characters you need a working python2 installation on your system. If no python is detected bashbot falls back to a **internal pure bash implementaion which may not work for some corner cases**. @@ -51,8 +51,6 @@ If no python is detected bashbot falls back to a **internal pure bash implementa ### Run as other user or system service 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 sudo ./bashbot.sh init @@ -77,13 +75,13 @@ 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: +From now on use 'bashbot.rc' to manage your bot: ```bash sudo ./bashbot.rc start ``` Type ```ps -ef | grep bashbot``` to verify your Bot is running as the desired user. -If you started bashbot by bashbot.rc you must use bashbot.rc also to manage your Bot! The following commands are availible: +If your Bot is started 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 @@ -102,6 +100,8 @@ 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``` +#### [Prev Expert Use](4_expert.md) +#### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 diff --git a/doc/5_practice.md b/doc/5_practice.md index 6491a35..1043ebb 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -24,8 +24,8 @@ If a command need more than 2-3 lines of code, you should use a function to sepe source "mycommands.inc.sh" case "$MESSAGE" in - '/process') # logic for /report is done in process_message - result="$(process_message "$MESSAGE") + '/process') # logic for /process is done in process_message + result="$(process_message "$MESSAGE")" send_normal_message "${CHAT[ID]}" "$result" ;; @@ -105,8 +105,11 @@ In bashbot.sh line 490: CONTACT[USER_ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","contact","user_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")" ^-- SC2034: CONTACT appears unused. Verify it or export it. ``` -Here are two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is really possible and much faster! +The example show two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, this is fixed and much faster as the "echo | sed" solution. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) -#### $$VERSION$$ v0.60-rc2-3-g4a944d9 +#### [Prev Best Practice](5_practice.md) +#### [Next Functions Reference](6_reference.md) + +#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 diff --git a/doc/6_reference.md b/doc/6_reference.md index 9138b2a..8c58c66 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -152,12 +152,12 @@ If your Bot is Admin of a chat he can kick and ban a user. ##### unban_chat_member -If your Bot is Admin af a chat he can unban a kicked user. +If your Bot is Admin of a chat he can unban a kicked user. *usage:* unban_chat_member "${CHAT[ID]}" "${USER[ID]}" ##### leave_chat -Bot will leave chat. +Bot will leave given chat. *usage:* leave_chat "${CHAT[ID]}" @@ -171,14 +171,14 @@ fi ---- ##### user_is_creator -Returns true (0) if user is creator of chat or chat is a private chat. +Return true (0) if user is creator of given chat or chat is a private chat. *usage:* user_is_creator "${CHAT[ID]}" "${USER[ID]}" *alias:* _is_creator ##### user_is_admin -Returns true (0) if user is admin or creator of chat. +Return true (0) if user is admin or creator of given chat. *usage:* user_is_admin "${CHAT[ID]}" "${USER[ID]}" @@ -193,8 +193,8 @@ fi ``` ##### user_is_botadmin -Returns true (0) if user is owner / admin of bot. -botadmin is stored in file './botadmin' +Return true (0) if user is owner / admin of bot. +Name or ID botadmin must be placed in './botadmin' file. *usage:* user_is_botadmin "${CHAT[ID]}" "${USER[ID]}" @@ -220,7 +220,7 @@ fi ### Interactive and backgound jobs ##### startproc -```startproc``` tarts a script (or C or python program etc.) running in parallel to your Bot. The text that the script outputs is sent time to the user or chat, user input will be sent back to the script. see [Advanced Usage](3_advanced.md#Interactive-Chats) +```startproc``` starts a script (or C or python program etc.) running in parallel to your Bot. The text that the script outputs is sent to the user or chat, user input will be sent back to the script. see [Advanced Usage](3_advanced.md#Interactive-Chats) *usage:* startproc "./script" @@ -230,7 +230,7 @@ startproc './calc' ``` ##### checkproc -Returns true (0) if an interactive script active in the given chat. +Return true (0) if an interactive script active in the given chat. *usage:* checkprog @@ -272,7 +272,7 @@ background "./notify" "notify" ``` ##### checkback -Returns true (0) if an background job is active in the given chat. +Return true (0) if an background job is active in the given chat. *usage:* checkback "jobname" @@ -346,5 +346,7 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca ##### inproc Send Input from Telegram to waiting Interactive Chat. -#### $$VERSION$$ v0.60-rc2-3-g4a944d9 +#### [Prev Best Practice](5_practice.md) + +#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 From 2e3c9753fe35030998bc860e2ecd8a055fdaf92b Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 16 Apr 2019 20:43:54 +0200 Subject: [PATCH 33/36] Bashbot v0.60-rc2 --- README.md | 52 +++++++++++++-------------- README.txt | 88 ++++++++++++++++++++++++---------------------- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 2 +- calc | 2 +- commands.sh | 2 +- doc/1_firstbot.md | 4 +-- doc/2_usage.md | 10 +++--- doc/3_advanced.md | 6 ++-- doc/4_expert.md | 3 +- doc/5_practice.md | 3 +- doc/6_reference.md | 12 +++---- notify | 2 +- question | 4 +-- version | 2 +- 16 files changed, 100 insertions(+), 96 deletions(-) diff --git a/README.md b/README.md index f8a062f..29c1c3d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. -[Download latest release from github](https://github.com/topkecleon/telegram-bot-bash/releases) +Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Downloads](https://github.com/topkecleon/telegram-bot-bash/releases) are availible on Githup. Released to the public domain wherever applicable. Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). @@ -30,72 +30,70 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/cop ## Update bashbot [Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases), extract all files and copy them to your bashbot dir. Now run ```sudo ./bashbot.sh init``` to setup your environment for the new release. -## Getting started -* [Create Telegram Bot with botfather](doc/1_firstbot.md) +## Bashbot Documentation +* [Create a new Telegram Bot with botfather](doc/1_firstbot.md) * [Getting Started](doc/2_usage.md) * Managing your Bot * Recieve data - * Send Messages - * Send files, location etc. + * Send messages + * Send files, locations, keyboards * [Advanced Features](doc/3_advanced.md) * Access Control * Interactive Chats * Background Jobs * Inline queries * [Expert Use](doc/4_expert.md) - * Handling UTF-8 + * Handling UTF-8 character sets * Run as other user or system service * Scedule bashbot from Cron * [Best Practices](doc/5_practice.md) - * Customizing commands.sh - * Seperate Bot logic from command + * Customize commands.sh + * Seperate logic from commands * Test your Bot with shellcheck -* [Bashbot functions reference](doc/6_reference.md) +* [Bashbot function reference](doc/6_reference.md) ## Note on Keyboards -To make use of Keyboards easier the keybord format for ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."``` was changed. -Keybords are now defined in an JSON Array notation e.g. "[ \\"yes\\" , \\"no\\" ]". +From Version 0.60 on keybord format for ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."``` was changed. +Keybords are now defined in JSON Array notation e.g. "[ \\"yes\\" , \\"no\\" ]". This has the advantage that you can create any type of keyboard supported by Telegram. **This is an incompatible change for keyboards used in older bashbot versions.** *Example Keyboards*: -- yes no in one row - - OLD format: "yes" "no" (two strings) - - NEW format: "[ \\"yes\\" , \\"no\\" ]" (string containing an array) +- OLD format: 'yes' 'no' (two strings) +- NEW format: '[ "yes" , "no" ]' (string containing an array) - new keybord layouts, no possible with old format: - - Yes No in two rows: "[ \\"yes\\" ] , [ \\"no\\" ]" - - numpad style keyboard: "[ \\"1\\" , \\"2\\" , \\"3\\" ] , [ \\"4\\" , \\"5\\" , \\"6\\" ] , [ \\"7\\" , \\"8\\" , \\"9\\" ] , [ \\"0\\" ]" + - Yes No in two rows: '[ "yes" ] , [ "no" ]' + - numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' ## Security Considerations Running a Telegram Bot means it is connected to the public and 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 are bash's 'quoting hell' and globbing. [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) +Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. More concret examples of security problems are: bash's 'quoting hell' and globbing. [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) -Whenever you are processing input from from untrusted sources (messages, files, network) you must be as carefull as possible, e.g. disable globbing (set -f) and quote everthing. +Whenever you are processing input from from untrusted sources (messages, files, network) you must be as carefull as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everthing. In addition disable not used Bot commands and delete unused scripts from your Bot, e.g. example scripts 'notify', 'calc', 'question', A powerful tool to improve your scripts robustness is ```shellcheck```. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). All bashbot scripts are checked by shellcheck. ### Run your Bot as a restricted user -**It's important to run your bot as a user, with almost no access rights.** - +**I recommend to run your bot as a user, with almost no access rights.** All files your Bot have write access to are in danger to be overwritten/deleted if your bot is hacked. -For the same reason ervery file your Bot can read is in danger of being disclosed. So please restict your Bots access rigths to the absolute minimum. +For the same reason ervery file your Bot can read is in danger to be disclosed. Restict your Bots access rigths to the absolute minimum. -**Never run your Bot as root, this is the most dangerous you can do!** Usually the user 'nobody' has almost no rights on Unix/Linux systems. See Expert use on how to run your Bot as an other user. +**Never run your Bot as root, this is the most dangerous you can do!** Usually the user 'nobody' has almost no rights on Unix/Linux systems. See [Expert use](doc/4_expert.md) on how to run your Bot as an other user. ### Secure your Bot installation -**Your Bot configuration should not be readable from other users.** If someone can read your Bots token he can act as your Bot and has access to all chats you Bot is in! +**Your Bot configuration must no be readable from other users.** Everyone who can read your Bots token can act as your Bot and has access to all chats your Bot is in! -Everyone with read access to 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 must be restricted to have write access to ```count``` and ```tmp-bot-bash``` only, all other files should be write protected. +Everyone with read access to your Bot files can extract your Bots data. Especially your Bot Token in ```token``` must be protected against other users. No one exept you must have write access to the Bot files. The Bot must be restricted to have write access to ```count``` and ```tmp-bot-bash``` only, all other files must be write protected. -To set access rights for your telegram-bot-bash directory to reasonable default values you must run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. +To set access rights for your bashbot directory to a reasonable default you must run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. ### Is this Bot insecure? -Bashbot is no more (in)secure as any other Bot written in any other language. But since YOU are responsible for your bots commands and run the Bot, you should know about the implications ... +Bashbot is not more (in)secure as any other Bot written in any other language, we have done our best to make it as secure as possible. But YOU are responsible for the bot commands you wrote and you should know about the risks ... ## That's it! If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 diff --git a/README.txt b/README.txt index fba63af..cad88e9 100644 --- a/README.txt +++ b/README.txt @@ -14,8 +14,9 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. -https://github.com/topkecleon/telegram-bot-bash/releases[Download latest -release from github] +Bashbot https://github.com/topkecleon/telegram-bot-bash[Documentation] +and https://github.com/topkecleon/telegram-bot-bash/releases[Downloads] +are availible on Githup. Released to the public domain wherever applicable. Elsewhere, consider it released under the http://www.wtfpl.net/txt/copying/[WTFPLv2]. @@ -43,49 +44,48 @@ update zip from github], extract all files and copy them to your bashbot dir. Now run `sudo ./bashbot.sh init` to setup your environment for the new release. -Getting started -~~~~~~~~~~~~~~~ +Bashbot Documentation +~~~~~~~~~~~~~~~~~~~~~ -* link:doc/1_firstbot.md[Create Telegram Bot with botfather] +* link:doc/1_firstbot.md[Create a new Telegram Bot with botfather] * link:doc/2_usage.md[Getting Started] ** Managing your Bot ** Recieve data -** Send Messages -** Send files, location etc. +** Send messages +** Send files, locations, keyboards * link:doc/3_advanced.md[Advanced Features] ** Access Control ** Interactive Chats ** Background Jobs ** Inline queries * link:doc/4_expert.md[Expert Use] -** Handling UTF-8 +** Handling UTF-8 character sets ** Run as other user or system service ** Scedule bashbot from Cron * link:doc/5_practice.md[Best Practices] -** Customizing commands.sh -** Seperate Bot logic from command +** Customize commands.sh +** Seperate logic from commands ** Test your Bot with shellcheck -* link:doc/6_reference.md[Bashbot functions reference] +* link:doc/6_reference.md[Bashbot function reference] Note on Keyboards ~~~~~~~~~~~~~~~~~ -To make use of Keyboards easier the keybord format for `send_keyboard` -and `send_message "mykeyboardstartshere ..."` was changed. Keybords are -now defined in an JSON Array notation e.g. "[ \"yes\" , \"no\" ]". This -has the advantage that you can create any type of keyboard supported by +From Version 0.60 on keybord format for `send_keyboard` and +`send_message "mykeyboardstartshere ..."` was changed. Keybords are now +defined in JSON Array notation e.g. "[ \"yes\" , \"no\" ]". This has the +advantage that you can create any type of keyboard supported by Telegram. *This is an incompatible change for keyboards used in older bashbot versions.* _Example Keyboards_: -* yes no in one row -** OLD format: "yes" "no" (two strings) -** NEW format: "[ \"yes\" , \"no\" ]" (string containing an array) +* OLD format: 'yes' 'no' (two strings) +* NEW format: '[ "yes" , "no" ]' (string containing an array) * new keybord layouts, no possible with old format: -** Yes No in two rows: "[ \"yes\" ] , [ \"no\" ]" -** numpad style keyboard: "[ \"1\" , \"2\" , \"3\" ] , [ \"4\" , \"5\" , -\"6\" ] , [ \"7\" , \"8\" , \"9\" ] , [ \"0\" ]" +** Yes No in two rows: '[ "yes" ] , [ "no" ]' +** numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ +"7" , "8" , "9" ] , [ "0" ]' Security Considerations ~~~~~~~~~~~~~~~~~~~~~~~ @@ -95,13 +95,15 @@ 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 are bash's 'quoting hell' and globbing. +problems are: 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 from untrusted sources (messages, -files, network) you must be as carefull as possible, e.g. disable -globbing (set -f) and quote everthing. +files, network) you must be as carefull as possible, e.g. set IFS +appropriate, disable globbing (set -f) and quote everthing. In addition +disable not used Bot commands and delete unused scripts from your Bot, +e.g. example scripts 'notify', 'calc', 'question', A powerful tool to improve your scripts robustness is `shellcheck`. You can https://www.shellcheck.net/[use it online] or @@ -111,41 +113,41 @@ locally]. All bashbot scripts are checked by shellcheck. Run your Bot as a restricted user ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -*It's important to run your bot as a user, with almost no access -rights.* - +*I recommend to run your bot as a user, with almost no access rights.* All files your Bot have write access to are in danger to be overwritten/deleted if your bot is hacked. For the same reason ervery -file your Bot can read is in danger of being disclosed. So please -restict your Bots access rigths to the absolute minimum. +file your Bot can read is in danger to be disclosed. Restict your Bots +access rigths to the absolute minimum. *Never run your Bot as root, this is the most dangerous you can do!* Usually the user 'nobody' has almost no rights on Unix/Linux systems. -See Expert use on how to run your Bot as an other user. +See link:doc/4_expert.md[Expert use] on how to run your Bot as an other +user. Secure your Bot installation ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -*Your Bot configuration should not be readable from other users.* If -someone can read your Bots token he can act as your Bot and has access -to all chats you Bot is in! +*Your Bot configuration must no be readable from other users.* Everyone +who can read your Bots token can act as your Bot and has access to all +chats your Bot is in! Everyone with read access to 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 must be restricted to have write access to `count` and -`tmp-bot-bash` only, all other files should be write protected. +users. No one exept you must have write access to the Bot files. The Bot +must be restricted to have write access to `count` and `tmp-bot-bash` +only, all other files must be write protected. -To set access rights for your telegram-bot-bash directory to reasonable -default values you must run `sudo ./bashbot.sh init` after every update -or change to your installation directory. +To set access rights for your bashbot directory to a reasonable default +you must run `sudo ./bashbot.sh init` after every update or change to +your installation directory. Is this Bot insecure? ^^^^^^^^^^^^^^^^^^^^^ -Bashbot is no more (in)secure as any other Bot written in any other -language. But since YOU are responsible for your bots commands and run -the Bot, you should know about the implications ... +Bashbot is not more (in)secure as any other Bot written in any other +language, we have done our best to make it as secure as possible. But +YOU are responsible for the bot commands you wrote and you should know +about the risks ... That's it! ~~~~~~~~~~ @@ -153,5 +155,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.60-rc2-4-g1bf26b9 +latexmath:[\[VERSION\]] v0.60-rc2-5-g591c583 ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index 630316d..bffec6d 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.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 9f9f9e8..0da76e8 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index dba5e46..fdc7123 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.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/calc b/calc index bfa0358..e4cb9d0 100755 --- a/calc +++ b/calc @@ -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.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/commands.sh b/commands.sh index 0dbbd26..7d4c402 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.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 34061ec..004132f 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -1,4 +1,4 @@ - +#### [Home](../README.md) ## Create a Telegram Bot with botfather 1. Message @botfather https://telegram.me/botfather with the following @@ -61,5 +61,5 @@ group. This step is up to you actually. #### [Next Getting started](2_usage.md) -#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 diff --git a/doc/2_usage.md b/doc/2_usage.md index c18dded..23250f6 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -1,3 +1,4 @@ +#### [Home](../README.md) ## Gettting Started 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. @@ -131,14 +132,15 @@ send_message "${CHAT[ID]}" "lol" "safe" More examples boutsend_message strings can be found in [Advanced Usage](3_advanced.md#Interactive-Chats) -#### Send files, location etc. +#### Send files, locations, keyboards. To send images, videos, voice files, photos etc. use the ```send_photo``` function (remember to change the safety Regex @ line 14 of command.sh to allow sending files only from certain directories): ```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\" ]" +send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" '[ "Yep" , "No" ]' # note the simgle quotes! +send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" "[ \\"Yep\\" , \\"No\\" ]" # within double quotes you must excape the inside double quots ``` To send locations use the ```send_location``` function: ```bash @@ -154,8 +156,8 @@ Allowed values: typing for text messages, upload_photo for photos, record_video send_action "${CHAT[ID]}" "action" ``` -#### [Prev Getting started](2_usage.md) +#### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 878a284..be7de82 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -1,4 +1,4 @@ - +#### [Home](../README.md) ## Advanced Features ### Access control @@ -153,8 +153,8 @@ To send stickers through an *inline query*: ```bash answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" ``` -#### [prev Advanced Usage](3_advanced.md) +#### [Prev Advanced Usage](3_advanced.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 diff --git a/doc/4_expert.md b/doc/4_expert.md index c6a3da3..a7786f5 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -1,3 +1,4 @@ +#### [Home](../README.md) ## Expert Use ### Handling UTF-8 character sets @@ -103,5 +104,5 @@ An example crontab is provided in ```bashbot.cron```. #### [Prev Expert Use](4_expert.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 diff --git a/doc/5_practice.md b/doc/5_practice.md index 1043ebb..56bb5af 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -1,3 +1,4 @@ +#### [Home](../README.md) ## Best Practices ### Customize commands.sh only @@ -111,5 +112,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 diff --git a/doc/6_reference.md b/doc/6_reference.md index 8c58c66..c596ff2 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -1,3 +1,4 @@ +#### [Home](../README.md) ## Bashbot function reference ### Send, forward, delete messages @@ -122,12 +123,11 @@ send_file "${CHAT[ID]}" "https://www.domain,com/something.gif" "Something" ##### send_keyboard Note: since version 0.6 send_keyboard was changed to use native "JSON Array" notation as used from Telegram. Example Keybord Array definitions: -- yes no in one row - - OLD format: "yes" "no" (two strings) - - NEW format: "[ \\"yes\\" , \\"no\\" ]" (string containing an array) +- OLD format: 'yes' 'no' (two strings) +- NEW format: '[ "yes" , "no" ]' (string containing an array) - new keybord layouts, no possible with old format: - - Yes No in two rows: "[ \\"yes\\" ] , [ \\"no\\" ]" - - numpad style keyboard: "[ \\"1\\" , \\"2\\" , \\"3\\" ] , [ \\"4\\" , \\"5\\" , \\"6\\" ] , [ \\"7\\" , \\"8\\" , \\"9\\" ] , [ \\"0\\" ]" + - Yes No in two rows: '[ "yes" ] , [ "no" ]' + - numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' *usage:* send_keyboard "chat-id" "keyboard" @@ -348,5 +348,5 @@ Send Input from Telegram to waiting Interactive Chat. #### [Prev Best Practice](5_practice.md) -#### $$VERSION$$ v0.60-rc2-4-g1bf26b9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 diff --git a/notify b/notify index 2bcc0f9..09b392e 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.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index b4fbb67..d4d3fb0 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.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -19,7 +19,7 @@ Would you like some tea (y/n)?" 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"' + echo 'Do you like Music? mykeyboardstartshere "Yass!" , "No"' read -r answer case $answer in 'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;; diff --git a/version b/version index 540199a..ea4636d 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.60-rc2-3-g4a944d9 +#### $$VERSION$$ v0.60-rc2-5-g591c583 # shellcheck disable=SC2016 # # Easy Versioning in git: From 19a0f7eac3c059006dd52cd402841de9c8876d51 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 16 Apr 2019 21:56:46 +0200 Subject: [PATCH 34/36] add backward compatibility for keyboards from interactive chats --- README.md | 21 +++++++++++---------- README.txt | 36 ++++++++++++++++++++---------------- bashbot.sh | 7 +++++-- question | 2 +- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 29c1c3d..9e003ba 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,20 @@ # bashbot A Telegram bot written in bash. -Depends on [tmux](http://github.com/tmux/tmux). -Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). - -For full UTF-8 support you need [python on your system](doc/4_expert.md#UTF-8-Support) (optional). - Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadelwartz). Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. -Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Downloads](https://github.com/topkecleon/telegram-bot-bash/releases) are availible on Githup. - Released to the public domain wherever applicable. Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). +## Prerequsites +Depends on [tmux](http://github.com/tmux/tmux). +Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). +For full UTF-8 support you need [python on your system](doc/4_expert.md#UTF-8-Support) (optional). + +Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Downloads](https://github.com/topkecleon/telegram-bot-bash/releases) are availible on www.github.com ## Install bashbot 1. Go to the directory you want to install bashbot, e.g. @@ -28,7 +27,9 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/cop 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. ## Update bashbot -[Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases), extract all files and copy them to your bashbot dir. Now run ```sudo ./bashbot.sh init``` to setup your environment for the new release. +1. [Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases) +2. Extract all files and copy them to your bashbot dir +3. Run ```sudo ./bashbot.sh init``` to setup your environment after the update- ## Bashbot Documentation * [Create a new Telegram Bot with botfather](doc/1_firstbot.md) @@ -87,7 +88,7 @@ For the same reason ervery file your Bot can read is in danger to be disclosed. Everyone with read access to your Bot files can extract your Bots data. Especially your Bot Token in ```token``` must be protected against other users. No one exept you must have write access to the Bot files. The Bot must be restricted to have write access to ```count``` and ```tmp-bot-bash``` only, all other files must be write protected. -To set access rights for your bashbot directory to a reasonable default you must run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. +To set access rights for your bashbot installation to a reasonable default run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. ### Is this Bot insecure? Bashbot is not more (in)secure as any other Bot written in any other language, we have done our best to make it as secure as possible. But YOU are responsible for the bot commands you wrote and you should know about the risks ... @@ -96,4 +97,4 @@ Bashbot is not more (in)secure as any other Bot written in any other language, w If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc2-6-g2e3c975 diff --git a/README.txt b/README.txt index cad88e9..2366bdd 100644 --- a/README.txt +++ b/README.txt @@ -3,23 +3,26 @@ bashbot A Telegram bot written in bash. +Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M +(@gnadelwartz). + +Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. + +Released to the public domain wherever applicable. Elsewhere, consider +it released under the http://www.wtfpl.net/txt/copying/[WTFPLv2]. + +Prerequsites +~~~~~~~~~~~~ + Depends on http://github.com/tmux/tmux[tmux]. Uses http://github.com/dominictarr/JSON.sh[JSON.sh]. For full UTF-8 support you need link:doc/4_expert.md#UTF-8-Support[python on your system] (optional). -Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M -(@gnadelwartz). - -Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. - Bashbot https://github.com/topkecleon/telegram-bot-bash[Documentation] and https://github.com/topkecleon/telegram-bot-bash/releases[Downloads] -are availible on Githup. - -Released to the public domain wherever applicable. Elsewhere, consider -it released under the http://www.wtfpl.net/txt/copying/[WTFPLv2]. +are availible on www.github.com Install bashbot ~~~~~~~~~~~~~~~ @@ -39,10 +42,11 @@ given by botfather. Update bashbot ~~~~~~~~~~~~~~ -https://github.com/topkecleon/telegram-bot-bash/releases[Download latest -update zip from github], extract all files and copy them to your bashbot -dir. Now run `sudo ./bashbot.sh init` to setup your environment for the -new release. +1. https://github.com/topkecleon/telegram-bot-bash/releases[Download +latest update zip from github] +2. Extract all files and copy them to your bashbot dir +3. Run `sudo ./bashbot.sh init` to setup your environment after the +update- Bashbot Documentation ~~~~~~~~~~~~~~~~~~~~~ @@ -137,8 +141,8 @@ users. No one exept you must have write access to the Bot files. The Bot must be restricted to have write access to `count` and `tmp-bot-bash` only, all other files must be write protected. -To set access rights for your bashbot directory to a reasonable default -you must run `sudo ./bashbot.sh init` after every update or change to +To set access rights for your bashbot installation to a reasonable +default run `sudo ./bashbot.sh init` after every update or change to your installation directory. Is this Bot insecure? @@ -155,5 +159,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.60-rc2-5-g591c583 +latexmath:[\[VERSION\]] v0.60-rc2-6-g2e3c975 ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.sh b/bashbot.sh index fdc7123..c3fcc5a 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.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc2-6-g2e3c975 # # Exit Codes: # - 0 sucess (hopefully) @@ -176,11 +176,14 @@ send_message() { sent=y fi if [ "$keyboard" != "" ]; then + if [[ "$keyboard" != *"["* ]]; then # pre 0.60 style + keyboard="[ ${keyboard//\" \"/\" , \"} ]" + fi send_keyboard "$chat" "$text" "$keyboard" sent=y fi if [ "$file" != "" ]; then - send_file "$chat" "$file" "" + send_file "$chat" "$file" "$text" sent=y fi if [ "$lat" != "" ] && [ "$long" != "" ] && [ "$address" = "" ] && [ "$title" = "" ]; then diff --git a/question b/question index d4d3fb0..2c969a9 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.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc2-6-g2e3c975 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment From f5162e26164fd6e9ebcc96ebc27d1998cc440141 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Apr 2019 09:34:02 +0200 Subject: [PATCH 35/36] Version 0.60-rc3 --- README.md | 14 +++++++------- README.txt | 20 ++++++++++---------- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 19 +++++++++++++++++-- calc | 2 +- commands.sh | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 28 +++++++++++++--------------- notify | 2 +- question | 2 +- version | 2 +- 16 files changed, 59 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 9e003ba..01d4187 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. Released to the public domain wherever applicable. Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). -## Prerequsites +## Prerequisites Depends on [tmux](http://github.com/tmux/tmux). Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). @@ -24,14 +24,14 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do ``` 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. +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. ## Update bashbot 1. [Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases) 2. Extract all files and copy them to your bashbot dir -3. Run ```sudo ./bashbot.sh init``` to setup your environment after the update- +3. Run ```sudo ./bashbot.sh init``` to setup your environment after the update -## Bashbot Documentation +## Documentation * [Create a new Telegram Bot with botfather](doc/1_firstbot.md) * [Getting Started](doc/2_usage.md) * Managing your Bot @@ -57,13 +57,13 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do From Version 0.60 on keybord format for ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."``` was changed. Keybords are now defined in JSON Array notation e.g. "[ \\"yes\\" , \\"no\\" ]". This has the advantage that you can create any type of keyboard supported by Telegram. -**This is an incompatible change for keyboards used in older bashbot versions.** +The old format is supported for backward compatibility, but may fail for strange corner cases. *Example Keyboards*: - OLD format: 'yes' 'no' (two strings) - NEW format: '[ "yes" , "no" ]' (string containing an array) -- new keybord layouts, no possible with old format: +- new keybord layouts, not possible with old format: - Yes No in two rows: '[ "yes" ] , [ "no" ]' - numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' @@ -97,4 +97,4 @@ Bashbot is not more (in)secure as any other Bot written in any other language, w If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.60-rc2-6-g2e3c975 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e diff --git a/README.txt b/README.txt index 2366bdd..1dc7eaa 100644 --- a/README.txt +++ b/README.txt @@ -11,8 +11,8 @@ Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. Released to the public domain wherever applicable. Elsewhere, consider it released under the http://www.wtfpl.net/txt/copying/[WTFPLv2]. -Prerequsites -~~~~~~~~~~~~ +Prerequisites +~~~~~~~~~~~~~ Depends on http://github.com/tmux/tmux[tmux]. Uses http://github.com/dominictarr/JSON.sh[JSON.sh]. @@ -35,7 +35,7 @@ Install bashbot .... git clone --recursive https://github.com/topkecleon/telegram-bot-bash .... -3. Change to directory `telegram-bot.bash`, run `./bashbot.sh init` and +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. @@ -46,10 +46,10 @@ Update bashbot latest update zip from github] 2. Extract all files and copy them to your bashbot dir 3. Run `sudo ./bashbot.sh init` to setup your environment after the -update- +update -Bashbot Documentation -~~~~~~~~~~~~~~~~~~~~~ +Documentation +~~~~~~~~~~~~~ * link:doc/1_firstbot.md[Create a new Telegram Bot with botfather] * link:doc/2_usage.md[Getting Started] @@ -79,14 +79,14 @@ From Version 0.60 on keybord format for `send_keyboard` and `send_message "mykeyboardstartshere ..."` was changed. Keybords are now defined in JSON Array notation e.g. "[ \"yes\" , \"no\" ]". This has the advantage that you can create any type of keyboard supported by -Telegram. *This is an incompatible change for keyboards used in older -bashbot versions.* +Telegram. The old format is supported for backward compatibility, but +may fail for strange corner cases. _Example Keyboards_: * OLD format: 'yes' 'no' (two strings) * NEW format: '[ "yes" , "no" ]' (string containing an array) -* new keybord layouts, no possible with old format: +* new keybord layouts, not possible with old format: ** Yes No in two rows: '[ "yes" ] , [ "no" ]' ** numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' @@ -159,5 +159,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.60-rc2-6-g2e3c975 +latexmath:[\[VERSION\]] v0.60-rc3-0-g19a0f7e ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index bffec6d..502c7b6 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.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 0da76e8..5361ac4 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index c3fcc5a..b84c82f 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.60-rc2-6-g2e3c975 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e # # Exit Codes: # - 0 sucess (hopefully) @@ -177,7 +177,7 @@ send_message() { fi if [ "$keyboard" != "" ]; then if [[ "$keyboard" != *"["* ]]; then # pre 0.60 style - keyboard="[ ${keyboard//\" \"/\" , \"} ]" + keyboard="[ ${keyboard//\" \"/\" \] , \[ \"} ]" fi send_keyboard "$chat" "$text" "$keyboard" sent=y @@ -357,7 +357,22 @@ answer_inline_query() { } + +old_send_keyboard() { + local chat="$1" + local text="$2" + shift 2 + local keyboard=init + OLDIFS=$IFS + IFS=$(echo -en "\"") + for f in "$@" ;do [ "$f" != " " ] && keyboard="$keyboard, [\"$f\"]";done + IFS=$OLDIFS + 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}")" +} + send_keyboard() { + if [[ "$3" != *'['* ]]; then old_send_keyboard "$@"; return; fi local chat="$1" local text="$2" local keyboard="$3" diff --git a/calc b/calc index e4cb9d0..c5e9135 100755 --- a/calc +++ b/calc @@ -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.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/commands.sh b/commands.sh index 7d4c402..c70a39d 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.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 004132f..7c92642 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -61,5 +61,5 @@ group. This step is up to you actually. #### [Next Getting started](2_usage.md) -#### $$VERSION$$ v0.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e diff --git a/doc/2_usage.md b/doc/2_usage.md index 23250f6..e04b367 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -159,5 +159,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e diff --git a/doc/3_advanced.md b/doc/3_advanced.md index be7de82..6bc50bc 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -156,5 +156,5 @@ answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" #### [Prev Advanced Usage](3_advanced.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e diff --git a/doc/4_expert.md b/doc/4_expert.md index a7786f5..26d7b74 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -104,5 +104,5 @@ An example crontab is provided in ```bashbot.cron```. #### [Prev Expert Use](4_expert.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e diff --git a/doc/5_practice.md b/doc/5_practice.md index 56bb5af..6b640ab 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ v0.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e diff --git a/doc/6_reference.md b/doc/6_reference.md index c596ff2..a84ec9d 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -90,14 +90,8 @@ answer_inline_query provide the result to a users Inline Query ---- -### File, Location, Venu, keyboards +### File, Location, Venue, Keyboard -##### get_file -*usage:* - -*example:* -```bash -``` ##### send_file send_file allows you to send different type's of files, e.g. photos, stickers, audio, media, etc. [see more](https://core.telegram.org/bots/api#sending-files) @@ -125,23 +119,22 @@ Note: since version 0.6 send_keyboard was changed to use native "JSON Array" not - OLD format: 'yes' 'no' (two strings) - NEW format: '[ "yes" , "no" ]' (string containing an array) -- new keybord layouts, no possible with old format: +- new keybord layouts, not possible with old format: - Yes No in two rows: '[ "yes" ] , [ "no" ]' - numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' -*usage:* send_keyboard "chat-id" "keyboard" +*usage:* send_keyboard "chat-id" "message" "keyboard" *example:* ```bash -send_keyboard "${CHAT[ID]}" "[ \\"yes\" , \\"no\" ]"" -send_keyboard "${CHAT[ID]}" "[ \\"yes\\" ] , [ \\"no\\" ]" -send_keyboard "${CHAT[ID]}" "[ \\"1\\" , \\"2\\" , \\"3\\" ] , [ \\"4\\" , \\"5\\" , \\"6\\" ] , [ \\"7\\" , \\"8\\" , \\"9\\" ] , [ \\"0\\" ]" +send_keyboard "${CHAT[ID]}" "Say yes or no" "[ \\"yes\" , \\"no\" ]"" +send_keyboard "${CHAT[ID]}" "Say yes or no" "[ \\"yes\\" ] , [ \\"no\\" ]" +send_keyboard "${CHAT[ID]}" "Enter digit" "[ \\"1\\" , \\"2\\" , \\"3\\" ] , [ \\"4\\" , \\"5\\" , \\"6\\" ] , [ \\"7\\" , \\"8\\" , \\"9\\" ] , [ \\"0\\" ]" ``` ##### remove_keyboard -*usage:* - +*usage:* remove_keybord "$CHAT[ID]" "message" ### Manage users @@ -304,6 +297,11 @@ fi ### Bashbot internal functions These functions are for internal use only and must not used in your bot commands. +##### get_file +*usage:* url="$(get_file "${CHAT[ID]}" "message")" + +---- + ##### send_text *usage:* send_text "${CHAT[ID]}" "message" @@ -348,5 +346,5 @@ Send Input from Telegram to waiting Interactive Chat. #### [Prev Best Practice](5_practice.md) -#### $$VERSION$$ v0.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e diff --git a/notify b/notify index 09b392e..c2010e5 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.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 2c969a9..5b51f26 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.60-rc2-6-g2e3c975 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index ea4636d..bd6b524 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.60-rc2-5-g591c583 +#### $$VERSION$$ v0.60-rc3-0-g19a0f7e # shellcheck disable=SC2016 # # Easy Versioning in git: From 209c4b34f417cf694880cf19249bea660f4085b4 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Apr 2019 10:35:18 +0200 Subject: [PATCH 36/36] Version 0.60 of bashbot --- README.md | 16 +++++++++------- README.txt | 16 +++++++++------- bashbot.cron | 2 +- bashbot.rc | 2 +- bashbot.sh | 2 +- calc | 2 +- commands.sh | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 8 ++++---- doc/5_practice.md | 2 +- doc/6_reference.md | 13 +++++++------ notify | 2 +- question | 2 +- version | 2 +- 16 files changed, 41 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 01d4187..52a856b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/cop Depends on [tmux](http://github.com/tmux/tmux). Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). -For full UTF-8 support you need [python on your system](doc/4_expert.md#UTF-8-Support) (optional). +Most complete [UTF-8 support for bashbot](doc/4_expert.md#Bashbot-UTF-8-Support) is availible if phyton is installed (optional). Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Downloads](https://github.com/topkecleon/telegram-bot-bash/releases) are availible on www.github.com @@ -57,14 +57,16 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do From Version 0.60 on keybord format for ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."``` was changed. Keybords are now defined in JSON Array notation e.g. "[ \\"yes\\" , \\"no\\" ]". This has the advantage that you can create any type of keyboard supported by Telegram. -The old format is supported for backward compatibility, but may fail for strange corner cases. +The old format is supported for backward compatibility, but may fail for corner cases. *Example Keyboards*: -- OLD format: 'yes' 'no' (two strings) -- NEW format: '[ "yes" , "no" ]' (string containing an array) -- new keybord layouts, not possible with old format: - - Yes No in two rows: '[ "yes" ] , [ "no" ]' +- yes no in two rows: + - OLD format: 'yes' 'no' (two strings) + - NEW format: '[ "yes" ] , [ "no" ]' (two arrays with a string) +- new layouts made easy with NEW format: + - Yes No in one row: '[ "yes" , "no" ]' + - Yes No plus Maybe in 2.row: '[ "yes" , "no" ] , [ "maybe" ]' - numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' ## Security Considerations @@ -97,4 +99,4 @@ Bashbot is not more (in)secure as any other Bot written in any other language, w If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 diff --git a/README.txt b/README.txt index 1dc7eaa..8efb506 100644 --- a/README.txt +++ b/README.txt @@ -80,14 +80,16 @@ From Version 0.60 on keybord format for `send_keyboard` and defined in JSON Array notation e.g. "[ \"yes\" , \"no\" ]". This has the advantage that you can create any type of keyboard supported by Telegram. The old format is supported for backward compatibility, but -may fail for strange corner cases. +may fail for corner cases. _Example Keyboards_: -* OLD format: 'yes' 'no' (two strings) -* NEW format: '[ "yes" , "no" ]' (string containing an array) -* new keybord layouts, not possible with old format: -** Yes No in two rows: '[ "yes" ] , [ "no" ]' +* yes no in two rows: +** OLD format: 'yes' 'no' (two strings) +** NEW format: '[ "yes" ] , [ "no" ]' (two arrays with a string) +* new layouts made easy with NEW format: +** Yes No in one row: '[ "yes" , "no" ]' +** Yes No plus Maybe in 2.row: '[ "yes" , "no" ] , [ "maybe" ]' ** numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' @@ -159,5 +161,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.60-rc3-0-g19a0f7e -++++++++++++++++++++++++++++++++++++++++++++ +latexmath:[\[VERSION\]] v0.60-0-gf5162e2 +++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.cron b/bashbot.cron index 502c7b6..e5aa77e 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.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 SHELL=/bin/sh diff --git a/bashbot.rc b/bashbot.rc index 5361ac4..58deb15 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index b84c82f..8376553 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.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/calc b/calc index c5e9135..b35eb40 100755 --- a/calc +++ b/calc @@ -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.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/commands.sh b/commands.sh index c70a39d..7c3f0fa 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.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 7c92642..4fe1822 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -61,5 +61,5 @@ group. This step is up to you actually. #### [Next Getting started](2_usage.md) -#### $$VERSION$$ v0.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 diff --git a/doc/2_usage.md b/doc/2_usage.md index e04b367..cfd993d 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -159,5 +159,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 6bc50bc..48155e8 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -156,5 +156,5 @@ answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" #### [Prev Advanced Usage](3_advanced.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 diff --git a/doc/4_expert.md b/doc/4_expert.md index 26d7b74..67391da 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -37,7 +37,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) -#### Bashbot's UTF-8 Support +#### Bashbot UTF-8 Support Bashbot handles all messages transparently, regardless of the charset in use. The only exception is when converting from JSON data to strings. Telegram use JSON to send / recieve data. JSON encodes strings as follow: Characters not ASCII *(>127)* are escaped as sequences of ```\uxxxx``` to be regular ASCII. In addition multibyte characters, *e.g. Emoticons or Arabic characters*, are send in double byte UTF-16 notation. @@ -45,8 +45,8 @@ The Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE0 **This "mixed" JSON encoding needs special handling and can not decoded from** ```echo -e``` or ```printf '%s\\n'``` -To to fully support decoding of multibyte characters you need a working python2 installation on your system. -If no python is detected bashbot falls back to a **internal pure bash implementaion which may not work for some corner cases**. +Most complete support for decoding of multibyte characters can only be provided if python is installed on your system. +**Without phyton bashbot falls back to an internal, pure bash implementation which may not work for some corner cases**. ### Run as other user or system service @@ -104,5 +104,5 @@ An example crontab is provided in ```bashbot.cron```. #### [Prev Expert Use](4_expert.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 diff --git a/doc/5_practice.md b/doc/5_practice.md index 6b640ab..b24b577 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ v0.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 diff --git a/doc/6_reference.md b/doc/6_reference.md index a84ec9d..5419742 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -117,13 +117,14 @@ send_file "${CHAT[ID]}" "https://www.domain,com/something.gif" "Something" ##### send_keyboard Note: since version 0.6 send_keyboard was changed to use native "JSON Array" notation as used from Telegram. Example Keybord Array definitions: -- OLD format: 'yes' 'no' (two strings) -- NEW format: '[ "yes" , "no" ]' (string containing an array) -- new keybord layouts, not possible with old format: - - Yes No in two rows: '[ "yes" ] , [ "no" ]' +- yes no in two rows: + - OLD format: 'yes' 'no' (two strings) + - NEW format: '[ "yes" ] , [ "no" ]' (two arrays with a string) +- new layouts made easy with NEW format: + - Yes No in one row: '[ "yes" , "no" ]' + - Yes No plus Maybe in 2.row: '[ "yes" , "no" ] , [ "maybe" ]' - numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' - *usage:* send_keyboard "chat-id" "message" "keyboard" *example:* @@ -346,5 +347,5 @@ Send Input from Telegram to waiting Interactive Chat. #### [Prev Best Practice](5_practice.md) -#### $$VERSION$$ v0.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 diff --git a/notify b/notify index c2010e5..56da2ac 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.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/question b/question index 5b51f26..c1dc69b 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.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/version b/version index bd6b524..bad3f2f 100755 --- a/version +++ b/version @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.60-rc3-0-g19a0f7e +#### $$VERSION$$ v0.60-0-gf5162e2 # shellcheck disable=SC2016 # # Easy Versioning in git: