Skip to content

Commit

Permalink
Merge pull request #182 Bashbot Version 1.51
Browse files Browse the repository at this point in the history
Version 1.51
  • Loading branch information
gnadelwartz authored Jun 3, 2021
2 parents 94aefbe + 36d8604 commit a233706
Show file tree
Hide file tree
Showing 84 changed files with 251 additions and 304 deletions.
206 changes: 38 additions & 168 deletions README.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,4 @@ See `mycommnds.sh.dist` for an example.

If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,5 @@ That's it all guys!
If you feel that there's something missing or if you found a bug, feel free to submit a
pull request!

$$VERSION$$ v1.5-0-g8adca9b
$$VERSION$$ v1.51-0-g6e66a28

2 changes: 1 addition & 1 deletion addons/antiFlood.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# this addon counts how many files, e.g. stickers, are sent to
# a chat and takes actions if threshold is reached
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28

# used events:
#
Expand Down
2 changes: 1 addition & 1 deletion addons/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Addons can register to bashbot events at startup
# by providing their name and a callback per event
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#
# If an event occurs each registered event function is called.
#
Expand Down
68 changes: 49 additions & 19 deletions bashbot.rc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# tested on: ubuntu, opensuse, debian
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
# shellcheck disable=SC2009
# shellcheck disable=SC2181
# shellcheck disable=SC2250
Expand Down Expand Up @@ -37,61 +37,91 @@ runas="nobody"
# edit the values of the following lines to fit your config:
# your bot name as given to botfather, e.g. mysomething_bot
name=""
[ -z "${name}" ] && name="unknown"

# your bot installation dir
bashbotdir="/usr/local/telegram-bot-bash"
databotdir="${bashbotdir}/data-bot-bash"
FIFO="${databotdir}/webhook-fifo-${name}"

# programs to run
bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh"
webhook="cd ${bashbotdir}; nohup ${bashbotdir}/bin/process_batch.sh --startbot --watch ${databotdir}/webhook-fifo-${name}"
webhook="cd ${bashbotdir}; nohup ${bashbotdir}/bin/process_batch.sh --startbot --watch ${FIFO}"
# set additionl parameter, e.g. debug
mode=""

# select logfile for webhook start stop and script errors
hooklog="DEBUG"
hooklog="WEBHOOK"

# END Configuration
#######################

[ "${name}" = "" ] && name="${runas}"

# check for bot status
ps -f -u "${runas}" | grep "${name}" | grep -qF "bashbot.sh startbot"
if [ "$?" = "0" ]; then
# printf "bashbot (%s) is running in poll mode\n" "${name}"
stat="poll"
else
ps -f -u "${runas}" | grep "${name}" | grep -qF "process_batch.sh --startbot"
if [ "$?" = "0" ]; then
#printf "bashbot (%s) is running in webhook mode\n" "${name}"
stat="hook"
elif [ "${name}" != "unknown" ]; then
#printf "bashbot (%s) is stopped\n" "${name}"
stat="stop"
else
stat="unknown"
fi
fi

