public static void ExecuteReminders()
        {
            DatabaseDataContext db = new DatabaseDataContext();

            Account account = new Account(ConfigurationManager.AppSettings["TwilioAccountSid"], ConfigurationManager.AppSettings["TwilioAuthToken"]);

            foreach (Reminder r in db.Reminders.Where(z => z.DueTime <= DateTime.UtcNow.AddMinutes(15)))
            {
                Hashtable data = new Hashtable();
                data.Add("To", r.PhoneNumber);
                data.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                data.Add("Body", "Your parking meter will expire in " + Convert.ToInt32(r.DueTime.Subtract(DateTime.UtcNow).TotalMinutes) + " minutes.");

                account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", data);

                db.Reminders.DeleteOnSubmit(r);
            }

            db.SubmitChanges();
        }
Exemple #2
0
        public void SendMessage()
        {
            Account account;
            Hashtable h;

            if (Message.Length > 160)
            {
                throw new ApplicationException("Message cannot be longer than 160 characters. Current message is: " + Message.Length);
            }

            // Create Twilio REST account object using Twilio account ID and token
            account = new Account(ACCOUNT_SID, ACCOUNT_TOKEN);

            h = new Hashtable();
            h.Add("From", CALLER_ID);
            h.Add("To", To);
            h.Add("Body", Message);

            account.request(String.Format("/{0}/Accounts/{1}/SMS/Messages", API_VERSION, ACCOUNT_SID), "POST", h);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            DatabaseDataContext db = new DatabaseDataContext();

            Account account = new Account(ConfigurationManager.AppSettings["TwilioAccountSid"], ConfigurationManager.AppSettings["TwilioAuthToken"]);

            if (Request["Body"].Trim().ToLower() == "" ||
                Request["Body"].Trim().ToLower() == "help")
            {
                Hashtable values1 = new Hashtable();
                values1.Add("To", Request["From"]);
                values1.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                values1.Add("Body", "Hi I'm Meter Maid. Send me a text with how much time your parking meter has left and I will remind you before it expires. Example: \"2 hours\" or \"30 minutes\".");

                account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values1);
            }
            else if (Request["Body"].Trim().ToLower() == "time")
            {
                try
                {
                    Reminder r = db.Reminders.Where(z => z.PhoneNumber == Request["From"]).OrderByDescending(z => z.CreatedDate).First();

                    Hashtable values2 = new Hashtable();
                    values2.Add("To", r.PhoneNumber);
                    values2.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                    values2.Add("Body", "You have " + Convert.ToInt32(r.DueTime.Subtract(DateTime.UtcNow).TotalMinutes) + " minutes left on your parking meter.");

                    account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values2);
                }
                catch (Exception ex)
                {
                    Hashtable values3 = new Hashtable();
                    values3.Add("To", Request["From"]);
                    values3.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                    values3.Add("Body", "You do not have an active parking meter!");

                    account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values3);

                    return;
                }
            }
            else if (Request["Body"].Trim().ToLower() == "cancel")
            {
                try
                {
                    foreach (Reminder r in db.Reminders.Where(z => z.PhoneNumber == Request["From"]))
                    {
                        db.Reminders.DeleteOnSubmit(r);
                    }

                    db.SubmitChanges();

                    Hashtable values4 = new Hashtable();
                    values4.Add("To", Request["From"]);
                    values4.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                    values4.Add("Body", "OK, I have canceled the reminder for your parking meter.");

                    account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values4);
                }
                catch (Exception ex)
                {
                    Hashtable values5 = new Hashtable();
                    values5.Add("To", Request["From"]);
                    values5.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                    values5.Add("Body", "You do not have an active parking meter!");

                    account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values5);

                    return;
                }
            }
            else
            {
                try
                {
                    Reminder data = new Reminder();

                    data.ID = Guid.NewGuid();
                    data.PhoneNumber = Request["From"];
                    data.DueTime = ParseDueTime(Request["Body"]);
                    data.CreatedDate = DateTime.UtcNow;

                    db.Reminders.InsertOnSubmit(data);

                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Hashtable values6 = new Hashtable();
                    values6.Add("To", Request["From"]);
                    values6.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                    values6.Add("Body", "I couldn't understand that, please try again. Text \"help\" for more information.");

                    account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values6);

                    return;
                }

                Hashtable values7 = new Hashtable();
                values7.Add("To", Request["From"]);
                values7.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                values7.Add("Body", "OK, got it. I will text you 15 minutes before your meter expires.");

                account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values7);
            }
        }