public int doSMSJob() { string formattedPhone = ""; var host = new ApiHost(new BasicAuth(ClientCredentials.GetClientId(), ClientCredentials.GetSecret())); if (MessageLogger.EnableLogging == true) { MessageLogger.LogStatus(MessageLogger.LogPath, "Starting New Batch............................................................."); MessageLogger.LogStatus(MessageLogger.LogPath, "Establishing Server Connection"); } var messageApi = new MessagingApi(host); foreach (var phone in Phone.PhoneNumbers) { if (MessageLogger.EnableLogging == true) { MessageLogger.LogStatus(MessageLogger.LogPath, "Sending to........ " + phone); } //Remove 0 and add +233 to the beginning of the phone numbers if (Phone.FormatLocal == true) { formattedPhone = Phone.Validate(phone); } //checking the Ghanaian standard mobile length if (phone.Length < 10) { // log unsent message to outBox MessageLogger.logOutbox(formattedPhone, "Rejected due to length < 10"); continue; } else { try { MessageResponse msge = messageApi.SendQuickMessage(MessageId, formattedPhone, MessageBody, RegisteredDelivery); MessageLogger.logSent(formattedPhone, "Successful"); } catch (Exception e) { MessageLogger.logOutbox(formattedPhone, "rejected due to server error !"); //pass control to the next iteration of the enclosing foreach statement continue; } } } //returns number of sent >1 when successful return(MessageLogger.sent.Count); }
public static string Validate(string phone) { int requiredPhoneLength = phone.Length - 1; // formatting with Ghana Country code string newPhone = "+233" + phone.Substring(1, requiredPhoneLength); if (MessageLogger.EnableLogging == true) { MessageLogger.LogStatus(MessageLogger.LogPath, "Formatted " + phone + " to " + newPhone); } return(newPhone); }