Skip to content

Commit 7506068

Browse files
committed
Merge branch 'master' of https://github.com/lnpbk/twilio into lnpbk-master
2 parents f751118 + 7e738f9 commit 7506068

15 files changed

+462
-93
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"require": {
1515
"php": ">=5.5.9",
16-
"twilio/sdk": "^4.11",
16+
"twilio/sdk": "^5.4.1",
1717
"illuminate/notifications": "5.1.*|5.2.*|5.3.*|5.4.*",
1818
"illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*",
1919
"illuminate/events": "5.1.*|5.2.*|5.3.*|5.4.*",

src/Exceptions/CouldNotSendNotification.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ public static function invalidReceiver()
3939
method or a phone_number attribute to your notifiable.'
4040
);
4141
}
42+
43+
public static function missingAlphaNumericSender()
44+
{
45+
return new static(
46+
'Notification was not sent. Missing `alphanumeric_sender` in config'
47+
);
48+
}
4249
}

src/Twilio.php

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace NotificationChannels\Twilio;
44

55
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
6-
use Services_Twilio as TwilioService;
6+
use Twilio\Rest\Client as TwilioService;
77

88
class Twilio
99
{
@@ -13,34 +13,38 @@ class Twilio
1313
protected $twilioService;
1414

1515
/**
16-
* Default 'from' from config.
17-
* @var string
16+
* @var TwilioConfig
1817
*/
19-
protected $from;
18+
private $config;
2019

2120
/**
2221
* Twilio constructor.
2322
*
24-
* @param TwilioService $twilioService
25-
* @param string $from
23+
* @param TwilioService $twilioService
24+
* @param TwilioConfig $config
2625
*/
27-
public function __construct(TwilioService $twilioService, $from)
26+
public function __construct(TwilioService $twilioService, TwilioConfig $config)
2827
{
2928
$this->twilioService = $twilioService;
30-
$this->from = $from;
29+
$this->config = $config;
3130
}
3231

3332
/**
3433
* Send a TwilioMessage to the a phone number.
3534
*
36-
* @param TwilioMessage $message
37-
* @param $to
35+
* @param TwilioMessage $message
36+
* @param string $to
37+
* @param bool $useAlphanumericSender
3838
* @return mixed
3939
* @throws CouldNotSendNotification
4040
*/
41-
public function sendMessage(TwilioMessage $message, $to)
41+
public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
4242
{
4343
if ($message instanceof TwilioSmsMessage) {
44+
if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
45+
$message->from($sender);
46+
}
47+
4448
return $this->sendSmsMessage($message, $to);
4549
}
4650

@@ -51,30 +55,68 @@ public function sendMessage(TwilioMessage $message, $to)
5155
throw CouldNotSendNotification::invalidMessageObject($message);
5256
}
5357

54-
protected function sendSmsMessage($message, $to)
58+
/**
59+
* Send an sms message using the Twilio Service.
60+
*
61+
* @param TwilioSmsMessage $message
62+
* @param string $to
63+
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
64+
*/
65+
protected function sendSmsMessage(TwilioSmsMessage $message, $to)
5566
{
56-
return $this->twilioService->account->messages->sendMessage(
57-
$this->getFrom($message),
58-
$to,
59-
trim($message->content)
60-
);
67+
$params = [
68+
'from' => $this->getFrom($message),
69+
'body' => trim($message->content),
70+
];
71+
72+
if ($serviceSid = $this->config->getServiceSid()) {
73+
$params['messagingServiceSid'] = $serviceSid;
74+
}
75+
76+
return $this->twilioService->messages->create($to, $params);
6177
}
6278

63-
protected function makeCall($message, $to)
79+
/**
80+
* Make a call using the Twilio Service.
81+
*
82+
* @param TwilioCallMessage $message
83+
* @param string $to
84+
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
85+
*/
86+
protected function makeCall(TwilioCallMessage $message, $to)
6487
{
65-
return $this->twilioService->account->calls->create(
66-
$this->getFrom($message),
88+
return $this->twilioService->calls->create(
6789
$to,
68-
trim($message->content)
90+
$this->getFrom($message),
91+
['url' => trim($message->content)]
6992
);
7093
}
7194

72-
protected function getFrom($message)
95+
/**
96+
* Get the from address from message, or config.
97+
*
98+
* @param TwilioMessage $message
99+
* @return string
100+
* @throws CouldNotSendNotification
101+
*/
102+
protected function getFrom(TwilioMessage $message)
73103
{
74-
if (! $from = $message->from ?: $this->from) {
104+
if (! $from = $message->getFrom() ?: $this->config->getFrom()) {
75105
throw CouldNotSendNotification::missingFrom();
76106
}
77107

78108
return $from;
79109
}
110+
111+
/**
112+
* Get the alphanumeric sender from config, if one exists.
113+
*
114+
* @return string|null
115+
*/
116+
protected function getAlphanumericSender()
117+
{
118+
if ($sender = $this->config->getAlphanumericSender()) {
119+
return $sender;
120+
}
121+
}
80122
}

src/TwilioCallMessage.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class TwilioCallMessage extends TwilioMessage
77
/**
88
* Set the message url.
99
*
10-
* @param string $url
11-
*
10+
* @param string $url
1211
* @return $this
1312
*/
1413
public function url($url)

src/TwilioChannel.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class TwilioChannel
2323
/**
2424
* TwilioChannel constructor.
2525
*
26-
* @param Twilio $twilio
27-
* @param Dispatcher $events
26+
* @param Twilio $twilio
27+
* @param Dispatcher $events
2828
*/
2929
public function __construct(Twilio $twilio, Dispatcher $events)
3030
{
@@ -35,8 +35,8 @@ public function __construct(Twilio $twilio, Dispatcher $events)
3535
/**
3636
* Send the given notification.
3737
*
38-
* @param mixed $notifiable
39-
* @param \Illuminate\Notifications\Notification $notification
38+
* @param mixed $notifiable
39+
* @param \Illuminate\Notifications\Notification $notification
4040
* @return mixed
4141
* @throws CouldNotSendNotification
4242
*/
@@ -45,6 +45,7 @@ public function send($notifiable, Notification $notification)
4545
try {
4646
$to = $this->getTo($notifiable);
4747
$message = $notification->toTwilio($notifiable);
48+
$useSender = $this->canReceiveAlphanumericSender($notifiable);
4849

4950
if (is_string($message)) {
5051
$message = new TwilioSmsMessage($message);
@@ -54,14 +55,21 @@ public function send($notifiable, Notification $notification)
5455
throw CouldNotSendNotification::invalidMessageObject($message);
5556
}
5657

57-
return $this->twilio->sendMessage($message, $to);
58+
return $this->twilio->sendMessage($message, $to, $useSender);
5859
} catch (Exception $exception) {
5960
$this->events->fire(
6061
new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage()])
6162
);
6263
}
6364
}
6465

