Ejemplo n.º 1
0
        private void NotifyBalanceChangeOnSendSuccessfully(MessagesResponse response)
        {
            if (response == null || !response.Messages.Any())
            {
                return;
            }
            MessageResponse messageResponse = response.Messages.First();

            if (messageResponse.Status == MessageResponseStatus.Accepted)
            {
                foreach (var item in response.Balances)
                {
                    if (!decimal.TryParse(item.BalanceValue, out decimal balance))
                    {
                        logger.Warn($"Невозможно преобразовать значение баланса из \"{item.BalanceValue}\" в число");
                        continue;
                    }
                    OnBalanceChange?.Invoke(this, new SmsBalanceEventArgs(ConvertBalanceType(item.Type), balance));
                }
            }
        }
Ejemplo n.º 2
0
        public ISmsSendResult SendSms(ISmsMessage message)
        {
            var response = smsRuProvider.Send(configuration.SmsNumberFrom, message.MobilePhoneNumber, message.MessageText, message.ScheduleTime);

            if (!string.IsNullOrEmpty(response))
            {
                var lines = response.Split('\n');

                var enumStatus = Enum.Parse(typeof(ResponseOnSendRequest), lines[0]);

                SmsSendResult smsSendResponse;

                switch (enumStatus)
                {
                case ResponseOnSendRequest.MessageAccepted:
                    smsSendResponse = new SmsSendResult(SmsSentStatus.Accepted);

                    var balanceLine = lines.FirstOrDefault(x => x.StartsWith(balanceStringPrefix));

                    var culture = CultureInfo.CreateSpecificCulture("ru-RU");
                    culture.NumberFormat.NumberDecimalSeparator = ".";

                    if (balanceLine != null && decimal.TryParse(balanceLine.Substring(balanceStringPrefix.Length), NumberStyles.AllowDecimalPoint, culture.NumberFormat, out decimal newBalance))
                    {
                        OnBalanceChange?.Invoke(this, new SmsBalanceEventArgs(BalanceType.CurrencyBalance, newBalance));
                    }
                    else
                    {
                        logger.Warn("Не удалось получить баланс в ответном сообщении");
                    }

                    break;

                case ResponseOnSendRequest.BadRecipient:
                case ResponseOnSendRequest.BlacklistedRecepient:
                case ResponseOnSendRequest.CantSendToThisNumber:
                case ResponseOnSendRequest.DayMessageLimitToNumber:
                    smsSendResponse = new SmsSendResult(SmsSentStatus.InvalidMobilePhone);
                    break;

                case ResponseOnSendRequest.MessageTextNotSpecified:
                    smsSendResponse = new SmsSendResult(SmsSentStatus.TextIsEmpty);
                    break;

                case ResponseOnSendRequest.BadSender:
                    smsSendResponse = new SmsSendResult(SmsSentStatus.SenderAddressInvalid);
                    break;

                case ResponseOnSendRequest.OutOfMoney:
                    smsSendResponse = new SmsSendResult(SmsSentStatus.NotEnoughBalance);
                    break;

                default:
                    smsSendResponse = new SmsSendResult(SmsSentStatus.UnknownError);
                    break;
                }

                return(smsSendResponse);
            }
            else
            {
                throw new Exception("Не получен ответ от сервера");
            }
        }