From 117255a958602a2af4559dca46407f055347f958 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Feb 2021 18:41:54 +0100 Subject: [PATCH] modules: implement processError in sendMessage.sh --- bashbot.sh | 14 ++++++------ modules/sendMessage.sh | 52 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 7d5a9d5..8f830a7 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.45-dev-11-g897458a +#### $$VERSION$$ v1.45-dev-12-g066274c ################################################################## # are we running in a terminal? @@ -466,17 +466,17 @@ download_file() { # if no specific function exist try to call bashbotProcessError processError(){ [[ "$4" != "4"* ]] && return 1 - local func="$1" err="$2" chat="$3" user="$4" msg="$5"; shift 5 + local func="$1" err="$2" # check for bashbotError${func} provided in mycommands # shellcheck disable=SC2082 - if _is_function "bashbotError${func}"; then - "bashbotError${func}" "${err}" "${chat}" "${user}" "${msg}" "$@" + if _is_function "bashbotError_${func}"; then + "bashbotError_${func}" "$@" # check for bashbotError${err} provided in mycommands - elif _is_function "bashbotError${err}"; then - "bashbotError${err}" "${func}" "${chat}" "${user}" "${msg}" "$@" + elif _is_function "bashbotError_${err}"; then + "bashbotError_${err}" "$@" # noting found, try bashbotProcessError else - _exec_if_function bashbotProcessError "${func}" "${err}" "${chat}" "${user}" "${msg}" "$@" + _exec_if_function bashbotProcessError "$@" fi } diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 52afdfe..d45c2a0 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117 -#### $$VERSION$$ v1.45-dev-8-g069570e +#### $$VERSION$$ v1.45-dev-12-g066274c # will be automatically sourced from bashbot @@ -37,46 +37,64 @@ send_normal_message() { text="${text:$((len+2))}" fi done + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" } # $1 CHAT $2 message send_markdown_message() { _format_message_url "$1" "$2" ',"parse_mode":"markdown"' "${MSG_URL}" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" } # $1 CHAT $2 message send_markdownv2_message() { _markdownv2_message_url "$1" "$2" ',"parse_mode":"markdownv2"' "${MSG_URL}" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" } # $1 CHAT $2 message send_html_message() { _format_message_url "$1" "$2" ',"parse_mode":"html"' "${MSG_URL}" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" } # $1 CHAT $2 msg-id $3 message edit_normal_message() { _format_message_url "$1" "$3" ',"message_id":'"$2"'' "${EDIT_URL}" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" } # $1 CHAT $2 msg-id $3 message edit_markdown_message() { _format_message_url "$1" "$3" ',"message_id":'"$2"',"parse_mode":"markdown"' "${EDIT_URL}" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" } # $1 CHAT $2 msg-id $3 message edit_markdownv2_message() { _markdownv2_message_url "$1" "$3" ',"message_id":'"$2"',"parse_mode":"markdownv2"' "${EDIT_URL}" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" } # $1 CHAT $2 msg-id $3 message edit_html_message() { _format_message_url "$1" "$3" ',"message_id":'"$2"',"parse_mode":"html"' "${EDIT_URL}" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" } # $1 chat $2 mesage_id, $3 caption edit_message_caption() { sendJson "$1" '"message_id":'"$2"',"caption":"'"$3"'"' "${URL}/editMessageCaption" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" } @@ -117,8 +135,10 @@ send_keyboard() { text='"text":"'"${text//$'\n'/\\n}"'"' fi local one_time=', "one_time_keyboard":true' && [ -n "$4" ] && one_time="" - sendJson "$1" "${text}"', "reply_markup": {"keyboard": [ '"$3"' ] '"${one_time}"'}' "${MSG_URL}" # '"text":"$2", "reply_markup": {"keyboard": [ $3 ], "one_time_keyboard": true}' + sendJson "$1" "${text}"', "reply_markup": {"keyboard": [ '"$3"' ] '"${one_time}"'}' "${MSG_URL}" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" } # $1 CHAT $2 message $3 remove @@ -130,8 +150,10 @@ remove_keyboard() { fi sendJson "$1" "${text}"', "reply_markup": {"remove_keyboard":true}' "${MSG_URL}" # delete message if no message or $3 not empty - [[ -z "$2" || -n "$3" ]] && delete_message "$1" "${BOTSENT[ID]}" "nolog" #JSON='"text":"$2", "reply_markup": {"remove_keyboard":true}' + [[ -z "$2" || -n "$3" ]] && delete_message "$1" "${BOTSENT[ID]}" "nolog" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" } # buttons will specified as "texts @@ -174,8 +196,10 @@ _button_row() { # raw inline functions, for special use # $1 CHAT $2 message-id $3 keyboard edit_inline_keyboard() { - sendJson "$1" '"message_id":'"$2"', "reply_markup": {"inline_keyboard": [ '"$3"' ]}' "${URL}/editMessageReplyMarkup" # JSON='"message_id":"$2", "reply_markup": {"inline_keyboard": [ $3->[{"text":"text", "url":"url"}]<- ]}' + sendJson "$1" '"message_id":'"$2"', "reply_markup": {"inline_keyboard": [ '"$3"' ]}' "${URL}/editMessageReplyMarkup" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" } @@ -183,7 +207,8 @@ edit_inline_keyboard() { send_inline_keyboard() { local text; text='"text":"'$(JsonEscape "$2")'"'; [ -z "$2" ] && text='"text":"..."' sendJson "$1" "${text}"', "reply_markup": {"inline_keyboard": [ '"$3"' ]}' "${MSG_URL}" - # JSON='"text":"$2", "reply_markup": {"inline_keyboard": [ $3->[{"text":"text", "url":"url"}]<- ]}' + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" } # $1 callback id, $2 text to show, alert if not empty @@ -196,6 +221,8 @@ answer_callback_query() { # $1 chat, $2 file_id on telegram server send_sticker() { sendJson "$1" '"sticker": "'"$2"'"' "${URL}/sendSticker" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" } @@ -314,6 +341,8 @@ send_file(){ send_action() { [ -z "$2" ] && return sendJson "$1" '"action": "'"$2"'"' "${URL}/sendChatAction" & + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" } # $1 chat $2 emoji “🎲”, “🎯”, “🏀”, “⚽”, “🎰" @@ -334,6 +363,9 @@ send_dice() { if [ "${BOTSENT[OK]}" = "true" ]; then BOTSENT[DICE]="${UPD["result,dice,emoji"]}" BOTSENT[RESULT]="${UPD["result,dice,value"]}" + else + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" fi } @@ -341,14 +373,18 @@ send_dice() { send_location() { [ -z "$3" ] && return sendJson "$1" '"latitude": '"$2"', "longitude": '"$3"'' "${URL}/sendLocation" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" } -# $1 CHAT $2 lat $3 long $4 title $5 address $6 foursquard id +# $1 CHAT $2 lat $3 long $4 title $5 address $6 foursquare id send_venue() { local add="" [ -z "$5" ] && return [ -n "$6" ] && add=', "foursquare_id": '"$6"'' sendJson "$1" '"latitude": '"$2"', "longitude": '"$3"', "address": "'"$5"'", "title": "'"$4"'"'"${add}" "${URL}/sendVenue" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" "$4" "$5" "$6" } @@ -360,12 +396,16 @@ send_venue() { forward_message() { [ -z "$3" ] && return sendJson "$1" '"from_chat_id": '"$2"', "message_id": '"$3"'' "${URL}/forwardMessage" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" } # $1 CHAT $2 from chat $3 from msg id copy_message() { [ -z "$3" ] && return sendJson "$1" '"from_chat_id": '"$2"', "message_id": '"$3"'' "${URL}/copyMessage" + # func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args + [ -n "${BOTSENT[ERROR]}" ] && processError "${FUNCNAME[0]}" "${BOTSENT[ERROR]}" "$1" "" "${BOTSENT[ERROR]}" "$2" "$3" } # $1 CHAT $2 bashbot formatted message, see manual advanced usage