Geeks With Blogs
Joe Mayo

Today, I released the latest version of LINQ to Twitter. In addition to fixing bugs, the highlighted features of this release include support for DM Events, Extended Tweets and .NET Core. Here’s a demo of using extended mode tweets in a Search query:

        static async Task DoSearchAsync(TwitterContext twitterCtx)
        {
            string searchTerm = "\"LINQ to Twitter\" OR Linq2Twitter OR LinqToTwitter OR JoeMayo";

            Search searchResponse =
                await
                (from search in twitterCtx.Search
                 where search.Type == SearchType.Search &&
                       search.Query == searchTerm &&
                       search.IncludeEntities == true &&
                       search.TweetMode == TweetMode.Extended
                 select search)
                .SingleOrDefaultAsync();

            if (searchResponse?.Statuses != null)
                searchResponse.Statuses.ForEach(tweet =>
                    Console.WriteLine(
                        "\n  User: {0} ({1})\n  Tweet: {2}", 
                        tweet.User.ScreenNameResponse,
                        tweet.User.UserIDResponse,
                        tweet.Text ?? tweet.FullText));
            else
                Console.WriteLine("No entries found.");
        }

Notice that the query now has a TweetMode property. You can set this to to enum TweetMode.Extended to request tweets that go beyond 140 characters. To handle the difference between Classic and Extended tweets, the Console.WriteLine statement uses either tweet.Text or tweet.FullText. A null tweet.Text tells that the tweet is extended, which is consistent with the Twitter API convention.

Here are a few API queries associated with Direct Message Events:

Show:

            DirectMessageEvents dmResponse =
                await
                    (from dm in twitterCtx.DirectMessageEvents
                     where dm.Type == DirectMessageEventsType.Show &&
                           dm.ID == 917929712638246916
                     select dm)
                    .SingleOrDefaultAsync();


            MessageCreate msgCreate = dmResponse?.Value?.DMEvent?.MessageCreate;

            if (dmResponse != null && msgCreate != null)
                Console.WriteLine(
                    "From ID: {0}\nTo ID:  {1}\nMessage Text: {2}",
                    msgCreate.SenderID ?? "None",
                    msgCreate.Target.RecipientID ?? "None",
                    msgCreate.MessageData.Text ?? "None");

List:

            int count = 10; // intentionally set to a low number to demo paging
            string cursor = "";
            List<DMEvent> allDmEvents = new List<DMEvent>();

            // you don't have a valid cursor until after the first query
            DirectMessageEvents dmResponse =
                await
                    (from dm in twitterCtx.DirectMessageEvents
                     where dm.Type == DirectMessageEventsType.List &&
                           dm.Count == count
                     select dm)
                    .SingleOrDefaultAsync();

            allDmEvents.AddRange(dmResponse.Value.DMEvents);
            cursor = dmResponse.Value.NextCursor;

            while (!string.IsNullOrWhiteSpace(cursor))
            {
                dmResponse =
                    await
                        (from dm in twitterCtx.DirectMessageEvents
                         where dm.Type == DirectMessageEventsType.List &&
                               dm.Count == count &&
                               dm.Cursor == cursor
                         select dm)
                        .SingleOrDefaultAsync();

                allDmEvents.AddRange(dmResponse.Value.DMEvents);
                cursor = dmResponse.Value.NextCursor;
            }

            if (!allDmEvents.Any())
            {
                Console.WriteLine("No items returned");
                return;
            }

            Console.WriteLine($"Response Count: {allDmEvents.Count}");
            Console.WriteLine("Responses:");

            allDmEvents.ForEach(evt =>
            {
                MessageCreate msgCreate = evt.MessageCreate;

                if (evt != null && msgCreate != null)
                    Console.WriteLine(
                        "From ID: {0}\nTo ID:  {1}\nMessage Text: {2}",
                        msgCreate.SenderID ?? "None",
                        msgCreate.Target?.RecipientID ?? "None",
                        msgCreate.MessageData?.Text ?? "None");
            });

New:

            const ulong Linq2TwitrID = 15411837;

            DirectMessageEvents message = 
                await twitterCtx.NewDirectMessageEventAsync(
                    Linq2TwitrID, 
                    "DM from @JoeMayo to @Linq2Twitr of $MSFT & $TSLA with #TwitterAPI #chatbot " +
                    "at http://bit.ly/2xSJWJk and http://amzn.to/2gD09X6 on " + DateTime.Now + "!'");

            DMEvent dmEvent = message?.Value?.DMEvent;
            if (dmEvent != null)
                Console.WriteLine(
                    "Recipient: {0}, Message: {1}, Date: {2}",
                    dmEvent.MessageCreate.Target.RecipientID,
                    dmEvent.MessageCreate.MessageData.Text,
                    dmEvent.CreatedTimestamp);

Here’s how to get LINQ to Twitter:

  1. Source code on GitHub: https://github.com/JoeMayo/LinqToTwitter
  2. Documentation: https://github.com/JoeMayo/LinqToTwitter/wiki
  3. NuGet Reference: https://www.nuget.org/packages/linqtotwitter/4.2.0
  4.   

Thanks to @dixinyan for the .NET Core PR.

@JoeMayo

Posted on Tuesday, December 19, 2017 8:03 AM | Back to top

Copyright © Joe Mayo | Powered by: GeeksWithBlogs.net