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

403 Forbidden on Filtered Stream v1.1 #1209

Open
Scobiform opened this issue Mar 16, 2023 · 5 comments
Open

403 Forbidden on Filtered Stream v1.1 #1209

Scobiform opened this issue Mar 16, 2023 · 5 comments

Comments

@Scobiform
Copy link

Hi there,

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

Exception... Reason : \n\n\n<title>Error 403
Code : -1

@orrishu
Copy link

orrishu commented Mar 16, 2023

+1

@Marilyth
Copy link

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

@Scobiform
Copy link
Author

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.

@lgjluis
Copy link

lgjluis commented Mar 17, 2023

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

@keytrap-x86
Copy link

keytrap-x86 commented Apr 29, 2023

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);

    }

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

6 participants
@Scobiform @keytrap-x86 @Marilyth @lgjluis @orrishu and others