1 | <?php |
||
2 | /* |
||
3 | * LibreNMS |
||
4 | * |
||
5 | * Copyright (c) 2017 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 Ciscospark extends Transport |
||
18 | { |
||
19 | public function deliverAlert($obj, $opts) |
||
20 | { |
||
21 | if (empty($this->config)) { |
||
22 | $room_id = $opts['roomid']; |
||
23 | $token = $opts['token']; |
||
24 | } else { |
||
25 | $room_id = $this->config['room-id']; |
||
26 | $token = $this->config['api-token']; |
||
27 | } |
||
28 | |||
29 | return $this->contactCiscospark($obj, $room_id, $token); |
||
30 | } |
||
31 | |||
32 | public function contactCiscospark($obj, $room_id, $token) |
||
33 | { |
||
34 | $text = null; |
||
35 | $data = [ |
||
36 | 'roomId' => $room_id, |
||
37 | ]; |
||
38 | |||
39 | $akey = 'text'; |
||
40 | if ($this->config['use-markdown'] === 'on') { |
||
41 | $lines = explode("\n", $obj['msg']); |
||
42 | $mlines = []; |
||
43 | /* Remove blank lines as they create weird markdown |
||
44 | * behaviors. |
||
45 | */ |
||
46 | foreach ($lines as $line) { |
||
47 | $line = trim($line); |
||
48 | if ($line != '') { |
||
49 | array_push($mlines, $line); |
||
50 | } |
||
51 | } |
||
52 | $text = implode("\n", $mlines); |
||
53 | $akey = 'markdown'; |
||
54 | } else { |
||
55 | $text = strip_tags($obj['msg']); |
||
56 | } |
||
57 | $data[$akey] = $text; |
||
58 | |||
59 | $curl = curl_init(); |
||
60 | set_curl_proxy($curl); |
||
61 | curl_setopt($curl, CURLOPT_URL, 'https://api.ciscospark.com/v1/messages'); |
||
62 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||
63 | curl_setopt($curl, CURLOPT_HTTPHEADER, [ |
||
64 | 'Content-type' => 'application/json', |
||
65 | 'Expect:', |
||
66 | 'Authorization: Bearer ' . $token, |
||
67 | ]); |
||
68 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
||
69 | $ret = curl_exec($curl); |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
70 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
71 | |||
72 | if ($code != 200) { |
||
73 | echo "Cisco Spark returned Error, retry later\r\n"; |
||
74 | |||
75 | return false; |
||
76 | } |
||
77 | |||
78 | return true; |
||
79 | } |
||
80 | |||
81 | public static function configTemplate() |
||
82 | { |
||
83 | return [ |
||
84 | 'config' => [ |
||
85 | [ |
||
86 | 'title' => 'API Token', |
||
87 | 'name' => 'api-token', |
||
88 | 'descr' => 'CiscoSpark API Token', |
||
89 | 'type' => 'text', |
||
90 | ], |
||
91 | [ |
||
92 | 'title' => 'RoomID', |
||
93 | 'name' => 'room-id', |
||
94 | 'descr' => 'CiscoSpark Room ID', |
||
95 | 'type' => 'text', |
||
96 | ], |
||
97 | [ |
||
98 | 'title' => 'Use Markdown?', |
||
99 | 'name' => 'use-markdown', |
||
100 | 'descr' => 'Use Markdown when sending the alert', |
||
101 | 'type' => 'checkbox', |
||
102 | 'default' => false, |
||
103 | ], |
||
104 | ], |
||
105 | 'validation' => [ |
||
106 | 'api-token' => 'required|string', |
||
107 | 'room-id' => 'required|string', |
||
108 | ], |
||
109 | ]; |
||
110 | } |
||
111 | } |
||
112 |