case "$1" in
'start')
[ "${stat}" != "stop" ] && printf "Warning, bot is running in mode: %s\n" "${stat}"
$runcmd "$bashbot start $mode" # >/dev/null 2>&1 </dev/null
RETVAL=$?
;;
'starthook')
[ -p "${FIFO}" ] || printf "Warning, webhook pipe not found: %s\n" "${FIFO##*/}"
[ "${stat}" != "stop" ] && printf "Warning, bot is running in mode: %s\n" "${stat}"
printf "Starting bashbot in webhook mode ... "
$runcmd "$webhook $mode </dev/null &>>${bashbotdir}/logs/WEBHOOK.log &" # >/dev/null 2>&1 </dev/null
$runcmd "$webhook $mode </dev/null &>>${bashbotdir}/logs/${hooklog}.log &" # >/dev/null 2>&1 </dev/null
sleep 1
$0 status
RETVAL=$?
;;
'stop')
[ "${stat}" != "poll" ] && printf "Warning, bot is not in poll mode: %s\n" "${stat}"
$runcmd "$bashbot stop $mode"
RETVAL=$?
;;
'stophook')
[ "${stat}" != "hook" ] && printf "Warning, bot is not in webhook mode: %s\n" "${stat}"
printf "Stopping bashbot webhook mode ... "
KILLID="$(ps -f -u "${runas}" | grep "process_batch.sh --startbot" | sed -E 's/[^0-9]+([0-9]+).*/\1/')"
KILLID="$(ps -f -u "${runas}" | grep "process_batch.sh --startbot" | sed -E 's/[^0-9]+([0-9]+).*/\1/' | tr -s "\r\n" " ")"
if [ -n "${KILLID}" ]; then
$runcmd "kill $(printf "%s" "${KILLID}" | tr -s "\r\n" " " )"
$runcmd "kill ${KILLID}; wait ${KILLID} 2>/dev/null"
sleep 1
$0 status
fi
RETVAL=$?
$0 status
;;
'status')
ps -f -u "${runas}" | grep "${name}" | grep -qF "bashbot.sh startbot"
if [ "$?" = "0" ]; then
printf "bashbot (%s) is running in poll mode\n" "${name}"
RETVAL=0
else
ps -f -u "${runas}" | grep "${name}" | grep -qF "process_batch.sh --startbot"
if [ "$?" = "0" ]; then
printf "bashbot (%s) is running in webhook mode\n" "${name}"
case "${stat}" in
"poll"*) printf "bashbot (%s) is running in poll mode\n" "${name}"
RETVAL=0
;;
"hook"*) printf "bashbot (%s) is running in webhook mode\n" "${name}"
RETVAL=0
else
printf "bashbot (%s) is stopped\n" "${name}"
;;
"stop"*) printf "bashbot (%s) is not running\n" "${name}"
RETVAL=1
fi
fi
;;
*) printf "bashbot (%s) status is %s\n" "${name}" "${stat}"
RETVAL=2
;;
esac
;;
'restart'|'reload')
$0 stop; $0 start
Expand Down
35 changes: 18 additions & 17 deletions bashbot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb
# 8 - curl/wget missing
# 10 - not bash!
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
##################################################################

# are we running in a terminal?
Expand Down Expand Up @@ -181,6 +181,7 @@ MODULEDIR="${SCRIPTDIR}/modules"
exit_source() { exit "$1"; }
if [[ "${SCRIPT}" != "${REALME}" || "$1" == "source" ]]; then
SOURCE="yes"
SCRIPT="${REALME}"
[ -z "$1" ] && exit_source() { printf "Exit from source ...\n"; return "$1"; }
fi

