/// <summary> /// This method called when the page is loaded into the browser. This method requests input stream and parses it to get message counts. /// </summary> /// <param name="sender">object, which invoked this method</param> /// <param name="e">EventArgs, which specifies arguments specific to this method</param> protected void Page_Load(object sender, EventArgs e) { System.IO.Stream stream = Request.InputStream; this.receivedMessagesFilePath = ConfigurationManager.AppSettings["ReceivedMessagesFilePath"]; if (string.IsNullOrEmpty(this.receivedMessagesFilePath)) { this.receivedMessagesFilePath = "~\\Messages.txt"; } string count = ConfigurationManager.AppSettings["NumberOfMessagesToStore"]; if (!string.IsNullOrEmpty(count)) { this.numberOfMessagesToStore = Convert.ToInt32(count); } if (null != stream) { byte[] bytes = new byte[stream.Length]; stream.Position = 0; stream.Read(bytes, 0, (int)stream.Length); string responseData = Encoding.ASCII.GetString(bytes); JavaScriptSerializer serializeObject = new JavaScriptSerializer(); InboundSMSMessage message = (InboundSMSMessage)serializeObject.Deserialize(responseData, typeof(InboundSMSMessage)); if (null != message) { this.SaveMessage(message); } } }
/// <summary> /// This method reads the incoming message and decides on to which message count needs to be updated. /// This method invokes another method to write the count to file /// </summary> /// <param name="message">InboundSMSMessage, message received from Request</param> private void SaveMessageCount(InboundSMSMessage message) { if (!string.IsNullOrEmpty(message.Message)) { string messageText = message.Message.Trim().ToLower(); string filePathConfigKey = string.Empty; switch (messageText) { case "basketball": filePathConfigKey = "BasketBallFilePath"; break; case "football": filePathConfigKey = "FootBallFilePath"; break; case "baseball": filePathConfigKey = "BaseBallFilePath"; break; } if (!string.IsNullOrEmpty(filePathConfigKey)) { this.WriteToFile(filePathConfigKey); } } }
public static void Execute() { // Configure in the 'app.config' which Logger levels are enabled(all levels are enabled in the example) // Check http://logging.apache.org/log4net/release/manual/configuration.html for more informations about the log4net configuration XmlConfigurator.Configure(new FileInfo("OneApiExamples.exe.config")); // Initialize Configuration object Configuration configuration = new Configuration(System.Configuration.ConfigurationManager.AppSettings.Get("Username"), System.Configuration.ConfigurationManager.AppSettings.Get("Password")); // Initialize SMSClient using the Configuration object SMSClient smsClient = new SMSClient(configuration); String response = null; while (response == null || !"1".Equals(response)) { // Send USSD and wait for the answer InboundSMSMessage inboundMessage = smsClient.UssdClient.SendMessage(destination, message); response = inboundMessage.Message; } // Send message and stop USSD session smsClient.UssdClient.StopSession(destination, "Cool"); }
/// <summary> /// This method called when the page is loaded into the web browser. This method requests input stream and parses it to get message counts. /// </summary> /// <param name="sender">object, which invoked this method</param> /// <param name="e">EventArgs, which specifies arguments specific to this method</param> protected void Page_Load(object sender, EventArgs e) { System.IO.Stream stream = Request.InputStream; if (null != stream) { byte[] bytes = new byte[stream.Length]; stream.Position = 0; stream.Read(bytes, 0, (int)stream.Length); string responseData = Encoding.ASCII.GetString(bytes); JavaScriptSerializer serializeObject = new JavaScriptSerializer(); InboundSMSMessage message = (InboundSMSMessage)serializeObject.Deserialize(responseData, typeof(InboundSMSMessage)); if (null != message) { this.SaveMessageCount(message); this.SaveMessage(message); } } /* * string inputStreamContents; * int stringLength; * int strRead; * System.IO.Stream str = Request.InputStream; * stringLength = Convert.ToInt32(str.Length); * byte[] stringArray = new byte[stringLength]; * strRead = str.Read(stringArray, 0, stringLength); * inputStreamContents = System.Text.Encoding.UTF8.GetString(stringArray); * System.IO.File.WriteAllText(Request.MapPath("~\\UAT\\Csharp-RESTful\\sms\\app2\\ListenerLog.txt"), inputStreamContents); */ }
/// <summary> /// This method reads the incoming message and stores the received message details. /// </summary> /// <param name="message">InboundSMSMessage, message received from Request</param> private void SaveMessage(InboundSMSMessage message) { try { List <string> list = new List <string>(); FileStream file = new FileStream(Request.MapPath(this.receivedMessagesFilePath), FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(file); string line; while ((line = sr.ReadLine()) != null) { list.Add(line); } sr.Close(); file.Close(); if (list.Count > this.numberOfMessagesToStore) { int diff = list.Count - this.numberOfMessagesToStore; list.RemoveRange(0, diff); } if (list.Count == this.numberOfMessagesToStore) { list.RemoveAt(0); } string messageLineToStore = message.DateTime.ToString() + "_-_-" + message.MessageId.ToString() + "_-_-" + message.Message.ToString() + "_-_-" + message.SenderAddress.ToString() + "_-_-" + message.DestinationAddress.ToString(); list.Add(messageLineToStore); using (StreamWriter sw = File.CreateText(Request.MapPath(this.receivedMessagesFilePath))) { int tempCount = 0; while (tempCount < list.Count) { string lineToWrite = list[tempCount]; sw.WriteLine(lineToWrite); tempCount++; } sw.Close(); } } catch (Exception ex) { return; } }
/// <summary> /// This method reads the incoming message and stores the received message details. /// </summary> /// <param name="message">InboundSMSMessage, message received from Request</param> private void SaveMessage(InboundSMSMessage message) { try { List<string> list = new List<string>(); FileStream file = new FileStream(Request.MapPath(this.receivedMessagesFilePath), FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(file); string line; while ((line = sr.ReadLine()) != null) { list.Add(line); } sr.Close(); file.Close(); if (list.Count > this.numberOfMessagesToStore) { int diff = list.Count - this.numberOfMessagesToStore; list.RemoveRange(0, diff); } if (list.Count == this.numberOfMessagesToStore) { list.RemoveAt(0); } string messageLineToStore = message.DateTime.ToString() + "_-_-" + message.MessageId.ToString() + "_-_-" + message.Message.ToString() + "_-_-" + message.SenderAddress.ToString() + "_-_-" + message.DestinationAddress.ToString(); list.Add(messageLineToStore); using (StreamWriter sw = File.CreateText(Request.MapPath(this.receivedMessagesFilePath))) { int tempCount = 0; while (tempCount < list.Count) { string lineToWrite = list[tempCount]; sw.WriteLine(lineToWrite); tempCount++; } sw.Close(); } } catch (Exception ex) { return; } }
/// <summary> /// This method reads the incoming message and stores the received message details. /// </summary> /// <param name="message">InboundSMSMessage, message received from Request</param> private void SaveMessage(InboundSMSMessage message) { string filePath = ConfigurationManager.AppSettings["MessagesFilePath"]; string messageLineToStore = message.DateTime.ToString() + "_-_-" + message.MessageId.ToString() + "_-_-" + message.Message.ToString() + "_-_-" + message.SenderAddress.ToString() + "_-_-" + message.DestinationAddress.ToString(); using (StreamWriter streamWriter = File.AppendText(Request.MapPath(filePath))) { streamWriter.WriteLine(messageLineToStore); streamWriter.Close(); } }