Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Twitter username in TwitterDirectMessage from User model routeNotificationForTwitter #88

Open
sts-ryan-holton opened this issue Aug 9, 2022 · 2 comments

Comments

@sts-ryan-holton
Copy link

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
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants