Skip to content

Instantly share code, notes, and snippets.

@cherts
Created May 15, 2020 17:59
Show Gist options
  • Select an option

  • Save cherts/04a0905d53c136ee8d5a81c4af93bc9d to your computer and use it in GitHub Desktop.

Select an option

Save cherts/04a0905d53c136ee8d5a81c4af93bc9d to your computer and use it in GitHub Desktop.
Fixed use http proxy for official media telegram in Zabbix 5.0
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>5.0</version>
<date>2020-05-15T17:57:20Z</date>
<media_types>
<media_type>
<name>Telegram</name>
<type>WEBHOOK</type>
<parameters>
<parameter>
<name>HTTPProxy</name>
<value/>
</parameter>
<parameter>
<name>Message</name>
<value>{ALERT.MESSAGE}</value>
</parameter>
<parameter>
<name>ParseMode</name>
<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>&lt;PLACE YOUR TOKEN&gt;</value>
</parameter>
</parameters>
<script>var Telegram = {&#13;
token: null,&#13;
to: null,&#13;
message: null,&#13;
proxy: null,&#13;
parse_mode: null,&#13;
&#13;
sendMessage: function() {&#13;
var params = {&#13;
chat_id: Telegram.to,&#13;
text: Telegram.message,&#13;
disable_web_page_preview: true,&#13;
disable_notification: false&#13;
},&#13;
data,&#13;
response,&#13;
request = new CurlHttpRequest(),&#13;
url = 'https://api.telegram.org/bot' + Telegram.token + '/sendMessage';&#13;
&#13;
if (Telegram.parse_mode !== null) {&#13;
params['parse_mode'] = Telegram.parse_mode;&#13;
}&#13;
&#13;
request.AddHeader('Content-Type: application/json');&#13;
&#13;
if (Telegram.proxy !== null) {&#13;
request.SetProxy(Telegram.proxy);&#13;
}&#13;
&#13;
data = JSON.stringify(params);&#13;
&#13;
// Remove replace() function if you want to see the exposed token in the log file.&#13;
Zabbix.Log(4, '[Telegram Webhook] URL: ' + url.replace(Telegram.token, '&lt;TOKEN&gt;'));&#13;
Zabbix.Log(4, '[Telegram Webhook] params: ' + data);&#13;
response = request.Post(url, data);&#13;
Zabbix.Log(4, '[Telegram Webhook] HTTP code: ' + request.Status());&#13;
&#13;
try {&#13;
response = JSON.parse(response);&#13;
}&#13;
catch (error) {&#13;
response = null;&#13;
}&#13;
&#13;
if (request.Status() !== 200 || typeof response.ok !== 'boolean' || response.ok !== true) {&#13;
if (typeof response.description === 'string') {&#13;
throw response.description;&#13;
}&#13;
else {&#13;
throw 'Unknown error. Check debug log for more information.'&#13;
}&#13;
}&#13;
}&#13;
}&#13;
&#13;
try {&#13;
var params = JSON.parse(value);&#13;
&#13;
if (typeof params.Token === 'undefined') {&#13;
throw 'Incorrect value is given for parameter &quot;Token&quot;: parameter is missing';&#13;
}&#13;
&#13;
Telegram.token = params.Token;&#13;
&#13;
if (params.HTTPProxy) {&#13;
Telegram.proxy = params.HTTPProxy;&#13;
Zabbix.Log(4, '[Telegram Webhook] Use proxy: ' + params.HTTPProxy);&#13;
}&#13;
&#13;
if (['Markdown', 'HTML', 'MarkdownV2'].indexOf(params.ParseMode) !== -1) {&#13;
Telegram.parse_mode = params.ParseMode;&#13;
}&#13;
&#13;
Telegram.to = params.To;&#13;
Telegram.message = params.Subject + '\n' + params.Message;&#13;
Telegram.sendMessage();&#13;
&#13;
return 'OK';&#13;
}&#13;
catch (error) {&#13;
Zabbix.Log(4, '[Telegram Webhook] notification failed: ' + error);&#13;
throw 'Sending failed: ' + error + '.';&#13;
}</script>
<timeout>10s</timeout>
<description>https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/media/telegram&#13;
&#13;
1. Register bot: send &quot;/newbot&quot; to @BotFather and follow instructions&#13;
2. Copy and paste the obtained token into the &quot;Token&quot; field above&#13;
3. If you want to send personal notifications, you need to get chat id of the user you want to send messages to:&#13;
3.1. Send &quot;/getid&quot; to &quot;@myidbot&quot; in Telegram messenger&#13;
3.2. Copy returned chat id and save it in the &quot;Telegram Webhook&quot; media for the user&#13;
3.3. Ask the user to send &quot;/start&quot; to your bot (Telegram bot won't send anything to the user without it)&#13;
4. If you want to send group notifications, you need to get group id of the group you want to send messages to:&#13;
4.1. Add &quot;@myidbot&quot; to your group&#13;
4.2. Send &quot;/getgroupid@myidbot&quot; in your group&#13;
4.3. Copy returned group id save it in the &quot;Telegram Webhook&quot; media for the user you created for group notifications&#13;
4.4. Send &quot;/start@your_bot_name_here&quot; 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}&#13;
Problem name: {EVENT.NAME}&#13;
Host: {HOST.NAME}&#13;
Severity: {EVENT.SEVERITY}&#13;
Operational data: {EVENT.OPDATA}&#13;
Original problem ID: {EVENT.ID}&#13;
{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}&#13;
Problem name: {EVENT.NAME}&#13;
Host: {HOST.NAME}&#13;
Severity: {EVENT.SEVERITY}&#13;
Original problem ID: {EVENT.ID}&#13;
{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}.&#13;
{EVENT.UPDATE.MESSAGE}&#13;
&#13;
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}&#13;
&#13;
Device IP: {DISCOVERY.DEVICE.IPADDRESS}&#13;
Device DNS: {DISCOVERY.DEVICE.DNS}&#13;
Device status: {DISCOVERY.DEVICE.STATUS}&#13;
Device uptime: {DISCOVERY.DEVICE.UPTIME}&#13;
&#13;
Device service name: {DISCOVERY.SERVICE.NAME}&#13;
Device service port: {DISCOVERY.SERVICE.PORT}&#13;
Device service status: {DISCOVERY.SERVICE.STATUS}&#13;
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}&#13;
Host IP: {HOST.IP}&#13;
Agent port: {HOST.PORT}</message>
</message_template>
</message_templates>
</media_type>
</media_types>
</zabbix_export>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment