示例#1
0
func CommandHandler(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	hook := r.URL.Path == "/slack_hook"
	if err == nil {
		decoder := schema.NewDecoder()
		command := new(robots.SlashCommand)
		err := decoder.Decode(command, r.PostForm)
		if err != nil {
			log.Println("Couldn't parse post request:", err)
		}
		if hook {
			c := strings.Split(command.Text, " ")
			command.Command = c[1]
			command.Text = strings.Join(c[2:], " ")
		} else {
			command.Command = command.Command[1:]
		}
		robot := GetRobot(command.Command)
		w.WriteHeader(http.StatusOK)
		if robot != nil {
			if hook {
				JSONResp(w, robot.Run(command))
			} else {
				plainResp(w, robot.Run(command))
			}
		} else {
			r := "No robot for that command yet :("
			if hook {
				JSONResp(w, r)
			} else {
				plainResp(w, r)
			}
		}
	}
}