Skip to content

Twiml voice calls #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class AccountApproved extends Notification
}
```

Or create a Twilio call:
Or create a Twilio call, using either an external TwiML url:

``` php
use NotificationChannels\Twilio\TwilioChannel;
Expand All @@ -153,6 +153,32 @@ class AccountApproved extends Notification
}
```

Or create a Twilio call, and send a TwiML response directly:

``` php
use NotificationChannels\Twilio\TwilioChannel;
use NotificationChannels\Twilio\TwilioCallMessage;
use Illuminate\Notifications\Notification;
use Twilio\TwiML\VoiceResponse;

class AccountApproved extends Notification
{
public function via($notifiable)
{
return [TwilioChannel::class];
}

public function toTwilio($notifiable)
{
return (new TwilioCallMessage())
->twiml(
(new VoiceResponse())
->say('Hello world')
);
}
}
```

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.

```php
Expand All @@ -174,6 +200,9 @@ public function routeNotificationForTwilio()

- `from('')`: Accepts a phone to use as the notification sender.
- `url('')`: Accepts an url for the call TwiML.
- `twiml(VoiceResponse)`: Accepts a \Twilio\TwiML\VoiceResponse containing the call TwiML.

> You can use *either* url() *or* twiml() on a TwilioCallMessage object, not both.

## Changelog

Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/InvalidConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public static function missingConfig(): self
{
return new self('Missing config. You must set either the username & password or SID and auth token');
}

public static function multipleContentTypes(): self
{
return new self('Unable to use URL and TWIML call types simultaneously. You can use only one type');
}
}
2 changes: 1 addition & 1 deletion src/Twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): Messa
protected function makeCall(TwilioCallMessage $message, ?string $to): CallInstance
{
$params = [
'url' => trim($message->content),
$message->contentType => trim($message->content),
];

$this->fillOptionalParams($params, $message, [
Expand Down
38 changes: 38 additions & 0 deletions src/TwilioCallMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

namespace NotificationChannels\Twilio;

use NotificationChannels\Twilio\Exceptions\InvalidConfigException;
use Twilio\TwiML\VoiceResponse;

class TwilioCallMessage extends TwilioMessage
{
public const STATUS_CANCELED = 'canceled';
public const STATUS_COMPLETED = 'completed';

public const TYPE_URL = 'url';
public const TYPE_TWIML = 'twiml';

/**
* @var int
*/
public $contentType = null;

/**
* @var null|string
*/
Expand Down Expand Up @@ -35,11 +46,38 @@ class TwilioCallMessage extends TwilioMessage
*/
public function url(string $url): self
{
$this->contentType(self::TYPE_URL);
$this->content = $url;

return $this;
}

/**
* Set the message twiml.
*
* @param string $twiml
* @return $this
*/
public function twiml(VoiceResponse $response): self
{
$this->contentType(self::TYPE_TWIML);
$this->content = (string) $response;

return $this;
}

protected function contentType(string $contentType)
{
if (
! is_null($this->contentType)
&& $contentType !== $this->contentType
) {
InvalidConfigException::multipleContentTypes();
}

$this->contentType = $contentType;
}

/**
* Set the message url request method.
*
Expand Down