Example #1
0
        public ServiceResponse <SendSmsResponse> SendSmsAync(SendSmsRequest smsRequest)
        {
            Log.Error("Started sms sending job");
            Log.Error("Message {@smsRequest} sent successfully", smsRequest);
            var endPoint = _smsConfig.Value.AfricasTalking.Endpoint + "/messaging";

            try
            {
                using (var client = new WebClient())
                {
                    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                    client.Headers.Add("apiKey", _smsConfig.Value.AfricasTalking.Password);
                    client.Headers.Add("accept", "application/json");
                    var values = new NameValueCollection
                    {
                        { "message", smsRequest.Message },
                        { "to", FormatRecipients(smsRequest.Recipients) },
                        { "username", _smsConfig.Value.AfricasTalking.Username },
                        { "from", _smsConfig.Value.AfricasTalking.From }
                    };

                    var responsebytes          = client.UploadValues(endPoint, "POST", values);
                    var result                 = Encoding.UTF8.GetString(responsebytes);
                    var africasTalkingResponse = JsonConvert.DeserializeObject <SendSmsResponse>(result);

                    //initiate the saving process
                    if (africasTalkingResponse.SMSMessageData.Recipients.Count > 0)
                    {
                        foreach (var recipient in africasTalkingResponse.SMSMessageData.Recipients)
                        {
                            var addSmsRequest = new AddSmsRequest
                            {
                                Message        = smsRequest.Message,
                                MessageId      = africasTalkingResponse.SMSMessageData.Recipients[0].messageId,
                                Recipient      = FormatRecipient(recipient.number),
                                MessageType    = smsRequest.MessageType,
                                SmsBillingType = (smsRequest.MessageType.Equals(MessageType.BULK) || smsRequest.MessageType.Equals(MessageType.SINGLE) || smsRequest.MessageType.Equals(MessageType.MONTHLYSMS)) ? SmsBillingType.FACTORY : SmsBillingType.CONTACT,
                                SmsCategory    = SmsCategory.OUTBOX,
                                SmsStatus      = recipient.status.Equals("success") ? SmsStatus.SUCCESS : SmsStatus.SENT
                            };

                            //BackgroundJob.Enqueue(() => _smsService.AddSms(addSmsRequest));
                        }
                    }
                    Log.Error("Ended sms sending job");
                    return(new ServiceResponse <SendSmsResponse>(africasTalkingResponse));
                }
            }
            catch (WebException ex)
            {
                Log.Error($"An Error Occured While Sending the sms {ex.Message}");
                return(new ServiceResponse <SendSmsResponse>($"An Error Occured While Sending the sms {ex.Message}"));
            }
        }
Example #2
0
        public IHttpActionResult AddSms(AddSmsRequest request)
        {
            request.ValidateNotNull();
            SmsDomain smsDomain = new SmsDomain()
            {
                PhoneNumber    = request.PhoneNumber,
                From           = request.From,
                NotificationId = request.NotificationId
            };

            return(Ok(new AddSmsResponse()
            {
                Data = _smsManipulation.AddSms(smsDomain),
                Success = Common.Enumerations.ResponseStatus.Succeeded
            }));
        }
Example #3
0
        public GeneralResponse AddSms(AddSmsRequest request)
        {
            GeneralResponse response = new GeneralResponse();

            try
            {
                Sms sms = new Sms();
                sms.ID             = Guid.NewGuid();
                sms.CreateDate     = PersianDateTime.Now;
                sms.CreateEmployee = _employeeRepository.FindBy(request.CreateEmployeeID);
                sms.Body           = request.Body;
                sms.Customer       = this._customerRepository.FindBy(request.CustomerID);
                sms.Note           = request.Note;
                sms.RowVersion     = 1;

                #region Validation

                if (sms.GetBrokenRules().Count() > 0)
                {
                    foreach (BusinessRule businessRule in sms.GetBrokenRules())
                    {
                        response.ErrorMessages.Add(businessRule.Rule);
                    }

                    return(response);
                }

                #endregion

                _smsRepository.Add(sms);
                _uow.Commit();
            }
            catch (Exception ex)
            {
                response.ErrorMessages.Add(ex.Message);
            }

            return(response);
        }