Skip to content

Commit d1144fd

Browse files
authored
Merge pull request #152 from alexw23/twilio-content-templates
Added support for content templates (i.e. whatsapp)
2 parents 11f2dff + 74dd90c commit d1144fd

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,34 @@ class AccountApproved extends Notification
131131
}
132132
```
133133

134+
You can also send using Content Templates:
135+
136+
``` php
137+
use NotificationChannels\Twilio\TwilioChannel;
138+
use NotificationChannels\Twilio\TwilioContentTemplateMessage;
139+
use Illuminate\Notifications\Notification;
140+
141+
class AccountApproved extends Notification
142+
{
143+
public function via($notifiable)
144+
{
145+
return [TwilioChannel::class];
146+
}
147+
148+
public function toTwilio($notifiable)
149+
{
150+
return (new TwilioContentTemplateMessage())
151+
->contentSid("HXXXXXXXXXXXXXXXXXXXXXXXX")
152+
->contentVariables([
153+
'1' => 'John Doe',
154+
'2' => 'ACME Inc.',
155+
]);
156+
}
157+
}
158+
```
159+
160+
*Note: if sending via WhatsApp, you must add `whatsapp:` to the beginning of the phone number (i.e. `->from('whatsapp:+61428000382')`). The number must also be approved as a [WhatsApp Sender](https://www.twilio.com/console/sms/whatsapp/senders).*
161+
134162
Or create a Twilio call:
135163

136164
``` php

src/Twilio.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): Messa
8989
]);
9090
}
9191

92+
if ($message instanceof TwilioContentTemplateMessage) {
93+
$this->fillOptionalParams($params, $message, [
94+
'contentSid',
95+
'contentVariables',
96+
]);
97+
}
98+
9299
return $this->twilioService->messages->create($to, $params);
93100
}
94101

src/TwilioContentTemplateMessage.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace NotificationChannels\Twilio;
4+
5+
class TwilioContentTemplateMessage extends TwilioSmsMessage
6+
{
7+
/**
8+
* The SID of the content template (starting with H)
9+
* @var null|string
10+
*/
11+
public $contentSid;
12+
13+
/**
14+
* The variables to replace in the content template
15+
* @var null|array|string
16+
*/
17+
public $contentVariables;
18+
19+
/**
20+
* Set the content sid (starting with H).
21+
*
22+
* @param string $contentSid
23+
* @return $this
24+
*/
25+
public function contentSid(string $contentSid): self
26+
{
27+
$this->contentSid = $contentSid;
28+
29+
return $this;
30+
}
31+
32+
/**
33+
* Set the content variables.
34+
*
35+
* @param array $contentVariables The variables to replace in the content template (i.e. ['1' => 'John Doe'])
36+
* @return $this
37+
*/
38+
public function contentVariables(array $contentVariables): self
39+
{
40+
$this->contentVariables = json_encode($contentVariables);
41+
42+
return $this;
43+
}
44+
}

0 commit comments

Comments
 (0)