From a8a3aa3b94ede637e283303ed6f8203704d889e2 Mon Sep 17 00:00:00 2001 From: Ido Ben-Hur Date: Tue, 3 Sep 2024 15:59:55 +0300 Subject: [PATCH] Debug failed telegram API calls Also: * some other send fixes * ccache indicator logic fix * fixed race condition due to non local vars --- makeBuild.sh | 16 +++++++++++----- telegramSend.sh | 46 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/makeBuild.sh b/makeBuild.sh index 4d4dbc8..5babe0c 100755 --- a/makeBuild.sh +++ b/makeBuild.sh @@ -92,7 +92,7 @@ tmpDir="$(mktemp -d)/" # global var for the tmp file dir currMsg="" # a var that stores the current message for edits tg_send() { - tgmsg=$1 + local tgmsg=$1 if [[ $isSilent == 0 ]]; then if [[ $TG_SEND_PRIOR_CMD != '' ]]; then eval "$TG_SEND_PRIOR_CMD" @@ -128,8 +128,14 @@ function tg_clean() { [[ $currMsg != "" ]] && currMsg="${currMsg}\n" currMsg="${currMsg}Script was stopped / canceled externally" - ./telegramSend.sh --tmp "${tmpDir}" --edit --disable-preview "${currMsg}" - ./telegramSend.sh --tmp "${tmpDir}" --unpin " " + local tgcmd="--tmp ${tmpDir} --edit --disable-preview" + local upintgcmd="--tmp ${tmpDir} --unpin" + if [[ $TG_SEND_CFG_FILE != '' ]]; then + tgcmd="${tgcmd} --config ${TG_SEND_CFG_FILE}" + upintgcmd="${upintgcmd} --config ${TG_SEND_CFG_FILE}" + fi + ./telegramSend.sh $tgcmd "${currMsg}" + ./telegramSend.sh $upintgcmd " " exit 1 } @@ -201,7 +207,7 @@ get_stats() bar="${bar} GB" if [[ $ccFiles != "" ]]; then bar="${bar}\n${ccFiles}" - if [[ $pccFiles != -1 ]] && [[ $pccFiles -gt $ccFiles ]]; then + if [[ $pccFiles != -1 ]] && [[ $ccFiles -gt $pccFiles ]]; then bar="${bar}↑" pccFlag=1 elif [[ $pccFlag == 1 ]]; then @@ -665,7 +671,7 @@ pre_build() eval $TARGET_CHOOSE_CMD # target if [[ $isClean == 1 ]]; then echo -e "${GREEN}Cleaning build${NC}" - tg_send "[pre-build] Cleaning build" + tg_send "[pre-build] Cleaning build" eval $CLEAN_CMD fi if [[ $installClean == 1 ]]; then diff --git a/telegramSend.sh b/telegramSend.sh index fad0563..734d01e 100755 --- a/telegramSend.sh +++ b/telegramSend.sh @@ -2,6 +2,10 @@ TG_SEND_CFG_FILE="telegram.conf" LAST_MSG_ID_FILE="lastMsgId.txt" +# Colors +RED="\033[1;31m" # For errors / warnings +NC="\033[0m" # reset color + isFile=0 isPreview=1 isCite=0 @@ -61,10 +65,15 @@ idFile="${tmpDir}${LAST_MSG_ID_FILE}" mId=$(cat "${idFile}") if [[ $isPin == -1 ]]; then - curl -s -X POST \ + out=$(curl -s -X POST \ -H 'Content-Type: application/json' \ -d "{\"chat_id\": \"${CHAT_ID}\", \"message_id\": \"${mId}\"}" \ - https://api.telegram.org/bot"${TOKEN}"/unpinChatMessage &> /dev/null + https://api.telegram.org/bot"${TOKEN}"/unpinChatMessage) + isOK=$(echo "$out" | jq -r .ok) + if [[ $isOK != "true" ]]; then + echo -e "${RED}Failed unpinning msg${NC}" + echo "$out" | jq + fi exit 0 fi @@ -72,9 +81,15 @@ if [[ $isFile == 1 ]]; then # no edit here! params="" [[ $isPreview == 0 ]] && params="&disable_web_page_preview=True" [[ $isCite == 1 ]] && params="${params}&reply_to_message_id=${mId}" - curl -s -F document=@"${MSG}" \ - https://api.telegram.org/bot$TOKEN/sendDocument?chat_id="${CHAT_ID}""${params}" \ - | jq -r .result | jq -r .message_id > "${idFile}" + out=$(curl -s -F document=@"${MSG}" \ + https://api.telegram.org/bot$TOKEN/sendDocument?chat_id="${CHAT_ID}""${params}") + isOK=$(echo "$out" | jq -r .ok) + if [[ $isOK != "true" ]]; then + echo -e "${RED}Failed sending file${NC}" + echo "$out" | jq + else + echo "$out" | jq -r .result | jq -r .message_id > "${idFile}" + fi exit 0 fi @@ -86,15 +101,26 @@ params="{\"chat_id\": \"${CHAT_ID}\", \"text\": \"${MSG}\", \"parse_mode\": \"HT [[ $isCite == 1 ]] && params="${params}, \"reply_to_message_id\": \"${mId}\"" [[ $isEdit == 1 ]] && params="${params}, \"message_id\": \"${mId}\"" params="${params}}" -curl -s -X POST \ +out=$(curl -s -X POST \ -H 'Content-Type: application/json' \ -d "${params}" \ - https://api.telegram.org/bot"${TOKEN}/${op}" \ - | jq -r .result | jq -r .message_id > "${idFile}" + https://api.telegram.org/bot"${TOKEN}/${op}") +isOK=$(echo "$out" | jq -r .ok) +if [[ $isOK != "true" ]]; then + echo -e "${RED}Failed sending msg${NC}" + echo "$out" | jq +else + echo "$out" | jq -r .result | jq -r .message_id > "${idFile}" +fi if [[ $isPin == 1 ]]; then - curl -s -X POST \ + out=$(curl -s -X POST \ -H 'Content-Type: application/json' \ -d "{\"chat_id\": \"${CHAT_ID}\", \"message_id\": \"$(cat "${idFile}")\", \"disable_notification\": \"true\"}" \ - https://api.telegram.org/bot"${TOKEN}"/pinChatMessage &> /dev/null + https://api.telegram.org/bot"${TOKEN}"/pinChatMessage) + isOK=$(echo "$out" | jq -r .ok) + if [[ $isOK != "true" ]]; then + echo -e "${RED}Failed pinning msg${NC}" + echo "$out" | jq + fi fi