Expand Down Expand Up @@ -675,25 +676,25 @@ sendJsonResult(){
# timeout, failed connection or blocked
if [ "${BOTSENT[ERROR]}" == "999" ];then
# check if default curl and args are OK
if ! curl -sL -k -m 2 "${URL}" >/dev/null 2>&1 ; then
printf "%(%c)T: BASHBOT IP Address seems blocked!\n" -1
# user provided function to recover or notify block
if _exec_if_function bashbotBlockRecover; then
BASHBOT_RETRY="2"
printf "bashbotBlockRecover returned true, retry %s ...\n" "$2"
sendJsonRetry "$2" "${BASHBOT_RETRY}" "${@:3}"
unset BASHBOT_RETRY
fi
return
fi
# are not blocked, default curl and args are working
if [ -n "${BASHBOT_CURL_ARGS}" ] || [ "${BASHBOT_CURL}" != "curl" ]; then
printf "Problem with \"%s %s\"? retry %s with default config ...\n"\
"${BASHBOT_CURL}" "${BASHBOT_CURL_ARGS}" "$2"
BASHBOT_RETRY="2"; BASHBOT_CURL="curl"; BASHBOT_CURL_ARGS=""
if ! curl -sL -k -m 2 "${URL}" >/dev/null 2>&1 ; then
printf "%(%c)T: BASHBOT IP Address seems blocked!\n" -1
# user provided function to recover or notify block
if _exec_if_function bashbotBlockRecover; then
BASHBOT_RETRY="2"
printf "bashbotBlockRecover returned true, retry %s ...\n" "$2"
sendJsonRetry "$2" "${BASHBOT_RETRY}" "${@:3}"
unset BASHBOT_RETRY
fi
# seems not blocked, try if blockrecover and default curl args working
elif [ -n "${BASHBOT_CURL_ARGS}" ] || [ "${BASHBOT_CURL}" != "curl" ]; then
printf "Problem with \"%s %s\"? retry %s with default config ...\n"\
"${BASHBOT_CURL}" "${BASHBOT_CURL_ARGS}" "$2"
BASHBOT_RETRY="2"; BASHBOT_CURL="curl"; BASHBOT_CURL_ARGS=""
_exec_if_function bashbotBlockRecover
sendJsonRetry "$2" "${BASHBOT_RETRY}" "${@:3}"
unset BASHBOT_RETRY
fi
[ -n "${BOTSENT[ERROR]}" ] && processError "$3" "${BOTSENT[ERROR]}" "$4" "" "${BOTSENT[DESCRIPTION]}" "$5" "$6"
fi
fi
} >>"${ERRORLOG}"
Expand Down
2 changes: 1 addition & 1 deletion bin/any_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ USAGE='any_command.sh [-h|--help] [--force|--reference] bot_command args ...'
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 30.01.2021 10:24
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
2 changes: 1 addition & 1 deletion bin/bashbot_env.inc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 18.12.2020 12:27
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

############
Expand Down
2 changes: 1 addition & 1 deletion bin/bashbot_init.inc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 27.01.2021 13:42
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================
# shellcheck disable=SC2059

Expand Down
2 changes: 1 addition & 1 deletion bin/bashbot_stats.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ USAGE='bashbot_stats.sh [-h|--help] [debug]'
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 23.12.2020 20:34
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

# set bashbot environment
Expand Down
2 changes: 1 addition & 1 deletion bin/delete_message.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ USAGE='delete_message.sh [-h|--help] "CHAT[ID]" "MESSAGE[ID]" [debug]'
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 03.01.2021 15:37
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
2 changes: 1 addition & 1 deletion bin/edit_buttons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ USAGE='send_message.sh [-h|--help] "CHAT[ID]" "MESSAGE[ID]" "text|url" ...'
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 21.01.2021 08:10
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
2 changes: 1 addition & 1 deletion bin/edit_message.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ USAGE='send_edit_message.sh [-h|--help] [format|caption] "CHAT[ID]" "MESSAGE[ID]
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 23.12.2020 16:52
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
2 changes: 1 addition & 1 deletion bin/kickban_user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ USAGE='kickban_user.sh [-h|--help] [-u|--unban] "CHAT[ID]" "USER[ID]" [debug]'
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 25.01.2021 20:34
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
16 changes: 9 additions & 7 deletions bin/process_batch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# FILE: bin/process_batch.sh
#
USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] [file] [debug]'
USAGE='process_batch.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n] [file] [debug]'
#
# DESCRIPTION: processes last 10 telegram updates in file, one update per line
#
Expand All @@ -21,13 +21,14 @@ USAGE='process_update.sh [-h|--help] [-s|--startbot] [-w|--watch] [-n|--lines n]
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 27.02.2021 13:14
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
# parse args
COMMAND="process_multi_updates"
lines="-n 10"
mode="batch"

