Skip to content

Commit ac464b2

Browse files
committed
Add notification support
1 parent e5870cd commit ac464b2

File tree

9 files changed

+350
-7
lines changed

9 files changed

+350
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Changelog
22

3-
## 1.0.1 (20??-??-??)
3+
## 1.1.0 (20??-??-??)
4+
5+
- Add a notification channel to send SMS messages

README.md

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ Likewise, you will also need to register the facade in your `config/app.php` fil
2222
'TwilioClient' => BabDev\Twilio\Facades\TwilioClient::class,
2323
```
2424

25+
## Configuration
26+
27+
This package can be configured using environment variables in your `.env` file for its basic usage. For advanced use cases, you can publish this package's configuration with this command:
28+
29+
```sh
30+
php artisan vendor:publish --provider="BabDev\Twilio\Providers\TwilioProvider" --tag="config"
31+
```
32+
33+
The below environment variables should be set:
34+
35+
- `TWILIO_CONNECTION`: The name of the default Twilio API connection for your application; if using a single connection this does not need to be changed
36+
- `TWILIO_NOTIFICATION_CHANNEL_CONNECTION`: If using Laravel's notifications system, the name of a Twilio API connection to use in the notification channel (defaulting to your default connection); if using a single connection this does not need to be changed
37+
- `TWILIO_API_SID`: The Twilio API SID to use for the default Twilio API connection
38+
- `TWILIO_API_AUTH_TOKEN`: The Twilio API authentication token to use for the default Twilio API connection
39+
- `TWILIO_API_FROM_NUMBER`: The default sending phone number to use for the default Twilio API connection, note the sending phone number can be changed on a per-message basis
40+
2541
## Usage
2642

2743
Using the `ConnectionManager`, you can quickly create calls and send SMS messages through Twilio's REST API using their PHP SDK. By default, the `ConnectionManager` fulfills the `TwilioClient` contract and can be used by injecting the service into your class/method, retrieving the service from the container, or using the `TwilioClient` facade.
@@ -73,14 +89,78 @@ final class MessagingController
7389
}
7490
```
7591

76-
## Multiple Connections
92+
## Notifications
7793

78-
If your application uses multiple sets of REST API credentials for the Twilio API, you can add the data for multiple connections to the package's configuration. First, you should publish this package's configuration:
94+
Messages can be sent as part of Laravel's [notifications system](https://laravel.com/docs/notifications). A notifiable (such as a User model) should include the "twilio" channel in its `via()` method. When routing the notification, the phone number the message should be sent to should be returned.
7995

