Skip to content

Commit 321ec6c

Browse files
committed
Merge remote-tracking branch 'TechWilk/twiml-voice-calls'
2 parents e1aef8b + ef8950e commit 321ec6c

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class AccountApproved extends Notification
131131
}
132132
```
133133

134-
Or create a Twilio call:
134+
Or create a Twilio call, using either an external TwiML url:
135135

136136
``` php
137137
use NotificationChannels\Twilio\TwilioChannel;
@@ -153,6 +153,32 @@ class AccountApproved extends Notification
153153
}
154154
```
155155

156+
Or create a Twilio call, and send a TwiML response directly:
157+
158+
``` php
159+
use NotificationChannels\Twilio\TwilioChannel;
160+
use NotificationChannels\Twilio\TwilioCallMessage;
161+
use Illuminate\Notifications\Notification;
162+
use Twilio\TwiML\VoiceResponse;
163+
164+
class AccountApproved extends Notification
165+
{
166+
public function via($notifiable)
167+
{
168+
return [TwilioChannel::class];
169+
}
170+
171+
public function toTwilio($notifiable)
172+
{
173+
return (new TwilioCallMessage())
174+
->twiml(
175+
(new VoiceResponse())
176+
->say('Hello world')
177+
);
178+
}
179+
}
180+
```
181+
156182
In order to let your Notification know which phone are you sending/calling to, the channel will look for the `phone_number` attribute of the Notifiable model. If you want to override this behaviour, add the `routeNotificationForTwilio` method to your Notifiable model.
157183

158184
```php
@@ -174,6 +200,9 @@ public function routeNotificationForTwilio()
174200

175201
- `from('')`: Accepts a phone to use as the notification sender.
176202
- `url('')`: Accepts an url for the call TwiML.
203+
- `twiml(VoiceResponse)`: Accepts a \Twilio\TwiML\VoiceResponse containing the call TwiML.
204+
205+
> You can use *either* url() *or* twiml() on a TwilioCallMessage object, not both.
177206
178207
## Changelog
179208

src/Exceptions/InvalidConfigException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ public static function missingConfig(): self
1010
{
1111
return new self('Missing config. You must set either the username & password or SID and auth token');
1212
}
13+
14+
public static function multipleContentTypes(): self
15+
{
16+
return new self('Unable to use URL and TWIML call types simultaneously. You can use only one type');
17+
}
1318
}

src/Twilio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): Messa
116116
protected function makeCall(TwilioCallMessage $message, ?string $to): CallInstance
117117
{
118118
$params = [
119-
'url' => trim($message->content),
119+
$message->contentType => trim($message->content),
120120
];
121121

122122
$this->fillOptionalParams($params, $message, [

src/TwilioCallMessage.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
namespace NotificationChannels\Twilio;
44

5+
use NotificationChannels\Twilio\Exceptions\InvalidConfigException;
6+
use Twilio\TwiML\VoiceResponse;
7+
58
class TwilioCallMessage extends TwilioMessage
69
{
710
public const STATUS_CANCELED = 'canceled';
811
public const STATUS_COMPLETED = 'completed';
912

13+
public const TYPE_URL = 'url';
14+
public const TYPE_TWIML = 'twiml';
15+
16+
/**
17+
* @var int
18+
*/
19+
public $contentType = null;
20+
1021
/**
1122
* @var null|string
1223
*/
@@ -35,11 +46,38 @@ class TwilioCallMessage extends TwilioMessage
3546
*/
3647
public function url(string $url): self
3748
{
49+
$this->contentType(self::TYPE_URL);
3850
$this->content = $url;
3951

4052
return $this;
4153
}
4254

55+
/**
56+
* Set the message twiml.
57+
*
58+
* @param string $twiml
59+
* @return $this
60+
*/
61+
public function twiml(VoiceResponse $response): self
62+
{
63+
$this->contentType(self::TYPE_TWIML);
64+
$this->content = (string) $response;
65+
66+
return $this;
67+
}
68+
69+
protected function contentType(string $contentType)
70+
{
71+
if (
72+
! is_null($this->contentType)
73+
&& $contentType !== $this->contentType
74+
) {
75+
InvalidConfigException::multipleContentTypes();
76+
}
77+
78+
$this->contentType = $contentType;
79+
}
80+
4381
/**
4482
* Set the message url request method.
4583
*

0 commit comments

Comments
 (0)