Released LINQ to Twitter v5.1.0

Today, I released LINQ to Twitter v5.1.0. This is a .1 release to fix bugs and add a few features that are new in the Twitter API. I’ll explain what’s new in this post. In the meantime, if you just want to grab the latest version of LINQ to Twitter, here are the links:

Removed Deprecated Methods

Generally, taking away from APIs is a breaking change, so I don’t do it lightly. I regularly mark members with an ObsoleteAttribute for several releases before removal. In all of the cases below, the endpoints have been removed from the Twitter API and you can’t use them anyway:

  • UpdateAccountBackgroundImageAsync
  • All DirectMessage APIs (replaced by DirectMessageEvents API)
  • Control, Site, and User Streams

Twitter pages haven’t had a background image for a while – thus removing this made sense. The DirectMessage APIs have been replaced with DirectMessageEvent APIs and are no longer available.

Control, Site, and User streams are no longer part of the Twitter API.

Added performBlock Parameter to ReportSpamAsync

Previously, you could just report spammers with ReportSpamAsync. This new performBlock parameter lets you also block a user at the same time as reporting, like this:

User spammer = await twitterCtx.ReportSpamAsync(
    SpammerScreenName, performBlock: true);

Added AdditionalOwners Parameter to UploadMediaAsync

According to the Twitter API docs, AdditionalOwners is:

A comma-separated list of user IDs to set as additional owners allowed to use the returned media_id in Tweets or Cards. Up to 100 additional owners may be specified.

The media_id that Twitter refers to is the MediaID property of the Media instance returned by the call to UploadMediaAsync, like this:

byte[] imageBytes = File.ReadAllBytes(@"..\..\..\images\TwitterTest.mp4");
const ulong JoeMayoUserID = 15411837;
var additionalOwners = new ulong[] { JoeMayoUserID };
string mediaType = "video/mp4";
string mediaCategory = "tweet_video";

Media media = await twitterCtx.UploadMediaAsync(
    imageBytes, mediaType, additionalOwners, mediaCategory);

The additionalOwners parameter is an IEnumerable<ulong>, belonging to an optional overload of UploadMediaAsync.

Added URL and Description Entities to User Object

On a user’s Twitter page, they can include a URL and Description. As with tweets, the Twitter API can extract URLs so you don’t have to do the parsing yourself. In addition to the profile URL, the Twitter API also extracts any URLs from a user’s profile description.

This LINQ to Twitter update adds an Entities property to the User object that makes it easy for you to read these URL entities. Here’s an example of a UserType.Show query:

var user =
    await
    (from usr in twitterCtx.User
     where usr.Type == UserType.Show &&
           usr.ScreenName == screenName &&
           usr.TweetMode == TweetMode.Extended
     select usr)
    .SingleOrDefaultAsync();

By setting the TweetMode parameter to TweetMode.Extended in the query, LINQ to Twitter will populate the Entities property on the user instance.

Added TweetMode Parameter to TweetAsync and ReplyAsync

This change adds a new parameter, TweetMode, to existing overloads because it makes sense to always be an option.

Back when Twitter started offering 280 character tweets, they represented this as Extended mode. The default was and still is Compatibility mode, which is the previous 140 character limit. To maintain consistency with the Twitter API behavior and documentation, LINQ to Twitter follows the same default convention, like this:

Status tweet = 
    await twitterCtx.TweetAsync(
        status, tweetMode: TweetMode.Extended);

Without the tweetMode parameter, the tweet.Text property will be truncated to 140 characters. However, by setting tweetMode to the TweetMode.Extended enum, the tweet.Text property is null and tweet.FullText contains the user’s entire tweet, up to the max 280 characters.

Added parameters Parameter to BeginAuthorizationAsync

This update allows you to share state across queries during the OAuth process. When performing OAuth in web apps, there’s a multi-step process, which boils down to two steps in LINQ to Twitter; calling the BeginAuthorizationAsync and CompleteAuthorizationAsync methods over the Twitter API OAuth process.

Setting the parameters parameter, a Dictionary<string, string>, sends name/value query string parameters to the Twitter API, which then return after the user authorizes your application. You would put code an MVC Begin action method, like this:

var parameters = new Dictionary<string, string> 
{ 
    { "my_custom_param", "val" } 
    
};
string twitterCallbackUrl = 
    Request.GetDisplayUrl().Replace("Begin", "Complete");
return await auth.BeginAuthorizationAsync(
    new Uri(twitterCallbackUrl), parameters);

When the Twitter API OAuth process returns to the Close action method, you can read the MVC Request property to find the parameter values.

Added ExecuteRawAsync Overload That Accepts an HttpMethod Parameter

ExecuteRawAsync and RawQuery are features of LINQ to Twitter that help you implement Twitter API features that might not have made it into LINQ to Twitter yet. e.g. What if the Twitter API adds a new parameter to and endpoint that LINQ to Twitter doesn’t have yet.

In the early days of the Twitter API, most endpoints worked with either a GET or POST HTTP call. However, newer APIs are more specific. e.g. Operations that delete/remove accept a DELETE HTTP method. Previously, ExecuteRawAsync assumed a POST endpoint, but this new change gives you the ability to use any HTTP method, like this:

string result = 
    await twitterCtx.ExecuteRawAsync(
        queryString, parameters, HttpMethod.Post);

This uses the System.Net.HttpMethod enum that is part of a new ExecuteRawAsync method overload.

Final Thoughts

So, that’s it for this version. These changes cover most of the items that people have asked for or that I had on the issues list. There’s much more to do, so stay tuned and let me know what you think.

@JoeMayo

Published by Joe Mayo

Author and independent software consultant. Specializing in Microsoft .NET technology. #ai #botframework #chatbots

Leave a comment