opt=0
while [[ "${opt}" -lt 5 && "$1" == "-"* ]]
Expand All @@ -40,6 +41,7 @@ do
;;
"-w"|"--watch")
follow="-f"
mode="webhook"
shift
;;
"-n"|"--lines")
Expand All @@ -61,8 +63,8 @@ file="${WEBHOOK}"
if [ -n "${startbot}" ]; then
# warn when starting bot without pipe
[ -p "${file}" ] || printf "%(%c)T: %b\n" -1 "${ORANGE}Warning${NC}: File is not a pipe:${GREY} ${file##*/}${NC}"
start_bot "$2" "webhook"
printf "%(%c)T: %b\n" -1 "${GREEN}Bot start actions done, start reading updates ....${NC}"
start_bot "$2" "${mode}"
printf "%(%c)T: %b\n" -1 "${GREEN}Bot startup actions done, start ${mode} updates ...${NC}"
fi
# check file exist
if [[ ! -r "${file}" || -d "${file}" ]]; then
Expand All @@ -74,14 +76,14 @@ fi
# ready, do stuff here -----

# kill all sub processes on exit
trap 'kill $(jobs -p) 2>/dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME}"' webhook stopped ..."; printf "%(%c)T: %s\n" -1 "Bot in batch mode stopped!"' EXIT HUP QUIT
trap 'printf "%(%c)T: %s\n" -1 "Bot in '"${mode}"' mode stopped"; kill $(jobs -p) 2>/dev/null; wait $(jobs -p) 2>/dev/null; send_normal_message "'"${BOTADMIN}"'" "Bot '"${BOTNAME} ${mode}"' stopped ..."' EXIT HUP QUIT

# wait after (first) update to avoid processing to many in parallel
UPDWAIT="0.5"
# use tail to read appended updates
# shellcheck disable=SC2086,SC2248
tail ${follow} ${lines} "${file}" |\
while IFS="" read -r input
tail ${follow} ${lines} "${file}" 2>/dev/null |\
while IFS="" read -r input 2>/dev/null
do
# read json from stdin and convert update format
# replace any ID named BOTADMIN with ID of bot admin
Expand Down
2 changes: 1 addition & 1 deletion bin/process_update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ USAGE='process_update.sh [-h|--help] [debug] [<file]'
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 30.01.2021 19:14
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
2 changes: 1 addition & 1 deletion bin/promote_user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ USAGE='promote_user.sh [-h|--help] "CHAT[ID]" "USER[ID]" "right[:true|false]" ..
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 25.01.2021 22:34
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
2 changes: 1 addition & 1 deletion bin/send_broadcast.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ USAGE='broadcast_message.sh [-h|--help] [--doit] [--groups|--both|--db=file] [fo
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 16.12.2020 16:14
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
2 changes: 1 addition & 1 deletion bin/send_buttons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ USAGE='send_message.sh [-h|--help] "CHAT[ID]" "message" "text|url" ...'
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 18.01.2021 11:34
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
6 changes: 3 additions & 3 deletions bin/send_dice.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ USAGE='send_dice.sh [-h|--help] "CHAT[ID]" "emoji" [debug]'
# DESCRIPTION: send an animated emoji (dice) to given chat
#
# OPTIONS: CHAT[ID] - ID number of CHAT or BOTADMIN to send to yourself
# emoji - must be one of: “🎲”, “🎯”, “🏀”, “⚽”, or “🎰”
# ":game_die:" ":dart:" ":basketball:" ":soccer:" :slot_machine:"
# emoji - must be one of: “🎲”, “🎯”, “🏀”, “⚽” “🎰” "🎳"
# :game_die: :dart: :basketball: :soccer: :slot_machine: :bowling:
#
# -h - display short help
# --help - this help
Expand All @@ -21,7 +21,7 @@ USAGE='send_dice.sh [-h|--help] "CHAT[ID]" "emoji" [debug]'
# AUTHOR: KayM (gnadelwartz), [email protected]
# CREATED: 07.02.2021 18:45
#
#### $$VERSION$$ v1.5-0-g8adca9b
#### $$VERSION$$ v1.51-0-g6e66a28
#===============================================================================

####
Expand Down
Loading

0 comments on commit a233706

Please sign in to comment.