using framework_library; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Web; namespace framework_business { /// /// Send Easy Class for SMS handling /// Author: Graham Rook /// Date: 10 October 2016 /// public class xSendEasy { /// /// Send an SMS as an individual message /// /// /// /// /// /// optional: can be a sender name or number /// optional: to indicate what date and time it should be sent format: yyMMddHHmmss000+ (1608302210000+) /// optional: mask to sepcify whetere certain features should be turned on or off /// optional: field that the client can supply to track the SMS to their database /// optional: second field that the client can supply to track the SMS to their database /// optional: third field that the client can supply to track the SMS to their database /// public static long SendSMS(string userId, string password, string mobileNumber, string body, string source = "", string deliverydate = "", string mask = "", string trackingCode1 = "", string trackingCode2 = "", string trackingCode3 = "") { long result = 0; send_easy.SMS_V2SoapClient sendAPI = new send_easy.SMS_V2SoapClient("sendeasy_service"); try { //send SMS result = sendAPI.SendSMS(userId, password, formatMobileNumber(mobileNumber), body, source, deliverydate, mask, "", "", ""); //if (easyResult >= 0)//OK //{ // result = true; //} //else //{ // //-10 invalid login // //-25 insuffucuent credit // //-40 Partially Sent // //Occurs when sendSMSMulti method was called and not all recipients could // //be sent the message. // //-50 Error queing SMS // //-60 SMS not found // //-70 Bulk SMS file could not be read // //-80 Bulk SMS file could not be saved // //TO DO handle error codes //} } catch (Exception ex) { exception.HandleException("xSendEasy:", MethodBase.GetCurrentMethod().Name, ex, userId); } finally { sendAPI.Close(); } return result; } /// /// Send an SMS as an individual message /// /// /// /// /// /// optional: can be a sender name or number /// optional: to indicate what date and time it should be sent format: yyMMddHHmmss000+ (1608302210000+) /// optional: mask to sepcify whetere certain features should be turned on or off /// optional: field that the client can supply to track the SMS to their database /// optional: second field that the client can supply to track the SMS to their database /// optional: third field that the client can supply to track the SMS to their database /// public static int SendSMSBatch(string userId, string password, string[] mobileNumbers, string body, string source = "", string deliverydate = "", string mask = "", string trackingCode1 = "", string trackingCode2 = "", string trackingCode3 = "") { int result = 0; send_easy.SMS_V2SoapClient sendAPI = new send_easy.SMS_V2SoapClient("sendeasy_service"); try { //send SMS result = sendAPI.SendSMSMulti(userId, password, formatMobileNumbers(mobileNumbers), body, source, deliverydate, mask, "", "", ""); //if (easyResult >= 0)//OK //{ // result = true; //} //else //{ // //-10 invalid login // //-25 insuffucuent credit // //-40 Partially Sent // //Occurs when sendSMSMulti method was called and not all recipients could // //be sent the message. // //-50 Error queing SMS // //-60 SMS not found // //-70 Bulk SMS file could not be read // //-80 Bulk SMS file could not be saved // //TO DO handle error codes //} } catch (Exception ex) { exception.HandleException("xSendEasy:", MethodBase.GetCurrentMethod().Name, ex, HttpContext.Current.Session["user"]); } finally { sendAPI.Close(); } return result; } /// /// validate SMS /// /// /// /// /// /// -10 invalid login /// -25 insuffucuent credit /// -40 Partially Sent /// Occurs when sendSMSMulti method was called and not all recipients could /// be sent the message. /// -50 Error queing SMS /// -60 SMS not found /// -70 Bulk SMS file could not be read /// -80 Bulk SMS file could not be saved /// public static string validateSMS(string userId, string password, long smsId) { string result = String.Empty; send_easy.SMS_V2SoapClient sendAPI = new send_easy.SMS_V2SoapClient("sendeasy_service"); try { result = sendAPI.QuerySMS(userId, password, smsId); } catch (Exception ex) { exception.HandleException("xSendEasy:", MethodBase.GetCurrentMethod().Name, ex, HttpContext.Current.Session["user"]); } finally { sendAPI.Close(); } return result; } /// /// Query balance /// /// /// /// /// Balance or Error code /// public static decimal queryCreditBalance(string userId, string password) { decimal result = 0M; send_easy.SMS_V2SoapClient sendAPI = new send_easy.SMS_V2SoapClient("sendeasy_service"); try { result = Convert.ToDecimal(sendAPI.QueryCreditBalance(userId, password)); } catch (Exception ex) { exception.HandleException("xSendEasy:", MethodBase.GetCurrentMethod().Name, ex, HttpContext.Current.Session["user"]); } finally { sendAPI.Close(); } return result; } /// /// returns the detail of the error /// /// /// public static string ReturnErrorDetail(int errorCode) { string result = String.Empty; switch (errorCode) { case -10: result = "Invalid Login."; break; case -25: result = "Insufficient credit."; break; case -40: result = "Partially Sent."; break; case -50: result = "Error queuing SMS."; break; case -60: result = "SMS not found."; break; case -70: result = "Bulk SMS file could not be read."; break; case -80: result = "Bulk SMS file could not be saved."; break; } return result; } #region private methods /// /// Format mobile number for Send Easy /// Example: 2784123456 /// /// /// private static string formatMobileNumber(string number) { string result = String.Empty; try { //replace spaces, +, hyphens and brackets result = number.Replace("+", "").Replace(" ", "").Replace("-", "").Replace("(", "").Replace(")", ""); //replace start 0 with 27 if (result.StartsWith("0")) { result = "27" + result.Substring(1); } } catch (Exception ex) { exception.HandleException("xSendEasy:", MethodBase.GetCurrentMethod().Name, ex, HttpContext.Current.Session["user"]); } return result; } /// /// Format mobile numbers for Send Easy /// Example: 2784123456 /// /// /// private static send_easy.ArrayOfString formatMobileNumbers(string[] numbers) { send_easy.ArrayOfString formattednumbers = new send_easy.ArrayOfString(); try { string formattedNumber = String.Empty; foreach (string number in numbers) { formattedNumber = number.Replace("+", "").Replace(" ", "").Replace("-", "").Replace("(", "").Replace(")", ""); //replace start 0 with 27 if (formattedNumber.StartsWith("0")) { formattedNumber = "27" + formattedNumber.Substring(1); } if (formattedNumber != String.Empty) formattednumbers.Add(formattedNumber); } } catch (Exception ex) { exception.HandleException("xSendEasy:", MethodBase.GetCurrentMethod().Name, ex, HttpContext.Current.Session["user"]); } return formattednumbers; } #endregion } }