Skip to content

Commit

Permalink
Merge pull request #4 from tylercd100/clickatell
Browse files Browse the repository at this point in the history
Clickatell Support
  • Loading branch information
tylercd100 authored May 1, 2017
2 parents 5c10856 + 6838578 commit 66d2592
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build
composer.lock
vendor
run.php
run.php
ubuntu-xenial-16.04-cloudimg-console.log
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `Monolog SMS` will be documented in this file.

### 1.3.0
- Added support for Clickatell

### 1.2.0
- Added character limit

Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
A Monolog Handler for SMS messaging services

Currently supported
- [Plivo](https://www.plivo.com/)
- [Twilio](https://www.twilio.com/)
- [Clickatell](https://www.clickatell.com/)
- [Plivo](https://www.plivo.com/)

## Installation

Expand All @@ -26,7 +27,7 @@ For Plivo:
use Tylercd100\Monolog\Handler\PlivoHandler;

$handler = new PlivoHandler($token,$auth_id,$fromPhoneNumber,$toPhoneNumber);
$logger = new Monolog\Logger('plivo.example');
$logger = new Monolog\Logger('channel.name');
$logger->pushHandler($handler);
$logger->addCritical("Foo Bar!");
```
Expand All @@ -36,7 +37,17 @@ For Twilio:
use Tylercd100\Monolog\Handler\TwilioHandler;

$handler = new TwilioHandler($secret,$sid,$fromPhoneNumber,$toPhoneNumber);
$logger = new Monolog\Logger('plivo.example');
$logger = new Monolog\Logger('channel.name');
$logger->pushHandler($handler);
$logger->addCritical("Foo Bar!");
```

For Clickatell:
```php
use Tylercd100\Monolog\Handler\ClickatellHandler;

$handler = new ClickatellHandler($authToken,$fromPhoneNumber (/*Optional*/),$toPhoneNumber (/*String|Array*/));
$logger = new Monolog\Logger('channel.name');
$logger->pushHandler($handler);
$logger->addCritical("Foo Bar!");
```
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "tylercd100/monolog-sms",
"description": "A Monolog Handler for SMS messaging services such as Plivo and Twilio.",
"description": "A Monolog Handler for SMS messaging services such as Twilio, Clickatell and Plivo.",
"keywords": [
"clickatell",
"composer",
"handler",
"monolog",
"plivo",
"twilio",
"handler",
"composer",
"tylercd100"
],
"homepage": "https://github.com/tylercd100/monolog-sms",
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/SMSFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public function __construct($format = null, $allowInlineLineBreaks = false, $ign
$dateFormat = null;
parent::__construct($format, $dateFormat, $allowInlineLineBreaks, $ignoreEmptyContextAndExtra);
}
}
}
69 changes: 69 additions & 0 deletions src/Handler/ClickatellHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Tylercd100\Monolog\Handler;

use Exception;
use Monolog\Logger;

/**
* Clickatell - Monolog Handler
* @url https://www.clickatell.com/developers/api-documentation/rest-api-request-parameters
*/
class ClickatellHandler extends SMSHandler
{
/**
* API version 1
*/
const API_V1 = '2010-04-01';

/**
* @param string $secret Twilio API Secret Token
* @param string $fromNumber The phone number that will be shown as the sender ID
* @param string $toNumber The phone number to which the message will be sent
* @param int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param bool $useSSL Whether to connect via SSL.
* @param string $host The Twilio server hostname.
* @param string $version The Twilio API version (default ClickatellHandler::API_V1)
* @param int $limit The character limit
*/
public function __construct($secret, $fromNumber, $toNumber, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'platform.clickatell.com', $version = self::API_V1, $limit = 160)
{
if ($version !== self::API_V1) {
throw new Exception("API Version \'{$version}\' is not supported!");
}
parent::__construct($secret, null, $fromNumber, $toNumber, $level, $bubble, $useSSL, $host, $version, $limit);
}

/**
* {@inheritdoc}
*
* @param array $record
* @return string
*/
protected function buildContent($record)
{
if (strlen($record['formatted']) > $this->limit) {
$record['formatted'] = substr($record['formatted'], 0, $this->limit);
}

$dataArray = [
'content' => $record['formatted'],
'to' => (!is_array($this->toNumber)? [$this->toNumber] : $this->toNumber)
];

($this->fromNumber)? $dataArray['from'] = $this->fromNumber : false;

return json_encode($dataArray);
}

/**
* Builds the URL for the API call
*
* @return string
*/
protected function buildRequestUrl()
{
return "POST /messages HTTP/1.1\r\n";
}
}
8 changes: 4 additions & 4 deletions src/Handler/PlivoHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class PlivoHandler extends SMSHandler
* @param bool $useSSL Whether to connect via SSL.
* @param string $host The Plivo server hostname.
* @param string $version The Plivo API version (default PlivoHandler::API_V1)
* @param string $limit The character limit
* @param int $limit The character limit
*/
public function __construct($authToken, $authId, $fromNumber, $toNumber, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'api.plivo.com', $version = self::API_V1, $limit = 160)
{
if($version !== self::API_V1){
if ($version !== self::API_V1) {
throw new Exception('API Version \'{$version}\' is not supported!');
}
parent::__construct($authToken, $authId, $fromNumber, $toNumber, $level, $bubble, $useSSL, $host, $version, $limit);
Expand All @@ -43,7 +43,7 @@ public function __construct($authToken, $authId, $fromNumber, $toNumber, $level
*/
protected function buildContent($record)
{
if(strlen($record['formatted']) > $this->limit){
if (strlen($record['formatted']) > $this->limit) {
$record['formatted'] = substr($record['formatted'], 0, $this->limit);
}

Expand All @@ -57,7 +57,7 @@ protected function buildContent($record)

/**
* Builds the URL for the API call
*
*
* @return string
*/
protected function buildRequestUrl()
Expand Down
23 changes: 15 additions & 8 deletions src/Handler/SMSHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ abstract class SMSHandler extends SocketHandler
*/
protected $version;

/**
* @var integer
*/
protected $limit;

/**
* @param string $authToken Plivo API Auth Token
* @param string $authId Plivo API Auth ID
Expand All @@ -50,12 +55,11 @@ abstract class SMSHandler extends SocketHandler
* @param bool $useSSL Whether to connect via SSL.
* @param string $host The Plivo server hostname.
* @param string $version The Plivo API version (default PlivoHandler::API_V1)
* @param string $limit The character limit
* @param int $limit The character limit
*/
public function __construct($authToken, $authId, $fromNumber, $toNumber, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'api.plivo.com', $version = null, $limit = 160)
{

if(empty($version)){
if (empty($version)) {
throw new Exception('API Version is empty');
}

Expand All @@ -69,7 +73,6 @@ public function __construct($authToken, $authId, $fromNumber, $toNumber, $level
$this->host = $host;
$this->version = $version;
$this->limit = $limit;

}

/**
Expand All @@ -94,7 +97,7 @@ abstract protected function buildContent($record);

/**
* Builds the URL for the API call
*
*
* @return string
*/
abstract protected function buildRequestUrl();
Expand All @@ -107,12 +110,16 @@ abstract protected function buildRequestUrl();
*/
private function buildHeader($content)
{
$auth = base64_encode($this->authId.":".$this->authToken);
$auth = $this->authToken;

if ($this->authId) {
$auth = "Basic " . base64_encode($this->authId.":".$this->authToken);
}

$header = $this->buildRequestUrl();

$header .= "Host: {$this->host}\r\n";
$header .= "Authorization: Basic ".$auth."\r\n";;
$header .= "Authorization: ".$auth."\r\n";
$header .= "Content-Type: application/json\r\n";
$header .= "Content-Length: " . strlen($content) . "\r\n";
$header .= "\r\n";
Expand All @@ -139,4 +146,4 @@ protected function getDefaultFormatter()
{
return new SMSFormatter();
}
}
}
8 changes: 4 additions & 4 deletions src/Handler/TwilioHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class TwilioHandler extends SMSHandler
* @param bool $useSSL Whether to connect via SSL.
* @param string $host The Twilio server hostname.
* @param string $version The Twilio API version (default TwilioHandler::API_V1)
* @param string $limit The character limit
* @param int $limit The character limit
*/
public function __construct($secret, $sid, $fromNumber, $toNumber, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'api.twilio.com', $version = self::API_V1, $limit = 160)
{
if($version !== self::API_V1){
if ($version !== self::API_V1) {
throw new Exception('API Version \'{$version}\' is not supported!');
}
parent::__construct($secret, $sid, $fromNumber, $toNumber, $level, $bubble, $useSSL, $host, $version, $limit);
Expand All @@ -44,7 +44,7 @@ public function __construct($secret, $sid, $fromNumber, $toNumber, $level = Logg
*/
protected function buildContent($record)
{
if(strlen($record['formatted']) > $this->limit){
if (strlen($record['formatted']) > $this->limit) {
$record['formatted'] = substr($record['formatted'], 0, $this->limit);
}

Expand All @@ -58,7 +58,7 @@ protected function buildContent($record)

/**
* Builds the URL for the API call
*
*
* @return string
*/
protected function buildRequestUrl()
Expand Down
Loading

0 comments on commit 66d2592

Please sign in to comment.