0
votes

I've been told that LinqToTwitter is updated to include the twitter new API for direct_messages/events. Though, for hours of searching and googling I couldn't find one good (ASP .Net C#) example of using LinqToTwitter (starting from the beginning).

So I have my twitter app 4 keys (consumerKey, consumerSecret, accessToken, accessTokenSecret), now what do I do?!

If anyone can just show me how do I start, then I can continue by myself! I've been using TweetMoaSharp but I think they are not planning to update it to include the new API from Twitter.

1

1 Answers

0
votes

They're called DirectMessageEvents, to distinguish them from the old DirectMessages. Here's an example of how to create a new DM:

    static async Task NewDirectMessageAsync(TwitterContext twitterCtx)
    {
        const ulong Linq2TwitrID = 15411837;// 16761255;

        DirectMessageEvents message = 
            await twitterCtx.NewDirectMessageEventAsync(
                Linq2TwitrID, 
                "DM from @JoeMayo to @Linq2Twitr of $MSFT & $TSLA with #TwitterAPI #chatbot " +
                "at http://example.com and http://mayosoftware.com 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);
    }

As you can see, there's a new DMEvent entity. The DMEvent.Value contains the Twitter response, and the other properties of DMEvent hold input parameters. Here's how to do a query that uses those parameters:

    static async Task ListDirectMessagesAsync(TwitterContext twitterCtx)
    {
        int count = 50; // 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 =>
        {
            DirectMessageCreate msgCreate = evt.MessageCreate;

            if (evt != null && msgCreate != null)
                Console.WriteLine(
                    $"DM ID: {evt.ID}\n" +
                    $"From ID: {msgCreate.SenderID ?? "None"}\n" +
                    $"To ID:  {msgCreate.Target?.RecipientID ?? "None"}\n" +
                    $"Message Text: {msgCreate.MessageData?.Text ?? "None"}");
        });
    }

Notice that the code uses cursors to move through the DMEvent list. I have a task to document these. I do have sample code that you can peruse to see how to use the various methods in the LINQ to Twitter Direct Message Demos project in the source code.