Giter Site home page Giter Site logo

Comments (5)

Marilyth avatar Marilyth commented on May 20, 2024 2

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

from tweetinvi.

orrishu avatar orrishu commented on May 20, 2024

+1

from tweetinvi.

Scobiform avatar Scobiform commented on May 20, 2024

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.

from tweetinvi.

lgjluis avatar lgjluis commented on May 20, 2024

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

from tweetinvi.

keytrap-x86 avatar keytrap-x86 commented on May 20, 2024

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

    }

from tweetinvi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.