• Stars
    star
    145
  • Rank 254,144 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“¨ Facebook Notifications Channel for Laravel

Facebook Notifications Channel for Laravel

Join PHP Chat Chat on Telegram Latest Version on Packagist Software License Quality Score Total Downloads

This package makes it easy to send notifications using the Facebook Messenger with Laravel.

Contents

Installation

You can install the package via composer:

composer require laravel-notification-channels/facebook

Setting up your Facebook Bot

Follow the Getting Started guide in order to create a Facebook Messenger app, a Facebook page and a page token, which is connecting both.

Next we need to add this token to our Laravel configurations. Create a new Facebook section inside config/services.php and place the page token there:

// config/services.php
...
'facebook' => [
    'page-token' => env('FACEBOOK_PAGE_TOKEN', 'YOUR PAGE TOKEN HERE'),
    
    // Optional - Omit this if you want to use default version.
    'version'    => env('FACEBOOK_GRAPH_API_VERSION', '4.0')
    
    // Optional - If set, the appsecret_proof will be sent to verify your page-token.
    'app-secret' => env('FACEBOOK_APP_SECRET', '')
],
...

Usage

Let's take an invoice-paid-notification as an example. You can now use the Facebook channel in your via() method, inside the InvoicePaid class. The to($userId) method defines the Facebook user, you want to send the notification to.

Based on the details you add (text, attachments etc.) will determine automatically the type of message to be sent. For example if you only add text() then it will be a basic message; using attach() will turn this into a attachment message. Having buttons or cards will change this to the Button Template and Generic Template respectivily

use NotificationChannels\Facebook\FacebookChannel;
use NotificationChannels\Facebook\FacebookMessage;
use NotificationChannels\Facebook\Components\Button;
use NotificationChannels\Facebook\Enums\NotificationType;

use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return [FacebookChannel::class];
    }

    public function toFacebook($notifiable)
    {
        $url = url('/invoice/' . $this->invoice->id);

        return FacebookMessage::create()
            ->to($notifiable->fb_messenger_user_id) // Optional
            ->text('One of your invoices has been paid!')
            ->isUpdate() // Optional
            ->isTypeRegular() // Optional
            // Alternate method to provide the notification type.
            // ->notificationType(NotificationType::REGULAR) // Optional
            ->buttons([
                Button::create('View Invoice', $url)->isTypeWebUrl(),
                Button::create('Call Us for Support!', '+1(212)555-2368')->isTypePhoneNumber(),
                Button::create('Start Chatting', ['invoice_id' => $this->invoice->id])->isTypePostback() // Custom payload sent back to your server
            ]); // Buttons are optional as well.
    }
}

The notification will be sent from your Facebook page, whose page token you have configured earlier. Here's a screenshot preview of the notification inside the chat window.

Laravel Facebook Notification Example

Message Examples

Basic Text Message

Send a basic text message to a user

return FacebookMessage::create('You have just paid your monthly fee! Thanks')
    ->to($notifiable->fb_messenger_user_id);
Attachment Message

Send a file attachment to a user (Example is sending a pdf invoice)

return FacebookMessage::create()
    ->attach(AttachmentType::FILE, url('invoices/'.$this->invoice->id))
    ->to($notifiable->fb_messenger_user_id);
Generic (Card Carousel) Message

Send a set of cards / items to a user displayed in a carousel (Example is sending a set of links). Note you can also add up to three buttons per card

return FacebookMessage::create()
    ->to($notifiable->fb_messenger_user_id) // Optional
    ->cards([
        Card::create('Card No.1 Title')
            ->subtitle('An item description')
            ->url('items/'.$this->item[0]->id)
            ->image('items/'.$this->item[0]->id.'/image'),
        
        Card::create('Card No.2 Title')
            ->subtitle('An item description')
            ->url('items/'.$this->item[1]->id)
            ->image('items/'.$this->item[1]->id.'/image')
        // could add buttons using ->buttons($array of Button)
    ]); 

Routing a message

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:

