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:
Andy Rosen
2018-10-17 09:19:28 -07:00
committed by Neil Lathwood
parent 001d7d7643
commit 4e5a0fa4d7
3 changed files with 119 additions and 1 deletions
+105
View 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',
]
];
}
}
+13 -1
View File
@@ -433,6 +433,18 @@ Click on 'Add Telegram config' and put your chat id and token into the relevant
| Chat ID | 34243432 |
| Token | 3ed32wwf235234 |
## Twilio SMS
Twilio will send your alert via SMS. From your Twilio account you will need your account SID, account token and your Twilio SMS phone number that you would like to send the alerts from. Twilio's APIs are located at: https://www.twilio.com/docs/api?filter-product=sms
**Example:**
| Config | Example |
| ------ | ------- |
| SID | ACxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| Token | 7xxxx573acxxxbc2xxx308d6xxx652d32 |
| Twilio SMS Number | 8888778660 |
## VictorOps
VictorOps provide a webHook url to make integration extremely simple. To get the URL required login to your VictorOps
account and go to:
@@ -489,4 +501,4 @@ They can be in international dialling format only.
| User | smsfeedback_user |
| Password | smsfeedback_password |
| Mobiles | 71234567890 |
| Sender name| CIA |
| Sender name| CIA |
@@ -67,6 +67,7 @@ if (LegacyAuth::user()->hasGlobalAdmin()) {
<option value="smsfeedback-form">SMSFeedback</option>
<option value="syslog-form">Syslog</option>
<option value="telegram-form">Telegram</option>
<option value="twilio-form">Twilio</option>
<option value="victorops-form">Victorops</option>
<!--Insert more transport type options here has support is added. Value should be: [transport_name]-form -->
</select>