func create(w http.ResponseWriter, r *http.Request) { db := context.Get(r, "db").(*mgo.Session) todoList, err := createBlankTodoList("+17022181502", "Basic Todo", db) if err != nil { log.Fatal(err) } log.Printf("Created %v", todoList) }
func todoList(w http.ResponseWriter, r *http.Request) { db := context.Get(r, "db").(*mgo.Session) todoList, err := todoListFor("", db) x, err := xml.MarshalIndent(todoList, "", " ") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/xml") w.Write(x) }
// Twilio sends a POST to the specified endpoint // e.g. // ToCountry=US&ToState=NV&SmsMessageSid=SM1b9d6ec899fc86c6c08b73e1bfb7861c&NumMedia=0&ToCity=&FromZip=89150&SmsSid=SM1b9d6ec899fc86c6c08b73e1bfb7861c&FromState=NV&SmsStatus=received&FromCity=LAS+VEGAS&Body=Test&FromCountry=US&To=%2B17025000247&ToZip=&NumSegments=1&MessageSid=SM1b9d6ec899fc86c6c08b73e1bfb7861c&AccountSid=AC3bcd52a18af4c60d5d63d3408973f830&From=%2B17022181502&ApiVersion=2010-04-01 func incoming(w http.ResponseWriter, r *http.Request) { body := r.PostFormValue("Body") from := r.PostFormValue("From") if body == "" || from == "" { http.Error(w, "Missing required key", http.StatusInternalServerError) data, _ := ioutil.ReadAll(r.Body) log.Printf("Unable to read expected fields from %v", string(data)) return } db := context.Get(r, "db").(*mgo.Session) todo, err := todoListFor(from, db) if err != nil { log.Printf("Error getting todolist: %v", err) } if todo == nil { log.Printf("Didn't pull up a todo list, creating a new one...") todo, err = createBlankTodoList(from, "Basic Todo", db) if err != nil { log.Printf("Creating a new blank todo didn't work right: %v", err) } } log.Printf("Working with %v", todo) todo.TodoItems = append(todo.TodoItems, TodoItem{Title: body}) log.Printf("TODO: need to save %v", todo) // c.UpdateId(todo.Id, bson.M{"$set": bson.M{"name": "updated name"}}) message := SmsResponse{XMLName: xml.Name{Local: "Response"}, Message: "Thank you, I got it."} x, err := xml.MarshalIndent(message, "", " ") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/xml") w.Write(x) }