Пример #1
0
        private async Task <BaseResponse <object> > SendInfoMessageBySms(InfoUserSms info, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                if (string.IsNullOrEmpty(info.PhoneNumber))
                {
                    info.ErrorString   = "No Phone Number";
                    info.IsSendSuccess = 1;
                    return(Result(info));
                }

                var requestContent = new StringContent("", Encoding.UTF8, "application/json");

                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.ConnectionClose = true;
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
                    var putUriPath = SendSmsUri
                                     + $"?secret-key={info.SecretKey}&customer-id={info.CustomerId}&user-id={info.UserId}&phone-number={info.PhoneNumber}&person-id={info.UserId}&language-id={info.LanguageId}";

                    using (var httpResponse = await httpClient.PostAsync(new Uri(putUriPath), requestContent, cancellationToken).ConfigureAwait(false))
                    {
                        // Convert response content to string data
                        var serverResponse = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        if (serverResponse.Contains("OK"))
                        {
                            info.IsSendSuccess = 1;
                        }

                        // Result
                        var data = JsonConvert.DeserializeObject <object>(serverResponse);
                        return(Result(data));
                    }
                }
            }
            catch (TaskCanceledException tEx)
            {
                Debug.WriteLine(tEx);
                info.ErrorString   = "Request Timeout: " + tEx;
                info.IsSendSuccess = 0;
                return(Result(info));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                info.ErrorString   = ex.ToString();
                info.IsSendSuccess = 0;
                return(Result(info));
            }
            finally
            {
                // Save MongoDB
                Logger?.Info($"Insert value to DB");
                info.DateSend = ConvertDateTime.GetDateTimeNowUtc();
                _connect.InsertMessageDocumentAsync(info);
                Logger?.Info($"Value insert DB: {JsonConvert.SerializeObject(info)}");
            }
        }
Пример #2
0
        private async Task <BaseResponse <object> > PushNotifyToClient(InfoUserSms info, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                // Push to Firebase
                // TODO
                return(null);
            }
            catch (TaskCanceledException tEx)
            {
                Debug.WriteLine(tEx);
                if (!cancellationToken.IsCancellationRequested)
                {
                    throw;
                }

                info.ErrorString   = "Request Timeout: " + tEx;
                info.IsSendSuccess = 0;
                return(null);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                info.ErrorString   = ex.ToString();
                info.IsSendSuccess = 0;
                return(null);
            }
            finally
            {
                // Save MongoDB
                Logger?.Info($"Insert value to DB");
                info.DateSend = ConvertDateTime.GetDateTimeNowUtc();
                _connect.InsertMessageDocumentAsync(info);
                Logger?.Info($"Value insert DB: {JsonConvert.SerializeObject(info)}");
            }
        }