This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia-telegram.xml
230 lines (230 loc) · 10 KB
/
media-telegram.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>5.0</version>
<date>2020-09-17T11:58:03Z</date>
<media_types>
<media_type>
<name>Telegram</name>
<type>WEBHOOK</type>
<parameters>
<parameter>
<name>Message</name>
<value>{ALERT.MESSAGE}</value>
</parameter>
<parameter>
<name>ParseMode</name>
<value>HTML</value>
</parameter>
<parameter>
<name>Subject</name>
<value>{ALERT.SUBJECT}</value>
</parameter>
<parameter>
<name>To</name>
<value>{ALERT.SENDTO}</value>
</parameter>
<parameter>
<name>Token</name>
<value></value>
</parameter>
</parameters>
<script>var Telegram = {
token: null,
to: null,
message: null,
proxy: null,
parse_mode: null,
sendMessage: function() {
var params = {
chat_id: Telegram.to,
text: Telegram.message,
disable_web_page_preview: true,
disable_notification: false
},
data,
response,
request = new CurlHttpRequest(),
url = 'https://api.telegram.org/bot' + Telegram.token + '/sendMessage';
if (Telegram.parse_mode !== null) {
params['parse_mode'] = Telegram.parse_mode;
}
if (Telegram.proxy) {
request.SetProxy(Telegram.proxy);
}
request.AddHeader('Content-Type: application/json');
data = JSON.stringify(params);
// Remove replace() function if you want to see the exposed token in the log file.
Zabbix.Log(4, '[Telegram Webhook] URL: ' + url.replace(Telegram.token, '<TOKEN>'));
Zabbix.Log(4, '[Telegram Webhook] params: ' + data);
response = request.Post(url, data);
Zabbix.Log(4, '[Telegram Webhook] HTTP code: ' + request.Status());
try {
response = JSON.parse(response);
}
catch (error) {
response = null;
}
if (request.Status() !== 200 || typeof response.ok !== 'boolean' || response.ok !== true) {
if (typeof response.description === 'string') {
throw response.description;
}
else {
throw 'Unknown error. Check debug log for more information.'
}
}
}
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/&lt;b&gt;/g,"<b>")
.replace(/&lt;\/b&gt;/g,"</b>")
.replace(/&lt;u&gt;/g,"<u>")
.replace(/&lt;\/u&gt;/g,"</u>")
.replace(/&lt;pre&gt;/g,"<pre>")
.replace(/&lt;\/pre&gt;/g,"</pre>")
.replace(/&lt;i&gt;/g,"<i>")
.replace(/&lt;\/i&gt;/g,"</i>")
.replace(/&lt;s&gt;/g,"<s>")
.replace(/&lt;\/s&gt;/g,"</s>")
.replace(/&lt;code&gt;/g,"<code>")
.replace(/&lt;\/code&gt;/g,"</code>");
}
try {
var params = JSON.parse(value);
if (typeof params.Token === 'undefined') {
throw 'Incorrect value is given for parameter "Token": parameter is missing';
}
Telegram.token = params.Token;
if (params.HTTPProxy) {
Telegram.proxy = params.HTTPProxy;
}
if (['Markdown', 'HTML', 'MarkdownV2'].indexOf(params.ParseMode) !== -1) {
Telegram.parse_mode = params.ParseMode;
}
Telegram.to = params.To;
temp_res = params.Subject;
if(/#SEV0#/i.test(params.Subject)){
temp_res = params.Subject.replace(/#SEV0#/i, String.fromCodePoint(0x1F636));
}else if (/#SEV1#/i.test(params.Subject)){
temp_res = params.Subject.replace(/#SEV1#/i, String.fromCodePoint(0x1F914));
}else if (/#SEV2#/i.test(params.Subject)){
temp_res = params.Subject.replace(/#SEV2#/i, String.fromCodePoint(0x1F62B));
}else if (/#SEV3#/i.test(params.Subject)){
temp_res = params.Subject.replace(/#SEV3#/i, String.fromCodePoint(0x1F62D));
}else if (/#SEV4#/i.test(params.Subject)){
temp_res = params.Subject.replace(/#SEV4#/i, String.fromCodePoint(0x1F628));
}else if (/#SEV5#/i.test(params.Subject)){
temp_res = params.Subject.replace(/#SEV5#/i, String.fromCodePoint(0x1F631));
}
if(/#STAPROBLEM#/i.test(temp_res)){
temp_res = temp_res.replace(/#STAPROBLEM#/i, String.fromCodePoint(0x26A0));
}else if (/#STAOK#/i.test(temp_res)){
temp_res = temp_res.replace(/#STAOK#/i, String.fromCodePoint(0x2705));
}
if(Telegram.parse_mode == 'HTML'){
Telegram.message = escapeHtml(temp_res + '\n' + params.Message);
}else {
Telegram.message = temp_res + '\n' + params.Message;
}
Telegram.sendMessage();
return 'OK';
}
catch (error) {
Zabbix.Log(4, '[Telegram Webhook] notification failed: ' + error);
throw 'Sending failed: ' + error + '.';
}</script>
<timeout>10s</timeout>
<description>https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/media/telegram
1. Register bot: send "/newbot" to @BotFather and follow instructions
2. Copy and paste the obtained token into the "Token" field above
3. If you want to send personal notifications, you need to get chat id of the user you want to send messages to:
3.1. Send "/getid" to "@myidbot" in Telegram messenger
3.2. Copy returned chat id and save it in the "Telegram Webhook" media for the user
3.3. Ask the user to send "/start" to your bot (Telegram bot won't send anything to the user without it)
4. If you want to send group notifications, you need to get group id of the group you want to send messages to:
4.1. Add "@myidbot" to your group
4.2. Send "/getgroupid@myidbot" in your group
4.3. Copy returned group id save it in the "Telegram Webhook" media for the user you created for group notifications
4.4. Send "/start@your_bot_name_here" in your group (Telegram bot won't send anything to the group without it)</description>
<message_templates>
<message_template>
<event_source>TRIGGERS</event_source>
<operation_mode>PROBLEM</operation_mode>
<subject>Problem: {EVENT.NAME}</subject>
<message>Problem started at {EVENT.TIME} on {EVENT.DATE}
Problem name: {EVENT.NAME}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Operational data: {EVENT.OPDATA}
Original problem ID: {EVENT.ID}
{TRIGGER.URL}</message>
</message_template>
<message_template>
<event_source>TRIGGERS</event_source>
<operation_mode>RECOVERY</operation_mode>
<subject>Resolved in {EVENT.DURATION}: {EVENT.NAME}</subject>
<message>Problem has been resolved in {EVENT.DURATION} at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}
Problem name: {EVENT.NAME}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Original problem ID: {EVENT.ID}
{TRIGGER.URL}</message>
</message_template>
<message_template>
<event_source>TRIGGERS</event_source>
<operation_mode>UPDATE</operation_mode>
<subject>Updated problem: {EVENT.NAME}</subject>
<message>{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.
{EVENT.UPDATE.MESSAGE}
Current problem status is {EVENT.STATUS}, acknowledged: {EVENT.ACK.STATUS}.</message>
</message_template>
<message_template>
<event_source>DISCOVERY</event_source>
<operation_mode>PROBLEM</operation_mode>
<subject>Discovery: {DISCOVERY.DEVICE.STATUS} {DISCOVERY.DEVICE.IPADDRESS}</subject>
<message>Discovery rule: {DISCOVERY.RULE.NAME}
Device IP: {DISCOVERY.DEVICE.IPADDRESS}
Device DNS: {DISCOVERY.DEVICE.DNS}
Device status: {DISCOVERY.DEVICE.STATUS}
Device uptime: {DISCOVERY.DEVICE.UPTIME}
Device service name: {DISCOVERY.SERVICE.NAME}
Device service port: {DISCOVERY.SERVICE.PORT}
Device service status: {DISCOVERY.SERVICE.STATUS}
Device service uptime: {DISCOVERY.SERVICE.UPTIME}</message>
</message_template>
<message_template>
<event_source>AUTOREGISTRATION</event_source>
<operation_mode>PROBLEM</operation_mode>
<subject>Autoregistration: {HOST.HOST}</subject>
<message>Host name: {HOST.HOST}
Host IP: {HOST.IP}
Agent port: {HOST.PORT}</message>
</message_template>
</message_templates>
</media_type>
</media_types>
</zabbix_export>