/// <summary>
        ///     Send direct text message to provided users and threads
        /// </summary>
        /// <param name="recipients">Comma-separated users PK</param>
        /// <param name="threadIds">Message thread ids</param>
        /// <param name="text">Message text</param>
        /// <returns>List of threads</returns>
        public async Task <IResult <InstaDirectInboxThreadList> > SendDirectTextAsync(string recipients, string threadIds,
                                                                                      string text)
        {
            UserAuthValidator.Validate(_userAuthValidate);
            var threads = new InstaDirectInboxThreadList();

            try
            {
                var directSendMessageUri = UriCreator.GetDirectSendMessageUri();
                var request = _httpHelper.GetDefaultRequest(HttpMethod.Post, directSendMessageUri, _deviceInfo);
                var fields  = new Dictionary <string, string> {
                    { "text", text }
                };
                if (!string.IsNullOrEmpty(recipients))
                {
                    fields.Add("recipient_users", "[[" + recipients + "]]");
                }
                else
                {
                    fields.Add("recipient_users", "[]");
                }
                //else
                //    return Result.Fail<InstaDirectInboxThreadList>("Please provide at least one recipient.");
                if (!string.IsNullOrEmpty(threadIds))
                {
                    fields.Add("thread_ids", "[" + threadIds + "]");
                }

                request.Content = new FormUrlEncodedContent(fields);
                var response = await _httpRequestProcessor.SendAsync(request);

                var json = await response.Content.ReadAsStringAsync();

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(Result.UnExpectedResponse <InstaDirectInboxThreadList>(response, json));
                }
                var result = JsonConvert.DeserializeObject <InstaSendDirectMessageResponse>(json);
                if (!result.IsOk())
                {
                    return(Result.Fail <InstaDirectInboxThreadList>(result.Status));
                }
                threads.AddRange(result.Threads.Select(thread =>
                                                       ConvertersFabric.Instance.GetDirectThreadConverter(thread).Convert()));
                return(Result.Success(threads));
            }
            catch (Exception exception)
            {
                _logger?.LogException(exception);
                return(Result.Fail <InstaDirectInboxThreadList>(exception));
            }
        }
예제 #2
0
        public async Task <IResult <InstaDirectInboxThreadList> > ShareMedia(string mediaId, InstaMediaType mediaType,
                                                                             params string[] threads)
        {
            var threadList = new InstaDirectInboxThreadList();

            try
            {
                var directSendMessageUri = UriCreator.GetShareMediaUri(mediaType);
                var request = HttpHelper.GetDefaultRequest(HttpMethod.Post, directSendMessageUri, _deviceInfo);
                var fields  = new Dictionary <string, string>
                {
                    { "media_id", mediaId },
                    { "unified_broadcast_format", "1" },
                    { "action", "send_item" }
                };

                if (threads == null || threads.Length < 1)
                {
                    return(Result.Fail <InstaDirectInboxThreadList>("Please provide at least one thread."));
                }
                fields.Add("thread_ids", "[" + string.Join(",", threads) + "]");

                request.Content = new FormUrlEncodedContent(fields);
                var response = await _httpRequestProcessor.SendAsync(request);

                var json = await response.Content.ReadAsStringAsync();

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(Result.UnExpectedResponse <InstaDirectInboxThreadList>(response, json));
                }
                var result = JsonConvert.DeserializeObject <InstaSendDirectMessageResponse>(json);
                if (!result.IsOk())
                {
                    return(Result.Fail <InstaDirectInboxThreadList>(result.Status));
                }
                threadList.AddRange(result.Threads.Select(thread =>
                                                          ConvertersFabric.Instance.GetDirectThreadConverter(thread).Convert()));
                return(Result.Success(threadList));
            }
            catch (Exception exception)
            {
                _logger?.LogException(exception);
                return(Result.Fail <InstaDirectInboxThreadList>(exception));
            }
        }
예제 #3
0
        public async Task <IResult <InstaDirectInboxThreadList> > SendLinkMessage(InstaMessageLink message, params long[] recipients)
        {
            var threads = new InstaDirectInboxThreadList();

            try
            {
                var directSendMessageUri = UriCreator.GetDirectSendLinkMessageUri();
                var request = HttpHelper.GetDefaultRequest(HttpMethod.Post, directSendMessageUri, _deviceInfo);
                var fields  = new Dictionary <string, string>
                {
                    { "link_text", message.Text },
                    { "link_urls", $"[\"{message.Url}\"]" },
                    { "action", "send_item" }
                };

                if (recipients == null || recipients.Length < 1)
                {
                    return(Result.Fail <InstaDirectInboxThreadList>("Please provide at least one recipient."));
                }

                fields.Add("recipient_users", "[[" + string.Join(",", recipients) + "]]");
                request.Content = new FormUrlEncodedContent(fields);
                var response = await _httpRequestProcessor.SendAsync(request);

                var json = await response.Content.ReadAsStringAsync();

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(Result.UnExpectedResponse <InstaDirectInboxThreadList>(response, json));
                }
                var result = JsonConvert.DeserializeObject <InstaSendDirectMessageResponse>(json);
                if (!result.IsOk())
                {
                    return(Result.Fail <InstaDirectInboxThreadList>(result.Status));
                }
                threads.AddRange(result.Threads.Select(thread =>
                                                       ConvertersFabric.Instance.GetDirectThreadConverter(thread).Convert()));
                return(Result.Success(threads));
            }
            catch (Exception exception)
            {
                _logger?.LogException(exception);
                return(Result.Fail <InstaDirectInboxThreadList>(exception));
            }
        }