Skip to content

Commit a88356d

Browse files
authored
Error handling
Makes it easier to debug parametrisation errors and transmission failures.
1 parent 2e486d5 commit a88356d

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

check_mk_telegram-notify.sh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@
99

1010
# Telegram API Token
1111
# Find telegram bot named "@botfarther", type /mybots, select your bot and select "API Token" to see your current token
12-
TOKEN=${NOTIFY_PARAMETER_1}
12+
if [ -z ${NOTIFY_PARAMETER_1} ]; then
13+
echo "No Telegram token ID provided. Exiting" >&2
14+
exit 2
15+
else
16+
TOKEN="${NOTIFY_PARAMETER_1}"
17+
fi
1318

1419
# Telegram Chat-ID or Group-ID
1520
# Open "https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates" inside your Browser and send a HELLO to your bot, refresh side
16-
CHAT_ID=${NOTIFY_PARAMETER_2}
21+
if [ -z ${NOTIFY_PARAMETER_2} ]; then
22+
echo "No Telegram Chat-ID or Group-ID provided. Exiting" >&2
23+
exit 2
24+
else
25+
CHAT_ID="${NOTIFY_PARAMETER_2}"
26+
fi
1727

1828
# Create a MESSAGE variable to send to your Telegram bot
1929
MESSAGE="${NOTIFY_HOSTNAME} (${NOTIFY_HOSTALIAS})%0A"
@@ -30,7 +40,10 @@ MESSAGE+="%0AIPv4: ${NOTIFY_HOST_ADDRESS_4} %0AIPv6: ${NOTIFY_HOST_ADDRESS_6}%0A
3040
MESSAGE+="${NOTIFY_SHORTDATETIME}"
3141

3242
# Send message to Telegram bot
33-
curl -s -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" -d chat_id="${CHAT_ID}" -d text="${MESSAGE}" >> /dev/null
34-
35-
# End of script
36-
exit 0
43+
curl -S -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" -d chat_id="${CHAT_ID}" -d text="${MESSAGE}"
44+
if [ $? -ne 0 ]; then
45+
echo "Not able to send Telegram message" >&2
46+
exit 2
47+
else
48+
exit 0
49+
fi

0 commit comments

Comments
 (0)