tweetinvi icon indicating copy to clipboard operation
tweetinvi copied to clipboard

403 Forbidden on Filtered Stream v1.1

Open Scobiform opened this issue 1 year ago • 5 comments

Hi there,

I am getting 403 Forbidden since 2 days on v1.1. Anyone else having issues?

Exception... Reason : \n

\n\nError 403 Code : -1

Scobiform avatar Mar 16 '23 10:03 Scobiform

+1

orrishu avatar Mar 16 '23 12:03 orrishu

It seems v1.1 streams are now deprecated. So you will have to migrate to v2. https://twittercommunity.com/t/announcing-the-deprecation-of-v1-1-statuses-filter-endpoint/182960

Marilyth avatar Mar 16 '23 16:03 Marilyth

It seems v1.1 streams are now deprecated. So you will have to migrate to v2. https://twittercommunity.com/t/announcing-the-deprecation-of-v1-1-statuses-filter-endpoint/182960

Oh damn. Thanks for the hint.

Scobiform avatar Mar 16 '23 16:03 Scobiform

In v2 only works Sample Stream. Do you have an example of Filtered Stream?

lgjluis avatar Mar 17 '23 04:03 lgjluis

In v2 only works Sample Stream. Do you have an example of Filtered Stream?

Filteread streams will stop today (29th of April) :

https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api

But anyways here you go

public void StartFilteredStream(ITwitterBot bot, CancellationToken token)
    {
        _logger.LogDebug("Starting filtered stream");

        // Create the twitter filtered stream
        FilteredStream = bot.AuthenticatedUser.Client.Streams.CreateFilteredStream(
                new CreateFilteredTweetStreamParameters
                {
                    TweetMode = TweetMode.Extended
                });

        // Set the mention targets (users which will set of the OnMention event)
        FilteredStream.AddTrack($"@{bot.Config.Name}");

        // Only look for the users in the mentions
        FilteredStream.MatchOn = MatchOn.UserMentionEntities;

        // Set the event that occurs on a mention
        FilteredStream.MatchingTweetReceived += async (sender, args) =>
        {
            var mentionedTweet = args.Tweet;
            if (mentionedTweet != null)
            {
                // For some reason, I need to do this to get the full tweet
                var (Tweet, Error) = await bot.GetTweet(mentionedTweet.Id);
                if (Tweet != null)
                    OnMention?.Invoke(bot, Tweet);
                else
                    if (string.IsNullOrEmpty(Error) == false)
                    _logger.LogWarning("Error getting tweet {id} : {err}", mentionedTweet.Id, Error);
            }
        };

        // Set these events just for info
        FilteredStream.DisconnectMessageReceived += (sender, args) =>
        {
            _logger.LogWarning("Stream DisconnectMessageReceived code: {code} reason: {reason} stream name: {name}",
                            args.DisconnectMessage?.Code, args.DisconnectMessage?.Reason, args.DisconnectMessage?.StreamName);
        };

        _ = Task.Run(async () =>
        {
            var streamFails = 0;
            // Start the stream and wait for it to end
            // If the stream ends prematurely, we let it having a break for 30 seconds.
            // If it fails 3 times, end the stream
            do
            {
                try
                {
                    await FilteredStream.StartMatchingAllConditionsAsync();
                }
                catch (TaskCanceledException)
                {
                    // Ignored
                }
                catch (TwitterException e)
                {
                    _logger.LogError("Twitter exception while starting filtered stream: {err}", e.Message);
                }
                catch (Exception ex)
                {
                    _logger.LogError("Error while starting filtered stream: {err}", ex.Message);
                }

                streamFails++;

                await Task.Delay(30_000);

            } while (streamFails < 3);

            // Send a message with the telegram bot to inform that the stream has ended
            await _telegramBot.SendMessage("⛔ The stream has ended. We'll try to restart the app...");

            // Try to restart the app
            _logger.LogDebug("Restarting app...");
            Process.Start(new ProcessStartInfo("/usr/sbin/service", "saveflix restart") { Verb = "sudo" });

        }, token);

    }

keytrap-x86 avatar Apr 29 '23 10:04 keytrap-x86