using Newtonsoft.Json; using System; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; namespace Neo.Afx.WinService.Messages { /// /// A wrapper class for sending SMS's using the Vodacom API. /// public class SmsVodacom : ISmsSender { private string UserReference { get; set; } private string UserName { get; set; } private string Password { get; set; } private string AuthorizationHeader { get; set; } private class MessageObject { [JsonProperty("to")] public string To { get; set; } [JsonProperty("message")] public string Message { get; set; } [JsonProperty("ems")] public string Ems { get; set; } [JsonProperty("userref")] public string Userref { get; set; } } private class MessageResponse { public HttpStatusCode HttpStatusCode { get; set; } public string Message { get; set; } } public int Credits { get { return 100000; } } #region Initialization /// /// Initializes a new instance of the class /// by using the given values. /// /// The API ID to be queried. /// The user name to be queried. /// The password to be queried. public SmsVodacom(string userReference, string userName, string password) { try { this.UserReference = userReference; this.UserName = userName; this.Password = password; this.AuthorizationHeader = this.EncodeAuthorizationHeader(); } catch (Exception ex) { throw new Exception(GetInnermostMessage(ex)); } } #endregion #region Send /// /// Sends the given message to the given cell number. /// /// The message to be sent. /// The cell number to which the message is sent. /// When this method returns, contains the error message if /// the SMS was not sent successfully; otherwise contains a null references. /// /// true if successful; false otherwise. /// public bool Send(string message, string cellNumber, out string error) { bool successful; try { cellNumber = cellNumber.Substring(0, 1) != "+" ? string.Concat("+", cellNumber) : cellNumber; var response = SendMsg(new MessageObject() { Message = message, To = cellNumber, Ems = "1", Userref = this.UserReference }); error = response.Message; if (response.HttpStatusCode != HttpStatusCode.OK) successful = false; else successful = true; } catch (Exception ex) { error = GetInnermostMessage(ex); successful = false; } return successful; } #endregion #region GetInnermostMessage public string GetInnermostMessage(Exception ex) { if (ex.InnerException == null) { return ex.Message; } else { return GetInnermostMessage(ex.InnerException); } } #endregion #region Helper Methods private string EncodeAuthorizationHeader() { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(string.Concat(this.UserName, ":", this.Password)); return System.Convert.ToBase64String(plainTextBytes); } private MessageResponse SendMsg(MessageObject messageObject) { try { using (var client = new HttpClient()) { client.Timeout = TimeSpan.FromMinutes(10); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", this.AuthorizationHeader); string request = JsonConvert.SerializeObject(messageObject); var content = new StringContent(request, Encoding.UTF8, "application/json"); var response = client.PostAsync("https://restapi.gsm.co.za/send/sms", content).Result; var contents = response.Content.ReadAsStringAsync().Result; return new MessageResponse() { HttpStatusCode = response.StatusCode, Message = contents }; } } catch (Exception ex) { return new MessageResponse() { HttpStatusCode = HttpStatusCode.InternalServerError, Message = "ERROR: " + GetInnermostMessage(ex) }; } } #endregion } }