...
/**
 * Route notifications for the Facebook channel.
 *
 * @return int
 */
public function routeNotificationForFacebook()
{
    return $this->fb_messenger_user_id;
}
...

Available Message methods

  • to($recipient, $type): (string|array) Recipient's page-scoped User id, phone_number, user_ref, post_id or comment_id (as one of the supported types - Use Enums\RecipientType to make it easier). Phone number supported format +1(212)555-2368. NOTE: Sending a message to phone numbers requires the pages_messaging_phone_number permission. Refer docs for more information.
  • text(''): (string) Notification message.
  • isResponse(): Set messaging_type as RESPONSE.
  • isUpdate(): (default) Set messaging_type as UPDATE.
  • isMessageTag($messageTag): (string) Set messaging_type as MESSAGE_TAG, you can refer and make use of the NotificationChannels\Facebook\Enums\MessageTag to make it easier to work with the message tag.
  • attach($attachment_type, $url): (AttachmentType, string) An attachment type (IMAGE, AUDIO, VIDEO, FILE) and the url of this attachment
  • buttons($buttons = []): (array) An array of "Call to Action" buttons (Created using NotificationChannels\Facebook\Components\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.
  • cards($cards = []): (array) An array of item cards to be displayed in a carousel (Created using NotificationChannels\Facebook\Components\Card::create()). You can add up to 10 cards. See Card methods below for more details.
  • 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.
  • imageAspectRatio(''): (string) Image Aspect Ratio if Card with image_url present. You can use of ImageAspectRatioType::SQUARE or ImageAspectRatioType::HORIZONTAL. This is an optional method, defaults to ImageAspectRatioType::HORIZONTAL aspect ratio (image should be 1.91:1).
  • isTypeRegular(): Helper method to create a notification type: REGULAR.
  • isTypeSilentPush(): Helper method to create a notification type: SILENT_PUSH.
  • isTypeNoPush(): Helper method to create a notification type: NO_PUSH.

Available Button methods

  • title(''): (string) Button Title.
  • data(''): (string) Button Data - It can be a web url, postback data or a formated phone number.
  • type(''): (string) Button Type - web_url, postback or phone_number. Use ButtonType enumerator for guaranteeing valid values
  • isTypeWebUrl(): Helper method to create a web_url type button.
  • isTypePhoneNumber(): Helper method to create a phone_number type button.
  • isTypePostback(): Helper method to create a postback type button.

Available Card methods

  • title(''): (string) Card Title.
  • subtitle(''): (string) Card Subtitle.
  • url(''): (string) Card Item Url.
  • image(''): (string) Card Image Url. Image ratio should be 1.91:1
  • buttons($buttons = []): (array) An array of "Call to Action" buttons (Created using NotificationChannels\Facebook\Components\Button::create()). You can add up to 3 buttons of one of the following types: web_url, postback or phone_number. See Button methods above for more details.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

telegram

✈️ Telegram Notifications Channel for Laravel
PHP
852
star
2

webpush

Webpush notifications channel for Laravel.
PHP
658
star
3

fcm

Firebase Cloud Messaging (FCM) notifications channel for Laravel
PHP
483
star
4

onesignal

OneSignal notifications channel for Laravel
PHP
283
star
5

pusher-push-notifications

Pusher Beams notifications channel for Laravel
PHP
271
star
6

discord

Discord notification channel for Laravel
PHP
223
star
7

channels

Submit suggestions & pull requests here for new notification channels
186
star
8

apn

APN push notifications channel for Laravel
PHP
185
star
9

twitter

Twitter Notifications Channel for Laravel
PHP
169
star
10

webhook

Webhook notifications channel for Laravel
PHP
154
star
11

microsoft-teams

Microsoft Teams Notifications Channel for Laravel
PHP
133
star
12

facebook-poster

Use notifications to create posts on Facebook
PHP
130
star
13

authy

@Authy notification channel for @Laravel, with the ability to send in-app, sms, and call verification tokens.
PHP
57
star
14

pushover

πŸ“± Pushover notifications channel for Laravel
PHP
54
star
15

gcm

Laravel Notification Channel for Google GCM (Android)
PHP
54
star
16

aws-sns

AWS SNS notification channel for Laravel
PHP
50
star
17

smsc-ru

smsc.ru notifications channel for Laravel 5.3+
PHP
49
star
18

skeleton

PHP
47
star
19

google-chat

Google Chat notification channel for Laravel
PHP
46
star
20

expo

Expo Notifications Channel for Laravel
PHP
37
star
21

backport

Laravel Notifications for Laravel 5.2 / 5.1
PHP
26
star
22

ionic-push-notifications

Ionic Push Notifications Channel for Laravel
PHP
25
star
23

trello

Trello notifications channel for Laravel
PHP
24
star
24

africastalking

AfricasTalking Notification Channel For Laravel
PHP
23
star
25

smsapi

Smsapi notification channel for Laravel 5.5+
PHP
22
star
26

messagebird

Messagebird notifications channel for Laravel
PHP
22
star
27

bearychat

BearyChat notifications channel for Laravel
PHP
22
star
28

website

Laravel Notifications Channel Website
JavaScript
21
star
29

hipchat

HipChat Notifications Channel for Laravel
PHP
18
star
30

gammu

Gammu SMS notifications channel for Laravel 5.3
PHP
14
star
31

pushbullet

Pushbullet notifications channel for Laravel
PHP
14
star
32

rocket-chat

Rocket.Chat notifications channel for Laravel
PHP
14
star
33

lob

lob.com notifications channel for Laravel
PHP
11
star
34

gitter

Gitter.im notifications channel for Laravel
PHP
10
star
35

pubnub

PubNub Notifications Channel for Laravel
PHP
9
star
36

laravel-notification-channels.github.io

Old Source code of laravel-notification-channels.com
HTML
9
star
37

jet-sms

JetSMS notifications channel for Laravel
PHP
9
star
38

clickatell

Clickatell notifications channel for Laravel
PHP
9
star
39

plivo

Plivo notifications channel for Laravel
PHP
8
star
40

wunderlist

Wunderlist notifications channel for Laravel 5.3
PHP
8
star
41

jusibe

Jusibe notifications channel for Laravel
PHP
8
star
42

cmsms

CMSMS notifications channel for Laravel
PHP
7
star
43

smspoh

Smspoh Notifications Channel for Laravel
PHP
7
star
44

workplace

Workplace notification channel for Laravel
PHP
6
star
45

sms77

Seven.io (formerly SMS77) notification channel for Laravel
PHP
6
star
46

turbosms

TurboSMS notification channel for Laravel
PHP
5
star
47

pagerduty

Laravel Notification Channel for PagerDuty
PHP
5
star
48

intercom

Intercom notifications channel for Laravel
PHP
5
star
49

infobip

Infobip notifications channel for Laravel.
PHP
4
star
50

evernote

Evernote notifications channel for Laravel 5.3
PHP
3
star
51

sipgate

Sipgate Notifications Channel for Laravel 5.5+, 6.X and 7
PHP
3
star
52

touch-sms

πŸ“² TouchSMS Notifications Channel for Laravel
PHP
3
star
53

netgsm

NetGsm notification channel for Laravel
3
star
54

pushwoosh

Pushwoosh notifications channel for Laravel
PHP
2
star
55

ovh-sms

PHP
2
star
56

46elks

46Elks notification channel for Laravel
PHP
2
star
57

todoist

Todoist notifications channel for Laravel 5.3
PHP
2
star
58

sms-broadcast

Laravel Notification Channel for Sms Broadcast
PHP
2
star
59

textlocal

PHP
2
star
60

maillift

MailLift notifications channel for Laravel 5.3
PHP
1
star
61

all-my-sms

AllMySms notification channel for Laravel
PHP
1
star
62

vodafone

Vodafone Notification Channel for Laravel
PHP
1
star