Skip to content

Commit

Permalink
Updated bash version after some time, little code changes, fix for #30
Browse files Browse the repository at this point in the history
…(thanks to @arbrk (Arseniy Belorukov))
  • Loading branch information
Ilya Ableev committed Jun 8, 2016
1 parent b0d49f7 commit d04793e
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions zbxtg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,78 @@ TO=$1
SUBJECT=$2
BODY=$3

TG_CHAT=0 # send message to chat or to private chat to user
TG_GROUP=0 # send message to chat or to private chat to user
TG_CHANNEL=0 # send message to channel
METHOD="txt" # sendMessage (simple text) or sendPhoto (attached image)

echo "${BODY}" | grep -q "${ZBX_TG_PREFIX};graphs" && METHOD="image"
echo "${BODY}" | grep -q "${ZBX_TG_PREFIX};chat" && TG_CHAT=1
echo "${BODY}" | grep -q "${ZBX_TG_PREFIX};chat" && TG_GROUP=1
echo "${BODY}" | grep -q "${ZBX_TG_PREFIX};group" && TG_GROUP=1
echo "${BODY}" | grep -q "${ZBX_TG_PREFIX};debug" && ISDEBUG="TRUE"
echo "${BODY}" | grep -q "${ZBX_TG_PREFIX};channel" && TG_CHANNEL=1

IS_DEBUG && LOG="${TMP_DIR}/debug.${TS}.log"
IS_DEBUG && echo -e "TMP_DIR=${TMP_DIR}\nTMP_COOKIE=${TMP_COOKIE}\nTMP_UIDS=${TMP_UIDS}" >>${LOG}

if [ "${TG_CHAT}" -eq 1 ]
if [ "${TG_GROUP}" -eq 1 ]
then
TG_CONTACT_TYPE="chat"
TG_CONTACT_TYPE="group"
else
TG_CONTACT_TYPE="user"
TG_CONTACT_TYPE="private"
fi

TG_CHAT_ID=$(cat ${TMP_UIDS} | awk -F ';' '{if ($1 == "'${TO}'" && $2 == "'${TG_CONTACT_TYPE}'") print $3}' | tail -1)

if [ "${TG_CHANNEL}" -eq 1 ]
then
TG_CHAT_ID="${TO}"
fi

if [ -z "${TG_CHAT_ID}" ]
then
TG_UPDATES=$(${CURL_TG}/getUpdates)
if [ "${TG_CHAT}" -eq 1 ]
then
TG_CHAT_ID=$(echo "${TG_UPDATES}" | sed -e 's/["}{]//g' | awk -F ',' '{if ($8 == "type:group" && $7 == "title:'${TO}'") {gsub("chat:id:", "", $6); print $6}}' | tail -1)
else
TG_CHAT_ID=$(echo "${TG_UPDATES}" | sed -e 's/["}{]//g' | awk -F ',' '{if ($10 == "type:private" && $5 == "username:'${TO}'") {gsub("chat:id:", "", $6); print $6}}' | tail -1)
fi
TG_UPDATES=$(${CURL_TG}/getUpdates | sed -e 's/},{/\n/')
for (( idx=${#TG_UPDATES[@]}-1 ; idx>=0 ; idx-- ))
do
UPDATE="${TG_UPDATES[idx]}"
echo "${UPDATE}"
if [ "${TG_GROUP}" -eq 1 ]
then
TG_CHAT_ID=$(echo "${UPDATE}" | sed -e 's/["}{]//g' | awk -F ',' '{if ($8 == "type:group" && $7 == "title:'${TO}'") {gsub("chat:id:", "", $6); print $6}}' | tail -1)
if [ "$(echo ${TG_CHAT_ID} | grep -Eq '\-[0-9]+' && echo 1 || echo 0)" -eq 1 ]
then
break
fi
else
TG_CHAT_ID=$(echo "${UPDATE}" | sed -e 's/["}{]//g' | awk -F ',' '{if ($10 == "type:private" && $5 == "username:'${TO}'") {gsub("chat:id:", "", $6); print $6}}' | tail -1)
if [ "$(echo ${TG_CHAT_ID} | grep -Eq '[0-9]+' && echo 1 || echo 0)" -eq 1 ]
then
break
fi
fi
done
echo "${TO};${TG_CONTACT_TYPE};${TG_CHAT_ID}" >>${TMP_UIDS}
fi

IS_DEBUG && echo "TG_CHAT_ID: ${TG_CHAT_ID}" >>${LOG}

TG_TEXT=$(echo "${BODY}" | grep -vE "^${ZBX_TG_PREFIX};"; echo "--"; echo "${ZBX_SERVER}")
TG_TEXT=$(echo "${BODY}" | grep -vE "^${ZBX_TG_PREFIX};")
if [ "${ZBX_TG_SIGN}" != "FALSE" ]
then
TG_TEXT=$(echo ${TG_TEXT}; echo "--"; echo "${ZBX_SERVER}")
fi

case "${METHOD}" in

"txt")
TG_MESSAGE=$(echo -e "${SUBJECT}\n${TG_TEXT}")
IS_DEBUG && echo "${CURL_TG}/sendMessage -F \"chat_id=${TG_CHAT_ID}\" -F \"text=${TG_MESSAGE}\"" >>${LOG}
${CURL_TG}/sendMessage -F "chat_id=${TG_CHAT_ID}" -F "text=${TG_MESSAGE}" >>${LOG} 2>&1
ANSWER=$(${CURL_TG}/sendMessage?chat_id=${TG_CHAT_ID} --form "text=${TG_MESSAGE}" 2>&1)
if [ "$(echo "${ANSWER}" | grep -Ec 'migrated.*supergroup')" -eq 1 ]
then
migrate_to_chat_id=$(echo "${ANSWER}" | sed -e 's/["}{]//g' | grep -Eo '\-[0-9]+$')
echo "${TO};${TG_CONTACT_TYPE};${migrate_to_chat_id}" >>${TMP_UIDS}
ANSWER=$(${CURL_TG}/sendMessage?chat_id=${migrate_to_chat_id} --form "text=${TG_MESSAGE}" 2>&1)
fi
;;

"image")
Expand All @@ -101,8 +132,8 @@ case "${METHOD}" in
then
echo "${ZBX_TG_PREFIX}: probably you will see MEDIA_CAPTION_TOO_LONG error, the message has been cut to 200 symbols, https://github.com/ableev/Zabbix-in-Telegram/issues/9#issuecomment-166895044"
fi
IS_DEBUG && echo "${CURL_TG}/sendPhoto -F \"chat_id=${TG_CHAT_ID}\" -F \"caption=${TG_CAPTION}\" -F \"photo=@${CACHE_IMAGE}\"" >>${LOG}
${CURL_TG}/sendPhoto -F "chat_id=${TG_CHAT_ID}" -F "caption=${TG_CAPTION}" -F "photo=@${CACHE_IMAGE}" >>${LOG} 2>&1
IS_DEBUG && echo "${CURL_TG}/sendPhoto?chat_id=${TG_CHAT_ID}\" --form \"caption=${TG_CAPTION}\" -F \"photo=@${CACHE_IMAGE}\"" >>${LOG}
ANSWER=$(${CURL_TG}/sendPhoto?chat_id=${TG_CHAT_ID} --form "caption=${TG_CAPTION}" -F "photo=@${CACHE_IMAGE}")
IS_DEBUG || rm ${CACHE_IMAGE}
;;

Expand Down

0 comments on commit d04793e

Please sign in to comment.