Skip to content

Commit 63a46d7

Browse files
committed
Initial Commit
0 parents  commit 63a46d7

File tree

12 files changed

+360
-0
lines changed

12 files changed

+360
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Nexmo / Laravel Notifications
2+
3+
Although Nexmo is [built in to Laravel](https://laravel.com/docs/5.7/notifications#sms-notifications) as the default SMS provider, that is only a small portion of the available communication channels we offer.
4+
5+
This package adds the ability to send notifications to WhatsApp, Facebook Messenger and Viber via Nexmo.
6+
7+
## Usage
8+
9+
To use this package, run `composer require nexmo/laravel-notifications`. Once it completes, you can implement the following methods on your notification:
10+
11+
* `toNexmoWhatsApp`
12+
* `toNexmoFacebook`
13+
* `toNexmoViberServiceMessage`
14+
* `toNexmoSms`
15+
16+
See [examples/Notification/MerryChristmas.php](examples/Notificatoin/MerryChristmas.php) for a complete example.
17+
18+
To send a notification, specify the class name you'd like to use:
19+
20+
```php
21+
// To a user
22+
$user->notify(new \App\Notifications\MerryChristmas());
23+
24+
// To any person
25+
Notification::route(
26+
\Nexmo\Notifications\WhatsApp::class,
27+
'YOUR_NUMBER'
28+
)->notify(new \App\Notifications\MerryChristmas());
29+
```
30+
31+
As each notification receives a `$notifiable` (usually a user) it can decide how best to route the information. In this case, it checks the `via_whatsapp` property on the user and sends via WhatsApp if it's true. Otherwise it falls back to email
32+
33+
```
34+
public function via($notifiable)
35+
{
36+
return $notifiable->via_whatsapp ? [\Nexmo\Notifications\WhatsApp::class] : ['mail'];
37+
}
38+
```
39+
40+
### Message Types
41+
42+
Nexmo supports multiple message types, depending on the channel that you're sending to. The `Text` type is the safest if you want to deliver to all channels:
43+
44+
```
45+
public function toNexmoWhatsApp($notifiable)
46+
{
47+
return (new \Nexmo\Notifications\Message\Text)
48+
->content('This is a message being sent to WhatsApp');
49+
}
50+
```
51+
52+
### Caveats
53+
54+
For some channels you need to send a templated message before you can send a free text message due to spam control rules. Here's an example of how to use a preapproved template intended for two-factor authentication purposes:
55+
56+
```
57+
public function toNexmoWhatsApp($notifiable)
58+
{
59+
return (new \Nexmo\Notifications\Message\Template)
60+
->name("whatsapp:hsm:technology:nexmo:verify")
61+
->parameters([
62+
["default" => "Your Brand"],
63+
["default" => "64873"],
64+
["default" => "10"],
65+
]);
66+
}
67+
```
68+
69+
If the recipient replies to your message, you can send them `Text` type messages without any issues
70+
71+
## Configuration
72+
73+
### Authentication
74+
75+
This notifications package is built on top of [nexmo/laravel](https://github.com/Nexmo/nexmo-laravel) and uses the Nexmo client from there.
76+
77+
For this to work, you need to set your application ID and path to your private key in the `.env` file:
78+
79+
```
80+
NEXMO_APPLICATION_ID=my_application_id
81+
NEXMO_PRIVATE_KEY=./private.key
82+
```
83+
84+
### Setting the `from` address
85+
86+
You can set a `from `address via the `.env` file. This package will look for provider specific entries before falling back to `NEXMO_FROM`.
87+
88+
```
89+
NEXMO_FROM_SMS=""
90+
NEXMO_FROM_WHATSAPP=""
91+
NEXMO_FROM_MESSENGER=""
92+
NEXMO_FROM_VIBER_SERVICE_MSG=""
93+
NEXMO_FROM="" # This is the default if any of the above aren't set
94+
```
95+
96+
Alternatively, you can set a `from` address for a single notification by calling the `->from()` method on a message:
97+
98+
```php
99+
public function toNexmoViberServiceMessage($notifiable)
100+
{
101+
return (new Text)->content('Merry Christmas Viber!')->from("YOUR_ID");
102+
}
103+
```

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "nexmo/laravel-notification",
3+
"description": "Send SMS, WhatsApp, Viber and Facebook Messenger messages via Nexmo",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Michael Heap",
9+
"email": "michael.heap@vonage.com"
10+
}
11+
],
12+
"require": {
13+
"guzzlehttp/guzzle": "^6.3",
14+
"nexmo/laravel": "^1.1"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Nexmo\\": "src"
19+
}
20+
}
21+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Notifications\Notification;
7+
8+
use Nexmo\Notifications\Sms;
9+
use Nexmo\Notifications\WhatsApp;
10+
use Nexmo\Notifications\Facebook;
11+
use Nexmo\Notifications\ViberServiceMessage;
12+
13+
use Nexmo\Notifications\Message\Text;
14+
15+
class MerryChristmas extends Notification
16+
{
17+
use Queueable;
18+
19+
public function via($notifiable)
20+
{
21+
return [
22+
Sms::class,
23+
WhatsApp::class,
24+
Facebook::class,
25+
ViberServiceMessage::class,
26+
];
27+
}
28+
29+
public function toNexmoWhatsApp($notifiable)
30+
{
31+
return (new Text)->content('Merry Christmas WhatsApp!');
32+
}
33+
34+
public function toNexmoFacebook($notifiable)
35+
{
36+
return (new Text)->content('Merry Christmas Facebook!');
37+
}
38+
39+
public function toNexmoViberServiceMessage($notifiable)
40+
{
41+
return (new Text)->content('Merry Christmas Viber!');
42+
}
43+
44+
public function toNexmoSms($notifiable)
45+
{
46+
return (new Text)->content('Merry Christmas SMS!');
47+
}
48+
}
49+

