diff options
author | Christopher Speller <crspeller@gmail.com> | 2016-02-08 07:31:33 -0500 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-02-08 07:31:33 -0500 |
commit | 9b35fb5fa59099fe644a1b1ef3734b09b50f98bf (patch) | |
tree | 2d8a1a3060addd6de1ebd5598feeb2d1267630c9 /api | |
parent | daa363574a3cb01fecdf8cdbfabd0a9739bf13c9 (diff) | |
parent | 2b32bad9937a429edc89067c54fb055de2b1316b (diff) | |
download | chat-9b35fb5fa59099fe644a1b1ef3734b09b50f98bf.tar.gz chat-9b35fb5fa59099fe644a1b1ef3734b09b50f98bf.tar.bz2 chat-9b35fb5fa59099fe644a1b1ef3734b09b50f98bf.zip |
Merge pull request #2092 from mattermost/fix-slash
Fixing slash commands
Diffstat (limited to 'api')
-rw-r--r-- | api/command.go | 65 |
1 files changed, 53 insertions, 12 deletions
diff --git a/api/command.go b/api/command.go index a8573cdcc..49d9e84f1 100644 --- a/api/command.go +++ b/api/command.go @@ -52,6 +52,8 @@ func InitCommand(r *mux.Router) { sr.Handle("/test", ApiAppHandler(testCommand)).Methods("POST") sr.Handle("/test", ApiAppHandler(testCommand)).Methods("GET") + sr.Handle("/test_e", ApiAppHandler(testEphemeralCommand)).Methods("POST") + sr.Handle("/test_e", ApiAppHandler(testEphemeralCommand)).Methods("GET") } func listCommands(c *Context, w http.ResponseWriter, r *http.Request) { @@ -107,9 +109,8 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) { provider := GetCommandProvider(trigger) if provider != nil { - response := provider.DoCommand(c, channelId, message) - handleResponse(c, w, response, channelId) + handleResponse(c, w, response, channelId, provider.GetCommand(c)) return } else { chanChan := Srv.Store.Channel().Get(channelId) @@ -187,7 +188,7 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) { if response == nil { c.Err = model.NewLocAppError("command", "api.command.execute_command.failed_empty.app_error", map[string]interface{}{"Trigger": trigger}, "") } else { - handleResponse(c, w, response, channelId) + handleResponse(c, w, response, channelId, cmd) } } else { body, _ := ioutil.ReadAll(resp.Body) @@ -205,21 +206,41 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewLocAppError("command", "api.command.execute_command.not_found.app_error", map[string]interface{}{"Trigger": trigger}, "") } -func handleResponse(c *Context, w http.ResponseWriter, response *model.CommandResponse, channelId string) { +func handleResponse(c *Context, w http.ResponseWriter, response *model.CommandResponse, channelId string, cmd *model.Command) { + + post := &model.Post{} + post.ChannelId = channelId + post.AddProp("from_webhook", "true") + + if utils.Cfg.ServiceSettings.EnablePostUsernameOverride { + if len(cmd.Username) != 0 { + post.AddProp("override_username", cmd.Username) + } else { + post.AddProp("override_username", model.DEFAULT_WEBHOOK_USERNAME) + } + } + + if utils.Cfg.ServiceSettings.EnablePostIconOverride { + if len(cmd.IconURL) != 0 { + post.AddProp("override_icon_url", cmd.IconURL) + } else { + post.AddProp("override_icon_url", model.DEFAULT_WEBHOOK_ICON) + } + } + if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL { - post := &model.Post{} - post.ChannelId = channelId post.Message = response.Text if _, err := CreatePost(c, post, true); err != nil { c.Err = model.NewLocAppError("command", "api.command.execute_command.save.app_error", nil, "") } } else if response.ResponseType == model.COMMAND_RESPONSE_TYPE_EPHEMERAL { - // post := &model.Post{} - // post.ChannelId = channelId - // post.Message = "TODO_EPHEMERAL: " + response.Text - // if _, err := CreatePost(c, post, true); err != nil { - // c.Err = model.NewLocAppError("command", "api.command.execute_command.save.app_error", nil, "") - // } + post.Message = response.Text + post.CreateAt = model.GetMillis() + SendEphemeralPost( + c.Session.TeamId, + c.Session.UserId, + post, + ) } w.Write([]byte(response.ToJson())) @@ -399,3 +420,23 @@ func testCommand(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(rc.ToJson())) } + +func testEphemeralCommand(c *Context, w http.ResponseWriter, r *http.Request) { + r.ParseForm() + + msg := "" + if r.Method == "POST" { + msg = msg + "\ntoken=" + r.FormValue("token") + msg = msg + "\nteam_domain=" + r.FormValue("team_domain") + } else { + body, _ := ioutil.ReadAll(r.Body) + msg = string(body) + } + + rc := &model.CommandResponse{ + Text: "test command response " + msg, + ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, + } + + w.Write([]byte(rc.ToJson())) +} |