diff options
Diffstat (limited to 'api/command.go')
-rw-r--r-- | api/command.go | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/api/command.go b/api/command.go index a8573cdcc..bebe6629c 100644 --- a/api/command.go +++ b/api/command.go @@ -4,6 +4,7 @@ package api import ( + "crypto/tls" "fmt" "io/ioutil" "net/http" @@ -52,6 +53,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 +110,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) @@ -172,7 +174,11 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) { method = "GET" } - client := &http.Client{} + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: *utils.Cfg.ServiceSettings.EnableInsecureOutgoingConnections}, + } + client := &http.Client{Transport: tr} + req, _ := http.NewRequest(method, cmd.URL, strings.NewReader(p.Encode())) req.Header.Set("Accept", "application/json") if cmd.Method == model.COMMAND_METHOD_POST { @@ -187,7 +193,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 +211,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 +425,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())) +} |