1 | <?php |
||
2 | /** |
||
3 | * LibreNMS Google Chat alerting transport |
||
4 | * |
||
5 | * This program is free software: you can redistribute it and/or modify |
||
6 | * it under the terms of the GNU General Public License as published by |
||
7 | * the Free Software Foundation, either version 3 of the License, or |
||
8 | * (at your option) any later version. |
||
9 | * |
||
10 | * This program is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
13 | * GNU General Public License for more details. |
||
14 | * |
||
15 | * You should have received a copy of the GNU General Public License |
||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
17 | * |
||
18 | * @link http://librenms.org |
||
19 | * |
||
20 | * @copyright 2021 Pablo Baldovi |
||
21 | * @author Pablo Baldovi <[email protected]> |
||
22 | */ |
||
23 | |||
24 | namespace LibreNMS\Alert\Transport; |
||
25 | |||
26 | use LibreNMS\Alert\Transport; |
||
27 | use Log; |
||
28 | |||
29 | class Googlechat extends Transport |
||
30 | { |
||
31 | public function deliverAlert($obj, $opts) |
||
32 | { |
||
33 | $googlechat_conf['webhookurl'] = $this->config['googlechat-webhook']; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
34 | |||
35 | return $this->contactGooglechat($obj, $googlechat_conf); |
||
36 | } |
||
37 | |||
38 | public static function contactGooglechat($obj, $data) |
||
39 | { |
||
40 | $payload = '{"text": "' . $obj['msg'] . '"}'; |
||
41 | |||
42 | Log::debug($payload); |
||
43 | |||
44 | // Create a new cURL resource |
||
45 | $ch = curl_init($data['webhookurl']); |
||
46 | |||
47 | // Attach encoded JSON string to the POST fields |
||
48 | curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); |
||
49 | |||
50 | // Set the content type to application/json |
||
51 | curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']); |
||
52 | |||
53 | // Return response instead of outputting |
||
54 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
55 | |||
56 | // Execute the POST request |
||
57 | $result = curl_exec($ch); |
||
58 | |||
59 | // Close cURL resource |
||
60 | |||
61 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
62 | curl_close($ch); |
||
63 | |||
64 | Log::debug($code); |
||
65 | |||
66 | if ($code != 200) { |
||
67 | Log::error('Google Chat Transport Error'); |
||
68 | Log::error($result); |
||
69 | |||
70 | return 'HTTP Status code ' . $code; |
||
71 | } |
||
72 | |||
73 | return true; |
||
74 | } |
||
75 | |||
76 | public static function configTemplate() |
||
77 | { |
||
78 | return [ |
||
79 | 'config' => [ |
||
80 | [ |
||
81 | 'title' => 'Webhook URL', |
||
82 | 'name' => 'googlechat-webhook', |
||
83 | 'descr' => 'Google Chat Room Webhook', |
||
84 | 'type' => 'text', |
||
85 | ], |
||
86 | ], |
||
87 | 'validation' => [ |
||
88 | 'googlechat-webhook' => 'required|string', |
||
89 | ], |
||
90 | ]; |
||
91 | } |
||
92 | } |
||
93 |