Skip to content

Commit b9058a7

Browse files
committed
Add a new driver for WinSMS gateway
1 parent 81b3e81 commit b9058a7

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

src/DriverManager.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Twilio\Rest\Client as Twilio;
99
use Elimuswift\SMS\Drivers\NexmoSMS;
1010
use Elimuswift\SMS\Drivers\TwilioSMS;
11+
use Elimuswift\SMS\Drivers\WinSMS;
1112

1213
/**
1314
* Create driver instances defined in config file.
@@ -78,4 +79,16 @@ protected function createTwilioDriver()
7879
$config['auth_token']
7980
);
8081
}
82+
83+
/**
84+
* Create an instance of the WinSMS driver.
85+
*
86+
* @return \Elimuswift\SMS\Drivers\WinSMS
87+
*/
88+
protected function createWinsmsDriver()
89+
{
90+
$config = $this->app['config']->get('sms.winsms', []);
91+
92+
return new WinSMS($config['api_key']);
93+
}
8194
}

src/Drivers/WinSMS.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Elimuswift\SMS\Drivers;
4+
5+
use Ramsey\Uuid\Uuid;
6+
use GuzzleHttp\Client;
7+
use Illuminate\Support\Collection;
8+
use Elimuswift\SMS\DoesNotReceive;
9+
use Elimuswift\SMS\OutgoingMessage;
10+
use Elimuswift\SMS\Contracts\DriverInterface;
11+
12+
/**
13+
* Send sms messages via africas talking.
14+
*
15+
* @author Master Weez
16+
**/
17+
class WinSMS extends AbstractSMS implements DriverInterface
18+
{
19+
use DoesNotReceive;
20+
21+
/**
22+
* Guzzle HTTP client.
23+
*
24+
* @var Client
25+
*/
26+
protected $client;
27+
28+
/**
29+
* WinSMS api endpoint.
30+
*
31+
* @var string
32+
**/
33+
protected $endpoint = 'https://www.winsms.co.za/api/rest/v1/';
34+
35+
/**
36+
* Create a new intance of WinSMS driver.
37+
*
38+
* @param Client $client
39+
**/
40+
public function __construct($apiKey)
41+
{
42+
$headers = [
43+
'AUTHORIZATION' => $apiKey,
44+
'Content-Type' => 'application/json',
45+
'Accept' => 'application/json',
46+
];
47+
$this->client = new Client(['base_uri' => $this->endpoint, 'headers' => $headers]);
48+
}
49+
50+
/**
51+
* Send sms message.
52+
*
53+
* @return object Elimuswift\SMS\WinSMS
54+
*
55+
* @param object $message Elimuswift\SMS\OutgoingMessage
56+
**/
57+
public function send(OutgoingMessage $message)
58+
{
59+
$body = [
60+
'message' => $message->composeMessage(),
61+
'recipients' => $this->prepareContacts($message->getTo()),
62+
'maxSegments' => ceil(strlen($message->composeMessage()) / 160),
63+
];
64+
65+
return $this->client->post('sms/outgoing/send', ['body' => json_encode($body)]);
66+
}
67+
68+
/**
69+
* Creates many IncomingMessage objects and sets all of the properties.
70+
*
71+
* @param string $rawMessage
72+
*
73+
* @return mixed
74+
*/
75+
protected function processReceive($rawMessage)
76+
{
77+
return $rawMessage;
78+
}
79+
80+
/**
81+
* Prepare message recipients.
82+
*
83+
* @param array $contacts
84+
*
85+
* @return array
86+
**/
87+
protected function prepareContacts(array $contacts)
88+
{
89+
return Collection::make($contacts)->transform(function ($value, $key) {
90+
return [
91+
'mobileNumber' => $value,
92+
'clientMessageId' => Uuid::uuid4(),
93+
];
94+
})->toArray();
95+
}
96+
}

src/SmsServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ public function boot()
2323
*/
2424
public function register()
2525
{
26+
$this->mergeConfigFrom(
27+
__DIR__.'/config/sms.php',
28+
'sms'
29+
);
2630
$this->registerSender();
2731
$this->app->bind('sms', function ($app) {
2832
$sms = new SMS($app['sms.sender'], $app);
2933
//Set the from setting
3034
if ($app['config']->has('sms.from')) {
31-
$sms->alwaysFrom($app['config']['sms']['from']);
35+
$sms->alwaysFrom($app['config']->get('sms.from'));
3236
}
3337

3438
return $sms;

src/config/sms.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@
1313
'username' => env('AT_USERNAME', 'africastalking.username'),
1414
'sandbox' => true,
1515
],
16+
1617
'nexmo' => [
1718
'api_key' => env('NEXMO_KEY', 'Your Nexmo API key'),
1819
'api_secret' => env('NEXMO_SECRET', 'Your Nexmo API secret'),
1920
'encoding' => env('NEXMO_ENCODING', 'unicode'), // Can be "unicode" or "gsm"
2021
],
22+
2123
'twilio' => [
2224
'account_sid' => env('TWILIO_SID', 'Your Twilio SID'),
2325
'auth_token' => env('TWILIO_TOKEN', 'Your Twilio Token'),
2426
'verify' => env('TWILIO_VERIFY', true),
2527
],
28+
29+
'winsms' => [
30+
'api_key' => env('WINSMS_SECRET'),
31+
],
2632
];

0 commit comments

Comments
 (0)