1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This program is free software: you can redistribute it and/or modify it |
4
|
|
|
* under the terms of the GNU General Public License as published by the |
5
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
6
|
|
|
* option) any later version. Please see LICENSE.txt at the top level of |
7
|
|
|
* the source code distribution for details. |
8
|
|
|
*/ |
9
|
|
|
/** |
10
|
|
|
* Twilio API Transport |
11
|
|
|
* |
12
|
|
|
* @author Andy Rosen <[email protected]> |
13
|
|
|
* @license GPL |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace LibreNMS\Alert\Transport; |
17
|
|
|
|
18
|
|
|
use LibreNMS\Alert\Transport; |
19
|
|
|
|
20
|
|
|
class Twilio extends Transport |
21
|
|
|
{ |
22
|
|
|
public function deliverAlert($obj, $opts) |
23
|
|
|
{ |
24
|
|
|
$twilio_opts['sid'] = $this->config['twilio-sid']; |
|
|
|
|
25
|
|
|
$twilio_opts['token'] = $this->config['twilio-token']; |
26
|
|
|
$twilio_opts['sender'] = $this->config['twilio-sender']; |
27
|
|
|
$twilio_opts['to'] = $this->config['twilio-to']; |
28
|
|
|
|
29
|
|
|
return $this->contacttwilio($obj, $twilio_opts); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function contactTwilio($obj, $opts) |
33
|
|
|
{ |
34
|
|
|
$params = [ |
35
|
|
|
'sid' => $opts['sid'], |
36
|
|
|
'token' => $opts['token'], |
37
|
|
|
'phone' => $opts['to'], |
38
|
|
|
'text' => $obj['title'], |
39
|
|
|
'sender' => $opts['sender'], |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
$url = 'https://api.twilio.com/2010-04-01/Accounts/' . $params['sid'] . '/Messages.json'; |
43
|
|
|
|
44
|
|
|
$data = [ |
45
|
|
|
'From' => $params['sender'], |
46
|
|
|
'Body' => $params['text'], |
47
|
|
|
'To' => $params['phone'], |
48
|
|
|
]; |
49
|
|
|
$post = http_build_query($data); |
50
|
|
|
|
51
|
|
|
$curl = curl_init($url); |
52
|
|
|
|
53
|
|
|
// set_curl_proxy($curl); |
54
|
|
|
|
55
|
|
|
curl_setopt($curl, CURLOPT_POST, true); |
56
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
57
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
58
|
|
|
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
59
|
|
|
curl_setopt($curl, CURLOPT_USERPWD, $params['sid'] . ':' . $params['token']); |
60
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); |
61
|
|
|
|
62
|
|
|
curl_exec($curl); |
63
|
|
|
|
64
|
|
|
if (curl_getinfo($curl, CURLINFO_RESPONSE_CODE)) { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function configTemplate() |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
'config' => [ |
73
|
|
|
[ |
74
|
|
|
'title' => 'SID', |
75
|
|
|
'name' => 'twilio-sid', |
76
|
|
|
'descr' => 'Twilio SID', |
77
|
|
|
'type' => 'text', |
78
|
|
|
], |
79
|
|
|
[ |
80
|
|
|
'title' => 'Token', |
81
|
|
|
'name' => 'twilio-token', |
82
|
|
|
'descr' => 'Twilio Account Token', |
83
|
|
|
'type' => 'text', |
84
|
|
|
], |
85
|
|
|
[ |
86
|
|
|
'title' => 'Mobile Number', |
87
|
|
|
'name' => 'twilio-to', |
88
|
|
|
'descr' => 'Mobile number to SMS', |
89
|
|
|
'type' => 'text', |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
'title' => 'Twilio SMS Number', |
93
|
|
|
'name' => 'twilio-sender', |
94
|
|
|
'descr' => 'Twilio sending number', |
95
|
|
|
'type' => 'text', |
96
|
|
|
], |
97
|
|
|
], |
98
|
|
|
'validation' => [ |
99
|
|
|
'twilio-sid' => 'required|string', |
100
|
|
|
'twilio-token' => 'required|string', |
101
|
|
|
'twilio-to' => 'required', |
102
|
|
|
'twilio-sender' => 'required', |
103
|
|
|
], |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|