protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["Body"].ToLower().Trim() == "update" ||
                Request["Body"].ToLower().Trim() == "nearby")
            {
                DatabaseDataContext db = new DatabaseDataContext();
                User u = db.Users.Single(r => r.PhoneNumber == Request["From"].Replace("+1", "").Replace(" 1", "").Trim());

                try
                {
                    Push.SendTextMessageNotifications(u.FoursquareUserID, true);
                }
                catch (Exception ex)
                {
                    var account = new TwilioRest.Account(ConfigurationManager.AppSettings["TwilioAccountSid"], ConfigurationManager.AppSettings["TwilioAuthToken"]);
                    var url = string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]);

                    var values = new Hashtable();

                    values.Add("To", Request["From"]);
                    values.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                    values.Add("Body", "Oops! An error has occurred, please try again later.");

                    account.request(url, "POST", values);
                }
            }
            else
            {
                var account = new TwilioRest.Account(ConfigurationManager.AppSettings["TwilioAccountSid"], ConfigurationManager.AppSettings["TwilioAuthToken"]);
                var url = string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]);

                var values = new Hashtable();

                values.Add("To", Request["From"]);
                values.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                values.Add("Body", "Hello from 4sqtransit, real-time public transit information on Foursquare! Visit us on the web at http://4sqtransit.com.");

                account.request(url, "POST", values);
            }
        }
Exemple #2
0
        public static void SendTextMessageNotifications(string userid, Boolean forceSend)
        {
            DatabaseDataContext db = new DatabaseDataContext();
            User u = db.Users.Single(r => r.FoursquareUserID == userid &&
                                          r.IsEnabled == true);

            System.Net.WebClient client = new System.Net.WebClient();
            var jsonResult1 = client.DownloadString("https://api.foursquare.com/v2/users/self?oauth_token=" + u.FoursquareAccessToken);
            var result = Json.Decode(jsonResult1);

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

            if (result.response.user.checkins.items[0].id != u.LastCheckinID ||
                forceSend == true)
            {
                bool isTransitStop = false;
                foreach (var c in result.response.user.checkins.items[0].venue.categories)
                {
                    if (c.icon.StartsWith("https://foursquare.com/img/categories/travel/") == true)
                    {
                        isTransitStop = true;
                        break;
                    }
                }

                if (isTransitStop == true ||
                    forceSend == true)
                {
                    var stops = OneTransitAPI.GetStopsByLocation(u.AgencyID, Convert.ToDouble(result.response.user.checkins.items[0].venue.location.lat), Convert.ToDouble(result.response.user.checkins.items[0].venue.location.lng), Convert.ToDouble(ConfigurationManager.AppSettings["TransitStopRadius"]));

                    if (stops.Length > 0)
                    {
                        StringBuilder msg = new StringBuilder();
                        msg.AppendFormat("{0}\n\n", stops[0].Name.ToUpper());

                        var times = OneTransitAPI.GetStopTimes(u.AgencyID, stops[0].ID.ToString());

                        int count = 0;
                        foreach (var t in times)
                        {
                            if (count >= 15)
                                break;
                            count++;

                            string time = t.DepartureTime;
                            if (time.StartsWith("0") == true)
                            {
                                time = time.Remove(0, 1);
                            }

                            string line;
                            if (t.Type == "realtime")
                            {
                                line = string.Format("{0}: {1}\n", t.RouteShortName, time);
                            }
                            else
                            {
                                line = string.Format("{0}: {1} (schd)\n", t.RouteShortName, time);
                            }

                            if ((msg.ToString().Length + line.Length) > 160)
                            {
                                var values1 = new Hashtable();
                                values1.Add("To", (string)result.response.user.contact.phone);
                                values1.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                                values1.Add("Body", msg.ToString());
                                account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values1);

                                msg.Clear();
                            }

                            msg.Append(line);
                        }

                        if (times.Length == 0)
                        {
                            msg.AppendFormat("There are no departures in the next 2 hours.");
                        }

                        if (msg.Length > 0)
                        {
                            var values2 = new Hashtable();
                            values2.Add("To", result.response.user.contact.phone);
                            values2.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                            values2.Add("Body", msg.ToString());
                            account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values2);
                        }
                    }
                    else
                    {
                        if (forceSend == true)
                        {
                            var values2 = new Hashtable();
                            values2.Add("To", result.response.user.contact.phone);
                            values2.Add("From", ConfigurationManager.AppSettings["TwilioNumber"]);
                            values2.Add("Body", "There are no stops within " + ConfigurationManager.AppSettings["TransitStopRadius"] + " meters of your location.");
                            account.request(string.Format("/2010-04-01/Accounts/{0}/SMS/Messages", ConfigurationManager.AppSettings["TwilioAccountSid"]), "POST", values2);
                        }
                    }
                }

                u.PhoneNumber = result.response.user.contact.phone != null ? (string)result.response.user.contact.phone : "0";
                u.LastCheckinID = (string)result.response.user.checkins.items[0].id;
            }

            db.SubmitChanges();
        }