Twitter User timeline

User timeline

The GetUserTimeline method allows you to get the most recent tweets of a given user (called user timeline). The method comes in a few overloads since you might not always need to specify the extra options.

For simple usage, you can specify either the ID of the user:

// Make the call to the API
TwitterTimelineResponse response = service.Statuses.GetUserTimeline(319219802);

// Iterate through the tweets
foreach (TwitterStatusMessage tweet in response.Body) {

    Console.WriteLine(tweet.User.Name);
    Console.WriteLine(tweet.Text);
    Console.WriteLine();

}

or the screen name:

// Make the call to the API
TwitterTimelineResponse response = service.Statuses.GetUserTimeline("abjerner");

// Iterate through the tweets
foreach (TwitterStatusMessage tweet in response.Body) {

    Console.WriteLine(tweet.User.Name);
    Console.WriteLine(tweet.Text);
    Console.WriteLine();

}

By default, the Twitter API will only return the 20 most recent tweets by the specified user. By specifying a second parameter for the method, you can tell the API to return even more tweets (or less) in each response. The maximum value seems to be 200 tweets:

// Make the call to the API
TwitterTimelineResponse response = service.Statuses.GetUserTimeline("abjerner", 200);

Options

Also by default, the API will return replies as well as retweets made by the user. If you wan't to filter those out, you can tell the API to so by specifying an instance of the TwitterUserTimelineOptions class:

// Make the call to the API
TwitterTimelineResponse response = service.Statuses.GetUserTimeline(new TwitterUserTimelineOptions {
    ScreenName = "abjerner",
    ExcludeReplies = true,
    IncludeRetweets = false
});

One think worth noticing is that the Twitter API will apply pagination, and then filter out replies and retweets. So assuming that you're requesting the 20 most recent tweets, and five of them are retweets, while the rest are regular tweets, the example above will only result in 15 tweets. So it may be easier to filter out replies and retweets in your own code.

Pagination

You can also specify options for pagination. This could look like:

// Declare the initial options
TwitterUserTimelineOptions options = new TwitterUserTimelineOptions {
    ScreenName = "abjerner",
    Count = 5
};

// Make the call to the API
TwitterTimelineResponse response = service.Statuses.GetUserTimeline(options);

// Iterate through the tweets
foreach (TwitterStatusMessage tweet in response.Body) {

    Console.WriteLine(tweet.User.Name + " (" + tweet.Id + ")");
    Console.WriteLine(tweet.Text);
    Console.WriteLine();

}

if (response.Body.Length >= options.Count) {

    // Set the maximum ID for the next request (last ID minus one)
    options.MaxId = response.Body.Last().Id - 1;

    // Make another call to the API
    response = service.Statuses.GetUserTimeline(options);

    // Iterate through the tweets
    foreach (TwitterStatusMessage tweet in response.Body) {

        Console.WriteLine(tweet.User.Name + " (" + tweet.Id + ")");
        Console.WriteLine(tweet.Text);
        Console.WriteLine();

    }

}

In the example above, the second page is requested by setting the MaxId property. This will tell the API to ignore tweets higher than the ID specified for this property.

The ID is inclusive, so for proper pagination , you should take the ID of the last tweet of the first page, and then subtract by one.

It is also possible to specify a SinceId property instead. This property is however exclusive.