diff --git a/src/DriverManager.php b/src/DriverManager.php index 3f368be..a3d2ea9 100755 --- a/src/DriverManager.php +++ b/src/DriverManager.php @@ -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; @@ -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. diff --git a/src/Drivers/WebSMS.php b/src/Drivers/WebSMS.php new file mode 100644 index 0000000..56adc0e --- /dev/null +++ b/src/Drivers/WebSMS.php @@ -0,0 +1,166 @@ +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; + } +}