src/Notifications/Facebook.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Nexmo\Notifications;
4+
5+
class Facebook extends MessageChannel {
6+
protected $channelName = 'messenger';
7+
protected $channelIdField = 'id';
8+
protected $notificationMappingMethod = 'toNexmoFacebook';
9+
}
10+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Nexmo\Notifications\Message;
4+
5+
class Template extends Base {
6+
7+
protected $name;
8+
protected $parameters;
9+
10+
public function name($name) {
11+
$this->name = $name;
12+
return $this;
13+
}
14+
15+
public function parameters($parameters) {
16+
$this->parameters = $parameters;
17+
return $this;
18+
}
19+
20+
public function toNexmoApi($from, $to) {
21+
return [
22+
'from' => $from,
23+
'to' => $to,
24+
'message' => [
25+
'content' => [
26+
'type' => 'template',
27+
"template" => [
28+
"name" => $this->name,
29+
"parameters" => $this->parameters
30+
]
31+
]
32+
]
33+
];
34+
}
35+
36+
}

src/Notifications/Message/Text.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Nexmo\Notifications\Message;
4+
5+
class Text extends Base {
6+
protected $content;
7+
8+
public function content($content) {
9+
$this->content = $content;
10+
return $this;
11+
}
12+
13+
public function toNexmoApi($from, $to) {
14+
return [
15+
'from' => $from,
16+
'to' => $to,
17+
'message' => [
18+
'content' => [
19+
'type' => 'text',
20+
'text' => $this->content
21+
]
22+
]
23+
];
24+
}
25+
}

src/Notifications/Message/base.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Nexmo\Notifications\Message;
4+
5+
abstract class Base {
6+
protected $from;
7+
8+
public function from($from) {
9+
$this->from = $from;
10+
return $this;
11+
}
12+
13+
public function getFrom($channelName){
14+
$defaultEnv = env('NEXMO_FROM');
15+
$specificEnv = env('NEXMO_FROM_'. strtoupper($channelName));
16+
17+
if (!$this->from) {
18+
if ($specificEnv) {
19+
return $specificEnv;
20+
}
21+
return $defaultEnv;
22+
}
23+
24+
return $this->from;
25+
}
26+
}

src/Notifications/MessageChannel.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Nexmo\Notifications;
4+
5+
use Nexmo;
6+
use Illuminate\Notifications\Notification;
7+
8+
abstract class MessageChannel {
9+
10+
protected $channelName;
11+
protected $channelIdField;
12+
protected $channelFromField;
13+
protected $notificationMappingMethod;
14+
15+
public function send($notifiable, Notification $notification)
16+
{
17+
if (! $to = $notifiable->routeNotificationFor(static::class)) {
18+
return;
19+
}
20+
21+
$message = $notification->{$this->notificationMappingMethod}($notifiable);
22+
23+
$response = $this->sendMessage(
24+
$message->toNexmoApi(
25+
$this->from($message->getFrom($this->channelName)),
26+
$this->to($to)
27+
)
28+
);
29+
}
30+
31+
protected function toEndpoint($id, $field) {
32+
return ['type' => $this->channelName, $field => $id];
33+
}
34+
35+
protected function to($id) {
36+
return $this->toEndpoint($id, $this->channelIdField);
37+
}
38+
39+
protected function from($id) {
40+
$field = $this->channelIdField;
41+
if ($this->channelFromField) {
42+
$field = $this->channelFromField;
43+
}
44+
return $this->toEndpoint($id, $field);
45+
}
46+
47+
protected function sendMessage($body) {
48+
$client = new \GuzzleHttp\Client();
49+
return $client->request('POST', 'https://api.nexmo.com/v0.1/messages', [
50+
'headers' => [
51+
'Authorization' => 'Bearer '. Nexmo::generateJwt(),
52+
'Content-Type' => 'application/json',
53+
'Accept' => 'application/json'
54+
],
55+
'body' => json_encode($body)
56+
]);
57+
}
58+
}
59+

src/Notifications/Sms.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Nexmo\Notifications;
4+
5+
class Sms extends MessageChannel {
6+
protected $channelName = 'sms';
7+
protected $channelIdField = 'number';
8+
protected $notificationMappingMethod = 'toNexmoSms';
9+
}
10+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Nexmo\Notifications;
4+
5+
class ViberServiceMessage extends MessageChannel {
6+
protected $channelName = 'viber_service_msg';
7+
protected $channelFromField = 'id';
8+
protected $channelIdField = 'number';
9+
protected $notificationMappingMethod = 'toNexmoViberServiceMessage';
10+
}
11+

src/Notifications/WhatsApp.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Nexmo\Notifications;
4+
5+
class WhatsApp extends MessageChannel {
6+
protected $channelName = 'whatsapp';
7+
protected $channelIdField = 'number';
8+
protected $notificationMappingMethod = 'toNexmoWhatsApp';
9+
}

0 commit comments

Comments
 (0)