This package makes it easy to send notifications using Twitter with Laravel 5.4.
- Installation
- Setting up the Twitter service
- Usage
- Send a direct message
- Changelog
- Testing
- Security
- Contributing
- Credits
- License
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,
],
...
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
.
Follow Laravel's documentation to add the channel to your Notification class.
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!');
}
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')
]);
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.
Please see CHANGELOG for more information what has changed recently.
$ composer test
If you discover any security related issues, please email c.rumpel@kabsi.at instead of using the issue tracker.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.