Skip to content

Commit c9cc249

Browse files
committed
Support URL or TWIML for CallMessage requests
1 parent 509c768 commit c9cc249

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

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)