using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Odbc; using System.Data.OleDb; using System.Net; using System.IO; /* * Wasp Africa Subscription System * 2011.03.11 */ namespace airtelserial { class Program { static void Main(string[] args) { try { string sender = args[0]; string shortcode = args[1]; if (shortcode.Length >= 5) { if (shortcode.Length > 5) { shortcode = shortcode.Substring(shortcode.Length - Math.Min(5, shortcode.Length)); } string message = ""; if (args.Length >= 3) { message = args[2].Trim().ToLowerInvariant(); } else { message = ""; } // need to check the sent message for a delivery report or if empty string str = "id:"; string item = ""; if (message.StartsWith(str)) { //no reply } else { string nSender = sender.Substring(sender.Length - Math.Min(9, sender.Length)); sender = "+254" + nSender; shortcode = "+" + shortcode; //response to blank if (message.Length == 0) { //string RespForEmpty = "Wasp Africa SMS menu. For details about Dosika reply with letter D: To subscribe Reply with R: other services reply with X. Customer Care Lines 0727808265/6"; //sendmessage(shortcode, sender, RespForEmpty, shortcode); } else { string msgEnvelope = ""; for (int i = 2; i <= args.Length - 1; i++) { msgEnvelope += args[i].Trim().ToLowerInvariant() + " "; } //String number = sender.Replace("+", "").Trim(); //string msgTlk = message.Substring(0, 3); char[] delimiters = new char[] { ' ' }; string[] parts = msgEnvelope.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); String serialNum = "error"; String isSerial = ""; string nameC = ""; if (parts.Length > 0) { int i = parts.Length; int x = 0; while (x < i) { //verify each part serialNum = parts[x].ToString(); if (serialNum.All(char.IsDigit)) { isSerial = serialNum; } else { nameC = parts[0]; } x++; } if (isSerial != "") { string seriCheck = "select * from serials where BarCodes like '%" + isSerial + "%'"; string isOkSerial = executedb(seriCheck); if ((isOkSerial.Trim().Equals("error")) || (isOkSerial.Trim().Equals(""))) { sendmessage(shortcode, sender, "Sorry " + nameC + ", the submitted details are invalid. Kindly check the number on the barcode and try again.", "1"); } else { sendmessage(shortcode, sender, "Hi " + nameC + ".Thank you for participating in the Sta-soft Mother's Love competition. Keep purchasing Sta-soft for more chances of winning our amazing experiences", "1"); } } else { sendmessage(shortcode, sender, "Sorry, the submitted details are invalid. Kindly check the number on the barcode and try again.", "1"); } // } } } } } catch (Exception e) { Console.WriteLine(e.Message); //sendmessage("4345", "+254721307883", "Error in db req in subscription" + e.Message, "4345"); } Console.ReadLine(); } // database handler method static string executedb(string SQL) { try { OleDbConnection con = new OleDbConnection("Provider=sqloledb;Network Library=DBMSSOCN;Data Source=192.168.7.24,5070; Initial Catalog=Subscription;User ID=smpptalk;Password=wasp2005"); OleDbCommand dbcomm = new OleDbCommand(SQL, con); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(dbcomm); con.Open(); if (SQL.StartsWith("select")) { string value = ""; try { if (con.State.ToString() == "Open") { //ExecuteScalar is for select statements value += dbcomm.ExecuteScalar(); } } catch { value = "error"; } con.Close(); myDataAdapter.Dispose(); return value; } else if (SQL.StartsWith("insert") || SQL.StartsWith("update")) { string value = ""; try { //Executenonquery is for select and update statements dbcomm.ExecuteNonQuery(); value = "InsertSuccess"; } catch (Exception insexc) { Console.WriteLine(insexc.Message); value = "error"; } con.Close(); myDataAdapter.Dispose(); return value; } con.Close(); myDataAdapter.Dispose(); return "error"; } catch (Exception) { return ("error"); } } // method to send back a response message static void sendmessage(string shortcode, string recipient, string message, string servicetype) { string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); /*string saveResp = executedb("insert into ozekimessageout (sender,receiver,msg,status,flag,scheduledtime,keyword) values ('" + shortcode.Replace("+", "").Trim() + "','" + recipient.Trim() + "','" + message + "','send','1','" + now + "','0')"); //Console.WriteLine("{SMS:TEXT}{}{" + shortcode.Replace("+", "").Trim() + "}{" + recipient.Replace("+", "").Trim() + "}{" + message + "}"); if (saveResp.Equals("InsertSuccess")) {*/ //SerialsLog] ( Recno, TransactionDate, Sender, Message, ID) string saveRespV = executedb("insert into SerialsLog_Airtel (TransactionDate, Sender, Message, ID) values ('" + now + "','" + recipient.Trim() + "','" + message + "','" + shortcode.Replace("+", "").Trim() + "')"); /*} else { writeError(saveResp); }*/ Console.WriteLine("{SMS:TEXT}{}{" + shortcode.Trim() + "}{" + recipient.Trim() + "}{" + message + "}"); } static public int createFileX() { int f = 0; string timeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string midnightDate = timeNow.Substring(0, 10).Replace("-", ""); string _directoryName = @"C:\wasp"; // check folder exists int x = 1; if (Directory.Exists(_directoryName)) { x = 0; } // if not then create else { Directory.CreateDirectory(_directoryName); x = 0; } if (x == 0) { string path = @"C:\wasp\" + midnightDate + ".txt"; if (!File.Exists(path)) { File.Create(path).Close(); //Console.WriteLine("File Created"); f = 1; } else { f = 0; } } return f; } static void writeError(string errMsg) { string timeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string midnightDate = timeNow.Substring(0, 10).Replace("-", ""); string path = @"C:\wasp\" + midnightDate + ".txt"; if (File.Exists(path)) { TextWriter tw = new StreamWriter(path, true); tw.WriteLine(timeNow + "-" + errMsg); tw.Close(); } else { createFileX(); TextWriter tw = new StreamWriter(path, true); tw.WriteLine(timeNow + "-" + errMsg); tw.Close(); } } } }