diff --git a/README.md b/README.md index 922be94..7a8c6ee 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ and/or read tweets from your account: foreach (var tweet in tweets) Console.WriteLine("{0}: {1}", tweet.UserName, tweet.Text); + // Send a DM to your followers + twitter.SendMessage("Hello there!!", "") + Copyright and license --------------------- diff --git a/src/TinyTwitter/TinyTwitter.cs b/src/TinyTwitter/TinyTwitter.cs index dc39647..69eaeb9 100644 --- a/src/TinyTwitter/TinyTwitter.cs +++ b/src/TinyTwitter/TinyTwitter.cs @@ -44,6 +44,54 @@ public void UpdateStatus(string message) .Execute(); } + public bool SendMessage(string message, string screenName) + { + var userId = GetIdByScreenName(screenName); + + var eventObj = new + { + type = "message_create", + message_create = new + { + target = new + { + recipient_id = userId + }, + message_data = new {text = message} + }, + }; + + try + { + new RequestBuilder(oauth, "POST", + "https://api.twitter.com/1.1/direct_messages/events/new.json") + .Execute(true, eventObj); + return true; + } + catch + { + return false; + } + + } + + public string GetIdByScreenName(string screenName) + { + var response = new RequestBuilder(oauth, "GET", "https://api.twitter.com/1.1/users/show.json") + .AddParameter("screen_name", screenName) + .Execute(); + + dynamic deserializedResponse = new JavaScriptSerializer().DeserializeObject(response); + + if (deserializedResponse != null && deserializedResponse["id_str"] != null) + { + return deserializedResponse["id_str"].ToString(); + } + + return null; + + } + public IEnumerable GetHomeTimeline(long? sinceId = null, long? maxId = null, int? count = 20) { return GetTimeline("https://api.twitter.com/1.1/statuses/home_timeline.json", sinceId, maxId, count, ""); @@ -125,8 +173,8 @@ public RequestBuilder AddParameter(string name, string value) return this; } - public string Execute() - { + public string Execute(bool isSendMessage = false, object eventObj = null) + { var timespan = GetTimestamp(); var nonce = CreateNonce(); @@ -138,14 +186,21 @@ public string Execute() var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl()); request.Method = method; - request.ContentType = "application/x-www-form-urlencoded"; + request.ContentType = isSendMessage ? "application/json": "application/x-www-form-urlencoded"; request.Headers.Add("Authorization", headerValue); - WriteRequestBody(request); + if (isSendMessage && eventObj != null) + { + WriteRequestBody(request, eventObj); + } + else + { + WriteRequestBody(request); + } // It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions - // after some requests. Abort the request seems to work. More info: + // after some requests. Abort the request seems to work. More info: // http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly var response = request.GetResponse(); @@ -154,16 +209,16 @@ public string Execute() using (var stream = response.GetResponseStream()) { - using (var reader = new StreamReader(stream)) - { - content = reader.ReadToEnd(); - } + using (var reader = new StreamReader(stream)) + { + content = reader.ReadToEnd(); + } } request.Abort(); return content; - } + } private void WriteRequestBody(HttpWebRequest request) { @@ -175,6 +230,20 @@ private void WriteRequestBody(HttpWebRequest request) stream.Write(requestBody, 0, requestBody.Length); } + public void WriteRequestBody(HttpWebRequest request, object eventObj) + { + using (var streamWriter = new StreamWriter(request.GetRequestStream())) + { + string json = new JavaScriptSerializer().Serialize( new + { + @event = eventObj + }); + + streamWriter.Write(json); + } + + } + private string GetRequestUrl() { if (method != "GET" || customParameters.Count == 0)