This package makes it easy to send notifications via Twitter with Laravel 5.4

1.0.3 2017-01-27 16:47 UTC

README

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

This package makes it easy to send notifications using Twitter with Laravel 5.4.

Contents

Installation

You can install this package via composer:

composer require laravel-notification-channels/twitter

Next add the service provider to your config/app.php:

...
'providers' => [
    ...
     NotificationChannels\Twitter\TwitterServiceProvider::class,
],
...

Setting up the Twitter service

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

...
'twitter' => [
    'consumer_key'    => getenv('TWITTER_CONSUMER_KEY'),
    'consumer_secret' => getenv('TWITTER_CONSUMER_SECRET'),
    'access_token'    => getenv('TWITTER_ACCESS_TOKEN'),
    'access_secret'   => getenv('TWITTER_ACCESS_SECRET')
]
...

This will load the Twitter app data from the .env file. Make sure to use the same keys you have used there like TWITTER_CONSUMER_KEY.

Usage

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

Publish Twitter status update

use NotificationChannels\Twitter\TwitterChannel;
use NotificationChannels\Twitter\TwitterMessage;

class NewsWasPublished extends Notification
{

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

    public function toTwitter($notifiable) {
        return new TwitterStatusUpdate('Laravel notifications are awesome!');
    }
}

Take a closer look at the TwitterStatusUpdate object. This is where the magic happens.

public function toTwitter($notifiable) {
    return new TwitterStatusUpdate('Laravel notifications are awesome!');
}

Publish Twitter status update with images

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

public function toTwitter($notifiable) {
    return (new TwitterStatusUpdate('Laravel notifications are awesom!'))->withImage('marcel.png');
}

If you want to use multiple images, just pass an array of paths.

return (new TwitterStatusUpdate('Laravel notifications are awesom!'))->withImage([
    public_path('marcel.png'),
    public_path('mohamed.png')
]);

Send a direct message

To send a Twitter direct message to a specific user, you will need the TwitterDirectMessage class. Provide the Twitter user handler as the first parameter and the the message as the second one.

public function toTwitter($notifiable) {
     return new TwitterDirectMessage('marcelpociot', 'Hey Marcel, it was nice meeting you at the Laracon.');
}

Make sure the user is following you on Twitter to make this work.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email c.rumpel@kabsi.at instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

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