コード例 #1
0
ファイル: EmailMain.cs プロジェクト: JonasESmith/ZendeskLead
        /// <summary>
        /// Creates the bash file pulling information from an Azure Database, Email, and Zendesk to create a bash file
        ///   to upload a zendesk lead.
        /// </summary>
        /// <param name="messageId"></param>
        public void BuildBashFile(string messageId)
        {
            string path    = AppDomain.CurrentDomain.BaseDirectory + "MakeNewLead.sh";
            Lead   newLead = new Lead();

            Return_Customer_Attributes(ref newLead, messageId);

            string originalCurl = string.Format("curl -v POST https://api.futuresimple.com/v2/leads -H \"Accept: application/json\" -H \"Content-Type: application/json\" " +
                                                "-H \"Authorization: Bearer {0} \" ", AccessToken);
            string dataString   = "-d '{ \"data\": { ";
            string firstName    = string.Format("\"first_name\": \"{0}\" ", newLead.first_name ?? "NULL");
            string lastName     = string.Format("\"last_name\": \"{0}\"", newLead.last_name ?? "NULL");
            string organization = string.Format("\"organization_name\": \"{0}\"", newLead.organization_name ?? "NULL");
            string title        = string.Format("\"title\": \"{0}\"", newLead.title ?? "NULL");
            string description  = string.Format("\"description\": \"{0}\"", newLead.description ?? "NULL");
            string industry     = string.Format("\"industry\": \"{0}\"", newLead.industry ?? "NULL");
            string website      = string.Format("\"website\": \"{0}\"", newLead.website ?? "NULL");
            string email        = "";

            if (!string.IsNullOrEmpty(newLead.email))
            {
                email = string.Format(",\"email\": \"{0}\"", newLead.email ?? " ");
            }
            string phone       = string.Format(",\"phone\": \"{0}\"", newLead.phone ?? "NULL");
            string mobile      = string.Format("\"mobile\": \"{0}\"", newLead.mobile ?? "NULL");
            string fax         = string.Format("\"fax\": \"{0}\"", newLead.fax ?? "NULL");
            string twitter     = string.Format("\"twitter\": \"{0}\"", newLead.twitter ?? "NULL");
            string facebook    = string.Format("\"facebook\": \"{0}\"", newLead.facebook ?? "NULL");
            string linkedin    = string.Format("\"linkedin\": \"{0}\"", newLead.linkedin ?? "NULL");
            string addressStrt = ("\"address\": { ");
            string address     = string.Format("\"line1\": \"{0}\" , \"city\": \"{1}\", \"postal_code\": \"{2}\", \"state\": \"{3}\", \"country\": \"{4}\"",
                                               newLead.lineOne, newLead.city, newLead.postal_code ?? "",
                                               newLead.state ?? "no state given", newLead.country);
            string tags         = (" }, \"tags\": [  ] ");
            string customfields = ("\"custom_fields\": { }");
            string endingString = ("} } '");

            // This needs to retrieve which salesRep the customer is using and determines which owner id to use
            //
            string ownerID      = string.Format(" \"owner_id\": {0}", GlobalSales.owner_id);
            string formatString = string.Format("{0} {1} {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9} {10} {11}, {12}, {13}, {14}, {15}, {16} {17} {18}, {19} {20}",
                                                originalCurl, dataString, firstName, lastName, organization,
                                                title, ownerID, description, industry, website, email,
                                                phone, fax, twitter, facebook, linkedin,
                                                addressStrt, address, tags, customfields, endingString);

            // writes the constructed bash script to a file.
            File.WriteAllText(path, formatString);

            // this tries to create a lead inside of zendesk by running the script this
            // will run the script in a shell form but cannot output any text
            try
            {
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName        = path;
                psi.UseShellExecute = true;

                Process p = Process.Start(psi);
                p.WaitForExit();
            }
            catch (Exception e)
            { MessageBox.Show(e.ToString(), "Message with script", MessageBoxButtons.OK); throw; }
        }
