1 | <?php |
||
2 | /* |
||
3 | * LibreNMS |
||
4 | * |
||
5 | * Copyright (c) 2016 Søren Friis Rosiak <[email protected]> |
||
6 | * This program is free software: you can redistribute it and/or modify it |
||
7 | * under the terms of the GNU General Public License as published by the |
||
8 | * Free Software Foundation, either version 3 of the License, or (at your |
||
9 | * option) any later version. Please see LICENSE.txt at the top level of |
||
10 | * the source code distribution for details. |
||
11 | */ |
||
12 | |||
13 | namespace LibreNMS\Alert\Transport; |
||
14 | |||
15 | use LibreNMS\Alert\Transport; |
||
16 | |||
17 | class Msteams extends Transport |
||
18 | { |
||
19 | public function deliverAlert($obj, $opts) |
||
20 | { |
||
21 | if (! empty($this->config)) { |
||
22 | $opts['url'] = $this->config['msteam-url']; |
||
23 | } |
||
24 | |||
25 | return $this->contactMsteams($obj, $opts); |
||
26 | } |
||
27 | |||
28 | public function contactMsteams($obj, $opts) |
||
29 | { |
||
30 | $url = $opts['url']; |
||
31 | $data = [ |
||
32 | 'title' => $obj['title'], |
||
33 | 'themeColor' => self::getColorForState($obj['state']), |
||
34 | 'text' => strip_tags($obj['msg'], '<strong><em><h1><h2><h3><strike><ul><ol><li><pre><blockquote><a><img><p>'), |
||
35 | ]; |
||
36 | $curl = curl_init(); |
||
37 | set_curl_proxy($curl); |
||
38 | curl_setopt($curl, CURLOPT_URL, $url); |
||
39 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||
40 | curl_setopt($curl, CURLOPT_HTTPHEADER, [ |
||
41 | 'Content-type' => 'application/json', |
||
42 | 'Expect:', |
||
43 | ]); |
||
44 | curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); |
||
45 | if ($this->config['use-json'] === 'on' && $obj['uid'] !== '000') { |
||
46 | curl_setopt($curl, CURLOPT_POSTFIELDS, $obj['msg']); |
||
47 | } |
||
48 | $ret = curl_exec($curl); |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
49 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
50 | if ($code != 200) { |
||
51 | var_dump('Microsoft Teams returned Error, retry later'); |
||
52 | |||
53 | return false; |
||
54 | } |
||
55 | |||
56 | return true; |
||
57 | } |
||
58 | |||
59 | public static function configTemplate() |
||
60 | { |
||
61 | return [ |
||
62 | 'config' => [ |
||
63 | [ |
||
64 | 'title' => 'Webhook URL', |
||
65 | 'name' => 'msteam-url', |
||
66 | 'descr' => 'Microsoft Teams Webhook URL', |
||
67 | 'type' => 'text', |
||
68 | ], |
||
69 | [ |
||
70 | 'title' => 'Use JSON?', |
||
71 | 'name' => 'use-json', |
||
72 | 'descr' => 'Compose MessageCard with JSON rather than Markdown', |
||
73 | 'type' => 'checkbox', |
||
74 | 'default' => false, |
||
75 | ], |
||
76 | ], |
||
77 | 'validation' => [ |
||
78 | 'msteam-url' => 'required|url', |
||
79 | ], |
||
80 | ]; |
||
81 | } |
||
82 | } |
||
83 |