Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/laravel-notification-channels/twitter

Twitter Notifications Channel for Laravel
https://github.com/laravel-notification-channels/twitter

laravel laravel-notification-channels laravel-package

Last synced: 1 day ago
JSON representation

Twitter Notifications Channel for Laravel

Awesome Lists containing this project

README

        

image

# Twitter notification channel for Laravel

[![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/twitter.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/twitter)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![StyleCI](https://styleci.io/repos/65847386/shield)](https://styleci.io/repos/65847386)
[![Quality Score](https://img.shields.io/scrutinizer/g/laravel-notification-channels/twitter.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/twitter)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/twitter/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/twitter/?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/twitter.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/twitter)

This package makes it easy to send Laravel notifications using [Twitter](https://dev.twitter.com/rest/public). (Laravel 8+)

PS: v8 now uses the new Twitter API V2. Please read the upgrade guide for your app [here](https://developer.twitter.com/en/docs/twitter-api/migrate/overview).

## Contents

- [About](#about)
- [Installation](#installation)
- [Setting up the Twitter service](#setting-up-the-twitter-service)
- [Usage](#usage)
- [Publish a Twitter status update](#publish-a-twitter-status-update)
- [Publish Twitter status update with images](#publish-twitter-status-update-with-images)
- [Publish Twitter status update with videos](#publish-twitter-status-update-with-videos)
- [Publish Twitter status update with both images and videos](#publish-twitter-status-update-with-both-images-and-videos)
- [Send a direct message](#send-a-direct-message)
- [Handle multiple Twitter Accounts](#handle-multiple-twitter-accounts)
- [Changelog](#changelog)
- [Testing](#testing)
- [Security](#security)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)

## About

This package is part of the [Laravel Notification Channels](http://laravel-notification-channels.com/) project. It provides additional Laravel Notification channels to the ones given by [Laravel](https://laravel.com/docs/master/notifications) itself.

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

## Installation

If you prefer a video, there is also [an introduction video](https://christoph-rumpel.com/2018/11/sending-laravel-notifications-via-twitter) available for you. If not, just read on.

You can install this package via composer:

``` bash
composer require laravel-notification-channels/twitter
```

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`:

image

After that, you have to regenerate your access token and secret. If done correctly, you should see the right permissions for your access tokens:

image

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

To load them, add this to your `config/services.php` file:

```php
...
'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](https://laravel.com/docs/master/notifications) for this process.

### Publish a Twitter status update

```php
withImage('marcel.png');
}
````
If you want to use multiple images, just pass an array of paths.
````php
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.
````php
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.
````php
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.
````php
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.
````php
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.
### 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.
````php
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.

````php
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](https://laravel.com/docs/master/notifications#customizing-the-recipient) can help you here. In your notifiable model, you can define the `routeNotifiactionForTwitter` method. Here you can override the provided settings.

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

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently.

## Testing

``` bash
$ composer test
```

## Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Credits

- [Christoph Rumpel](https://github.com/christophrumpel)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.