You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
-`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
+
25
41
## Usage
26
42
27
43
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
73
89
}
74
90
```
75
91
76
-
## Multiple Connections
92
+
## Notifications
77
93
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.
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
+
}
82
156
```
83
157
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
+
84
164
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.
85
165
86
166
```php
@@ -89,6 +169,8 @@ Then, in your newly created `config/twilio.php` file, you can add new connection
0 commit comments