laravel-notification-channels / twitter

Twitter Notifications Channel for Laravel
http://laravel-notification-channels.com
MIT License
169 stars 44 forks source link

Driver [Twitter] not supported #85

Closed sts-ryan-holton closed 2 years ago

sts-ryan-holton commented 2 years ago

I came across this issue, which is identical to mine, except let's fast forward to present day, I'm working inside a Laravel 8 application and am using PHP 7.4, I'm using version 5.1.0 of this package and have just set it up, my notification class (the important bits) looks like this...

<?php

namespace App\Notifications\Monitor;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Messages\NexmoMessage;

use NotificationChannels\Discord\DiscordChannel;
use NotificationChannels\Discord\DiscordMessage;
use NotificationChannels\Twitter\TwitterChannel;
use NotificationChannels\Twitter\TwitterDirectMessage;

use App\Models\NotificationHistory;
use App\Traits\Plans;

class MonitorDown extends Notification implements ShouldQueue
{
    use Queueable, Plans;

    public $monitor;
    public $emailData;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($monitor, $emailData)
    {
        $this->monitor = $monitor;
        $this->emailData = $emailData;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        $preferences = json_decode($notifiable->notification_preferences);

        if (!$notifiable->is_subscribed) {
            $preferences->monitors->down = array_merge(array_diff($preferences->monitors->down, [
                'nexmo', 'discord', 'twitter'
            ]));
        }

        // not working
        return ['twitter', 'discord'];

        // not working
        return [TwitterChannel::class, 'discord'];

        // not working
        return [TwitterChannel::class];

        // return $preferences->monitors->down;
    }

    /**
     * Determine which queues should be used for each notification channel.
     *
     * @return array
     */
    public function viaQueues()
    {
        return [
            'mail' => 'notifications',
            'database' => 'notifications',
            'slack' => 'notifications',
            'nexmo' => 'notifications',
            'discord' => 'notifications',
            'twitter' => 'notifications'
        ];
    }

    /**
     * Get the Twitter representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toTwitter($notifiable)
    {
        $data = [
            'type' => 'monitor-down',
            'name' => $this->emailData['monitor_name'],
            'url' => $this->emailData['monitor_url'],
            'value' => 'down'
        ];

        try {
            $this->createHistoryEntry('twitter', 'Monitor Down', $data, $notifiable);
        } catch (\Exception $e) { }

        $monitorName = $this->emailData['monitor_name'];
        $monitorURL = $this->emailData['monitor_url'];

        return new TwitterDirectMessage("factfinder_all", "Your monitor $monitorName -- ($monitorURL) -- has just gone DOWN");
    }

    /**
     * Create a history entry
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function createHistoryEntry($method = 'database', $type = '', $data, $notifiable)
    {
        try {
            $history = new NotificationHistory();
            $history->method = $method;
            $history->type = $type;
            $history->data = json_encode($data);
            $history->notification_id = isset($this->id) ? $this->id : '';
            $history->user_id = $notifiable['id'];
            $history->save();
        } catch (\Exception $e) { }
    }
}

You'll see that I've tried several ways to get the notifications to work:

// not working
return ['twitter', 'discord'];

// not working
return [TwitterChannel::class, 'discord'];

// not working
return [TwitterChannel::class];

But still, the error returned is:

InvalidArgumentException: Driver [twitter] not supported.

sts-ryan-holton commented 2 years ago

Figured it out, looks like only [TwitterChannel::class] with composer dump-autoload works