Skip to content
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

Added support for content templates (i.e. whatsapp) #152

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ class AccountApproved extends Notification
}
```

You can also send using Content Templates:

``` php
use NotificationChannels\Twilio\TwilioChannel;
use NotificationChannels\Twilio\TwilioContentTemplateMessage;
use Illuminate\Notifications\Notification;

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

public function toTwilio($notifiable)
{
return (new TwilioContentTemplateMessage())
->contentSid("HXXXXXXXXXXXXXXXXXXXXXXXX")
->contentVariables([
'1' => 'John Doe',
'2' => 'ACME Inc.',
]);
}
}
```

*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).*

Or create a Twilio call:

``` php
Expand Down
7 changes: 7 additions & 0 deletions src/Twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): Messa
]);
}

if ($message instanceof TwilioContentTemplateMessage) {
$this->fillOptionalParams($params, $message, [
'contentSid',
'contentVariables',
]);
}

return $this->twilioService->messages->create($to, $params);
}

Expand Down
44 changes: 44 additions & 0 deletions src/TwilioContentTemplateMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace NotificationChannels\Twilio;

class TwilioContentTemplateMessage extends TwilioSmsMessage
{
/**
* The SID of the content template (starting with H)
* @var null|string
*/
public $contentSid;

/**
* The variables to replace in the content template
* @var null|array|string
*/
public $contentVariables;

/**
* Set the content sid (starting with H).
*
* @param string $contentSid
* @return $this
*/
public function contentSid(string $contentSid): self
{
$this->contentSid = $contentSid;

return $this;
}

/**
* Set the content variables.
*
* @param array $contentVariables The variables to replace in the content template (i.e. ['1' => 'John Doe'])
* @return $this
*/
public function contentVariables(array $contentVariables): self
{
$this->contentVariables = json_encode($contentVariables);

return $this;
}
}