Software Architect / Microsoft MVP (AI) and Pluralsight Author

C#, Social Opinion API, Twitter API v2

Tracking User Mentions in Tweets with the User Mention Timeline and Social Opinion API

Often, you’ll received notifications that your account has been mentioned in a Tweet. These notifications get displayed in the Mentions section of your Twitter account. The User Mention Timeline API lets you fetch Tweets that mention a specific user via the numerical user id.

I’ve added support for this new Twitter API v2 endpoint to the Social Opinion API. This let’s you call the new endpoint with 2 lines of code.

Key Features

Here are some of the key features of the endpoint:

  • Tweets delivered in chronological order – most recent first
  • Upto 800 Tweets can be fetched
  • Results are paginated – 100 Tweets per page
  • Pagination Tokens are provided to help page through results
  • Tweet Ids of newest & oldest Tweets are included in a “meta” object – useful in polling scenarios
  • Support for Application scoped requests (Bearer Token) and User scoped requests (OAuth 1.0a)

Let’s dive into an example now.

An Example

Here we can see the code that’s needed to call the User Mentioned endpoint using the Social Opinion API:

TimelineService timeLineService = new TimelineService(oAuthInfo);
timeLineService.GetUserMentionedTimeline("38906681", null, 10, null, null, null, null);

The first parameter –38912381, is the ID of the Twitter account we’re interested in. You can get this using the User Lookup API. The third parameter –5, is the number of Tweets we want to return.  I’m passing null into the other parameters which include:

  • pagination_token
  • start_time
  • end_time
  • since_id
  • until_id

The code returns an object UserMentionedTimelineModel which contains many properties.  This gives you rich insights into the Tweet and User that was mentioned. We can see a selection of the properties and datatypes you get access below.  Here we can see the “root” level properties:

public string in_reply_to_user_id { get; set; }
public Entities entities { get; set; }
public string source { get; set; }
public string lang { get; set; }
public bool possibly_sensitive { get; set; }
public string id { get; set; }
public DateTime created_at { get; set; }
public string text { get; set; }
public string reply_settings { get; set; }
public PublicMetrics public_metrics { get; set; }
public string author_id { get; set; }
public List<ReferencedTweet> referenced_tweets { get; set; }
public Geo geo { get; set; }
public Attachments attachments { get; set; }

The Includes property (below) is useful to help you quickly see all users, tweets, places, and media that are contained in your data returned by your method call:

public class Includes
{
  public List<User> users { get; set; }

  public List<Tweet> tweets { get; set; }

  public List<Place> places { get; set; }

  public List<Medium> media { get; set; }

}

A Closer Look at the Debugger

When we examine the debugger, we can see the following data is returned from our method call timeLineService.GetUserMentionedTimeline(“38906681”null, 10, nullnullnullnull);

We can expand the Includes property and see additional properties:

If we expand the data property we get access to the tweets we requested where my account was mentioned:

We can open one of these from the collection and see further insights. First, the actual mention:

Next, we can see some metrics:

Other Tweets may contain geo or place information. For example, here we can see a different method call that contained Tweets with location data:

These are just a few of the properties in the object graph.

Wrapping Up

There are many use cases for this endpoint and the enhanced signals that it delivers. You can use to better understand who is engaging with you or a collection of accounts. You can also use this to create programmatic systems or AdTech solutions.

You can find the Social Opinion API on NuGet here.

Got a question? Interested in an integration or feature request? Get in contact with me.

JOIN MY EXCLUSIVE EMAIL LIST
Get the latest content and code from the blog posts!
I respect your privacy. No spam. Ever.

Leave a Reply