Example #1
0
        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"}");
                }
            });
        }
Example #2
0
        static async Task ShowDirectMessagesAsync(TwitterContext twitterCtx)
        {
            DirectMessageEvents dmResponse =
                await
                    (from dm in twitterCtx.DirectMessageEvents
                    where dm.Type == DirectMessageEventsType.Show &&
                    dm.ID == 917929712638246916
                    select dm)
                .SingleOrDefaultAsync();

            DirectMessageCreate 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");
            }
        }
        public async Task NewDirectMessageEventsAsync_WithRecipientID_ReturnsCreatedDM()
        {
            const ulong  RecipientID = 1;
            const string Text        = "Hi";
            var          ctx         = InitializeTwitterContext();

            DirectMessageEvents dmEvt = await ctx.NewDirectMessageEventAsync(RecipientID, Text);

            Assert.IsNotNull(dmEvt);
            DirectMessageEventsValue dmVal = dmEvt.Value;

            Assert.IsNotNull(dmVal);
            DMEvent evt = dmVal.DMEvent;

            Assert.IsNotNull(evt);
            Assert.AreEqual("message_create", evt.Type);
            Assert.AreEqual("917929712638246916", evt.ID);
            Assert.AreEqual("1507686472459", evt.CreatedTimestamp);
            Assert.AreEqual(new DateTime(2017, 10, 11, 01, 47, 52, 459), evt.CreatedAt);
            DirectMessageCreate mc = evt.MessageCreate;

            Assert.IsNotNull(mc);
            Assert.AreEqual("15411837", mc.SenderID);
            DirectMessageTarget tgt = mc.Target;

            Assert.IsNotNull(tgt);
            Assert.AreEqual("16761255", tgt.RecipientID);
            DirectMessageData md = mc.MessageData;

            Assert.IsNotNull(md);
            Assert.AreEqual(
                "DM from @JoeMayo to @Linq2Twitr of $MSFT &amp; $TSLA with #TwitterAPI #chatbot at https://t.co/ikGq2AnAir and https://t.co/nLvwwjz40q on 10/10/2017 6:47:46 PM!'",
                md.Text);
            Entities ent = md.Entities;

            Assert.IsNotNull(ent);
            List <HashTagEntity> hashtags = ent.HashTagEntities;

            Assert.IsNotNull(hashtags);
            Assert.AreEqual(2, hashtags.Count);
            HashTagEntity ht1 = hashtags[0];

            Assert.IsNotNull(ht1);
            Assert.AreEqual("TwitterAPI", ht1.Text);
            int[] ht1i = ht1.Indices;
            Assert.IsNotNull(ht1i);
            Assert.AreEqual(58, ht1i[0]);
            Assert.AreEqual(69, ht1i[1]);
            HashTagEntity ht2 = hashtags[1];

            Assert.IsNotNull(ht2);
            Assert.AreEqual("chatbot", ht2.Text);
            int[] ht2i = ht2.Indices;
            Assert.IsNotNull(ht2i);
            Assert.AreEqual(70, ht2i[0]);
            Assert.AreEqual(78, ht2i[1]);
            List <SymbolEntity> syms = ent.SymbolEntities;

            Assert.IsNotNull(syms);
            Assert.AreEqual(2, syms.Count);
            SymbolEntity sym1 = syms[0];

            Assert.IsNotNull(sym1);
            Assert.AreEqual("MSFT", sym1.Text);
            int[] sym1i = sym1.Indices;
            Assert.IsNotNull(sym1i);
            Assert.AreEqual(35, sym1i[0]);
            Assert.AreEqual(40, sym1i[1]);
            SymbolEntity sym2 = syms[1];

            Assert.IsNotNull(sym2);
            Assert.AreEqual("TSLA", sym2.Text);
            int[] sym2i = sym2.Indices;
            Assert.IsNotNull(sym2i);
            Assert.AreEqual(47, sym2i[0]);
            Assert.AreEqual(52, sym2i[1]);
            List <UserMentionEntity> usrs = ent.UserMentionEntities;

            Assert.IsNotNull(usrs);
            Assert.AreEqual(2, usrs.Count);
            UserMentionEntity um1 = usrs[0];

            Assert.IsNotNull(um1);
            Assert.AreEqual("JoeMayo", um1.ScreenName);
            Assert.AreEqual("Joe Mayo", um1.Name);
            Assert.AreEqual(15411837ul, um1.Id);
            Assert.AreEqual("15411837", um1.IdStr);
            int[] um1i = um1.Indices;
            Assert.IsNotNull(um1i);
            Assert.AreEqual(2, um1i.Length);
            Assert.AreEqual(8, um1i[0]);
            Assert.AreEqual(16, um1i[1]);
            UserMentionEntity um2 = usrs[1];

            Assert.IsNotNull(um2);
            Assert.AreEqual("Linq2Twitr", um2.ScreenName);
            Assert.AreEqual("LINQ to Twitr", um2.Name);
            Assert.AreEqual(16761255ul, um2.Id);
            Assert.AreEqual("16761255", um2.IdStr);
            int[] um2i = um2.Indices;
            Assert.IsNotNull(um2i);
            Assert.AreEqual(2, um2i.Length);
            Assert.AreEqual(20, um2i[0]);
            Assert.AreEqual(31, um2i[1]);
            List <UrlEntity> urls = ent.UrlEntities;

            Assert.IsNotNull(urls);
            Assert.AreEqual(2, urls.Count);
            UrlEntity url1 = urls[0];

            Assert.IsNotNull(url1);
            Assert.AreEqual("https://t.co/ikGq2AnAir", url1.Url);
            Assert.AreEqual("http://bit.ly/2xSJWJk", url1.ExpandedUrl);
            Assert.AreEqual("bit.ly/2xSJWJk", url1.DisplayUrl);
            int[] url1i = url1.Indices;
            Assert.IsNotNull(url1i);
            Assert.AreEqual(2, url1i.Length);
            Assert.AreEqual(82, url1i[0]);
            Assert.AreEqual(105, url1i[1]);
            UrlEntity url2 = urls[1];

            Assert.IsNotNull(url2);
            Assert.AreEqual("https://t.co/nLvwwjz40q", url2.Url);
            Assert.AreEqual("http://amzn.to/2gD09X6", url2.ExpandedUrl);
            Assert.AreEqual("amzn.to/2gD09X6", url2.DisplayUrl);
            int[] url2i = url2.Indices;
            Assert.IsNotNull(url2i);
            Assert.AreEqual(2, url2i.Length);
            Assert.AreEqual(110, url2i[0]);
            Assert.AreEqual(133, url2i[1]);
        }
