protected void Page_Load(object sender, EventArgs e)
        {
            DatabaseDataContext db = new DatabaseDataContext();

            User u = db.Users.Single(r => r.FoursquareUserID == Request.QueryString["id"]);
            db.Users.DeleteOnSubmit(u);

            db.SubmitChanges();
        }
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();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Foursquare.Initialize(ConfigurationManager.AppSettings["FoursquareConsumerKey"], ConfigurationManager.AppSettings["FoursquareConsumerSecret"]);

            if (Session["accessToken"] != null)
            {
                System.Net.WebClient client = new System.Net.WebClient();
                var jsonResult = client.DownloadString("https://api.foursquare.com/v2/users/self?oauth_token=" + Session["accessToken"]);
                var result = Json.Decode(jsonResult);

                DatabaseDataContext db = new DatabaseDataContext();

                var userid = (string)result.response.user.id;

                var count = (from r in db.Users where r.FoursquareUserID == userid select r).Count();
                if (count == 0)
                {
                    User u = new User();

                    u.FoursquareUserID = (string)result.response.user.id;
                    u.FoursquareAccessToken = (string)Session["accessToken"];
                    u.FirstName = result.response.user.firstName != null ? (string)result.response.user.firstName : "";
                    u.LastName = result.response.user.lastName != null ? (string)result.response.user.lastName : "";
                    u.PhoneNumber = result.response.user.contact.phone != null ? (string)result.response.user.contact.phone : "0";
                    u.AgencyID = (string)Session["TRANSIT_AGENCY"];
                    u.LastCheckinID = (string)result.response.user.checkins.items[0].id;
                    u.IsEnabled = true;
                    u.CreatedDate = DateTime.UtcNow;

                    db.Users.InsertOnSubmit(u);

                    this.ddWelcomeMessage.Text = string.Format("Welcome to 4sqtransit, {0}!</b><br /><br />\n", u.FirstName);
                }
                else
                {
                    User u = db.Users.Single(r => r.FoursquareUserID == userid);

                    u.FoursquareAccessToken = (string)Session["accessToken"];
                    u.PhoneNumber = result.response.user.contact.phone != null ? (string)result.response.user.contact.phone : "0";
                    u.AgencyID = (string)Session["TRANSIT_AGENCY"];
                    u.IsEnabled = true;

                    this.ddWelcomeMessage.Text = string.Format("<b>Hello, {0}. Welcome back!</b><br /><br />\n", u.FirstName);
                }

                this.ddVenueName.Text = result.response.user.checkins.items[0].venue.name;
                this.ddTransitAgency.Text = Session["TRANSIT_AGENCY_NAME"].ToString();

                if (result.response.user.contact.phone != null)
                {
                    this.ddPhoneMessage.Text = string.Format("Text messages will be sent to <b>{0}-{1}-{2}</b>. You can change this number at any time on Foursquare.\n", result.response.user.contact.phone.Substring(0, 3), result.response.user.contact.phone.Substring(3, 3), result.response.user.contact.phone.Substring(6, 4));
                }
                else
                {
                    this.ddPhoneMessage.Text = string.Format("You do not have a phone number associated with your Foursquare account. You will not receive text messages until you add a phone number on Foursquare.\n");
                }

                this.uxMapViewer.Text = string.Format("<img alt='bingMap' src='http://dev.virtualearth.net/REST/v1/Imagery/Map/road/{0},{1}/15?mapSize=492,250&pushpin={0},{1};34&mapVersion=v1&key={2}'></img>", result.response.user.checkins.items[0].venue.location.lat, result.response.user.checkins.items[0].venue.location.lng, ConfigurationManager.AppSettings["BingMapsKey"]);

                db.SubmitChanges();

                this.ddUnsubscribe.NavigateUrl = string.Format("/unsubscribe.aspx?id={0}", result.response.user.id);
            }
            else
            {
                var link = Foursquare.GetAuthorizationUrl(Page.ResolveUrl("~/authorize.aspx"));
                Response.Redirect(link);
            }
        }