laravel-notification-channels / twitter

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

Bug: defined property: stdClass::$id #101

Open sts-ryan-holton opened 11 months ago

sts-ryan-holton commented 11 months ago

:wave: Hi,

Just reaching out as I've identified a problem with the getReceiver function, firstly, not quite sure why I'm getting the error in the first place, I'm using v8.0.0 of the package, getting the error:

ErrorException: Undefined property: stdClass::$id in /var/www/domain-monitor-api/vendor/laravel-notification-channels/twitter/src/TwitterDirectMessage.php:45

Presumably the error handling of the $user->id here needs to be more robust?

/**
 * Get Twitter direct message receiver.
 *
 * @return string|mixed
 *
 * @throws CouldNotSendNotification
 */
public function getReceiver(TwitterOAuth $twitter): mixed
{
    if (is_int($this->to)) {
        return $this->to;
    }

    $user = $twitter->get('users/show', [
        'screen_name' => $this->to,
        'include_user_entities' => false,
        'skip_status' => true,
    ]);

    if ($twitter->getLastHttpCode() === 404) {
        throw CouldNotSendNotification::userWasNotFound($twitter->getLastBody());
    }

    return $user->id;
}
christophrumpel commented 11 months ago

Hey. thanks for info. Can you check what "$user" contains? Looks like it is not null?

Greets

sts-ryan-holton commented 11 months ago

I get the following error back on the $user variable:

{
   "user":{
      "stdClass":{
         "errors":[
            {
               "message":"You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product",
               "code":453
            }
         ]
      }
   }
}

This is how I want to send a message

/**
 * Get the Twitter representation of the notification.
 *
 * @param  mixed  $notifiable
 */
public function toTwitter($notifiable)
{
    $userIntegration = $this->getUserIntegration($notifiable->id, 'twitter');

    if (! $userIntegration) {
        exit;

        return [];
    }

    try {
        $this->createHistoryEntry('twitter', 'Monitor Test Notification', [], $notifiable);
    } catch (\Exception $e) {
    }

    return new TwitterDirectMessage($userIntegration->schema->twitter_username, "This is your monitor test notification!");
}

$userIntegration->schema->twitter_username returns the twitter username, without the @ symbol.

I have the following permissions:

Created with Read, Write, and Direct Messages permissions

And I'm on the free plan for the api