From 1c5fa2e6c76a8e456e9a6154d72edd5a5df76b1f Mon Sep 17 00:00:00 2001 From: Josh Deck Date: Wed, 24 Apr 2024 13:56:08 -0400 Subject: [PATCH] Initial commit --- .gitignore | 9 ++++ Program.cs | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 Program.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ca2d45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +################################################################################ +# This .gitignore file was automatically created by Microsoft(R) Visual Studio. +################################################################################ + +/.vs +/obj +/TwilioDDT.sln +/TwilioDDT.csproj +/Credentials.cs diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..b3fca54 --- /dev/null +++ b/Program.cs @@ -0,0 +1,154 @@ +using System.Data.SqlClient; +using Twilio; +using Twilio.Types; +using Twilio.Rest.Api.V2010.Account; + +public class TwilioDDT +{ + Dictionary results = new Dictionary(); + + public static void Main(string[] args) + { + string PhoneNumber = ""; + string[] NumbersToText = ["248-891-3712"]; + + TextAgent agent = new TextAgent(PhoneNumber, NumbersToText); + var message = ""; + + var results = agent.SendTexts(message); + Console.WriteLine(results.ToString()); + } + + public class TextAgent + { + // Class Variables + PhoneNumber PhoneNumber; // Number to send from + string[] InputNumbers = Array.Empty(); // Numbers to blast to + + public TextAgent(string ShopNumber, string[] TargetList) + { + PhoneNumber = new PhoneNumber(ShopNumber); + InputNumbers = TargetList; + } + + + + public Dictionary SendTexts(string GenericMessage) + { + if (InputNumbers.Length < 1 || GenericMessage.Length < 1) + { + // Input data is invalid; do not initiate sequence + + // + return new Dictionary(); + } + + // Initialize SQL Agent + SqlDriver db = new SqlDriver("CONN_STRING"); + db.GatherUsers("SQL_QUERY_TO_GET_USERS"); + // TODO: read values from table into list + + var result = new Dictionary(); + foreach (string current in InputNumbers) // TODO: replace this with the rows in db + { + // TODO: Use values from SQL list as the fields here + + // Logic to add customer's name in the generic message + Console.WriteLine(GenericMessage); + var custName = "Josh Deck"; // Replace with lookup when data is provided + var properMessage = string.Format("Hi {},\nStop in at Global Oil Change Center during the month of May and get $10 Off your Full Synthetic Oil Change and a free set of wiper blades. Show this text to receive discount. From your friends at Global Oil in Troy. Please reply STOP to end texts.", custName); + var currentResult = SendMessage(properMessage, current); + result.Add(currentResult[0], currentResult[1]); + } + + return result; + } + + + + private string[] SendMessage(string Message, string Destination) + { + var result = new string[] { }; + var DestinationNumber = new PhoneNumber(Destination); + try + { + TwilioClient.Init(Credentials.TwilioSID, Credentials.TwilioAuthToken); + var message = MessageResource.Create( + to: DestinationNumber, + from: PhoneNumber, + body: Message + ); + + result = [Destination, Message]; + } + catch (Exception ex) + { + result = [Destination, ex.Message]; + } + return result; + } + } + + public class SqlDriver + { + private SqlConnection? conn = null; + private List>? data = null; + + public SqlDriver(string connectionString) + { + conn = new SqlConnection(connectionString); + } + public void GatherUsers(string query) + { + if (conn == null || data != null) + { + // Report error + return; + } + + data = new List> { }; + using (SqlCommand command = new SqlCommand(query, conn)) + { + try + { + conn.Open(); + SqlDataReader reader = command.ExecuteReader(); + + Dictionary column; + while (reader.Read()) + { + // Empty out the column + column = new Dictionary(); + + // Read the data from the table + // TODO: get actual fields needed + column["NAME"] = reader["NAME"].ToString(); + + data.Add(column); + } + conn.Close(); + } + catch + { + // Handle the error and/or cry + } + } + + } + public void ReportText() + { + if (conn == null) + { + // Report error + return; + } + + using (SqlCommand command = new SqlCommand("PROCEDURE_NAME", conn) { CommandType = System.Data.CommandType.StoredProcedure }) + { + // TODO: add parameters to SP + conn.Open(); + command.ExecuteNonQuery(); + } + } + } +} \ No newline at end of file