Skip to content

Commit

Permalink
Debug failed telegram API calls
Browse files Browse the repository at this point in the history
Also:
* some other send fixes
* ccache indicator logic fix
* fixed race condition due to non local vars
  • Loading branch information
idoybh committed Sep 3, 2024
1 parent 2e835dc commit a8a3aa3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
16 changes: 11 additions & 5 deletions makeBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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</code>"
tg_send "[pre-build] Cleaning build"
eval $CLEAN_CMD
fi
if [[ $installClean == 1 ]]; then
Expand Down
46 changes: 36 additions & 10 deletions telegramSend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,20 +65,31 @@ 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

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

Expand All @@ -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

0 comments on commit a8a3aa3

Please sign in to comment.