static async Task NewDirectMessageWithMediaAsync(TwitterContext twitterCtx) { const ulong Linq2TwitrID = 15411837;// 16761255; string mediaCategory = "dm_image"; Media?media = await twitterCtx.UploadMediaAsync( File.ReadAllBytes(@"..\..\..\images\200xColor_2.png"), mediaType : "image/png", additionalOwners : null, mediaCategory : mediaCategory, shared : true); 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 + "!'", media?.MediaID ?? 0ul); 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); } }
static async Task ListDirectMessagesAsync(TwitterContext twitterCtx) { 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"); } }); }
public void ProcessResults_WithShowQuery_ReplacesInputParams() { const ulong ID = 5; var dmReqProc = new DirectMessageEventsRequestProcessor <DirectMessageEvents> { BaseUrl = "https://api.twitter.com/1.1/", Type = DirectMessageEventsType.Show, ID = ID }; List <DirectMessageEvents> dms = dmReqProc.ProcessResults(TestQuerySingleResponse); DirectMessageEvents dm = dms.First(); Assert.AreEqual(ID, dm.ID); }
static async Task RequestButtonChoiceAsync(TwitterContext twitterCtx) { const ulong Linq2TwitrID = 15411837;// 16761255; var buttons = new List <CallToAction> { new CallToAction { Label = "Visit LINQ to Twitter Website", Url = "https://github.com/JoeMayo/LinqToTwitter", Type = "web_url" }, new CallToAction { Label = "Visit @JoeMayo on Twitter", Url = "https://twitter.com/JoeMayo", Type = "web_url" }, new CallToAction { Label = "Visit @Linq2Twitr on Twitter", Url = "https://twitter.com/Linq2Twitr", Type = "web_url" } }; DirectMessageEvents message = await twitterCtx.RequestButtonChoiceAsync( Linq2TwitrID, "Where would you like to go?", buttons); 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); } }
public void ProcessResults_WithListQuery_ReplacesInputParams() { const int Count = 4; const string Cursor = "ABC123"; var dmReqProc = new DirectMessageEventsRequestProcessor <DirectMessageEvents> { BaseUrl = "https://api.twitter.com/1.1/", Type = DirectMessageEventsType.List, Count = Count, Cursor = Cursor }; List <DirectMessageEvents> dms = dmReqProc.ProcessResults(TestQueryMultipleResponses); DirectMessageEvents dm = dms.First(); Assert.AreEqual(Count, dm.Count); Assert.AreEqual(Cursor, dm.Cursor); }
static async Task RequestQuickReplyOptionsAsync(TwitterContext twitterCtx) { const ulong Linq2TwitrID = 15411837;// 16761255; var options = new List <QuickReplyOption> { new QuickReplyOption { Label = "Option 1", Description = "Description for the first option.", Metadata = "abc001" }, new QuickReplyOption { Label = "Option 2", Description = "Description for the second option.", Metadata = "abc002" }, new QuickReplyOption { Label = "Option 3", Description = "Description for the third option.", Metadata = "abc003" }, }; DirectMessageEvents message = await twitterCtx.RequestQuickReplyOptionsAsync( Linq2TwitrID, "What is your choice?", options); 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); } }
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(); 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"); } }
static async Task NewDirectMessageAsync(TwitterContext twitterCtx) { const ulong Linq2TwitrID = 16761255; 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); } }
static async Task NewDirectMessageWithCoordinatesAsync(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://bit.ly/2xSJWJk and http://amzn.to/2gD09X6 on " + DateTime.Now + "!'", latitude : -122.443893, longitude : 37.771718); 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); } }
public void ProcessResults_ForList_HandlesMultipleResults() { var dmReqProc = new DirectMessageEventsRequestProcessor <DirectMessageEvents> { BaseUrl = "https://api.twitter.com/1.1/", Type = DirectMessageEventsType.List }; List <DirectMessageEvents> dms = dmReqProc.ProcessResults(TestQueryMultipleResponses); Assert.IsNotNull(dms?.SingleOrDefault()); DirectMessageEvents dmEvt = dms.First(); Assert.IsNotNull(dmEvt); DirectMessageEventsValue dmVal = dmEvt.Value; Assert.IsNotNull(dmVal); List <DMEvent> evts = dmVal.DMEvents; Assert.IsNotNull(evts); Assert.AreEqual(2, evts.Count); Assert.AreEqual("OTE3ODE0NTUzMzExOTMyNDIy", dmVal.NextCursor); }
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 & $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]); }
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 & $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")); }