Ejemplo n.º 1
0
        public void HitBasecamp()
        {
            const string token = "BAhbByIBuXsiZXhwaXJlc19hdCI6IjIwMTMtMTAtMDlUMjI6NTc6MzhaIiwidXNlcl9pZHMiOlsxNDA3NDYwOCwxODYwOTE1NV0sImNsaWVudF9pZCI6ImE5NzE4NTMwMmUwMzkyNmJjYzZkOGJjMmEwMDhiMWI0MjkzMjY4YzciLCJ2ZXJzaW9uIjoxLCJhcGlfZGVhZGJvbHQiOiJkN2RhNDI0N2YwZWY1ZGE3MjUxZTA3M2FmNWVjMWVjMiJ9dToJVGltZQ02ZRzAMipp5g==--e8da8fc12e002549da602cddad8a9c1f797e3724";
            const string task = "finish writing some code";

            var client = new BasecampClient();
            client.CreateTask(task, token);
        }
        // POST api/<controller>
        public HttpResponseMessage Post(FormDataCollection form)
        {
            var command = new InboundMail();
            command.Sender = form.Get("sender");
            command.Body = form.Get("body-plain");
            command.Stripped = form.Get("stripped-text");

            string token;

            using (var db = new UsersContext())
            {
                var user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == command.Sender.ToLower());

                if (user == null)
                    return Request.CreateResponse(HttpStatusCode.BadRequest);

                if (user.BasecampCredentials == null || string.IsNullOrWhiteSpace(user.BasecampCredentials.AccessToken))
                    return Request.CreateResponse(HttpStatusCode.BadRequest);

                token = user.BasecampCredentials.AccessToken;
            }

            var basecamp = new BasecampClient();

            var lookfor = "I will complete";
            var rx = new Regex(@"(\S.+?[.!?])(?=\s+|$)");
            foreach (Match match in rx.Matches(command.Body))
            {
                var index = match.Value.IndexOf(lookfor);

                if (index >= 0)
                {
                    var msg = match.Value.Replace(lookfor, "Complete");

                    var notice = "task created ok";
                    try
                    {
                        basecamp.CreateTask(msg, token);
                    }
                    catch (System.Exception e)
                    {
                        notice = e.ToString();
                    }

            //					SendSimpleMessage(command.Sender, notice);
                    break;
                }
            }

            return Request.CreateResponse(HttpStatusCode.Accepted);
        }