/// <summary> /// sends a new direct message to specified userr /// </summary> /// <param name="ctx">Twitter Context</param> /// <param name="user">UserID or ScreenName of user to send to</param> /// <param name="text">Direct message contents</param> /// <param name="wrapLinks">Shorten links using Twitter's t.co wrapper</param> /// <param name="callback">Async Callback</param> /// <returns>Direct message element</returns> public static DirectMessage NewDirectMessage(this TwitterContext ctx, string user, string text, Action <TwitterAsyncResponse <DirectMessage> > callback) { if (string.IsNullOrEmpty(user)) { throw new ArgumentException("user is a required parameter.", "user"); } if (string.IsNullOrEmpty(text)) { throw new ArgumentException("text is a required parameter.", "text"); } var newUrl = ctx.BaseUrl + "direct_messages/new.json"; var reqProc = new DirectMessageRequestProcessor <DirectMessage>(); var twitExe = ctx.TwitterExecutor; twitExe.AsyncCallback = callback; var resultsJson = twitExe.PostToTwitter( newUrl, new Dictionary <string, string> { { "user", user }, { "text", text } }, response => reqProc.ProcessActionResult(response, DirectMessageType.Show)); DirectMessage dm = reqProc.ProcessActionResult(resultsJson, DirectMessageType.Show); return(dm); }
/// <summary> /// deletes a direct message /// </summary> /// <param name="ctx">Twitter Context</param> /// <param name="id">id of direct message</param> /// <param name="callback">Async Callback</param> /// <returns>direct message element</returns> public static DirectMessage DestroyDirectMessage(this TwitterContext ctx, string id, bool includeEntites, Action <TwitterAsyncResponse <DirectMessage> > callback) { if (string.IsNullOrEmpty(id)) { throw new ArgumentException("id is a required parameter.", "id"); } var destroyUrl = ctx.BaseUrl + "direct_messages/destroy.json"; var reqProc = new DirectMessageRequestProcessor <DirectMessage>(); var twitExe = ctx.TwitterExecutor; twitExe.AsyncCallback = callback; var resultsJson = twitExe.PostToTwitter( destroyUrl, new Dictionary <string, string> { { "id", id }, { "include_entities", includeEntites.ToString().ToLower() } }, response => reqProc.ProcessActionResult(response, DirectMessageType.Show)); DirectMessage dm = reqProc.ProcessActionResult(resultsJson, DirectMessageType.Show); return(dm); }
public void DirectMessage_Constructor_Parses_Json() { var dmJson = JsonMapper.ToObject(DirectMessagesResponse); var dm = new DirectMessage(dmJson); Assert.Equal(new DateTimeOffset(2012, 7, 22, 18, 38, 09, new TimeSpan(0L)).DateTime, dm.CreatedAt); Assert.Equal(15411837ul, dm.SenderID); Assert.Equal("JoeMayo", dm.SenderScreenName); Assert.NotNull(dm.Sender); Assert.Equal(16761255ul, dm.RecipientID); Assert.Equal("Linq2Tweeter", dm.RecipientScreenName); Assert.NotNull(dm.Recipient); Assert.Equal(227110300510461952ul, dm.IDResponse); Assert.Equal("227110300510461952", dm.IDString); Assert.Equal("Testing entities from @JoeMayo : http://t.co/Q1CQHi2 #entitytest", dm.Text); Entities entities = dm.Entities; Assert.NotNull(entities); List<UserMentionEntity> userMentions = entities.UserMentionEntities; Assert.NotNull(userMentions); Assert.NotEmpty(userMentions); UserMentionEntity usrMention = userMentions.FirstOrDefault(); Assert.NotNull(usrMention); Assert.Equal("Joe Mayo", usrMention.Name); }
public T ProcessActionResult(string responseJson, Enum theAction) { JsonData dmJson = JsonMapper.ToObject(responseJson); var dm = new DirectMessage(dmJson); return(dm.ItemCast(default(T))); }
public void DirectMessage_Can_Serialize() { var dm = new DirectMessage(); var stringBuilder = new StringBuilder(); var writer = XmlWriter.Create(stringBuilder); var xmlSerializer = new XmlSerializer(typeof(DirectMessage)); xmlSerializer.Serialize(writer, dm); }
void ParseJson(string json) { JsonData jsonObj = JsonMapper.ToObject(json); if (jsonObj == null || jsonObj.InstObject == null) { EntityType = StreamEntityType.Unknown; return; } var inst = jsonObj.InstObject; try { if (inst.ContainsKey("control")) { EntityType = StreamEntityType.Control; Entity = new Control(jsonObj); } else if (inst.ContainsKey("delete")) { EntityType = StreamEntityType.Delete; Entity = new Delete(jsonObj); } else if (inst.ContainsKey("direct_message")) { EntityType = StreamEntityType.DirectMessage; var dmObj = jsonObj.GetValue<JsonData>("direct_message"); Entity = new DirectMessage(dmObj); } else if (inst.ContainsKey("disconnect")) { EntityType = StreamEntityType.Disconnect; Entity = new Disconnect(jsonObj); } else if (inst.ContainsKey("event")) { EntityType = StreamEntityType.Event; Entity = new Event(jsonObj); } else if (inst.ContainsKey("for_user")) { EntityType = StreamEntityType.ForUser; Entity = new ForUser(jsonObj); } else if (inst.ContainsKey("friends") && inst.Count == 1) { EntityType = StreamEntityType.FriendsList; Entity = new FriendsList(jsonObj); } else if (inst.ContainsKey("geo_scrub")) { EntityType = StreamEntityType.GeoScrub; Entity = new GeoScrub(jsonObj); } else if (inst.ContainsKey("limit")) { EntityType = StreamEntityType.Limit; Entity = new Limit(jsonObj); } else if (inst.ContainsKey("warning") && inst.ContainsKey("percent_full")) { EntityType = StreamEntityType.Stall; Entity = new Stall(jsonObj); } else if (inst.ContainsKey("status_withheld")) { EntityType = StreamEntityType.StatusWithheld; Entity = new StatusWithheld(jsonObj); } else if (inst.ContainsKey("warning") && inst.ContainsKey("user_id")) { EntityType = StreamEntityType.TooManyFollows; Entity = new TooManyFollows(jsonObj); } else if (inst.ContainsKey("retweeted")) { EntityType = StreamEntityType.Status; Entity = new Status(jsonObj); } else if (inst.ContainsKey("user_withheld")) { EntityType = StreamEntityType.UserWithheld; Entity = new UserWithheld(jsonObj); } else { EntityType = StreamEntityType.Unknown; } } catch (Exception ex) { string parseError = string.Format( "Error parsing twitter message. Please create a new issue on the LINQ to Twitter site at https://linqtotwitter.codeplex.com/ " + "with this info. \n\nMessage Type: {0}, Message Text:\n {1} \nException Details: {2} \n", EntityType, json, ex.ToString()); EntityType = StreamEntityType.ParseError; Entity = parseError; if (TwitterExecute.Log != null) TwitterExecute.Log.WriteLine(parseError); } }
void ParseJson(string json) { JsonData jsonObj = JsonMapper.ToObject(json); if (jsonObj == null || jsonObj.InstObject == null) { EntityType = StreamEntityType.Unknown; return; } var inst = jsonObj.InstObject; try { if (inst.ContainsKey("control")) { EntityType = StreamEntityType.Control; Entity = new Control(jsonObj); } else if (inst.ContainsKey("delete")) { EntityType = StreamEntityType.Delete; Entity = new Delete(jsonObj); } else if (inst.ContainsKey("direct_message")) { EntityType = StreamEntityType.DirectMessage; var dmObj = jsonObj.GetValue <JsonData>("direct_message"); Entity = new DirectMessage(dmObj); } else if (inst.ContainsKey("disconnect")) { EntityType = StreamEntityType.Disconnect; Entity = new Disconnect(jsonObj); } else if (inst.ContainsKey("event")) { EntityType = StreamEntityType.Event; Entity = new Event(jsonObj); } else if (inst.ContainsKey("for_user")) { EntityType = StreamEntityType.ForUser; Entity = new ForUser(jsonObj); } else if (inst.ContainsKey("friends") && inst.Count == 1) { EntityType = StreamEntityType.FriendsList; Entity = new FriendsList(jsonObj); } else if (inst.ContainsKey("geo_scrub")) { EntityType = StreamEntityType.GeoScrub; Entity = new GeoScrub(jsonObj); } else if (inst.ContainsKey("limit")) { EntityType = StreamEntityType.Limit; Entity = new Limit(jsonObj); } else if (inst.ContainsKey("warning") && inst.ContainsKey("percent_full")) { EntityType = StreamEntityType.Stall; Entity = new Stall(jsonObj); } else if (inst.ContainsKey("status_withheld")) { EntityType = StreamEntityType.StatusWithheld; Entity = new StatusWithheld(jsonObj); } else if (inst.ContainsKey("warning") && inst.ContainsKey("user_id")) { EntityType = StreamEntityType.TooManyFollows; Entity = new TooManyFollows(jsonObj); } else if (inst.ContainsKey("retweeted")) { EntityType = StreamEntityType.Status; Entity = new Status(jsonObj); } else if (inst.ContainsKey("user_withheld")) { EntityType = StreamEntityType.UserWithheld; Entity = new UserWithheld(jsonObj); } else { EntityType = StreamEntityType.Unknown; } } catch (Exception ex) { string parseError = string.Format( "Error parsing twitter message. Please create a new issue on the LINQ to Twitter site at https://linqtotwitter.codeplex.com/ " + "with this info. \n\nMessage Type: {0}, Message Text:\n {1} \nException Details: {2} \n", EntityType, json, ex.ToString()); EntityType = StreamEntityType.ParseError; Entity = parseError; if (TwitterExecute.Log != null) { TwitterExecute.Log.WriteLine(parseError); } } }