66+
/**
67+
* Get the address to send a notification to.
68+
*
69+
* @param mixed $notifiable
70+
* @return mixed
71+
* @throws CouldNotSendNotification
72+
*/
6573
protected function getTo($notifiable)
6674
{
6775
if ($notifiable->routeNotificationFor('twilio')) {
@@ -73,4 +81,17 @@ protected function getTo($notifiable)
7381

7482
throw CouldNotSendNotification::invalidReceiver();
7583
}
84+
85+
/**
86+
* Get the alphanumeric sender.
87+
*
88+
* @param $notifiable
89+
* @return mixed|null
90+
* @throws CouldNotSendNotification
91+
*/
92+
protected function canReceiveAlphanumericSender($notifiable)
93+
{
94+
return method_exists($notifiable, 'canReceiveAlphanumericSender') &&
95+
$notifiable->canReceiveAlphanumericSender();
96+
}
7697
}

src/TwilioConfig.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace NotificationChannels\Twilio;
4+
5+
class TwilioConfig
6+
{
7+
/**
8+
* @var array
9+
*/
10+
private $config;
11+
12+
/**
13+
* TwilioConfig constructor.
14+
*
15+
* @param array $config
16+
*/
17+
public function __construct(array $config)
18+
{
19+
$this->config = $config;
20+
}
21+
22+
/**
23+
* Get the account sid.
24+
*
25+
* @return string
26+
*/
27+
public function getAccountSid()
28+
{
29+
return $this->config['account_sid'];
30+
}
31+
32+
/**
33+
* Get the auth token.
34+
*
35+
* @return string
36+
*/
37+
public function getAuthToken()
38+
{
39+
return $this->config['auth_token'];
40+
}
41+
42+
/**
43+
* Get the default from address.
44+
*
45+
* @return string
46+
*/
47+
public function getFrom()
48+
{
49+
return $this->config['from'];
50+
}
51+
52+
/**
53+
* Get the alphanumeric sender.
54+
*
55+
* @return string
56+
*/
57+
public function getAlphanumericSender()
58+
{
59+
if (isset($this->config['alphanumeric_sender'])) {
60+
return $this->config['alphanumeric_sender'];
61+
}
62+
}
63+
64+
/**
65+
* Get the service sid.
66+
*
67+
* @return string
68+
*/
69+
public function getServiceSid()
70+
{
71+
if (isset($this->config['sms_service_sid'])) {
72+
return $this->config['sms_service_sid'];
73+
}
74+
}
75+
}

src/TwilioMessage.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ abstract class TwilioMessage
1919
public $from;
2020

2121
/**
22+
* Create a message object.
2223
* @param string $content
23-
*
2424
* @return static
2525
*/
2626
public static function create($content = '')
@@ -31,7 +31,7 @@ public static function create($content = '')
3131
/**
3232
* Create a new message instance.
3333
*
34-
* @param string $content
34+
* @param string $content
3535
*/
3636
public function __construct($content = '')
3737
{
@@ -41,8 +41,7 @@ public function __construct($content = '')
4141
/**
4242
* Set the message content.
4343
*
44-
* @param string $content
45-
*
44+
* @param string $content
4645
* @return $this
4746
*/
4847
public function content($content)
@@ -55,8 +54,7 @@ public function content($content)
5554
/**
5655
* Set the phone number the message should be sent from.
5756
*
58-
* @param string $from
59-
*
57+
* @param string $from
6058
* @return $this
6159
*/
6260
public function from($from)
@@ -65,4 +63,14 @@ public function from($from)
6563

6664
return $this;
6765
}
66+
67+
/**
68+
* Get the from address.
69+
*
70+
* @return string
71+
*/
72+
public function getFrom()
73+
{
74+
return $this->from;
75+
}
6876
}

0 commit comments

Comments
 (0)