From df03727e1403b098c604cf894f8841a4d0536adf Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 14 May 2019 13:16:58 +0200 Subject: [PATCH] some fixes for updates from older versions --- bashbot.sh | 23 +++++++++++++---------- commands.sh | 14 +------------- doc/0_install.md | 11 +++++++++-- doc/7_develop.md | 30 +++++++++++++++--------------- modules/inline.sh | 2 -- modules/sendMessage.sh | 5 ++--- mycommands.sh | 4 ++-- 7 files changed, 42 insertions(+), 47 deletions(-) delete mode 100644 modules/inline.sh diff --git a/bashbot.sh b/bashbot.sh index 3937a3d..18f6c39 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-dev2-9-ga79f97f +#### $$VERSION$$ v0.80-dev2-11-gb55c171 # # Exit Codes: # - 0 sucess (hopefully) @@ -119,7 +119,7 @@ elif [ ! -w "${COUNTFILE}" ]; then fi -BOTTOKEN="$(cat "${TOKENFILE}")" +BOTTOKEN="$(< "${TOKENFILE}")" URL='https://api.telegram.org/bot'$BOTTOKEN ME_URL=$URL'/getMe' @@ -129,7 +129,7 @@ GETFILE_URL=$URL'/getFile' unset USER declare -A BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY -export res BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY +export res BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY CAPTION NAME COMMANDS="${BASHBOT_ETC:-.}/commands.sh" if [ "$1" != "source" ]; then @@ -314,13 +314,12 @@ start_bot() { [[ "${DEBUG}" = *"debug" ]] && exec &>>"DEBUG.log" [ "${DEBUG}" != "" ] && date && echo "Start BASHBOT in Mode \"${DEBUG}\"" [[ "${DEBUG}" = "xdebug"* ]] && set -x - while true; do { - + while true; do UPDATE="$(curl -s "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n)" # Offset OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" - OFFSET=$((OFFSET+1)) + ((OFFSET++)) if [ "$OFFSET" != "1" ]; then mysleep="100" @@ -328,16 +327,18 @@ start_bot() { fi # adaptive sleep in ms rounded to next lower second [ "${mysleep}" -gt "999" ] && sleep "${mysleep%???}" - mysleep=$((mysleep+addsleep)); [ "${mysleep}" -gt "${maxsleep}" ] && mysleep="${maxsleep}" - } + # bash aritmetic + ((mysleep+= addsleep , mysleep= mysleep>maxsleep ?maxsleep:mysleep)) done } # initialize bot environment, user and permissions bot_init() { - # move tmpdir to datadir + # upgrade from old version local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash" [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${TMPDIR}"; rmdir "${OLDTMP}"; } + [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" + #setup bashbot [[ "$(id -u)" -eq "0" ]] && RUNUSER="nobody" echo -n "Enter User to run basbot [$RUNUSER]: " read -r TOUSER @@ -365,7 +366,7 @@ getBotName() { ME="$(getBotName)" if [ "$ME" = "" ]; then - if [ "$(cat "${TOKENFILE}")" = "bashbottestscript" ]; then + if [ "$(< "${TOKENFILE}")" = "bashbottestscript" ]; then ME="bashbottestscript" else echo -e "${RED}ERROR: Can't connect to Telegram Bot! May be your TOKEN is invalid ...${NC}" @@ -407,6 +408,8 @@ if [ "$1" != "source" ]; then # internal options only for use from bashbot and developers case "$1" in "outproc") # forward output from interactive and jobs to chat + [ "$3" = "" ] && echo "No file to read from" && exit 3 + [ "$2" = "" ] && echo "No chat to send to" && exit 3 until [ "$line" = "imprettydarnsuredatdisisdaendofdacmd" ];do line="" read -r -t 10 line diff --git a/commands.sh b/commands.sh index 0de1afe..79469d2 100644 --- a/commands.sh +++ b/commands.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-dev2-7-g92f022d +#### $$VERSION$$ v0.80-dev2-11-gb55c171 # # adjust your language setting here, e.g.when run from other user or cron. @@ -56,18 +56,6 @@ export FILE_REGEX='/home/user/allowed/.*' if [ "${1}" != "source" ];then - # this was here from beginning, can some tell me what this is for? - # I GUESS it was used to auto send files and locations?? - # this will be removed!!! - if ! tmux ls 2>/dev/null | grep -v send | grep -q "$copname"; then - [ ! -z "${URLS[*]}" ] && { - curl -s "${URLS[*]}" -o "$NAME" - send_file "${CHAT[ID]}" "$NAME" "$CAPTION" - rm -f "$NAME" - } - [ ! -z "${LOCATION[*]}" ] && send_location "${CHAT[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}" - fi - # detect inline commands.... # no default commands, all processing is done in myinlines() if [ "$INLINE" != "0" ] && [ "${iQUERY[ID]}" != "" ]; then diff --git a/doc/0_install.md b/doc/0_install.md index eb65418..96c557c 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -34,13 +34,20 @@ As an alternative to download the zip files, you can clone the github repository 2. [Download latest release zip from github](https://github.com/topkecleon/telegram-bot-bash/releases) 3. Extract all files to your existing bashbot dir **Note: all files execpt 'mycommands.sh' and 'commands.sh' may overwritten!** -4. Run ```sudo ./bashbot.sh init``` to setup your environment after the update +4. Save your your current 'commands.sh' and run ```cp commands.sh.dist commands.sh``` +5. Run ```sudo ./bashbot.sh init``` to setup your environment after the update + +If you modified 'commands.sh' re apply all changes to the new 'commands.sh'. To avoid this all your modifications +must be done in 'mycommands.sh' only. ### Notes on Updates #### Location of tmp / data dir From version 0.70 on the tmp dir is renamed to 'data-bot-bash' to reflect the fact that not only temporary files are stored. an existing 'tmp-bot-bash' will be automatically renamed after update. +From version 0.60 on your commands must be placed in 'mycommands.sh'. If you update from a version with your commands +in 'commands.sh' move all your commands and functions to 'mycommands.sh'. + From version 0.50 on the temporary files are no more placed in '/tmp'. instead a dedicated tmp dir is used. #### Changes to send_keyboard in v0.6 @@ -63,5 +70,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v0.80-dev2-1-g0b36bc5 +#### $$VERSION$$ v0.80-dev2-11-gb55c171 diff --git a/doc/7_develop.md b/doc/7_develop.md index 7fca705..a68f4e5 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -53,24 +53,24 @@ A typical bashbot develop loop looks as follow: ### bash only commands We state bashbot is a bash only bot, but this is not true. bashbot is a bash script using bash features PLUS external commands. -Usually bash is used unix/linux environment where many external (GNU) commands are availible, but if one command is missing, bashbot may not work. +Usually bash is used in a unix/linux environment where many (GNU) commands are availible, but if commands are missing, bashbot may not work. -To avoid this and make bashbot working on as many platforms as psossible - from embedded linux to mainframes - I recommed to restrict -yourself to the commandset and features provided by bash and busybox/toybox. -See [Bash Builtins](https://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html) less as you migth expect and +To avoid this and make bashbot working on as many platforms as possible - from embedded linux to mainframe - I recommed to restrict +ourself to the commands provided by bash and busybox/toybox. +See [Bash Builtins](https://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html) and [Busybox Builtins](https://busybox.net/downloads/BusyBox.html) -example list of mumimum availible commandsi for bash scripts. Do you find curl on the list? +mumimum availible commands in bash and busybox. Do you find curl on the list? ```bash - .\*, [\*, [[\*, basename, break, builtin\*, bzcat, caller\*, cat, cd\*, chattr, - chgrp, chmod, chown, clear, command\*, continue \*, cp, cut, date, declare\*, - dc, dd, df, diff, dirname, du, echo\*, ed, eval\*, exec\*, exit \*, expr\*, find, - fuser, getopt\*, grep, hash\*, head, hexdump, id, kill, killall, last, length, - less, let\*, ln, local\*, logname, ls, lsattr, lsmod, man, mapfile\*, md5sum, mkdir, - mkfifo, mknod, more, mv, nice, nohup, passwd, patch, printf\*, ps, pwd\*, read\*, - readarray\*, readonly\* return\*, rm, rmdir, sed, seq, sha1sum, sha256sum, sha512sum, - shift\*, sleep, source\*, sort, split, stat, strings, su, sync, tail, tar, tee, test, - time, times\*, timeout, touch, tr, trap\*, true, umask\*, unix2dos, usleep, uudecode, + .*, [*, [[*, basename, break, builtin*, bzcat, caller*, cat, cd*, chattr, + chgrp, chmod, chown, clear, command*, continue *, cp, cut, date, declare*, + dc, dd, df, diff, dirname, du, echo*, ed, eval*, exec*, exit *, expr*, find, + fuser, getopt*, grep, hash*, head, hexdump, id, kill, killall, last, length, + less, let*, ln, local*, logname, ls, lsattr, lsmod, man, mapfile*, md5sum, mkdir, + mkfifo, mknod, more, mv, nice, nohup, passwd, patch, printf*, ps, pwd*, read*, + readarray*, readonly* return*, rm, rmdir, sed, seq, sha1sum, sha256sum, sha512sum, + shift*, sleep, source*, sort, split, stat, strings, su, sync, tail, tar, tee, test, + time, times*, timeout, touch, tr, trap*, true, umask*, unix2dos, usleep, uudecode, uuencode, vi, wc, wget, which, who, whoami, xargs, yes ``` commands marked with \* are bash builtins, all others are external programms. Calling an external programm is more expensive then using bulitins @@ -187,5 +187,5 @@ fi #### [Prev Function Reference](6_reference.md) #### [Next Bashbot Environment](8_custom.md) -#### $$VERSION$$ v0.80-dev2-10-g5f945e5 +#### $$VERSION$$ v0.80-dev2-11-gb55c171 diff --git a/modules/inline.sh b/modules/inline.sh deleted file mode 100644 index 601d1e5..0000000 --- a/modules/inline.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -echo "use answerInline.sh" >&2 diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 6ae610f..9e6a117 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-dev2-9-ga79f97f +#### $$VERSION$$ v0.80-dev2-11-gb55c171 # source from commands.sh to use the sendMessage functions @@ -141,13 +141,12 @@ send_file() { ;; esac send_action "$chat_id" "$STATUS" - # convert over to sendJson!! + # convert over to sendJson!! much better: use sendjson in case above ... # shellcheck disable=SC2034 res="$(curl -s "$CUR_URL" -F "chat_id=$chat_id" -F "$WHAT=@$file" -F "caption=$CAPTION")" } # 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 sendJson "${1}" '"action": "'"${2}"'"' "$ACTION_URL" diff --git a/mycommands.sh b/mycommands.sh index 808aee7..df21f79 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -2,7 +2,7 @@ # files: mycommands.sh.dist # copy to mycommands.sh and add all your commands and functions here ... # -#### $$VERSION$$ v0.80-dev2-6-g5b10e75 +#### $$VERSION$$ v0.80-dev2-11-gb55c171 # # uncomment the following lines to overwrite info and help messages @@ -24,7 +24,7 @@ else # NOTE: command can have @botname attached, you must add * in case tests... mycommands() { - case "$MESSAGE" in + case "${MESSAGE}" in '/echo'*) # example echo command send_normal_message "${CHAT[ID]}" "$MESSAGE" ;;