Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit bea53b7

Browse files
committed
initial commit
1 parent d619013 commit bea53b7

14 files changed

+1137
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

+158-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,158 @@
1-
# laravel-notification-channel-twilio
1+
# Twilio notifications channel for Laravel
2+
3+
## Contents
4+
5+
- [Installation](#installation)
6+
- [Usage](#usage)
7+
- [Available Message methods](#available-message-methods)
8+
9+
## Installation
10+
11+
You can install the package via composer:
12+
13+
``` bash
14+
composer require justcoded/laravel-notification-channel-twilio
15+
```
16+
17+
### Configuration
18+
19+
Add your Twilio Account SID, Auth Token, and From Number (optional) to your `.env`:
20+
21+
```dotenv
22+
TWILIO_USERNAME=XYZ # optional when using auth token
23+
TWILIO_PASSWORD=ZYX # optional when using auth token
24+
TWILIO_AUTH_TOKEN=ABCD # optional when using username and password
25+
TWILIO_ACCOUNT_SID=1234 # always required
26+
TWILIO_FROM=100000000 # optional default from
27+
TWILIO_ALPHA_SENDER=HELLO # optional
28+
TWILIO_DEBUG_TO=23423423423 # Set a number that call calls/messages should be routed to for debugging
29+
TWILIO_SMS_SERVICE_SID=MG0a0aaaaaa00aa00a00a000a00000a00a # Optional but recommended
30+
```
31+
32+
### Advanced configuration
33+
34+
Run `php artisan vendor:publish --provider="Justcoded\NotificationChannelTwilio\TwilioProvider"`
35+
```
36+
/config/twilio-notification-channel.php
37+
```
38+
39+
#### Suppressing specific errors or all errors
40+
41+
Publish the config using the above command, and edit the `ignored_error_codes` array. You can get the list of
42+
exception codes from [the documentation](https://www.twilio.com/docs/api/errors).
43+
44+
If you want to suppress all errors, you can set the option to `['*']`. The errors will not be logged but notification
45+
failed events will still be emitted.
46+
47+
#### Recommended Configuration
48+
49+
Twilio recommends always using a [Messaging Service](https://www.twilio.com/docs/sms/services) because it gives you
50+
access to features like Advanced Opt-Out, Sticky Sender, Scaler, Geomatch, Shortcode Reroute, and Smart Encoding.
51+
52+
Having issues with SMS? Check Twilio's [best practices](https://www.twilio.com/docs/sms/services/services-best-practices).
53+
54+
## Upgrading from 2.x to 3.x
55+
56+
If you're upgrading from version `2.x`, you'll need to make sure that your set environment variables match those above
57+
in the config section. None of the environment variable names have changed, but if you used different keys in your
58+
`services.php` config then you'll need to update them to match the above, or publish the config file and change the
59+
`env` key.
60+
61+
You should also remove the old entry for `twilio` from your `services.php` config, since it's no longer used.
62+
63+
The main breaking change between `2.x` and `3.x` is that failed notification will now throw an exception unless they are
64+
in the list of ignored error codes (publish the config file to edit these).
65+
66+
You can replicate the `2.x` behaviour by setting `'ignored_error_codes' => ['*']`, which will case all exceptions to be
67+
suppressed.
68+
69+
## Usage
70+
71+
Now you can use the channel in your `via()` method inside the notification:
72+
73+
``` php
74+
use Justcoded\NotificationChannelTwilio\TwilioChannel;
75+
use Justcoded\NotificationChannelTwilio\TwilioSmsMessage;
76+
use Illuminate\Notifications\Notification;
77+
78+
class AccountApproved extends Notification
79+
{
80+
public function via($notifiable)
81+
{
82+
return [TwilioChannel::class];
83+
}
84+
85+
public function toTwilio($notifiable)
86+
{
87+
return (new TwilioSmsMessage())
88+
->content("Your {$notifiable->service} account was approved!");
89+
}
90+
}
91+
```
92+
93+
You can also send an MMS:
94+
95+
``` php
96+
use Justcoded\NotificationChannelTwilio\TwilioChannel;
97+
use Justcoded\NotificationChannelTwilio\TwilioMmsMessage;
98+
use Illuminate\Notifications\Notification;
99+
100+
class AccountApproved extends Notification
101+
{
102+
public function via($notifiable)
103+
{
104+
return [TwilioChannel::class];
105+
}
106+
107+
public function toTwilio($notifiable)
108+
{
109+
return (new TwilioMmsMessage())
110+
->content("Your {$notifiable->service} account was approved!")
111+
->mediaUrl("https://picsum.photos/300");
112+
}
113+
}
114+
```
115+
116+
Or create a Twilio call:
117+
118+
``` php
119+
use Justcoded\NotificationChannelTwilio\TwilioChannel;
120+
use Justcoded\NotificationChannelTwilio\TwilioCallMessage;
121+
use Illuminate\Notifications\Notification;
122+
123+
class AccountApproved extends Notification
124+
{
125+
public function via($notifiable)
126+
{
127+
return [TwilioChannel::class];
128+
}
129+
130+
public function toTwilio($notifiable)
131+
{
132+
return (new TwilioCallMessage())
133+
->url("http://example.com/your-twiml-url");
134+
}
135+
}
136+
```
137+
138+
In order to let your Notification know which phone are you sending/calling to, the channel will look for the `phone_number` attribute of the Notifiable model. If you want to override this behaviour, add the `routeNotificationForTwilio` method to your Notifiable model.
139+
140+
```php
141+
public function routeNotificationForTwilio()
142+
{
143+
return '+1234567890';
144+
}
145+
```
146+
147+
### Available Message methods
148+
149+
#### TwilioSmsMessage
150+
151+
- `from('')`: Accepts a phone to use as the notification sender.
152+
- `content('')`: Accepts a string value for the notification body.
153+
- `messagingServiceSid('')`: Accepts a messaging service SID to handle configuration.
154+
155+
#### TwilioCallMessage
156+
157+
- `from('')`: Accepts a phone to use as the notification sender.
158+
- `url('')`: Accepts an url for the call TwiML.

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "justcoded/laravel-notification-channel-twilio",
3+
"description": "Provides Twilio notification channel for Laravel",
4+
"keywords": ["laravel", "twilio", "notification", "sms", "call", "mms"],
5+
"authors": [
6+
{
7+
"name": "JustCoded Team",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"php": ">=7.2",
13+
"twilio/sdk": "~6.0",
14+
"illuminate/notifications": "^5.8 || ^6.0 || ^7.0 || ^8.0",
15+
"illuminate/support": "^5.8 || ^6.0 || ^7.0 || ^8.0",
16+
"illuminate/events": "^5.8 || ^6.0 || ^7.0 || ^8.0",
17+
"illuminate/queue": "^5.8 || ^6.0 || ^7.0 || ^8.0"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"NotificationChannels\\Twilio\\": "src"
22+
}
23+
},
24+
"config": {
25+
"sort-packages": true
26+
},
27+
"extra": {
28+
"laravel": {
29+
"providers": [
30+
"NotificationChannels\\Twilio\\TwilioProvider"
31+
]
32+
}
33+
}
34+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
return [
4+
'username' => env('TWILIO_USERNAME'), // optional when using auth token
5+
'password' => env('TWILIO_PASSWORD'), // optional when using auth token
6+
'auth_token' => env('TWILIO_AUTH_TOKEN'), // optional when using username and password
7+
'account_sid' => env('TWILIO_ACCOUNT_SID'),
8+
9+
'from' => env('TWILIO_FROM'), // optional
10+
'alphanumeric_sender' => env('TWILIO_ALPHA_SENDER'),
11+
12+
/**
13+
* See https://www.twilio.com/docs/sms/services.
14+
*/
15+
'sms_service_sid' => env('TWILIO_SMS_SERVICE_SID'),
16+
17+
/**
18+
* Specify a number where all calls/messages should be routed. This can be used in development/staging environments
19+
* for testing.
20+
*/
21+
'debug_to' => env('TWILIO_DEBUG_TO'),
22+
23+
/**
24+
* If an exception is thrown with one of these error codes, it will be caught & suppressed.
25+
* To replicate the 2.x behaviour, specify '*' in the array, which will cause all exceptions to be suppressed.
26+
* Suppressed errors will not be logged or reported, but the `NotificationFailed` event will be emitted.
27+
*
28+
* @see https://www.twilio.com/docs/api/errors
29+
*/
30+
'ignored_error_codes' => [
31+
21608, // The 'to' phone number provided is not yet verified for this account.
32+
21211, // Invalid 'To' Phone Number
33+
21614, // 'To' number is not a valid mobile number
34+
21408, // Permission to send an SMS has not been enabled for the region indicated by the 'To' number
35+
],
36+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NotificationChannels\Twilio\Exceptions;
6+
7+
use NotificationChannels\Twilio\TwilioCallMessage;
8+
use NotificationChannels\Twilio\TwilioSmsMessage;
9+
10+
class CouldNotSendNotification extends \Exception
11+
{
12+
public static function invalidMessageObject($message): self
13+
{
14+
$className = is_object($message) ? get_class($message) : 'Unknown';
15+
16+
return new static(
17+
"Notification was not sent. Message object class `{$className}` is invalid. It should
18+
be either `".TwilioSmsMessage::class.'` or `'.TwilioCallMessage::class.'`');
19+
}
20+
21+
public static function missingFrom(): self
22+
{
23+
return new static('Notification was not sent. Missing `from` number.');
24+
}
25+
26+
public static function invalidReceiver(): self
27+
{
28+
return new static(
29+
'The notifiable did not have a receiving phone number. Add a routeNotificationForTwilio
30+
method or a phone_number attribute to your notifiable.'
31+
);
32+
}
33+
34+
public static function missingAlphaNumericSender(): self
35+
{
36+
return new static(
37+
'Notification was not sent. Missing `alphanumeric_sender` in config'
38+
);
39+
}
40+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NotificationChannels\Twilio\Exceptions;
6+
7+
class InvalidConfigException extends \Exception
8+
{
9+
public static function missingConfig(): self
10+
{
11+
return new self('Missing config. You must set either the username & password or SID and auth token');
12+
}
13+
}

0 commit comments

Comments
 (0)