Software Architect / Microsoft MVP (AI) and Pluralsight Author

C#, Social Opinion, Social Opinion API, Twitter API v2

Support for the new Twitter API Tweet Blocks Endpoint added to Social Opinion API

I’ve implemented the new V2 Blocks endpoint that recently shipped by Twitter. This is available in my NuGet package and on GitHub. It’s written in C#.

This endpoint lets you programmatically:

  • Get a list of accounts an account has blocked
  • Block an account
  • Unblock an account

 

You can use these endpoints to build a better experience on the platform. For example, by creating configurable business rules around why an account should be blocked. Another example might be applying machine learning to discover the type of accounts a person tends to block

Using The Counts Enpoint with The Social Opinion API

You can use this end point with a few lines of code. The first thing you need to do is create an instance of the Tweet Service and pass in your OAuth tokens:

BlocksService tweetsService = new BlocksService(oAuthInfo);

You are now free to call each of the methods

Get a List of Accounts an Account Has Blocked

This method will get you a list of the accounts that the authentication account has blocked.

Each item (BlockModel) in the list will contain all publicly available properties for each blocked user.

List<BlocksModel> lists = blocksService.GetBlocks("958676983", 10);

You can see an example of the data you get here:

public class BlockData
{
    public string url { get; set; }
    public string profile_image_url { get; set; }
    public bool verified { get; set; }
    public PublicMetrics public_metrics { get; set; }
    public string name { get; set; }
    public Entities entities { get; set; }
    public string id { get; set; }
    public string description { get; set; }
    public bool @protected { get; set; }
    public string username { get; set; }
    public string location { get; set; }
    public DateTime created_at { get; set; }
    public string pinned_tweet_id { get; set; }
}

We can take a closer look at the Public Metrics and see some other properties:

public class PublicMetrics
{
    public int followers_count { get; set; }
    public int following_count { get; set; }
    public int tweet_count { get; set; }
    public int listed_count { get; set; }
    public int retweet_count { get; set; }
    public int reply_count { get; set; }
    public int like_count { get; set; }
    public int quote_count { get; set; }
}

Further insights about the blocked account are also found in the Entities property for the blocked user such as the bio field, any urls or hashtags they have on their account:

public class Entities
{
    public Description description { get; set; }
    public Url url { get; set; }
    public List<Annotation> annotations { get; set; }
    public List<Url> urls { get; set; }
    public List<Hashtag> hashtags { get; set; }
    public List<Mention> mentions { get; set; }
}

Block an Account

This method lets the authenticated account block a target user account

bool blockedResult = blocksService.Block("958676983", "34655603");

The first parameter is the id you are blocking on behalf of.  The second parameter is the target id of the account you want to block.

A boolean (true) is returned when the operation has been successful and the account has been blocked.

Unblock an Account

This method lets the authenticated account unblock a target user account

The first parameter is the id you are unblocking on behalf of.  The second parameter is the target id of the account you want to unblock.

A boolean (false) is returned when the operation has been successful, and the account has been unblocked

bool result = blocksService.UnBlock("958676983", "34655603");

Use Cases

There are a few use cases for this, I see the main ones being around creating automation to improve customer or user experience on the platform.

Automation

You could apply Azure Cognitive Services Text Analytics and infer the sentiment or detect profanity from an account. You might then decide to automatically block an account after a certain threshold had been reached.

Account Analysis

Use the Blocks API in conjunction with the with the Timeline API to create an overview of an account. Based on configurable rules you could block or unblock accounts.

Research

Identify accounts that a user tends to block. Use the Recent Search API to fetch data from blocked accounts and identify patterns.

~

Free NuGet Package: You can find the free Social Opinion NuGet package here.

 

If you want to understand your followers, get richer analytics, perform social media monitoring, schedule tweets, or automate other aspects of your Twitter account, you can do that using Social Opinion

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