From 601fe0ea51ee3c612acbbcc1bc69f1aabbbcfb81 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Jun 2020 13:30:59 +0200 Subject: [PATCH] interactive working again, more logging --- bashbot.rc | 19 +++----- bashbot.sh | 11 +++-- commands.sh | 6 ++- examples/background-scripts/run_diskusage.sh | 10 ++++- .../background-scripts/run_filecontent.sh | 9 +++- examples/background-scripts/run_filename.sh | 9 +++- examples/background-scripts/run_notify.sh | 10 ++++- examples/calc.sh | 18 +++++--- examples/notify.sh | 10 ++++- examples/question.sh | 20 ++++++--- modules/background.sh | 5 ++- scripts/interactive.sh.clean | 44 +++++++++++++++++++ 12 files changed, 131 insertions(+), 40 deletions(-) create mode 100755 scripts/interactive.sh.clean diff --git a/bashbot.rc b/bashbot.rc index 565ad94..e2d1569 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 # shellcheck disable=SC2009 # shellcheck disable=SC2181 @@ -31,29 +31,22 @@ runcmd="echo Dry run:" # not actived until you edit lines below # runcmd="runuser $runas -s /bin/bash -c " # runasuser with *runuser* # edit the values of the following lines to fit your config: -start="/usr/local/telegram-bot-bash/bashbot.sh" # location of your bashbot.sh script +start="cd /usr/local/telegram-bot-bash; /usr/local/telegram-bot-bash/bashbot.sh" # location of your bashbot.sh script name='' # your bot name as given to botfather, e.g. mysomething_bot # END Configuration ####################### -lockfile="$(dirname $start)/lockfile" [ "$name" = "" ] && name="$runas" case "$1" in 'start') $runcmd "$start start" # >/dev/null 2>&1 /dev/null 2>&1 - fi ;; 'stop') $runcmd "$start kill" RETVAL=$? - if [ "$RETVAL" = "0" ]; then - rm -f "$lockfile" - fi ;; 'status') ps -f -u "$runas" | grep "$name" | grep -qF "bashbot.sh startbot" @@ -76,10 +69,10 @@ case "$1" in 'suspendback'|'resumeback'|'killback') $runcmd "$start $1" RETVAL=$? - # kill inotifywait from runuser if long running bg scripts use it - KILLINOTIFY="" - if [ "$1" != "resumeback" ] && [ -n "${KILLINOTIFY}" ]; then - kill -9 "$(ps -u "$runas" | grep inotifywait | sed 's/ .*//')" >/dev/null 2>&1 + # kill inotifywait from runuser + if [ "$1" != "resumeback" ]; then + # shellcheck disable=SC2046 + kill -9 $(ps -u "$runas" | grep inotifywait | sed 's/ .*//') >/dev/null 2>&1 fi ;; *) diff --git a/bashbot.sh b/bashbot.sh index 89e824c..7701d8a 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.96-dev3-8-gd42429e +#### $$VERSION$$ v0.96-dev3-12-g3f85134 # # Exit Codes: # - 0 sucess (hopefully) @@ -320,8 +320,9 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then -H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" - [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"${LOGDIR}/ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & + [ "${BOTSENT[OK]}" != "true" ] &&\ + printf "%s: CHAT[ID]=%s ACTION=%s\nRESULT=%s\n" "$(date)" "${1}" "${2}" "${res}" >>"${LOGDIR}/ERROR.log" } #$1 Chat, $2 what , $3 file, $4 URL, $5 caption sendUpload() { @@ -334,8 +335,9 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" fi BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" - [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"${LOGDIR}/ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & + [ "${BOTSENT[OK]}" != "true" ] &&\ + printf "%s: CHAT[ID]=%s ACTION=%s\nRESULT=%s\n" "$(date)" "${1}" "${2}" "${res}" >>"${LOGDIR}/ERROR.log" } else # simple curl or wget call outputs result to stdout @@ -352,8 +354,9 @@ else --header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" - [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"${LOGDIR}/ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & + [ "${BOTSENT[OK]}" != "true" ] &&\ + printf "%s: CHAT[ID]=%s ACTION=%s\nRESULT=%s\n" "$(date)" "${1}" "${2}" "${res}" >>"${LOGDIR}/ERROR.log" } sendUpload() { printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"${LOGDIR}/ERROR.log" diff --git a/commands.sh b/commands.sh index ffda32b..5628a99 100644 --- a/commands.sh +++ b/commands.sh @@ -15,7 +15,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.96-dev2-9-ged14e85 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 # # adjust your language setting here, e.g.when run from other user or cron. @@ -126,7 +126,9 @@ if [ -z "${1}" ] || [[ "${1}" == *"debug"* ]];then unban_chat_member "${CHAT[ID]}" "${USER[ID]}" ;; - *) # forward messages to optional dispatcher + '/'*) # discard all unkown commands + : ;; + *) # forward message to interactive chats _exec_if_function send_interactive "${CHAT[ID]}" "${MESSAGE}" ;; esac diff --git a/examples/background-scripts/run_diskusage.sh b/examples/background-scripts/run_diskusage.sh index a5dec8e..1d897d0 100755 --- a/examples/background-scripts/run_diskusage.sh +++ b/examples/background-scripts/run_diskusage.sh @@ -1,10 +1,16 @@ #!/bin/bash # file: run_diskcusage.sh # example for an background job display a system value - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filecontent.sh b/examples/background-scripts/run_filecontent.sh index 5964805..80c7260 100755 --- a/examples/background-scripts/run_filecontent.sh +++ b/examples/background-scripts/run_filecontent.sh @@ -2,7 +2,13 @@ # file: run_filename # background job to display content of all new files in WATCHDIR # -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -33,3 +39,4 @@ loop_callback() { } watch_dir_loop "$WATCHDIR" + diff --git a/examples/background-scripts/run_filename.sh b/examples/background-scripts/run_filename.sh index 34bfb50..e6f1434 100755 --- a/examples/background-scripts/run_filename.sh +++ b/examples/background-scripts/run_filename.sh @@ -2,7 +2,13 @@ # file: run_filename # background job to display all new files in WATCHDIR # -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -31,3 +37,4 @@ loop_callback() { } watch_dir_loop "$WATCHDIR" + diff --git a/examples/background-scripts/run_notify.sh b/examples/background-scripts/run_notify.sh index 9da4cfa..20433d9 100755 --- a/examples/background-scripts/run_notify.sh +++ b/examples/background-scripts/run_notify.sh @@ -1,10 +1,16 @@ #!/bin/bash # file: notify.sh # example for an background job, run with startback notify.sh - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/calc.sh b/examples/calc.sh index d2bd29b..40f4ea4 100755 --- a/examples/calc.sh +++ b/examples/calc.sh @@ -1,11 +1,18 @@ #!/bin/bash # file: calc.sh # example for an interactive chat, run with startproc calc.sh - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +# +#### $$VERSION$$ v0.96-dev3-12-g3f85134 -#### $$VERSION$$ v0.96-dev-7-g0153928 +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + +INPUT="${3:-/dev/stdin}" # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -18,11 +25,11 @@ unset IFS echo 'Starting Calculator ...' echo 'Enter first number.' -read -r A +read -r A <"${INPUT}" echo 'Enter second number.' -read -r B +read -r B <"${INPUT}" echo 'Select Operation: mykeyboardstartshere [ "Addition" , "Subtraction" , "Multiplication" , "Division" , "Cancel" ]' -read -r opt +read -r opt <"${INPUT}" echo -n 'Result: ' case $opt in 'add'* | 'Add'* ) res="$(( A + B ))" ;; @@ -34,3 +41,4 @@ case $opt in esac echo "$res" echo "Bye .." + diff --git a/examples/notify.sh b/examples/notify.sh index 1fc2d54..6254ec9 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -1,10 +1,16 @@ #!/bin/bash # file: notify.sh # example for an background job, run with startback notify.sh - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question.sh b/examples/question.sh index 8e850ad..757d4c2 100755 --- a/examples/question.sh +++ b/examples/question.sh @@ -1,11 +1,19 @@ #!/bin/bash # file: question.sh # example for an interactive chat, run with startproc question.sh - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +# +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe + +INPUT="${3:-/dev/stdin}" -#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -18,16 +26,16 @@ unset IFS echo "Why hello there. Would you like some tea (y/n)?" -read -r answer +read -r answer <"${INPUT}" [[ $answer =~ ^([yY][eE][sS]|[yY])$ ]] && echo "OK then, here you go: http://www.rivertea.com/blog/wp-content/uploads/2013/12/Green-Tea.jpg" || echo "OK then." until [ "$SUCCESS" = "y" ] ;do echo 'Do you like Music? mykeyboardstartshere "Yass!" , "No"' - read -r answer + read -r answer <"${INPUT}" case $answer in 'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;; 'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;; - '') echo "empty answer!" && exit;; + '') echo "empty answer!" && cleanup "$3";; *) SUCCESS=n;; esac done -exit + diff --git a/modules/background.sh b/modules/background.sh index 060306d..e624bca 100644 --- a/modules/background.sh +++ b/modules/background.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.96-dev3-11-g515e07d +#### $$VERSION$$ v0.96-dev3-12-g3f85134 # source from commands.sh if you want ro use interactive or background jobs @@ -61,7 +61,8 @@ start_proc() { local fifo; fifo="${DATADIR:-.}/$(procname "$1")" kill_proc "$1" mkfifo "${fifo}" - nohup bash -c "{ tail -f < \"${fifo}\" | $2 \"$3\" \"$4\" \"${fifo}\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\"; }" &>>"${fifo}.log" & + nohup bash -c "{ $2 \"$4\" \"$5\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" + rm \"${fifo}\"; [ -s \"${fifo}.log\" ] || rm -f \"${fifo}.log\"; }" &>>"${fifo}.log" & } diff --git a/scripts/interactive.sh.clean b/scripts/interactive.sh.clean new file mode 100755 index 0000000..e6e72c0 --- /dev/null +++ b/scripts/interactive.sh.clean @@ -0,0 +1,44 @@ +#!/bin/bash +# file: interactive.sh +# template for an interactive chat +# test it with: start_proc "${CHAT[ID]}" "./scripts/interactive.sh.clean" +# +# This file is public domain in the USA and all free countries. +# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +# +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe + + + +# 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 + +####################### +# place your commands here +# +# IMPORTTANT: to read user input from $INPUT +INPUT="${3:-/dev/stdin}" +# +# read -r variable <"${INPUT}" + +# example wait form user input and echo it +echo "Enter a message:" + +read -r test <"${INPUT}" +echo -e "Your Message: ${test}\nbye!" + + +# your commands ends here +###################### +