mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-11-21 23:25:08 +00:00
Cosmetic updates to spelling and indentation
This commit is contained in:
parent
0e030aa670
commit
12579c8712
142
bashbot.sh
142
bashbot.sh
@ -21,7 +21,7 @@
|
||||
# - 4 unknown command
|
||||
# - 5 cannot connect to telegram bot
|
||||
# - 6 mandatory module not found
|
||||
# - 6 can't get bottoken
|
||||
# - 7 can't get bottoken
|
||||
# shellcheck disable=SC2140,SC2031,SC2120,SC1091
|
||||
|
||||
# are we running in a terminal?
|
||||
@ -130,7 +130,7 @@ fi
|
||||
ADDONDIR="${BASHBOT_ETC:-.}/addons"
|
||||
RUNUSER="${USER}" # USER is overwritten by bashbot array :-(, save original
|
||||
|
||||
# OK everything setup, lest start
|
||||
# OK everything setup, lets start
|
||||
if [[ -z "${SOURCE}" && -z "$BASHBOT_HOME" ]] && ! cd "${RUNDIR}" ; then
|
||||
echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}"
|
||||
exit 1
|
||||
@ -177,7 +177,7 @@ if [ -z "${BOTTOKEN}" ]; then
|
||||
[ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTCONFIG}.jssh"
|
||||
fi
|
||||
|
||||
# no boteadmin, setup botadmin
|
||||
# no botadmin, setup botadmin
|
||||
if [ -z "$(getConfigKey "botadmin")" ]; then
|
||||
# convert old admin
|
||||
if [ -r "${BOTADMIN}" ]; then
|
||||
@ -237,16 +237,16 @@ debug_checks "start SOURCE=${SOURCE:-no}" "$@"
|
||||
if [ -z "${BOTTOKEN}" ]; then
|
||||
BOTTOKEN="$(getConfigKey "bottoken")"
|
||||
if [ -z "${BOTTOKEN}" ]; then
|
||||
BOTERROR="Warning: can't get bot token, try to recover working config"
|
||||
echo -e "${ORANGE}${BOTERROR}${NC}"
|
||||
if [ -r "${BOTCONFIG}.jssh.ok" ]; then
|
||||
log_error "${BOTERROR}"
|
||||
cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh"
|
||||
BOTTOKEN="$(getConfigKey "bottoken")"
|
||||
else
|
||||
echo -e "${RED}Error: Missing bot token! remove ${BOTCONFIG}.jssh and run \"bashbot.sh init\" may fix it.${NC}"
|
||||
exit 7
|
||||
fi
|
||||
BOTERROR="Warning: can't get bot token, try to recover working config"
|
||||
echo -e "${ORANGE}${BOTERROR}${NC}"
|
||||
if [ -r "${BOTCONFIG}.jssh.ok" ]; then
|
||||
log_error "${BOTERROR}"
|
||||
cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh"
|
||||
BOTTOKEN="$(getConfigKey "bottoken")"
|
||||
else
|
||||
echo -e "${RED}Error: Missing bot token! remove ${BOTCONFIG}.jssh and run \"bashbot.sh init\" may fix it.${NC}"
|
||||
exit 7
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
@ -255,12 +255,12 @@ fi
|
||||
if ! check_token "${BOTTOKEN}"; then
|
||||
echo -e "${ORANGE}Warning: your bottoken may incorrect. it should have the following format:${NC}"
|
||||
echo -e "${GREY}123456789${RED}:${GREY}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4${ORANGE} => ${NC}\c"
|
||||
echo -e "${GREY}8-10 digits${RED}:${GREY}35 alnum characters + '_-'${NC}"
|
||||
echo -e "${GREY}8-10 digits${RED}:${GREY}35 alphanumeric characters + '_-'${NC}"
|
||||
echo -e "${ORANGE}Your current token is: '${GREY}^$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")${ORANGE}'${NC}"
|
||||
[[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}: ]] &&\
|
||||
echo -e "${ORANGE}Possible problem in the digits part, len is $(($(wc -c <<<"${BOTTOKEN%:*}")-1))${NC}"
|
||||
[[ ! "${BOTTOKEN}" =~ :[a-zA-Z0-9_-]{35}$ ]] &&\
|
||||
echo -e "${ORANGE}Posilbe problem in the charatcers part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NC}"
|
||||
echo -e "${ORANGE}Possible problem in the characters part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NC}"
|
||||
fi
|
||||
|
||||
|
||||
@ -287,7 +287,7 @@ export res CAPTION
|
||||
|
||||
|
||||
##################
|
||||
# read commamds file if we are not sourced
|
||||
# read commands file if we are not sourced
|
||||
COMMANDS="${BASHBOT_ETC:-.}/commands.sh"
|
||||
if [ -z "${SOURCE}" ]; then
|
||||
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
|
||||
@ -337,14 +337,14 @@ procname(){
|
||||
printf '%s\n' "$2${ME}_$1"
|
||||
}
|
||||
|
||||
# $1 string to search for proramm incl. parameters
|
||||
# returns a list of PIDs of all current bot proceeses matching $1
|
||||
# $1 string to search for programme incl. parameters
|
||||
# returns a list of PIDs of all current bot processes matching $1
|
||||
proclist() {
|
||||
# shellcheck disable=SC2009
|
||||
ps -fu "${UID}" | grep -F "$1" | grep -v ' grep'| grep -F "${ME}" | sed 's/\s\+/\t/g' | cut -f 2
|
||||
}
|
||||
|
||||
# $1 string to search for proramm to kill
|
||||
# $1 string to search for programme to kill
|
||||
killallproc() {
|
||||
local procid; procid="$(proclist "$1")"
|
||||
if [ -n "${procid}" ] ; then
|
||||
@ -359,7 +359,7 @@ killallproc() {
|
||||
}
|
||||
|
||||
|
||||
# $ chat $2 mesgid $3 nolog
|
||||
# $ chat $2 msg_id $3 nolog
|
||||
declare -xr DELETE_URL=$URL'/deleteMessage'
|
||||
delete_message() {
|
||||
[ -z "$3" ] && printf "%s: Delete Message CHAT=%s MSG_ID=%s\n" "$(date)" "${1}" "${2}" >>"${UPDATELOG}"
|
||||
@ -397,7 +397,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then
|
||||
sendJsonResult "${res}" "sendJson (curl)" "$@"
|
||||
[ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "${@}" &
|
||||
}
|
||||
#$1 Chat, $2 what , $3 file, $4 URL, $5 caption
|
||||
#$1 Chat, $2 what, $3 file, $4 URL, $5 caption
|
||||
sendUpload() {
|
||||
[ "$#" -lt 4 ] && return
|
||||
if [ -n "$5" ]; then
|
||||
@ -479,13 +479,13 @@ sendJsonResult(){
|
||||
else
|
||||
# oops something went wrong!
|
||||
if [ "${res}" != "" ]; then
|
||||
BOTSENT[ERROR]="$(JsonGetValue '"error_code"' <<< "${1}")"
|
||||
BOTSENT[DESCRIPTION]="$(JsonGetString '"description"' <<< "${1}")"
|
||||
BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${1}")"
|
||||
BOTSENT[ERROR]="$(JsonGetValue '"error_code"' <<< "${1}")"
|
||||
BOTSENT[DESCRIPTION]="$(JsonGetString '"description"' <<< "${1}")"
|
||||
BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${1}")"
|
||||
else
|
||||
BOTSENT[OK]="false"
|
||||
BOTSENT[ERROR]="999"
|
||||
BOTSENT[DESCRIPTION]="Send to telegram not possible, timeout/broken/no connection"
|
||||
BOTSENT[OK]="false"
|
||||
BOTSENT[ERROR]="999"
|
||||
BOTSENT[DESCRIPTION]="Send to telegram not possible, timeout/broken/no connection"
|
||||
fi
|
||||
# log error
|
||||
[[ "${BOTSENT[ERROR]}" = "400" && "${BOTSENT[DESCRIPTION]}" == *"starting at byte offset"* ]] &&\
|
||||
@ -496,36 +496,36 @@ sendJsonResult(){
|
||||
[ -n "${BASHBOT_RETRY}${BASHBOT_WGET}" ] && return
|
||||
|
||||
# OK, we can retry sendJson, let's see what's failed
|
||||
# throttled, telegram say we send to much messages
|
||||
# throttled, telegram say we send too many messages
|
||||
if [ -n "${BOTSENT[RETRY]}" ]; then
|
||||
BASHBOT_RETRY="$(( ++BOTSENT[RETRY] ))"
|
||||
printf "Retry %s in %s seconds ...\n" "${2}" "${BASHBOT_RETRY}"
|
||||
sendJsonRetry "${2}" "${BASHBOT_RETRY}" "${@:3}"
|
||||
unset BASHBOT_RETRY
|
||||
return
|
||||
BASHBOT_RETRY="$(( ++BOTSENT[RETRY] ))"
|
||||
printf "Retry %s in %s seconds ...\n" "${2}" "${BASHBOT_RETRY}"
|
||||
sendJsonRetry "${2}" "${BASHBOT_RETRY}" "${@:3}"
|
||||
unset BASHBOT_RETRY
|
||||
return
|
||||
fi
|
||||
# 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 "%s: BASHBOT IP Address seems blocked!\n" "$(date)"
|
||||
# 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
|
||||
if ! curl -sL -k -m 2 "${URL}" >/dev/null 2>&1 ; then
|
||||
printf "%s: BASHBOT IP Address seems blocked!\n" "$(date)"
|
||||
# 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
|
||||
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=""
|
||||
sendJsonRetry "${2}" "${BASHBOT_RETRY}" "${@:3}"
|
||||
unset BASHBOT_RETRY
|
||||
fi
|
||||
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=""
|
||||
sendJsonRetry "${2}" "${BASHBOT_RETRY}" "${@:3}"
|
||||
unset BASHBOT_RETRY
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
} >>"${ERRORLOG}"
|
||||
@ -557,15 +557,15 @@ getBotName() {
|
||||
# pure bash implementation, done by KayM (@gnadelwartz)
|
||||
# see https://stackoverflow.com/a/55666449/9381171
|
||||
JsonDecode() {
|
||||
local out="$1" remain="" U=""
|
||||
local out="$1" remain="" U=""
|
||||
local regexp='(.*)\\u[dD]([0-9a-fA-F]{3})\\u[dD]([0-9a-fA-F]{3})(.*)'
|
||||
while [[ "${out}" =~ $regexp ]] ; do
|
||||
U=$(( ( (0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ) | ( 0xd${BASH_REMATCH[3]} & 0x3ff ) + 0x10000 ))
|
||||
remain="$(printf '\\U%8.8x' "${U}")${BASH_REMATCH[4]}${remain}"
|
||||
out="${BASH_REMATCH[1]}"
|
||||
done
|
||||
while [[ "${out}" =~ $regexp ]] ; do
|
||||
U=$(( ( (0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ) | ( 0xd${BASH_REMATCH[3]} & 0x3ff ) + 0x10000 ))
|
||||
remain="$(printf '\\U%8.8x' "${U}")${BASH_REMATCH[4]}${remain}"
|
||||
out="${BASH_REMATCH[1]}"
|
||||
done
|
||||
# this echo must stay for correct decoding!
|
||||
echo -e "${out}${remain}"
|
||||
echo -e "${out}${remain}"
|
||||
}
|
||||
|
||||
JsonGetString() {
|
||||
@ -595,7 +595,7 @@ process_client() {
|
||||
# log message on debug
|
||||
[[ -n "${debug}" ]] && printf "\n%s: New Message ==========\n%s\n" "$(date)" "$UPDATE" >>"${LOGDIR}/MESSAGE.log"
|
||||
|
||||
# check for uers / groups to ignore
|
||||
# check for users / groups to ignore
|
||||
jssh_updateArray_async "BASHBOTBLOCKED" "${BLOCKEDFILE}"
|
||||
[ -n "${USER[ID]}" ] && [[ -n "${BASHBOTBLOCKED[${USER[ID]}]}" || -n "${BASHBOTBLOCKED[${CHAT[ID]}]}" ]] && return
|
||||
|
||||
@ -783,7 +783,7 @@ process_message() {
|
||||
MESSAGE[0]+="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's|\\/|/|g')"
|
||||
MESSAGE[ID]="${UPD["result",${num},"message","message_id"]}"
|
||||
|
||||
# Chat ID is now parsed when update isreceived
|
||||
# Chat ID is now parsed when update is received
|
||||
CHAT[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","last_name"]}")"
|
||||
CHAT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","first_name"]}")"
|
||||
CHAT[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","chat","username"]}")"
|
||||
@ -793,7 +793,7 @@ process_message() {
|
||||
CHAT[TYPE]="$(JsonDecode "${UPD["result",${num},"message","chat","type"]}")"
|
||||
CHAT[ALL_ADMIN]="${UPD["result",${num},"message","chat","all_members_are_administrators"]}"
|
||||
|
||||
# user ID is now parsed when update isreceived
|
||||
# user ID is now parsed when update is received
|
||||
#USER[ID]="${UPD["result",${num},"message","from","id"]}"
|
||||
USER[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","first_name"]}")"
|
||||
USER[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","last_name"]}")"
|
||||
@ -890,7 +890,7 @@ process_message() {
|
||||
[ -z "${MESSAGE[0]}" ] && [ -n "${SERVICE[PINNED]}" ] &&\
|
||||
MESSAGE[0]="/_new_pinned_message ${USER[ID]} ${PINNED[ID]} ${PINNED[MESSAGE]}"
|
||||
fi
|
||||
# set SSERVICE to yes if a service message was received
|
||||
# set SERVICE to yes if a service message was received
|
||||
[[ "${SERVICE[*]}" =~ ^[[:blank:]]*$ ]] || SERVICE[0]="yes"
|
||||
|
||||
# split message in command and args
|
||||
@ -968,7 +968,7 @@ start_bot() {
|
||||
process_updates "${DEBUGMSG}"
|
||||
fi
|
||||
else
|
||||
# ups, something bad happened, wait maxsleep*10
|
||||
# oops, something bad happened, wait maxsleep*10
|
||||
(( nextsleep=nextsleep*2 , nextsleep= nextsleep>maxsleep*10 ?maxsleep*10:nextsleep ))
|
||||
[ "${OFFSET}" = "-999" ] &&\
|
||||
log_error "Repeated timeout/broken/no connection on telegram update, sleep $(_round_float "${nextsleep}e-3")s"
|
||||
@ -1004,7 +1004,7 @@ bot_init() {
|
||||
done
|
||||
echo "Done."
|
||||
fi
|
||||
#setup bashbot
|
||||
# setup bashbot
|
||||
[[ "${UID}" -eq "0" ]] && RUNUSER="nobody"
|
||||
echo -n "Enter User to run bashbot [$RUNUSER]: "
|
||||
read -r TOUSER
|
||||
@ -1068,11 +1068,11 @@ if [ -z "${SOURCE}" ]; then
|
||||
##############
|
||||
# internal options only for use from bashbot and developers
|
||||
case "${1}" in
|
||||
# update botname botname when starting only
|
||||
# update botname when starting only
|
||||
"botname"|"start"*)
|
||||
ME="$(getBotName)"
|
||||
if [ -n "${ME}" ]; then
|
||||
# ok we have a connection an got botname, save it
|
||||
# ok we have a connection and got botname, save it
|
||||
[ -n "${CLEAR}" ] && echo -e "${GREY}Bottoken is valid ...${NC}"
|
||||
jssh_updateKeyDB "botname" "${ME}" "${BOTCONFIG}"
|
||||
rm -f "${BOTCONFIG}.jssh.flock"
|
||||
@ -1102,7 +1102,7 @@ if [ -z "${SOURCE}" ]; then
|
||||
debug_checks "end outproc" "$@"
|
||||
exit
|
||||
;;
|
||||
# finally starts the read update loop, internal use only1
|
||||
# finally starts the read update loop, internal use only1
|
||||
"startbot" )
|
||||
start_bot "$2"
|
||||
debug_checks "end startbot" "$@"
|
||||
@ -1114,7 +1114,7 @@ if [ -z "${SOURCE}" ]; then
|
||||
debug_checks "end init" "$@"
|
||||
exit
|
||||
;;
|
||||
# print usage sats
|
||||
# print usage stats
|
||||
"count") echo -e "${RED}Command ${GREY}count${RED} is deprecated, use ${GREY}stats{$RED}instead.${NC}";&
|
||||
"stats")
|
||||
ME="$(getConfigKey "botname")"
|
||||
@ -1150,7 +1150,7 @@ if [ -z "${SOURCE}" ]; then
|
||||
debug_checks "end $1" "$@"
|
||||
exit
|
||||
;;
|
||||
# sedn message to all users
|
||||
# send message to all users
|
||||
'broadcast')
|
||||
ME="$(getConfigKey "botname")"
|
||||
declare -A SENDALL
|
||||
@ -1171,7 +1171,7 @@ if [ -z "${SOURCE}" ]; then
|
||||
debug_checks "end $1" "$@"
|
||||
exit
|
||||
;;
|
||||
# does what is says
|
||||
# does what it says
|
||||
"status")
|
||||
ME="$(getConfigKey "botname")"
|
||||
SESSION="${ME:-_bot}-startbot"
|
||||
@ -1186,7 +1186,7 @@ if [ -z "${SOURCE}" ]; then
|
||||
debug_checks "end status" "$@"
|
||||
;;
|
||||
|
||||
# start bot as background jod and check if bot is running
|
||||
# start bot as background job and check if bot is running
|
||||
"start")
|
||||
# shellcheck disable=SC2086
|
||||
SESSION="${ME:-_bot}-startbot"
|
||||
@ -1244,6 +1244,6 @@ if [ -z "${SOURCE}" ]; then
|
||||
# warn if root
|
||||
if [[ "${UID}" -eq "0" ]] ; then
|
||||
echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}"
|
||||
echo -e "${ORANGE}You are at HIGH RISK when running a Telegram BOT with root privilegs!${NC}"
|
||||
echo -e "${ORANGE}You are at HIGH RISK when running a Telegram BOT with root privileges!${NC}"
|
||||
fi
|
||||
fi # end source
|
||||
|
Loading…
Reference in New Issue
Block a user