Use notification to create posts on Facebook

2.0.0 2019-10-04 02:13 UTC

README

Latest Version on Packagist Software License Build Status StyleCI Quality Score Code Coverage Total Downloads

This package makes it easy to post to Facebook using Laravel notification channels.

Contents

Installation

You can install this package via Composer:

composer require laravel-notification-channels/facebook-poster

Configuration

You will need to create a Facebook app in order to use this channel. Within in this app you will find the app ID and secret. Place them inside your .env file. In order to load them, add this to your config/services.php file:

...
'facebook_poster' => [
    'client_id' => getenv('FACEBOOK_APP_ID'),
    'client_secret' => getenv('FACEBOOK_APP_SECRET'),
    'access_token' => getenv('FACEBOOK_ACCESS_TOKEN'),
],

You will need to create a long-life access token for your Facebook page. You can do so with the Graph API Explorer. Select your Facebook App, then select a Page Access Token for your page. Next, make sure you have both manage_pages and publish_pages as permissions - you may be prompted to authorize them.

Once you have an access token, copy it into the Access Token Tool to extend it for a longer period of time so it doesn't expire.

Usage

Follow Laravel's documentation to add the channel to your Notification class.

Publish Facebook post

use NotificationChannels\FacebookPoster\FacebookPosterChannel;
use NotificationChannels\FacebookPoster\FacebookPosterPost;

class NewsWasPublished extends Notification
{

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [FacebookPosterChannel::class];
    }

    /** 
     * Get the Facebook post representation of the notification.
     *
     * @param  mixed  $notifiable.
     * @return \NotificationChannels\FacebookPoster\FacebookPosterPost
     */
    public function toFacebookPoster($notifiable) {
        return new FacebookPosterPost('Laravel notifications are awesome!');
    }
}

Publish Facebook post with link

It is possible to publish link with your post too. You just have to pass the URL to the withLink method.

public function toFacebookPoster($notifiable) {
    return (new FacebookPosterPost('Laravel notifications are awesome!'))
        ->withLink('https://laravel.com');
}

Publish Facebook post with image

It is possible to publish image with your post too. You just have to pass the image path to the withImage method.

public function toFacebookPoster($notifiable) {
    return (new FacebookPosterPost('Laravel notifications are awesome!'))
        ->withImage(url('image.jpg'));
}

Note that an absolute URL is required.

Publish Facebook post with video

It is also possible to publish video with your post too. You just have to pass the video path to the withVideo method.

public function toFacebookPoster($notifiable) {
    return (new FacebookPosterPost('Laravel notifications are awesome!'))
    	->withVideo('video.mp4', 'My video',  'Remember to like and subscribe.');
}

Publish Facebook scheduled post

It is also possible to publish a scheduled post. You just need to pass an instance of DateTimeInterface in - so any DateTime or Carbon instance will work.

public function toFacebookPoster($notifiable) {
    return (new FacebookPosterPost('Laravel notifications are awesome!'))
    	->scheduledFor(now()->addWeek());
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Credits

License

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