80-
```sh
81-
php artisan vendor:publish --provider="BabDev\Twilio\Providers\TwilioProvider" --tag="config"
96+
```php
97+
namespace App\Models;
98+
99+
use Illuminate\Foundation\Auth\User as Authenticatable;
100+
use Illuminate\Notifications\Notifiable;
101+
use Illuminate\Notifications\Notification;
102+
103+
class User extends Authenticatable
104+
{
105+
use Notifiable;
106+
107+
/**
108+
* Get the notification's delivery channels.
109+
*
110+
* @param mixed $notifiable
111+
*
112+
* @return array
113+
*/
114+
public function via($notifiable)
115+
{
116+
// This application's users can receive notifications by mail and Twilio SMS
117+
return ['mail', 'twilio'];
118+
}
119+
120+
/**
121+
* Get the notification routing information for the Twilio driver.
122+
*
123+
* @param Notification $notification
124+
*
125+
* @return string
126+
*/
127+
public function routeNotificationForTwilio($notification)
128+
{
129+
return $this->mobile_number;
130+
}
131+
}
132+
```
133+
134+
For notifications that support being sent as an SMS, you should define a `toTwilio` method on the notification class. This method will receive a $notifiable entity and should return a string containing the message text.
135+
136+
```php
137+
namespace App\Notifications;
138+
139+
use Illuminate\Notifications\Notification;
140+
141+
final class PasswordExpiredNotification extends Notification
142+
{
143+
/**
144+
* Get the Twilio / SMS representation of the notification.
145+
*
146+
* @param mixed $notifiable
147+
*
148+
* @return string
149+
*/
150+
public function toTwilio($notifiable)
151+
{
152+
// The $notifiable in this example is your User model
153+
return sprintf('Hello %s, this is a note that the password for your %s account has expired.', $notifiable->name, config('app.name'));
154+
}
155+
}
82156
```
83157

158+
Your default connection is used for the notification channel by default. If your application utilizes multiple Twilio API connections, you can set the connection which should be used using the `TWILIO_NOTIFICATION_CHANNEL_CONNECTION` environment variable.
159+
160+
## Multiple Connections
161+
162+
If your application uses multiple sets of REST API credentials for the Twilio API, you can add the data for multiple connections to the package's configuration. If you have not already, you will need to publish this package's configuration.
163+
84164
Then, in your newly created `config/twilio.php` file, you can add new connections to the `connections` array. Note, the default "twilio" connection has been created for you and uses environment variables by default. You are free to change the default connection for your application and the name of the "twilio" connection if desired.
85165

86166
```php
@@ -89,6 +169,8 @@ Then, in your newly created `config/twilio.php` file, you can add new connection
89169
return [
90170
'default' => env('TWILIO_CONNECTION', 'twilio'),
91171

172+
'notification_channel' => env('TWILIO_NOTIFICATION_CHANNEL_CONNECTION', env('TWILIO_CONNECTION', 'twilio')),
173+
92174
'connections' => [
93175
'twilio' => [
94176
'sid' => env('TWILIO_API_SID', ''),

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"orchestra/testbench": "^4.0|^5.0",
1616
"phpunit/phpunit": "^8.0|^9.0"
1717
},
18+
"suggest": {
19+
"illuminate/notifications": "To use Laravel's notifications component with this package"
20+
},
1821
"autoload": {
1922
"psr-4": {
2023
"BabDev\\Twilio\\": "src/"

config/twilio.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515

1616
'default' => env('TWILIO_CONNECTION', 'twilio'),
1717

18+
/*
19+
|--------------------------------------------------------------------------
20+
| Notification Channel Connection Name
21+
|--------------------------------------------------------------------------
22+
|
23+
| Here you may specify which of the Twilio connections below you wish
24+
| to use as the connection when using Laravel's notifications system.
25+
| By default, this uses your default connection.
26+
|
27+
*/
28+
29+
'notification_channel' => env('TWILIO_NOTIFICATION_CHANNEL_CONNECTION', env('TWILIO_CONNECTION', 'twilio')),
30+
1831
/*
1932
|--------------------------------------------------------------------------
2033
| Twilio Connections
@@ -25,7 +38,7 @@
2538
| all of which are required parameters for creating a connection to the
2639
| Twilio REST API.
2740
|
28-
| To create a new connection, duplicate the "default" connection
41+
| To create a new connection, duplicate the "twilio" connection
2942
| configuration as a new entry in your connections array and give
3043
| it a unique name. You can now access this connection through the
3144
| connection manager.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace BabDev\Twilio\Notifications\Channels;
4+
5+
use BabDev\Twilio\Contracts\TwilioClient;
6+
use Illuminate\Notifications\Notification;
7+
use Twilio\Exceptions\TwilioException;
8+
use Twilio\Rest\Api\V2010\Account\MessageInstance;
9+
10+
final class TwilioChannel
11+
{
12+
/**
13+
* @var TwilioClient
14+
*/
15+
private $twilio;
16+
17+
/**
18+
* Creates a new Twilio notification channel.
19+
*
20+
* @param TwilioClient $twilio The Twilio client.
21+
*/
22+
public function __construct(TwilioClient $twilio)
23+
{
24+
$this->twilio = $twilio;
25+
}
26+
27+
/**
28+
* Send the given notification.
29+
*
30+
* @param mixed $notifiable
31+
* @param Notification $notification
32+
*
33+
* @return MessageInstance|null
34+
*
35+
* @throws TwilioException on Twilio API failure
36+
*/
37+
public function send($notifiable, Notification $notification): ?MessageInstance
38+
{
39+
$to = $notifiable->routeNotificationFor('twilio', $notification);
40+
41+
if (!$to) {
42+
return null;
43+
}
44+
45+
$message = $notification->toTwilio($notifiable);
46+
47+
if (!$message) {
48+
return null;
49+
}
50+
51+
return $this->twilio->message($to, $message);
52+
}
53+
}

src/Providers/TwilioProvider.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
use BabDev\Twilio\ConnectionManager;
66
use BabDev\Twilio\Contracts\TwilioClient;
7+
use BabDev\Twilio\Notifications\Channels\TwilioChannel;
78
use BabDev\Twilio\Twilio\Http\LaravelHttpClient;
89
use GuzzleHttp\Client as Guzzle;
10+
use Illuminate\Contracts\Config\Repository;
911
use Illuminate\Contracts\Foundation\Application;
1012
use Illuminate\Contracts\Support\DeferrableProvider;
1113
use Illuminate\Http\Client\Factory;
14+
use Illuminate\Notifications\ChannelManager;
15+
use Illuminate\Support\Facades\Notification;
1216
use Illuminate\Support\ServiceProvider;
1317
use Twilio\Http\Client as TwilioHttpClient;
1418
use Twilio\Http\CurlClient;
@@ -55,6 +59,7 @@ public function register(): void
5559
{
5660
$this->registerConnectionManager();
5761
$this->registerHttpClient();
62+
$this->registerNotificationChannel();
5863

5964
$this->mergeConfigFrom(__DIR__ . '/../../config/twilio.php', 'twilio');
6065
}
@@ -101,4 +106,31 @@ static function (Application $app): TwilioHttpClient {
101106
}
102107
);
103108
}
109+
110+
/**
111+
* Registers the binding for the notification channel.
112+
*
113+
* @return void
114+
*/
115+
private function registerNotificationChannel(): void
116+
{
117+
Notification::resolved(static function (ChannelManager $manager): void {
118+
$manager->extend(
119+
'twilio',
120+
static function (Application $app): TwilioChannel {
121+
/** @var Repository $config */
122+
$config = $app->make('config');
123+
124+
/** @var ConnectionManager $manager */
125+
$manager = $app->make(ConnectionManager::class);
126+
127+
return new TwilioChannel(
128+
$manager->connection(
129+
$config->get('twilio.notification_channel', $config->get('twilio.default', 'twilio'))
130+
)
131+
);
132+
}
133+
);
134+
});
135+
}
104136
}

0 commit comments

Comments
 (0)