Skip to content

Commit b322246

Browse files
authored
Merge pull request #14 from progamernoob/main
Added ability to use custom user attributes and macros, made the log less noisy
2 parents cde27af + 8107d20 commit b322246

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Telegram has long been one of my real-time communication media. It is obvious to
1616
- [ACTIVATE CHANGES](#activate-changes)
1717
- [PRIVACY ANONYMIZATION / MASQUERADING](#privacy-anonymization--masquerading)
1818
- [PAGER ADDRESS CHAT-ID INSTEAD OF TELEGRAM GROUP-ID](#pager-address-chat-id-instead-of-telegram-group-id)
19+
- [Custom user attribute](#custom-user-attribute)
1920
- [TROUBLESHOOTING](#troubleshooting)
2021
- [CONTRIBUTIONS](#contributions)
2122
- [LICENSE](#license)
@@ -151,6 +152,27 @@ else
151152
fi
152153
```
153154

155+
### Custom user attribute
156+
You can add a custom user attribute and use it in your script.
157+
Go TO `Setup>UsersCustom>user attributes` and create a custom attribute named `TELEGRAMCHAT`, do not change the name, you can change the Title and Help Text.
158+
<img src="images/create_custom_user_attribute.PNG" alt="Custom User attribute creation" width="100%"/>
159+
160+
#### Optional custom macro
161+
Go To `Setup>Events>Notification configuration>` and edit your notifcation. Add a custom macro.
162+
163+
Regex : ```\d{5,}```
164+
165+
The Macro checks for an at least 5 digit long number, if your Chatid differs, you can adjust that, this also ensures that only those with a chatid set up will get notified. (could work also as a whitelist)
166+
167+
<img src="images/notification_custom_macro.PNG" alt="Custom macro for notification" width="100%"/>
168+
169+
170+
Edit your user and enter your Group/Chat-ID
171+
172+
173+
<img src="images/user_add_chat_id.PNG" alt="Custom macro for notification" width="100%"/>
174+
175+
154176
## TROUBLESHOOTING
155177
For more details and troubleshooting with parameters please check:
156178
* Check_MK notification logfile: /omd/sites/{sitename}/var/log/notify.log

check_mk_telegram-notify.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# Author : https://github.com/filipnet/checkmk-telegram-notify
77
# License : BSD 3-Clause "New" or "Revised" License
88
# ======================================================================================
9-
109
# Telegram API Token
1110
# Find telegram bot named "@botfather", type /mybots, select your bot and select "API Token" to see your current token
1211
if [ -z ${NOTIFY_PARAMETER_1} ]; then
@@ -19,8 +18,12 @@ fi
1918
# Telegram Chat-ID or Group-ID
2019
# Open "https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates" inside your Browser and send a HELLO to your bot, refresh side
2120
if [ -z ${NOTIFY_PARAMETER_2} ]; then
22-
echo "No Telegram Chat-ID or Group-ID provided. Exiting" >&2
23-
exit 2
21+
if [ -z ${NOTIFY_CONTACT_TELEGRAMCHAT} ]; then
22+
echo "No Telegram Chat-ID or Group-ID provided. Exiting" >&2
23+
exit 2
24+
else
25+
CHAT_ID="${NOTIFY_CONTACT_TELEGRAMCHAT}"
26+
fi
2427
else
2528
CHAT_ID="${NOTIFY_PARAMETER_2}"
2629
fi
@@ -55,7 +58,9 @@ if [[ ${NOTIFY_PARAMETER_3} == "privacy" ]]; then
5558
NOTIFY_HOST_ADDRESS_6="${sec1}:${sec2}:${sec3}:${sec4}:ffff:ffff:ffff:${sec8}"
5659
fi
5760
else
58-
echo "Invalid privacy parameter, check your Check_MK settings." >&2
61+
if [ ! -z ${NOTIFY_PARAMETER_3} ]; then
62+
echo "Invalid privacy parameter, check your Check_MK settings." >&2
63+
fi
5964
fi
6065

6166
# Set an appropriate emoji for the current state
@@ -97,10 +102,11 @@ MESSAGE+="%0AIPv4: ${NOTIFY_HOST_ADDRESS_4} %0AIPv6: ${NOTIFY_HOST_ADDRESS_6}%0A
97102
MESSAGE+="${NOTIFY_SHORTDATETIME} | ${OMD_SITE}"
98103

99104
# Send message to Telegram bot
100-
curl -S -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" -d chat_id="${CHAT_ID}" -d text="${MESSAGE}"
105+
response=$(curl -S -s -q -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" -d chat_id="${CHAT_ID}" -d text="${MESSAGE}")
101106
if [ $? -ne 0 ]; then
102107
echo "Not able to send Telegram message" >&2
108+
echo $response >&2
103109
exit 2
104110
else
105111
exit 0
106-
fi
112+
fi
14.2 KB
Loading
5.16 KB
Loading

images/user_add_chat_id.PNG

8.1 KB
Loading

0 commit comments

Comments
 (0)