public void OutboundSMSSerializeTest() { string receivercodes = "tel:+18002521111"; //string receivercodes = "tel:+18002521111,tel:+19793235555"; ISMSConfiguration config = new SMSConfiguration(); config.BaseUrlMessageApi = Configuration["SMS.AttApi:BaseUrlMessageApi"]; var msgBox = new MessageBox(config); var msgs = msgBox.GetMessages("0"); //string receivercodes = msgs[0].ReceiverCode; var wrapper = new OutboundSMSRequestWrapper { outboundSMSRequest = new OutboundSMSRequest { address = Common.SplitReceiverCodes(receivercodes), message = "test message" } }; object requestObj = Common.OutboundSMSRequestAdapter(wrapper); string output = JsonConvert.SerializeObject(requestObj); logger.LogInformation("output of OutboundSMSSerializeTest: "); logger.LogInformation(output); Assert.NotNull(wrapper); }
public int Send() { //get messages that haven't sent yet, where status = 0 var msgs = m_msgBox.GetMessages("0"); //retrieve messages from db, send to sms api and get message ID foreach (var msg in msgs) { var reqWrapper = new OutboundSMSRequestWrapper { outboundSMSRequest = new OutboundSMSRequest { address = Common.SplitReceiverCodes(msg.ReceiverCode), message = msg.Message } }; if (reqWrapper.outboundSMSRequest.address == null) { continue; } var res = new RestfulHelper() .SendSMSAsync(m_config.UrlSendSMS, AccessToken, reqWrapper) .GetAwaiter() .GetResult(); // if no qualified object gets from response, ignore this send request if (res == null || res.outboundSMSResponse == null) { continue; } msg.MessageID = res.outboundSMSResponse.messageId; //if there is no need to check sms delivery status, update status as "sent and delivered" if not check if (!m_config.VerifyMessageDeliveryStatus) { msg.Status = "2"; //sent and delivered msg.SendTime = DateTime.Now; } else { msg.Status = "1"; //just mark as sent msg.SendTime = DateTime.Now; } } int sentCount = 0; //update message ID and status back to DB foreach (var msg in msgs) { if (!String.IsNullOrEmpty(msg.MessageID)) { m_msgBox.UpdateMessage(msg); sentCount++; } } return(sentCount); }
public static void AttachTelPrefix(OutboundSMSRequestWrapper request) { List <string> addressFormated = new List <string>(); foreach (string addr in request.outboundSMSRequest.address) { addressFormated.Add(addr.Trim().Length >= 10 ? "tel:" + addr : addr); } request.outboundSMSRequest.address = addressFormated.ToArray(); }
public void AttachTelPrefixTest() { OutboundSMSRequestWrapper wrapper = new OutboundSMSRequestWrapper(); wrapper.outboundSMSRequest = new OutboundSMSRequest(); wrapper.outboundSMSRequest.address = new string[] { "6262521073", "48507075", "6262170884" }; Common.AttachTelPrefix(wrapper); string[] expected = new string[] { "tel:6262521073", "48507075", "tel:6262170884" }; Assert.Equal(expected, wrapper.outboundSMSRequest.address); }
///<summary> /// when send to muitple receiver, the expected ATT SMS adderss format is ["tel:21212,tel:343434"] /// ,but when send to single receiver, the expected ATT SMS adderss format is "tel:21212", there is no square bracket embraced. /// while c# array with one node only would still have the [] embraced, so there needs to be an adapter to follow ATT REST API specification ///</summary> public static object OutboundSMSRequestAdapter(OutboundSMSRequestWrapper request) { if (request.outboundSMSRequest.address.Length == 1) { return(new OutboundSMSSingleReceiverRequestWrapper { outboundSMSRequest = new OutboundSMSSingleReceiverRequest { address = request.outboundSMSRequest.address[0], message = request.outboundSMSRequest.message } }); } return(request); }
public async Task <OutboundSMSResponseWrapper> SendSMSAsync(string url, string accessToken, OutboundSMSRequestWrapper obSMSReqWrapper) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("Authorization", accessToken); Common.AttachTelPrefix(obSMSReqWrapper); object requestObj = Common.OutboundSMSRequestAdapter(obSMSReqWrapper); string payload = JsonConvert.SerializeObject(requestObj); string responseString = await PostMethodAsync(url, payload, CONTENTTYPE_JSON); if (String.IsNullOrEmpty(responseString)) { return(null); } var response = JsonConvert.DeserializeObject <OutboundSMSResponseWrapper>(responseString); return(response); }