GithubHelp home page GithubHelp logo

salimedia / twitter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from laravel-notification-channels/twitter

0.0 2.0 0.0 90 KB

Twitter Notifications Channel for Laravel 5.6+

Home Page: http://laravel-notification-channels.com

License: MIT License

PHP 100.00%

twitter's Introduction

Twitter notification channel for Laravel 5.6+

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.6+. If you have an older Laravel application, you can use version 1.*. But be aware that these versions are no longer maintained.

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.

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

Installation

If you prefer a video, there is also an introduction video available for you. If not, just read one.

You can install this package via composer:

composer require laravel-notification-channels/twitter

The service provider gets loaded automatically.

Setting up the Twitter service

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

...
'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')
]
...

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

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.

Publish Twitter status update

<?php
use NotificationChannels\Twitter\TwitterChannel;
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($notifiable)
    {
        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.

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 have to pass the image path to the withImage method.

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

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

return (new TwitterStatusUpdate('Laravel notifications are awesome!'))->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.');
}

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.

public function toTwitter($notifiable)
{
     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 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.

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

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security-related issues, please email [email protected] 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.

twitter's People

Contributors

freekmurze avatar ahmedash95 avatar mpociot avatar bmitch avatar jono20201 avatar enniel avatar carusogabriel avatar gms8994 avatar gpluess avatar mohamedsabil83 avatar

Watchers

James Cloos avatar Salim ELABOUDI avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.