laravel-notification-channels / twitter

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

Set Twitter username in TwitterDirectMessage from User model routeNotificationForTwitter #88

Open sts-ryan-holton opened 2 years ago

sts-ryan-holton commented 2 years ago

I have a routeNotificationForTwitter on my User model which returns the Twitter username for my user, how can I access this within my notification's toTwitter method?

/**
 * Route the notification for Twitter.
 *
 * @return string
 */
public function routeNotificationForTwitter($notification)
{
    try {
        $availableIntegration = AvailableIntegration::where('slug', 'twitter')
                                                    ->first();

        $userIntegration = UserIntegration::where('user_id', $this->id)
                                          ->where('available_integration_id', $availableIntegration->id)
                                          ->first();

        if (!$userIntegration) {
            return null;
        }

        return $userIntegration->schema->twitter_username;
    } catch (\Exception $e) { }

    return null;
}
    /**
     * 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");
    }
hcivelek commented 1 year ago

You can send the $user to the notification class as a parameter of the __construct() method. In inside, you can save it like $this->user = $user. Then you can use it toTwitter() method.

hcivelek commented 1 year ago

That was the answer to "how to reach $user in the notification class" above. But with the routeNotificationForTwitter() method you don't need anything. The method does it by itself.