|
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. |
0 commit comments