• Stars
    star
    169
  • Rank 223,802 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 8 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Twitter Notifications Channel for Laravel

image

Twitter notification channel for Laravel

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

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

PS: v.7.0.0 only supports Laravel 10 and PHP 8.1. If you have an older Laravel application or PHP version, you can use an older version of this package. Be aware that these 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 on.

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 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 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 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.

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.

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.

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.

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.

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.

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.

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

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.

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.

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.

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.

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.

More Repositories

1

telegram

✈️ Telegram Notifications Channel for Laravel
PHP
852
star
2

webpush

Webpush notifications channel for Laravel.
PHP
658
star
3

fcm

Firebase Cloud Messaging (FCM) notifications channel for Laravel
PHP
483
star
4

onesignal

OneSignal notifications channel for Laravel
PHP
275
star
5

pusher-push-notifications

Pusher Beams notifications channel for Laravel
PHP
271
star
6

discord

Discord notification channel for Laravel
PHP
223
star
7

channels

Submit suggestions & pull requests here for new notification channels
186
star
8

apn

APN push notifications channel for Laravel
PHP
185
star
9

webhook

Webhook notifications channel for Laravel
PHP
154
star
10

facebook

πŸ“¨ Facebook Notifications Channel for Laravel
PHP
145
star
11

microsoft-teams

Microsoft Teams Notifications Channel for Laravel
PHP
133
star
12

facebook-poster

Use notifications to create posts on Facebook
PHP
130
star
13

authy

@Authy notification channel for @Laravel, with the ability to send in-app, sms, and call verification tokens.
PHP
57
star
14

pushover

πŸ“± Pushover notifications channel for Laravel
PHP
54
star
15

gcm

Laravel Notification Channel for Google GCM (Android)
PHP
54
star
16

aws-sns

AWS SNS notification channel for Laravel
PHP
50
star
17

smsc-ru

smsc.ru notifications channel for Laravel 5.3+
PHP
49
star
18

skeleton

PHP
47
star
19

google-chat

Google Chat notification channel for Laravel
PHP
46
star
20

expo

Expo Notifications Channel for Laravel
PHP
37
star
21

backport

Laravel Notifications for Laravel 5.2 / 5.1
PHP
26
star
22

ionic-push-notifications

Ionic Push Notifications Channel for Laravel
PHP
25
star
23

trello

Trello notifications channel for Laravel
PHP
24
star
24

africastalking

AfricasTalking Notification Channel For Laravel
PHP
23
star
25

messagebird

Messagebird notifications channel for Laravel
PHP
22
star
26

bearychat

BearyChat notifications channel for Laravel
PHP
22
star
27

smsapi

Smsapi notification channel for Laravel 5.5+
PHP
22
star
28

website

Laravel Notifications Channel Website
JavaScript
21
star
29

hipchat

HipChat Notifications Channel for Laravel
PHP
18
star
30

gammu

Gammu SMS notifications channel for Laravel 5.3
PHP
14
star
31

pushbullet

Pushbullet notifications channel for Laravel
PHP
14
star
32

rocket-chat

Rocket.Chat notifications channel for Laravel
PHP
14
star
33

lob

lob.com notifications channel for Laravel
PHP
11
star
34

gitter

Gitter.im notifications channel for Laravel
PHP
10
star
35

pubnub

PubNub Notifications Channel for Laravel
PHP
9
star
36

laravel-notification-channels.github.io

Old Source code of laravel-notification-channels.com
HTML
9
star
37

jet-sms

JetSMS notifications channel for Laravel
PHP
9
star
38

clickatell

Clickatell notifications channel for Laravel
PHP
9
star
39

plivo

Plivo notifications channel for Laravel
PHP
8
star
40

wunderlist

Wunderlist notifications channel for Laravel 5.3
PHP
8
star
41

jusibe

Jusibe notifications channel for Laravel
PHP
8
star
42

cmsms

CMSMS notifications channel for Laravel
PHP
7
star
43

smspoh

Smspoh Notifications Channel for Laravel
PHP
7
star
44

workplace

Workplace notification channel for Laravel
PHP
6
star
45

sms77

Seven.io (formerly SMS77) notification channel for Laravel
PHP
6
star
46

turbosms

TurboSMS notification channel for Laravel
PHP
5
star
47

pagerduty

Laravel Notification Channel for PagerDuty
PHP
5
star
48

intercom

Intercom notifications channel for Laravel
PHP
5
star
49

infobip

Infobip notifications channel for Laravel.
PHP
4
star
50

evernote

Evernote notifications channel for Laravel 5.3
PHP
3
star
51

sipgate

Sipgate Notifications Channel for Laravel 5.5+, 6.X and 7
PHP
3
star
52

touch-sms

πŸ“² TouchSMS Notifications Channel for Laravel
PHP
3
star
53

netgsm

NetGsm notification channel for Laravel
3
star
54

pushwoosh

Pushwoosh notifications channel for Laravel
PHP
2
star
55

ovh-sms

PHP
2
star
56

46elks

46Elks notification channel for Laravel
PHP
2
star
57

todoist

Todoist notifications channel for Laravel 5.3
PHP
2
star
58

sms-broadcast

Laravel Notification Channel for Sms Broadcast
PHP
2
star
59

textlocal

PHP
2
star
60

maillift

MailLift notifications channel for Laravel 5.3
PHP
1
star
61

all-my-sms

AllMySms notification channel for Laravel
PHP
1
star
62

vodafone

Vodafone Notification Channel for Laravel
PHP
1
star