LinqToTwitter:direct_messages / events示例吗?

时间:2018-07-17 06:14:23

标签: c# asp.net twitter linq-to-twitter

有人告诉我LinqToTwitter已更新,其中包括用于direct_messages / events的twitter新API。 但是,在数小时的搜索和谷歌搜索中,我找不到一个使用LinqToTwitter的好示例(ASP.Net C#)(从头开始)。

所以我有我的Twitter应用程序4个密钥(consumerKey,consumerSecret,accessToken,accessTokenSecret),现在我该怎么办?!

如果有人可以告诉我如何开始,那么我可以自己继续!我一直在使用TweetMoaSharp,但我认为他们不打算对其进行更新以包括Twitter的新API。

1 个答案:

答案 0 :(得分:0)

它们被称为DirectMessageEvents,以区别于旧的DirectMessages。这是有关如何创建新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);
    }

如您所见,有一个新的DMEvent实体。 DMEvent.Value包含Twitter响应,DMEvent的其他属性保留输入参数。使用这些参数的查询方法如下:

    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"}");
        });
    }

请注意,代码使用光标在DMEvent列表中移动。我有一个任务来记录这些。我确实有示例代码,您可以仔细阅读这些示例代码,以了解如何在源代码的LINQ to Twitter Direct Message Demos项目中使用各种方法。