bin: process_batch: fix options bashbot.rc: support webhook

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-03-02 18:05:14 +01:00
parent 24158142f9
commit 7b8e391479
2 changed files with 43 additions and 14 deletions

View File

@ -5,7 +5,7 @@
# #
# tested on: ubuntu, opensuse, debian # tested on: ubuntu, opensuse, debian
# #
#### $$VERSION$$ v1.45-dev-59-ga9ac7ea #### $$VERSION$$ v1.45-dev-60-g2415814
# shellcheck disable=SC2009 # shellcheck disable=SC2009
# shellcheck disable=SC2181 # shellcheck disable=SC2181
@ -34,11 +34,14 @@ runas="nobody"
# runcmd="runuser ${runas} -s /bin/bash -c " # runasuser with *runuser* # runcmd="runuser ${runas} -s /bin/bash -c " # runasuser with *runuser*
# edit the values of the following lines to fit your config: # edit the values of the following lines to fit your config:
# your bot installation dir
bashbotdir="/usr/local/telegram-bot-bash"
bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh"
# your bot name as given to botfather, e.g. mysomething_bot # your bot name as given to botfather, e.g. mysomething_bot
name="" name=""
# your bot installation dir
bashbotdir="/usr/local/telegram-bot-bash"
databotdir="${bashbotdir}/data-bot-bash"
# programs to run
bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh"
webhook="cd ${bashbotdir}; ${bashbotdir}/bin/process_batch.sh --start --watch ${databotdir}/webhook-fifo-${name}"
# set additionl parameter, e.g. debug # set additionl parameter, e.g. debug
mode="" mode=""
@ -53,25 +56,45 @@ case "$1" in
$runcmd "$bashbot start $mode" # >/dev/null 2>&1 </dev/null $runcmd "$bashbot start $mode" # >/dev/null 2>&1 </dev/null
RETVAL=$? RETVAL=$?
;; ;;
'starthook')
# shellcheck disable=SC2250
$runcmd "$webhook $mode" # >/dev/null 2>&1 </dev/null
RETVAL=$?
;;
'stop') 'stop')
# shellcheck disable=SC2250 # shellcheck disable=SC2250
$runcmd "$bashbot stop $mode" $runcmd "$bashbot stop $mode"
RETVAL=$? RETVAL=$?
;; ;;
'stophook')
# shellcheck disable=SC2250
pkill -u "${runas}" "process_batch.sh"
RETVAL=$?
;;
'status') 'status')
ps -f -u "${runas}" | grep "${name}" | grep -qF "bashbot.sh startbot" ps -f -u "${runas}" | grep "${name}" | grep -qF "process_batch.sh --start --watch"
if [ "$?" = "0" ]; then if [ "$?" = "0" ]; then
printf "bashbot (%s) is running\n" "${name}" printf "bashbot (%s) is running in poll mode\n" "${name}"
RETVAL=0
else
ps -f -u "${runas}" | grep "${name}" | grep -qF ""
if [ "$?" = "0" ]; then
printf "bashbot (%s) is running in webhook mode\n" "${name}"
RETVAL=0 RETVAL=0
else else
printf "bashbot (%s) is stopped\n" "${name}" printf "bashbot (%s) is stopped\n" "${name}"
RETVAL=1 RETVAL=1
fi fi
fi
;; ;;
'restart'|'reload') 'restart'|'reload')
$0 stop; $0 start $0 stop; $0 start
RETVAL=$? RETVAL=$?
;; ;;
'restarthook'|'reloadhook')
$0 stophook; $0 starthook
RETVAL=$?
;;
'restartback') 'restartback')
$0 suspendback; $0 resumeback $0 suspendback; $0 resumeback
RETVAL=$? RETVAL=$?
@ -87,7 +110,7 @@ case "$1" in
fi fi
;; ;;
*) *)
printf "%s\n" "Usage: $0 { start | stop | restart | reload | restartback | suspendback | resumeback | killback }" printf "%s\n" "Usage: $0 { start | stop | starthook | restart | reload | restartback | suspendback | resumeback | killback }"
RETVAL=1 RETVAL=1
;; ;;
esac esac

View File

@ -29,12 +29,16 @@ USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n]
COMMAND="process_multi_updates" COMMAND="process_multi_updates"
lines="-n 10" lines="-n 10"
opt=0
while [[ "$opt" -lt 5 && "$1" == "-"* ]]
do
(( opt++ ))
case "$1" in case "$1" in
"-s"|"--startbot") "-s"|"--startbot")
startbot="yes" startbot="yes"
shift shift
;; ;;
"-f"|"--follow") "-w"|"--watch")
follow="-f" follow="-f"
shift shift
;; ;;
@ -43,6 +47,7 @@ case "$1" in
shift 2 shift 2
;; ;;
esac esac
done
# set bashbot environment # set bashbot environment
source "${0%/*}/bashbot_env.inc.sh" "debug" # debug source "${0%/*}/bashbot_env.inc.sh" "debug" # debug
@ -55,8 +60,9 @@ file="${WEBHOOK}"
# start bot # start bot
if [ -n "${startbot}" ]; then if [ -n "${startbot}" ]; then
# warn when starting bot without pipe # warn when starting bot without pipe
[ -p "${WEBHOOK}" ] || printf "%b\n" "${ORANGE}Warning${NC}: File is not a pipe:${GREY} ${file##*/}${NC}" [ -p "${file}" ] || printf "%b\n" "${ORANGE}Warning${NC}: File is not a pipe:${GREY} ${file##*/}${NC}"
start_bot "$2" start_bot "$2"
printf "${GREEN}Bot start actions done, start reading updates ....${NN}"
fi fi
# check file exist # check file exist
if [[ ! -r "${file}" || -d "${file}" ]]; then if [[ ! -r "${file}" || -d "${file}" ]]; then
@ -68,7 +74,7 @@ fi
# ready, do stuff here ----- # ready, do stuff here -----
# kill all sub processes on exit # kill all sub processes on exit
trap 'kill $(jobs -p) 2>/dev/null' EXIT HUP QUIT trap 'kill $(jobs -p) 2>/dev/null; printf "Bot in batch mode killed!\n"' EXIT HUP QUIT
# use tail to read appended updates # use tail to read appended updates
# shellcheck disable=SC2086,SC2248 # shellcheck disable=SC2086,SC2248