Beispiel #1
0
        /// <summary>
        /// Act as sms was sent. Log event to console
        /// </summary>
        /// <param name="toPhone"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task <bool> SendSMS(string toPhone, ISMS data)
        {
            try
            {
                if (string.IsNullOrEmpty(toPhone))
                {
                    throw new ArgumentException(localizer["Phone must not be empty"].Value);
                }

                if (data is null)
                {
                    throw new ArgumentNullException(localizer["SMS data must not be empty"].Value);
                }

                var token = await GetToken();

                logger.LogInformation($"Sending SMS message to {toPhone}");
                var request = new RestRequest("api/v1/messages", Method.POST, DataFormat.Json);

                var text = data.GetText();
                if (text.Length >= 70)
                {
                    // zrus diakritiku
                    text = Helpers.Text.RemoveDiacritism(text);
                }

                if (text.Length > 160)
                {
                    text = text.Substring(0, 158) + "..";
                }

                request.AddJsonBody(new GoSMSSendMessage()
                {
                    channel    = settings.Value.Channel,
                    message    = text,
                    recipients = toPhone
                });
                request.AddHeader("Authorization", $"Bearer {token}");

                logger.LogInformation($"Sending SMS {Helpers.Hash.GetSHA256Hash(settings.Value.CoHash + toPhone)}");
                var response = await smsApiRestClient.ExecuteAsync(request);

                if (response.IsSuccessful)
                {
                    logger.LogInformation($"Sending SMS OK {Helpers.Hash.GetSHA256Hash(settings.Value.CoHash + toPhone)}");
                    return(true);
                }
                logger.LogError($"Error sending sms: {response.Content}");
                return(false);
            }
            catch (Exception exc)
            {
                logger.LogError(exc, $"Error sending SMS to: {toPhone} {exc.Message}");
                return(false);
            }
        }
        /// <summary>
        /// Act as sms was sent. Log event to console
        /// </summary>
        /// <param name="toPhone"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task <bool> SendSMS(string toPhone, ISMS data)
        {
            try
            {
                try
                {
                    var text = data.GetText();
                    if (text.Length >= 70)
                    {
                        text = Helpers.Text.RemoveDiacritism(text);
                    }

                    if (text.Length > 160)
                    {
                        text = text.Substring(0, 158) + "..";
                    }

                    var msg = new RabbitSMSMessage()
                    {
                        Phone   = toPhone,
                        Message = text,
                        User    = settings.Value.GatewayUser
                    };
                    var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg));


                    var factory = new ConnectionFactory()
                    {
                        HostName    = settings.Value.HostName,
                        UserName    = settings.Value.RabbitUserName,
                        Password    = settings.Value.RabbitPassword,
                        VirtualHost = settings.Value.VirtualHost
                    };
                    using var connection = factory.CreateConnection();
                    using var channel    = connection.CreateModel();
                    channel.BasicPublish(exchange: settings.Value.Exchange,
                                         routingKey: settings.Value.QueueName,
                                         body: body);
                    logger.LogInformation($"Sent SMS to {settings.Value.HostName}/{settings.Value.VirtualHost}/{settings.Value.QueueName} {Helpers.Hash.GetSHA256Hash(settings.Value.CoHash + toPhone)}");

                    return(true);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, $"Failed to PostMessagesAsync to rabbit sms queue. Exception: {ex.Message}");
                    throw;
                }
            }
            catch (Exception exc)
            {
                logger.LogError(exc, $"Error sending SMS to: {toPhone} {exc.Message}");
                return(false);
            }
        }
        /// <summary>
        /// Act as sms was sent. Log event to console
        /// </summary>
        /// <param name="toPhone"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task <bool> SendSMS(string toPhone, ISMS data)
        {
            try
            {
                try
                {
                    var text = data.GetText();
                    if (text.Length >= 70)
                    {
                        text = Helpers.Text.RemoveDiacritism(text);
                    }

                    if (text.Length > 160)
                    {
                        text = text.Substring(0, 158) + "..";
                    }

                    var msg = new GoSMSSendMessage()
                    {
                        channel    = settings.Value.Channel,
                        message    = text,
                        recipients = toPhone
                    };

                    var sendMessageRequest = new SendMessageRequest
                    {
                        QueueUrl    = settings.Value.QueueURL,
                        MessageBody = JsonConvert.SerializeObject(msg)
                    };
                    logger.LogInformation($"Sending SMS {Helpers.Hash.GetSHA256Hash(settings.Value.CoHash + toPhone)}");
                    await amazonSQSClient.SendMessageAsync(sendMessageRequest);

                    return(true);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, $"Failed to PostMessagesAsync to sms queue. Exception: {ex.Message}");
                    throw;
                }
            }
            catch (Exception exc)
            {
                logger.LogError(exc, $"Error sending SMS to: {toPhone} {exc.Message}");
                return(false);
            }
        }