Skip to content

Account Activity

linvi edited this page Aug 11, 2018 · 10 revisions

Introduction

Account Activity is Twitter replacement of the old User Stream. It uses Webhooks that Twitter uses to push events to your https server.

To learn more on how to use Twitter Webhooks please go here.

Important

To use webhooks with Tweetinvi, you will need to use the following nuget package :

TweetinviAPI.WebhooksPlugin

Sample Project

To make your life easier and help you understand the process of handling webhooks and account activities please check the following project :

https://github.com/linvi/tweetinvi/tree/master/Examplinvi.WebhooksServer

Listen to account's events

Configure your server to receive Webhooks events

Before you can use Account Activity, you will need to register the Tweetinvi middeware for your AspNet project. When you do so you will use a WebhookConfiguration that will reuse here.

Tweetinvi middleware initialization can be done as followed :

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var consumerOnlyCredentials = new ConsumerOnlyCredentials("CONSUMER_TOKEN", "CONSUMER_SECRET")
    {
        ApplicationOnlyBearerToken = "BEARER_TOKEN"
    };

    WebhookServerInitialization(app, consumerOnlyCredentials);
}

private static void WebhookServerInitialization(IApplicationBuilder app, IConsumerOnlyCredentials consumerOnlyCredentials)
{
    // Extend Tweetinvi to allow AspNet to have extra functionalities
    Plugins.Add<WebhooksPlugin>();

    WebhookConfiguration = new WebhookConfiguration(consumerOnlyCredentials);

    app.UseTweetinviWebhooks(WebhookConfiguration);
}

Subscriptions

Only the accounts who subscribed to your Twitter webhook will be sending events to your server.

If you need to know which accounts are subscribed to your webhook you can do the following :

var subscriptions = await Webhooks.GetListOfSubscriptionsAsync(environment.Name, consumerOnlyCredentials);
var userIds = subscriptions.Subscriptions.Select(x => x.UserId);

Twitter will send the events to the url specified by the webhook. As a result only the accounts that subscribe to a webhook pointing to your server will receive events.

Register your account activities and events

On a server configured with Tweetinvi's webhooks, the registration to an Account Activity event is as easy as the following.

var activityStream = Stream.CreateAccountActivityStream(userId);

activityStream.TweetCreated += (sender, args) =>
{
    var tweet = args.Tweet;
    Console.WriteLine($"Tweet {tweet.Id} has just been published!");
};

WebhookConfiguration.AddActivityStream(activityStream);
Clone this wiki locally