|
| 1 | +# Facebook Notifications Channel for Laravel 5.3 [WIP] |
| 2 | + |
| 3 | +[](https://packagist.org/packages/laravel-notification-channels/facebook) |
| 4 | +[](LICENSE.md) |
| 5 | +[](https://travis-ci.org/laravel-notification-channels/facebook) |
| 6 | +[](https://styleci.io/repos/xxxxxx) |
| 7 | +[](https://insight.sensiolabs.com/projects/xxxxxxxxxx) |
| 8 | +[](https://scrutinizer-ci.com/g/laravel-notification-channels/facebook) |
| 9 | +[](https://packagist.org/packages/laravel-notification-channels/facebook) |
| 10 | + |
| 11 | +This package makes it easy to send notifications using [Facebook](https://developers.facebook.com/docs/messenger-platform/product-overview) with Laravel 5.3. |
| 12 | + |
| 13 | +## Contents |
| 14 | + |
| 15 | +- [Installation](#installation) |
| 16 | + - [Setting up the Facebook service](#setting-up-the-facebook-service) |
| 17 | +- [Usage](#usage) |
| 18 | + - [Available Message methods](#available-message-methods) |
| 19 | +- [Changelog](#changelog) |
| 20 | +- [Testing](#testing) |
| 21 | +- [Security](#security) |
| 22 | +- [Contributing](#contributing) |
| 23 | +- [Credits](#credits) |
| 24 | +- [License](#license) |
| 25 | + |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +You can install the package via composer: |
| 30 | + |
| 31 | +``` bash |
| 32 | +composer require laravel-notification-channels/facebook |
| 33 | +``` |
| 34 | + |
| 35 | +You must install the service provider: |
| 36 | + |
| 37 | +```php |
| 38 | +// config/app.php |
| 39 | +'providers' => [ |
| 40 | + NotificationChannels\Facebook\FacebookServiceProvider::class, |
| 41 | +]; |
| 42 | +``` |
| 43 | + |
| 44 | +## Setting up your Facebook Bot |
| 45 | + |
| 46 | +Follow the [Getting Started](https://developers.facebook.com/docs/messenger-platform/quickstart) guide and get a page token. |
| 47 | + |
| 48 | +Then, configure your Facebook Page Token: |
| 49 | + |
| 50 | +```php |
| 51 | +// config/services.php |
| 52 | +'facebook' => [ |
| 53 | + 'page-token' => env('FACEBOOK_PAGE_TOKEN', 'YOUR BOT TOKEN HERE') |
| 54 | +] |
| 55 | +``` |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +You can now use the channel in your `via()` method inside the Notification class. |
| 60 | + |
| 61 | +``` php |
| 62 | +use NotificationChannels\Facebook\FacebookChannel; |
| 63 | +use NotificationChannels\Facebook\FacebookMessage; |
| 64 | +use NotificationChannels\Facebook\Attachment\Button; |
| 65 | +use NotificationChannels\Facebook\NotificationType; |
| 66 | + |
| 67 | +use Illuminate\Notifications\Notification; |
| 68 | + |
| 69 | +class InvoicePaid extends Notification |
| 70 | +{ |
| 71 | + public function via($notifiable) |
| 72 | + { |
| 73 | + return [FacebookChannel::class]; |
| 74 | + } |
| 75 | + |
| 76 | + public function toFacebook($notifiable) |
| 77 | + { |
| 78 | + $url = url('/invoice/' . $this->invoice->id); |
| 79 | + |
| 80 | + return FacebookMessage::create() |
| 81 | + ->to($this->user->fb_messenger_user_id) // Optional |
| 82 | + ->text('One of your invoices has been paid!') |
| 83 | + ->notificationType(NotificationType::REGULAR) // Optional |
| 84 | + ->buttons([ |
| 85 | + Button::create('View Invoice', $url)->isTypeWebUrl(), |
| 86 | + Button::create('Call Us for Support!', '+1(212)555-2368')->isTypePhoneNumber(), |
| 87 | + Button::create('Start Chatting', ['invoice_id' => $this->invoice->id])->isTypePostback() // Custom payload sent back to your server |
| 88 | + ]); // Buttons are optional as well. |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +Here's a screenshot preview of the above notification on Facebook Messenger: |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +### Routing a message |
| 98 | + |
| 99 | +You can either send the notification by providing with the page-scoped user id (PSID) of the recipient to the `to($userId)` method like shown in the above example or add a `routeNotificationForFacebook()` method in your notifiable model: |
| 100 | + |
| 101 | +``` php |
| 102 | +... |
| 103 | +/** |
| 104 | + * Route notifications for the Facebook channel. |
| 105 | + * |
| 106 | + * @return int |
| 107 | + */ |
| 108 | +public function routeNotificationForFacebook() |
| 109 | +{ |
| 110 | + return $this->fb_messenger_user_id; |
| 111 | +} |
| 112 | +... |
| 113 | +``` |
| 114 | + |
| 115 | +### Available Message methods |
| 116 | + |
| 117 | +- `to($userIdOrPhoneNumber)`: (string) Recipient's page-scoped User ID or Phone number of with the format `+1(212)555-2368`. **NOTE:** Sending a message to phone numbers requires the `pages_messaging_phone_number` permission. Refer [docs](https://developers.facebook.com/docs/messenger-platform/send-api-reference#phone_number) for more information. |
| 118 | +- `text('')`: (string) Notification message. |
| 119 | +- `buttons($buttons = [])`: (array) An array of "Call to Action" buttons (Created using `NotificationChannels\Facebook\Attachment\Button::create()`). You can add up to 3 buttons of one of the following types: `web_url`, `postback` or `phone_number`. See Button methods below for more details. |
| 120 | +- `notificationType('')`: (string) Push Notification type: `REGULAR` will emit a sound/vibration and a phone notification; `SILENT_PUSH` will just emit a phone notification, `NO_PUSH` will not emit either. You can make use of `NotificationType::REGULAR`, `NotificationType::SILENT_PUSH` and `NotificationType::NO_PUSH` to make it easier to work with the type. This is an optional method, defaults to `REGULAR` type. |
| 121 | + |
| 122 | +### Available Button methods |
| 123 | + |
| 124 | +- `title('')`: (string) Button Title. |
| 125 | +- `data('')`: (string) Button Data - It can be a web url, postback data or a formated phone number. |
| 126 | +- `type('')`: (string) Button Type - `web_url`, `postback` or `phone_number`. |
| 127 | +- `isTypeWebUrl()`: Helper method to create a `web_url` type button. |
| 128 | +- `isTypePhoneNumber()`: Helper method to create a `phone_number` type button. |
| 129 | +- `isTypePostback()`: Helper method to create a `postback` type button. |
| 130 | + |
| 131 | +## Contributing |
| 132 | + |
| 133 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 134 | + |
| 135 | +## Credits |
| 136 | + |
| 137 | +- [Syed Irfaq R.](https://github.com/irazasyed) |
| 138 | +- [All Contributors](../../contributors) |
| 139 | + |
| 140 | +## License |
| 141 | + |
| 142 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments