示例#1
0
        /// <summary>
        /// Send a tweet with media.
        /// </summary>
        /// <param name="message">The text to be tweeted.</param>
        /// <param name="filePath">The file path in which to send</param>
        /// <param name="replyToId">Optional ID of a tweet in which to reply.</param>
        /// <returns>The response of the request sent.</returns>
        public TwitterResponse SendTweetWithMedia(string message, string filePath, string replyToId = null)
        {
            TwitterResponse tweetResponse;
            RestRequest     uploadMediaRequest = new RestRequest(UPLOAD_MEDIA_URL, Method.POST);

            uploadMediaRequest.AlwaysMultipartFormData = true;
            uploadMediaRequest.AddFile("media", File.ReadAllBytes(filePath), filePath, "appliation/octet-stream");
            IRestResponse uploadMediaResponse = twitterUploadClient.Execute(uploadMediaRequest);

            if (uploadMediaResponse.StatusCode == HttpStatusCode.OK)
            {
                dynamic content = SimpleJson.DeserializeObject(uploadMediaResponse.Content);
                string  mediaId = content.media_id_string;
                tweetResponse = SendTweetMaster(message, mediaId, replyToId);
            }
            else
            {
                tweetResponse               = new TwitterResponse();
                tweetResponse.Failed        = true;
                tweetResponse.FailedMessage = "Failed to upload media";
                tweetResponse.StatusCode    = uploadMediaResponse.StatusCode;
            }
            return(tweetResponse);
        }
示例#2
0
        private TwitterResponse SendTweetMaster(string message, string mediaIds = null, string replyToId = null)
        {
            RestRequest tweetRequest = new RestRequest(TWEET_URL, Method.POST);

            tweetRequest.AddQueryParameter("status", message);
            if (mediaIds != null)
            {
                tweetRequest.AddQueryParameter("media_ids", mediaIds);
            }
            if (replyToId != null)
            {
                tweetRequest.AddQueryParameter("in_reply_to_status_id", replyToId);
            }
            IRestResponse   response      = twitterApiClient.Execute(tweetRequest);
            TwitterResponse tweetResponse = new TwitterResponse();

            tweetResponse.StatusCode = response.StatusCode;
            if (response.StatusCode != HttpStatusCode.OK)
            {
                tweetResponse.Failed        = true;
                tweetResponse.FailedMessage = "Failed to post tweet";
            }
            return(tweetResponse);
        }