コード例 #2
0
ファイル: EmailMain.cs プロジェクト: JonasESmith/ZendeskLead
        // *********************************************** //
        //  This section creates a bash file, to be execu- //
        //    -ted to allow forz zendesk to be updated     //
        //    with important customer information.         //
        //                                                 //
        // *********************************************** //
        #region ZENDESK ADD LEAD...
        void Return_Customer_Attributes(ref Lead lead, string messageID)
        {
            try
            {
                Google.Apis.Gmail.v1.Data.Message message = googleAPI.GetMessage(messageID);

                string connectString = "Server=tcp:server.database.windows.net,1433;Initial Catalog={databaseName};Persist Security Info=False;User ID={yourIdHere};" +
                                       "Password={yourPassword};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";

                string customerId = "";
                string body       = googleAPI.GetEmailBody(message);

                body = body.Replace("<p>", "");
                body = body.Replace("</p>", "");
                body = body.Replace("\r", "");
                body = body.Replace("\n", "");
                body = body.Replace("<br/>", ",");
                body = body.Replace("<br />", ",");

                string[] words = body.Split(',');

                for (int i = 0; i < words.Length; i++)
                {
                    if (words[i].Contains("Full name"))
                    {
                        string lastNames = "";

                        string   fullName = words[i].Replace("Full name: ", "");
                        string[] names    = fullName.Split(' ');

                        List <string> nameList = new List <string>();

                        for (int j = 0; j < names.Length; j++)
                        {
                            if (!string.IsNullOrEmpty(names[j]))
                            {
                                nameList.Add(names[j]);
                            }
                        }

                        if (nameList.Count <= 2)
                        {
                            lead.last_name = nameList[1];
                        }
                        else
                        {
                            for (int j = 1; j < nameList.Count; j++)
                            {
                                lastNames += nameList[j] + " ";
                            }
                            lead.last_name = lastNames;
                        }

                        lead.first_name = nameList[0];
                    }
                    else if (words[i].Contains("Position:"))
                    {
                        string position = words[i].Replace("Position: ", "");
                        lead.title = position;
                    }
                    else if (words[i].Contains("Store Name:"))
                    {
                        string storeName = words[i].Replace("Store Name: ", "");
                        lead.organization_name = storeName;
                    }
                    else if (words[i].Contains("CustomerID:"))
                    {
                        customerId = Regex.Replace(words[i].Replace("CustomerID:", ""), @"\s+", "");
                    }
                    else if (words[i].Contains("Email:"))
                    {
                        lead.email = Regex.Replace(words[i].Replace("Email:", ""), @"\s+", "");
                    }

                    lead.industry = "CBD";
                }

                customerId = null;

                if (string.IsNullOrEmpty(customerId))
                {
                    string idqueryString = string.Format("Select ID from Customer Where USERNAME = '******'", lead.email);


                    using (SqlConnection connection = new SqlConnection(connectString))
                    {
                        SqlCommand command = new SqlCommand(idqueryString, connection);
                        connection.Open();
                        SqlDataReader reader = command.ExecuteReader();

                        try
                        {
                            while (reader.Read())
                            {
                                if (!string.IsNullOrEmpty(reader["ID"].ToString()))
                                {
                                    customerId = reader["ID"].ToString();
                                }
                            }
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }

                string queryString = string.Format("Select * from Customer Where id = {0}", customerId);

                using (SqlConnection connection = new SqlConnection(connectString))
                {
                    SqlCommand command = new SqlCommand(queryString, connection);
                    // command.Parameters.AddWithValue();
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();

                    try
                    {
                        while (reader.Read())
                        {
                            if (!string.IsNullOrEmpty(reader["Email"].ToString()))
                            {
                                lead.email = reader["email"].ToString();
                            }
                        }
                    }
                    finally
                    {
                        reader.Close();
                    }
                }

                List <string> idList = new List <string>();
                queryString = string.Format("Select ID From GenericAttribute where EntityId = {0}", customerId);
                //queryString = string.Format("Select * From Address where id = {0}", shipingAddress);

                using (SqlConnection connection = new SqlConnection(connectString))
                {
                    SqlCommand command = new SqlCommand(queryString, connection);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();

                    try
                    {
                        while (reader.Read())
                        {
                            idList.Add(reader["ID"].ToString());
                        }
                    }
                    finally
                    {
                        reader.Close();
                    }
                }

                string customAttributeXML = "";

                for (int i = 0; i < idList.Count(); i++)
                {
                    queryString = string.Format("Select * From GenericAttribute where EntityId = {0} and ID = {1}", customerId, idList[i]);

                    using (SqlConnection connection = new SqlConnection(connectString))
                    {
                        SqlCommand command = new SqlCommand(queryString, connection);
                        connection.Open();
                        SqlDataReader reader = command.ExecuteReader();

                        try
                        {
                            while (reader.Read())
                            {
                                if (reader["Key"].ToString() == "Phone")
                                {
                                    lead.phone = reader["Value"].ToString();
                                }
                                else if (reader["Key"].ToString() == "Company")
                                {
                                    lead.organization_name = reader["Value"].ToString();
                                }
                                else if (reader["Key"].ToString() == "CustomCustomerAttributes")
                                {
                                    customAttributeXML = reader["Value"].ToString();
                                }
                                else if (reader["Key"].ToString() == "StreetAddress")
                                {
                                    lead.lineOne = reader["Value"].ToString();
                                }
                                else if (reader["Key"].ToString() == "City")
                                {
                                    lead.city = reader["Value"].ToString();
                                }
                                else if (reader["Key"].ToString() == "StateProvinceId")
                                {
                                    lead.state = reader["Value"].ToString();
                                }
                                else if (reader["Key"].ToString() == "CountryId")
                                {
                                    lead.country = reader["Value"].ToString();
                                }
                            }
                        }
                        finally
                        { reader.Close(); }
                    }
                }

                string   CustomAttributes = ProcessXML(customAttributeXML);
                string[] custom           = CustomAttributes.Split(',');
                custom[1]        = custom[1].Replace("'", "");
                lead.description = string.Format("TaxID - {0}", custom[1]);

                SalesRepCheck(custom[0]);

                queryString = string.Format("Select Name From Country where Id = {0}", lead.country);

                using (SqlConnection connection = new SqlConnection(connectString))
                {
                    SqlCommand command = new SqlCommand(queryString, connection);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();

                    try
                    {
                        while (reader.Read())
                        {
                            if (!string.IsNullOrEmpty(reader["Name"].ToString()))
                            {
                                lead.country = reader["Name"].ToString();
                            }
                        }
                    }
                    finally
                    {
                        reader.Close();
                    }
                }

                if (lead.state != "0")
                {
                    queryString = string.Format("Select Name From StateProvince where Id = {0}", lead.state);

                    using (SqlConnection connection = new SqlConnection(connectString))
                    {
                        SqlCommand command = new SqlCommand(queryString, connection);

                        connection.Open();
                        SqlDataReader reader = command.ExecuteReader();

                        try
                        {
                            while (reader.Read())
                            {
                                if (!string.IsNullOrEmpty(reader["Name"].ToString()))
                                {
                                    lead.state = reader["Name"].ToString();
                                }
                            }
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }
                else
                {
                    lead.state = null;
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.ToString());
            }
        }