Пример #1
0
        public async Task <CommunicationResponseViewModel> SendSMSPOSTMethodAsync(MessageObjectViewModel messageObjectViewModel)
        {
            var numbers = NumberFormatter.FormatNumbers(messageObjectViewModel);

            if (numbers == null)
            {
                return(new CommunicationResponseViewModel()
                {
                    ValidationStatus = "Failed"
                });
            }
            var response = await SendSms(messageObjectViewModel, numbers);

            return(response);
        }
Пример #2
0
        public static Dictionary <string, string> FormatNumbers(MessageObjectViewModel messageObjectViewModel)
        {
            Dictionary <string, string> Keys = new Dictionary <string, string>();

            if (messageObjectViewModel.GroupedContacts != null)
            {
                foreach (var Contacts in messageObjectViewModel.GroupedContacts)
                {
                    if (Contacts.PhoneNumber != null && Contacts.PhoneNumber.Length > 3)
                    {
                        Keys.Add(Contacts.PhoneNumber, Contacts.Name);
                    }
                }
            }

            if (messageObjectViewModel.Contacts != null)
            {
                foreach (var Contacts in messageObjectViewModel.Contacts)
                {
                    if (Contacts.PhoneNumber != null && Contacts.PhoneNumber.Length > 3)
                    {
                        Keys.Add(Contacts.PhoneNumber, Contacts.Name);
                    }
                }
            }


            if (messageObjectViewModel.ToContacts != null)
            {
                var Numbers = messageObjectViewModel.ToContacts.Split(",");
                foreach (var Contacts in Numbers)
                {
                    if (Contacts != null && Contacts.Length > 3)
                    {
                        Keys.Add(Contacts, "Unknown");
                    }
                }
            }
            if (Keys != null)
            {
                return(Keys);
            }

            return(null);
        }
Пример #3
0
        ///sends SMS,Returns Response
        private async Task <CommunicationResponseViewModel> SendSms(MessageObjectViewModel messageObjectViewModel, Dictionary <string, string> numbers)
        {
            var smsresponse = new CommunicationResponseViewModel();

            smsresponse.ReturnObjects = new List <CommunicationReportDetail>();
            smsresponse.MessageType   = CommunicationMedium.SMS;
            string wsUrl = "http://api.ebulksms.com/sendsms.json";

            EbulkSMSObject smsjson = new EbulkSMSObject();

            smsjson.SMS      = new SMS();
            smsjson.SMS.auth = new Auth {
                apikey = "", username = ""
            };

            smsjson.SMS.message = new Message {
                flash = "0", messagetext = messageObjectViewModel.Message, sender = messageObjectViewModel.Subject
            };
            List <Gsm> gsmNumbers = new List <Gsm>();
            //generate a unique id for the messages.


            string msgid = "";

            foreach (var item in numbers)
            {
                msgid = msgid = Guid.NewGuid().ToString();
                gsmNumbers.Add(new Gsm {
                    msidn = item.Key, msgid = msgid
                });

                smsresponse.ReturnObjects.Add(new CommunicationReportDetail()
                {
                    Date = DateTime.Now,
                    IsDeliveryReportChecked = false,
                    MesssageUUID            = msgid,
                    Recipient      = item.Key,
                    Name           = item.Value,
                    DeliveryReport = "awaiting report",
                    ID             = Guid.NewGuid()
                });
            }


            smsjson.SMS.recipients = new Recipients {
                gsm = gsmNumbers.ToArray()
            };

            var    client        = new HttpClient();
            string smsjsonString = Newtonsoft.Json.JsonConvert.SerializeObject(smsjson);

            HttpContent contentPost = new StringContent(smsjsonString, Encoding.UTF8, "application/json");
            var         response    = await client.PostAsync(new Uri(wsUrl), contentPost);

            string jsonResult = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                EbulkResponse res = JsonConvert.DeserializeObject <EbulkResponse>(jsonResult);
                switch (res.response.status)
                {
                case "INVALID_JSON":
                    smsresponse.Message   = "Invalid message formatting, please contact the software Provider";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "MISSING_USERNAME":
                    smsresponse.Message   = "The missing username, please contact the software provider";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "MISSING_APIKEY":
                    smsresponse.Message   = "The missing key, please contact the software provider";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "AUTH_FAILURE":
                    smsresponse.Message   = "Authentication failure, please contact the software provider";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "MISSING_SENDER":
                    smsresponse.Message   = "Missing Sender Name, please enter the sender's name";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "MISSING_MESSAGE":
                    smsresponse.Message   = "Missing message, please enter the sms message";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "MISSING_RECIPIENT":
                    smsresponse.Message   = "Missing recipients, please enter the sms recipients";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "INVALID_MESSAGE":
                    smsresponse.Message   = "Missing recipients, please enter the sms recipients";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "INVALID_SENDER":
                    smsresponse.Message   = "Invalid Sender, please enter a valid sender's name";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "UNKNOWN_ERROR":
                    smsresponse.Message   = "Unknown issues, please try again";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "INSUFFICIENT_MESSAGE":
                    smsresponse.Message   = "Error 101, please contact the administrator of the software";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;

                case "SUCCESS":
                    smsresponse.Message          = "Success";
                    smsresponse.Response         = "Message sent";
                    smsresponse.Status           = true;
                    smsresponse.ValidationStatus = "Correct";
                    smsresponse.UnitsUsed        = decimal.Parse(res.response.cost);
                    smsresponse.Subject          = smsjson.SMS.message.sender;
                    smsresponse.MessageBody      = smsjson.SMS.message.messagetext;
                    smsresponse.MessageType      = CommunicationMedium.SMS;

                    break;

                default:
                    smsresponse.Message   = "Error 303, try again or please contact the administrator of the software";
                    smsresponse.Response  = "Message was not sent";
                    smsresponse.Status    = false;
                    smsresponse.UnitsUsed = 0;
                    break;
                }
                return(smsresponse);
            }
            else
            {
                return(new CommunicationResponseViewModel {
                    UnitsUsed = 0, Status = false, Message = "Error 404", Response = "The message was not sent please try again"
                });;
            }
        }