Example #4
0
        public void ProcessResults_ForShow_HandlesSingleResult()
        {
            var dmReqProc = new DirectMessageEventsRequestProcessor <DirectMessageEvents>
            {
                BaseUrl = "https://api.twitter.com/1.1/",
                Type    = DirectMessageEventsType.Show
            };

            List <DirectMessageEvents> dms = dmReqProc.ProcessResults(TestQuerySingleResponse);

            Assert.IsNotNull(dms?.SingleOrDefault());

            DirectMessageEvents dmEvt = dms.First();

            Assert.IsNotNull(dmEvt);
            DirectMessageEventsValue dmVal = dmEvt.Value;

            Assert.IsNotNull(dmVal);
            DMEvent evt = dmVal.DMEvent;

            Assert.IsNotNull(evt);
            Assert.AreEqual("message_create", evt.Type);
            Assert.AreEqual("917929712638246916", evt.ID);
            Assert.AreEqual("1507686472459", evt.CreatedTimestamp);
            Assert.AreEqual(new DateTime(2017, 10, 11, 01, 47, 52, 459), evt.CreatedAt);
            DirectMessageCreate mc = evt.MessageCreate;

            Assert.IsNotNull(mc);
            Assert.AreEqual("15411837", mc.SenderID);
            Assert.AreEqual("472356", mc.SourceAppID);
            DirectMessageTarget tgt = mc.Target;

            Assert.IsNotNull(tgt);
            Assert.AreEqual("16761255", tgt.RecipientID);
            DirectMessageData md = mc.MessageData;

            Assert.IsNotNull(md);
            Assert.AreEqual(
                "DM from @JoeMayo to @Linq2Twitr of $MSFT &amp; $TSLA with #TwitterAPI #chatbot at https://t.co/ikGq2AnAir and https://t.co/nLvwwjz40q on 10/10/2017 6:47:46 PM!'",
                md.Text);
            Entities ent = md.Entities;

            Assert.IsNotNull(ent);
            List <HashTagEntity> hashtags = ent.HashTagEntities;

            Assert.IsNotNull(hashtags);
            Assert.AreEqual(2, hashtags.Count);
            HashTagEntity ht1 = hashtags[0];

            Assert.IsNotNull(ht1);
            Assert.AreEqual("TwitterAPI", ht1.Text);
            int[] ht1i = ht1.Indices;
            Assert.IsNotNull(ht1i);
            Assert.AreEqual(58, ht1i[0]);
            Assert.AreEqual(69, ht1i[1]);
            HashTagEntity ht2 = hashtags[1];

            Assert.IsNotNull(ht2);
            Assert.AreEqual("chatbot", ht2.Text);
            int[] ht2i = ht2.Indices;
            Assert.IsNotNull(ht2i);
            Assert.AreEqual(70, ht2i[0]);
            Assert.AreEqual(78, ht2i[1]);
            List <SymbolEntity> syms = ent.SymbolEntities;

            Assert.IsNotNull(syms);
            Assert.AreEqual(2, syms.Count);
            SymbolEntity sym1 = syms[0];

            Assert.IsNotNull(sym1);
            Assert.AreEqual("MSFT", sym1.Text);
            int[] sym1i = sym1.Indices;
            Assert.IsNotNull(sym1i);
            Assert.AreEqual(35, sym1i[0]);
            Assert.AreEqual(40, sym1i[1]);
            SymbolEntity sym2 = syms[1];

            Assert.IsNotNull(sym2);
            Assert.AreEqual("TSLA", sym2.Text);
            int[] sym2i = sym2.Indices;
            Assert.IsNotNull(sym2i);
            Assert.AreEqual(47, sym2i[0]);
            Assert.AreEqual(52, sym2i[1]);
            List <UserMentionEntity> usrs = ent.UserMentionEntities;

            Assert.IsNotNull(usrs);
            Assert.AreEqual(2, usrs.Count);
            UserMentionEntity um1 = usrs[0];

            Assert.IsNotNull(um1);
            Assert.AreEqual("JoeMayo", um1.ScreenName);
            Assert.AreEqual("Joe Mayo", um1.Name);
            Assert.AreEqual(15411837ul, um1.Id);
            Assert.AreEqual("15411837", um1.IdStr);
            int[] um1i = um1.Indices;
            Assert.IsNotNull(um1i);
            Assert.AreEqual(2, um1i.Length);
            Assert.AreEqual(8, um1i[0]);
            Assert.AreEqual(16, um1i[1]);
            UserMentionEntity um2 = usrs[1];

            Assert.IsNotNull(um2);
            Assert.AreEqual("Linq2Twitr", um2.ScreenName);
            Assert.AreEqual("LINQ to Twitr", um2.Name);
            Assert.AreEqual(16761255ul, um2.Id);
            Assert.AreEqual("16761255", um2.IdStr);
            int[] um2i = um2.Indices;
            Assert.IsNotNull(um2i);
            Assert.AreEqual(2, um2i.Length);
            Assert.AreEqual(20, um2i[0]);
            Assert.AreEqual(31, um2i[1]);
            List <UrlEntity> urls = ent.UrlEntities;

            Assert.IsNotNull(urls);
            Assert.AreEqual(2, urls.Count);
            UrlEntity url1 = urls[0];

            Assert.IsNotNull(url1);
            Assert.AreEqual("https://t.co/ikGq2AnAir", url1.Url);
            Assert.AreEqual("http://bit.ly/2xSJWJk", url1.ExpandedUrl);
            Assert.AreEqual("bit.ly/2xSJWJk", url1.DisplayUrl);
            int[] url1i = url1.Indices;
            Assert.IsNotNull(url1i);
            Assert.AreEqual(2, url1i.Length);
            Assert.AreEqual(82, url1i[0]);
            Assert.AreEqual(105, url1i[1]);
            UrlEntity url2 = urls[1];

            Assert.IsNotNull(url2);
            Assert.AreEqual("https://t.co/nLvwwjz40q", url2.Url);
            Assert.AreEqual("http://amzn.to/2gD09X6", url2.ExpandedUrl);
            Assert.AreEqual("amzn.to/2gD09X6", url2.DisplayUrl);
            int[] url2i = url2.Indices;
            Assert.IsNotNull(url2i);
            Assert.AreEqual(2, url2i.Length);
            Assert.AreEqual(110, url2i[0]);
            Assert.AreEqual(133, url2i[1]);
            JsonElement app       = dmVal.Apps;
            JsonElement appDetail = app.GetProperty("472356");

            Assert.AreEqual("472356", appDetail.GetString("id"));
            Assert.AreEqual("LINQ to Tweeter", appDetail.GetString("name"));
            Assert.AreEqual("https://github.com/JoeMayo/LinqToTwitter", appDetail.GetString("url"));
        }