mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Added Twilio SMS as an Alerting Transport (#9305)
DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926` After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
This commit is contained in:
committed by
Neil Lathwood
parent
001d7d7643
commit
4e5a0fa4d7
105
LibreNMS/Alert/Transport/Twilio.php
Normal file
105
LibreNMS/Alert/Transport/Twilio.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version. Please see LICENSE.txt at the top level of
|
||||
* the source code distribution for details.
|
||||
*/
|
||||
/**
|
||||
* Twilio API Transport
|
||||
* @author Andy Rosen <arosen@arosen.net>
|
||||
* @license GPL
|
||||
* @package LibreNMS
|
||||
* @subpackage Alerts
|
||||
*/
|
||||
namespace LibreNMS\Alert\Transport;
|
||||
|
||||
use LibreNMS\Alert\Transport;
|
||||
|
||||
class Twilio extends Transport
|
||||
{
|
||||
public function deliverAlert($obj, $opts)
|
||||
{
|
||||
$twilio_opts['sid'] = $this->config['twilio-sid'];
|
||||
$twilio_opts['token'] = $this->config['twilio-token'];
|
||||
$twilio_opts['sender'] = $this->config['twilio-sender'];
|
||||
$twilio_opts['to'] = $this->config['twilio-to'];
|
||||
return $this->contacttwilio($obj, $twilio_opts);
|
||||
}
|
||||
|
||||
public static function contactTwilio($obj, $opts)
|
||||
{
|
||||
$params = [
|
||||
'sid' => $opts['sid'],
|
||||
'token' => $opts['token'],
|
||||
'phone' => $opts['to'],
|
||||
'text' => $obj['title'],
|
||||
'sender' => $opts['sender'],
|
||||
];
|
||||
|
||||
$url = 'https://api.twilio.com/2010-04-01/Accounts/' . $params['sid'] . '/Messages.json';
|
||||
|
||||
$data = [
|
||||
'From' => $params['sender'],
|
||||
'Body' => $params['text'],
|
||||
'To' => $params['phone'],
|
||||
];
|
||||
$post = http_build_query($data);
|
||||
|
||||
$curl = curl_init($url);
|
||||
|
||||
// set_curl_proxy($curl);
|
||||
|
||||
curl_setopt($curl, CURLOPT_POST, true);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||
curl_setopt($curl, CURLOPT_USERPWD, $params["sid"]. ":" . $params["token"]);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
|
||||
|
||||
curl_exec($curl);
|
||||
|
||||
if (curl_getinfo($curl, CURLINFO_RESPONSE_CODE)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static function configTemplate()
|
||||
{
|
||||
return [
|
||||
'config' => [
|
||||
[
|
||||
'title' => 'SID',
|
||||
'name' => 'twilio-sid',
|
||||
'descr' => 'Twilio SID',
|
||||
'type' => 'text',
|
||||
],
|
||||
[
|
||||
'title' => 'Token',
|
||||
'name' => 'twilio-token',
|
||||
'descr' => 'Twilio Account Token',
|
||||
'type' => 'text',
|
||||
],
|
||||
[
|
||||
'title' => 'Mobile Number',
|
||||
'name' => 'twilio-to',
|
||||
'descr' => 'Mobile number to SMS',
|
||||
'type' => 'text',
|
||||
],
|
||||
[
|
||||
'title' => 'Twilio SMS Number',
|
||||
'name' => 'twilio-sender',
|
||||
'descr' => 'Twilio sending number',
|
||||
'type' => 'text',
|
||||
],
|
||||
],
|
||||
'validation' => [
|
||||
'twilio-sid' => 'required|string',
|
||||
'twilio-token' => 'required|string',
|
||||
'twilio-to' => 'required',
|
||||
'twilio-sender' => 'required',
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user