2017 © Pedro Peláez
 

library twitter

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

image

laravel-notification-channels/twitter

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

  • Saturday, February 10, 2018
  • by laravel-notification-channels
  • Repository
  • 5 Watchers
  • 51 Stars
  • 10,077 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 12 Forks
  • 0 Open issues
  • 25 Versions
  • 20 % Grown

The README.md

image, (*1)

Twitter notification channel for Laravel

Latest Version on Packagist Software License StyleCI Quality Score Code Coverage Total Downloads, (*2)

This package makes it easy to send Laravel notifications using Twitter. (Laravel 8+), (*3)

PS: v8 now uses the new Twitter API V2. Please read the upgrade guide for your app here., (*4)

Contents

About

This package is part of the Laravel Notification Channels project. It provides additional Laravel Notification channels to the ones given by Laravel itself., (*5)

The Twitter channel makes it possible to send out Laravel notifications as a Twitter tweet (post on the timeline) or as a direct message., (*6)

Installation

If you prefer a video, there is also an introduction video available for you. If not, just read on., (*7)

You can install this package via composer:, (*8)

``` bash composer require laravel-notification-channels/twitter, (*9)


The service provider gets loaded automatically. ### Twitter App & Credentials You will need to [create](https://developer.twitter.com/apps/) a Twitter app to use this channel. Within this app, you will find the `keys and access tokens`. Your Twitter app `must be within a project`. Also, make sure to activate the `user authentication settings`: <img width="1289" alt="image" src="https://github.com/laravel-notification-channels/twitter/assets/1394539/9be260ba-d8db-4af1-be58-9e4ce3b0176d"> After that, you have to regenerate your access token and secret. If done correctly, you should see the right permissions for your access tokens: <img width="796" alt="image" src="https://github.com/laravel-notification-channels/twitter/assets/1394539/6ee5fc99-0373-4ebb-b8c5-4c84a82632df"> Make sure to copy the right credentials and place them inside your `.env` file. ```env TWITTER_CONSUMER_KEY=your-consumer-key TWITTER_CONSUMER_SECRET=your-consumer-secret TWITTER_ACCESS_TOKEN=your-accesss_token TWITTER_ACCESS_SECRET=your-access-token-secret

image, (*10)

To load them, add this to your config/services.php file:, (*11)

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

Usage

To use this package, you need to create a notification class, like NewsWasPublished from the example below, in your Laravel application. Make sure to check out Laravel's documentation for this process., (*12)

Publish a Twitter status update

<?php

use Illuminate\Notifications\Notification;
use NotificationChannels\Twitter\TwitterChannel;
use NotificationChannels\Twitter\TwitterMessage;
use NotificationChannels\Twitter\TwitterStatusUpdate;

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(mixed $notifiable): TwitterMessage
    {
        return new TwitterStatusUpdate('Laravel notifications are awesome!');
    }
}

Take a closer look at the toTwitter method. Here, we define what kind of Twitter message we want to trigger. In this case, it is a status update message, which is just a new message in your timeline., (*13)

public function toTwitter(mixed $notifiable): TwitterMessage
{
    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 have to pass the image path to the withImage method., (*14)

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

If you want to use multiple images, just pass an array of paths., (*15)

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

Publish Twitter status update with videos

It is possible to publish videos with your status update too. You have to pass the video path to the withVideo method., (*16)

public function toTwitter(mixed $notifiable): TwitterMessage
{
    return (new TwitterStatusUpdate('Laravel notifications are awesome!'))->withVideo('video.mp4');
}

If you want to use multiple videos, just pass an array of paths., (*17)

return (new TwitterStatusUpdate('Laravel notifications are awesome!'))->withVideo([
    public_path('video1.mp4'),
    public_path('video.gif')
]);

Publish Twitter status update with both images and videos

It is also possible to publish both images and videos with your status by using a mixture of the two methods., (*18)

return (new TwitterStatusUpdate('Laravel notifications are awesome!'))->withVideo([
    public_path('video1.mp4'),
    public_path('video.gif')
])->withImage([
    public_path('marcel.png'),
    public_path('mohamed.png')
]);

Publish a Twitter status update in reply to another tweet

Additionally, you can publish a status update in reply to another tweet. This is possible by using the inReplyTo method., (*19)

public function toTwitter(mixed $notifiable): TwitterMessage
{
    return (new TwitterStatusUpdate('@christophrumpel Laravel notifications are awesome!'))->inReplyTo(123);
}

Note that the reply status ID will be ignored if you omit the author of the original tweet, according to Twitter docs., (*20)

Send a direct message (NOT working with the FREE Twitter API plan!)

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 message as the second one., (*21)

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

You can also provide the user ID instead of the screen name. This would prevent an extra Twitter API call. Make sure to pass it as an integer when you do., (*22)

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

Handle multiple Twitter Accounts

There might be cases where you need to handle multiple Twitter accounts. This means you need to be able to change the provided keys and tokens of your Twitter app. Luckily, Laravel can help you here. In your notifiable model, you can define the routeNotifiactionForTwitter method. Here you can override the provided settings., (*23)

public function routeNotificationForTwitter($notification)
{
   return [
      'TWITTER_CONSUMER_KEY',
      'TWITTER_CONSUMER_SECRET',
      'TWITTER_ACCESS_TOKEN',
      'TWITTER_ACCESS_SECRET',
   ];
}

Changelog

Please see CHANGELOG for more information about what has changed recently., (*24)

Testing

bash $ composer test, (*25)

Security

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

Contributing

Please see CONTRIBUTING for details., (*27)

Credits

License

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

The Versions

06/09 2016

dev-analysis-q2Z529

dev-analysis-q2Z529 https://github.com/laravel-notification-channels/twitter

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

  Sources   Download

MIT

The Requires

 

The Development Requires

04/09 2016

dev-analysis-zOL6rB

dev-analysis-zOL6rB https://github.com/laravel-notification-channels/twitter

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

  Sources   Download

MIT

The Requires

 

The Development Requires

27/08 2016
25/08 2016
22/08 2016

dev-analysis-XV0lBK

dev-analysis-XV0lBK https://github.com/laravel-notification-channels/twitter

This package makes it easy to send notifications via Twitter status updates with Laravel 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires

22/08 2016

dev-analysis-XpE1e6

dev-analysis-XpE1e6 https://github.com/laravel-notification-channels/twitter

This package makes it easy to send notifications via Twitter status updates with Laravel 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires