Skip to content

Commit

Permalink
websms
Browse files Browse the repository at this point in the history
  • Loading branch information
mannysoft committed Aug 28, 2017
1 parent 01c8fc6 commit 5054ea8
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/DriverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Mannysoft\SMS\Drivers\PlivoSMS;
use Mannysoft\SMS\Drivers\SMSAPISMS;
use Mannysoft\SMS\Drivers\CheapGlobalSMS;
use Mannysoft\SMS\Drivers\WebSMS;
use Mannysoft\SMS\Drivers\TwilioSMS;
use Mannysoft\SMS\Drivers\VoxboneSMS;

Expand Down Expand Up @@ -255,6 +256,20 @@ protected function createCheapGlobalSMSDriver()

return $provider;
}

/**
* Create an instance of the WebSMS driver.
*
* @return SMSAPI
*/
protected function createWebSMSDriver()
{
$config = $this->app['config']->get('sms.web_sms', []);

$provider = new WebSMS(new Client(), $config['key']);

return $provider;
}

/**
* Create an instance of the flowroute driver.
Expand Down
166 changes: 166 additions & 0 deletions src/Drivers/WebSMS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

namespace Mannysoft\SMS\Drivers;

use SMSApi\Client;
use SMSApi\Api\SmsFactory;
use SMSApi\Exception\SmsapiException;
use Mannysoft\SMS\OutgoingMessage;
use Mannysoft\SMS\SMSNotSentException;
use Mannysoft\SMS\MakesRequests;

class WebSMS extends AbstractSMS implements DriverInterface
{
use MakesRequests;

protected $apiBase = 'https://websms.com.cy/api';

public $key;
public $password;
protected $client;
/**
*
*
* @param $authId
* @param $authToken
*/
public function __construct($client, $key)
{
$this->client = $client;
$this->key = $key;
}

/**
* Sends a SMS message.
*
* @param \SimpleSoftwareIO\SMS\OutgoingMessage $message
*/
public function send(OutgoingMessage $message)
{
$from = $message->getFrom();
$composeMessage = $message->composeMessage();

//Convert to callfire format.
$numbers = implode(',', $message->getTo());

$data = [
'key' => $this->key,
'from' => $from,
'to' => $numbers,
'message' => $composeMessage,
//'encoding' => 'GSM',
];

$this->buildCall('/send-sm');
$this->buildBody($data);

$response = $this->getRequest();
$body = json_decode($response->getBody(), true);
// if ($this->hasError($body)) {
// $this->handleError($body);
// }

return $response;
}

/**
* Processing the raw information from a request and inputs it into the IncomingMessage object.
*
* @param $raw
*/
protected function processReceive($raw)
{
$incomingMessage = $this->createIncomingMessage();
$incomingMessage->setRaw($raw);
$incomingMessage->setMessage($raw->resource_uri);
$incomingMessage->setFrom($raw->message_uuid);
$incomingMessage->setId($raw->message_uuid);
$incomingMessage->setTo($raw->to_number);
}

/**
* Checks the server for messages and returns their results.
*
* @param array $options
*
* @return array
*/
public function checkMessages(array $options = [])
{
$start = array_key_exists('start', $options) ? $options['start'] : 0;
$end = array_key_exists('end', $options) ? $options['end'] : 25;

$rawMessages = $this->plivo->get_messages([
'offset' => $start,
'limit' => $end,
]);

$incomingMessages = [];

foreach ($rawMessages['objects'] as $rawMessage) {
$incomingMessage = $this->createIncomingMessage();
$this->processReceive($incomingMessage, $rawMessage);
$incomingMessages[] = $incomingMessage;
}

return $incomingMessages;
}

/**
* Gets a single message by it's ID.
*
* @param string|int $messageId
*
* @return \SimpleSoftwareIO\SMS\IncomingMessage
*/
public function getMessage($messageId)
{
$rawMessage = $this->plivo->get_message(['record_id' => $messageId]);
$incomingMessage = $this->createIncomingMessage();
$this->processReceive($incomingMessage, $rawMessage);

return $incomingMessage;
}

/**
* Receives an incoming message via REST call.
*
* @param mixed $raw
*
* @return \SimpleSoftwareIO\SMS\IncomingMessage
*/
public function receive($raw)
{
if ($this->verify) {
$this->validateRequest();
}

$incomingMessage = $this->createIncomingMessage();
$incomingMessage->setRaw($raw->get());
$incomingMessage->setMessage($raw->get('resource_uri'));
$incomingMessage->setFrom($raw->get('from_number'));
$incomingMessage->setId($raw->get('message_uuid'));
$incomingMessage->setTo($raw->get('to_number'));

return $incomingMessage;
}

/**
* Checks if a message is authentic from Plivo.
*
* @throws \InvalidArgumentException
*/
protected function validateRequest()
{
$data = $_POST;
$url = $this->url;
$signature = $_SERVER['X-Plivo-Signature'];
$authToken = $this->authToken;

if (!$this->plivo->validate_signature($url, $data, $signature, $authToken)) {
throw new \InvalidArgumentException('This request was not able to verify it came from Plivo.');
}

return true;
}
}

0 comments on commit 5054ea8

Please sign in to comment.