summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-09-28 12:08:16 -0400
committerCorey Hulen <corey@hulen.com>2017-09-28 09:08:16 -0700
commitf263d2b9510fb557fe075dee5097cb32e2b1e5e2 (patch)
tree5e7ca4a3b5e44a685b1e1f8761983dd57441a751
parent884cf494cbef36786cb035b14bbbccbb19c122c3 (diff)
downloadchat-f263d2b9510fb557fe075dee5097cb32e2b1e5e2.tar.gz
chat-f263d2b9510fb557fe075dee5097cb32e2b1e5e2.tar.bz2
chat-f263d2b9510fb557fe075dee5097cb32e2b1e5e2.zip
PLT-7684 Add support to outgoing webhooks and slash commands to set post type and props (#7531)
* Add support to outgoing webhooks and slash commands to set post type and props * Fix nil access
-rw-r--r--api/post_test.go10
-rw-r--r--api4/command.go2
-rw-r--r--api4/command_test.go8
-rw-r--r--app/command.go2
-rw-r--r--app/webhook.go6
-rw-r--r--model/command_response.go2
-rw-r--r--model/outgoing_webhook.go28
-rw-r--r--model/outgoing_webhook_test.go13
8 files changed, 68 insertions, 3 deletions
diff --git a/api/post_test.go b/api/post_test.go
index 1fb6b3ded..f34141f77 100644
--- a/api/post_test.go
+++ b/api/post_test.go
@@ -295,6 +295,16 @@ func testCreatePostWithOutgoingHook(
}
}
+ resp := &model.OutgoingWebhookResponse{}
+ resp.Text = new(string)
+ *resp.Text = "some test text"
+ resp.Username = "testusername"
+ resp.IconURL = "http://www.mattermost.org/wp-content/uploads/2016/04/icon.png"
+ resp.Props = map[string]interface{}{"someprop": "somevalue"}
+ resp.Type = "custom_test"
+
+ w.Write([]byte(resp.ToJson()))
+
success <- true
}))
defer ts.Close()
diff --git a/api4/command.go b/api4/command.go
index 64426193d..d051d57f6 100644
--- a/api4/command.go
+++ b/api4/command.go
@@ -305,6 +305,8 @@ func testCommand(c *Context, w http.ResponseWriter, r *http.Request) {
rc := &model.CommandResponse{
Text: "test command response " + msg,
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
+ Type: "custom_test",
+ Props: map[string]interface{}{"someprop": "somevalue"},
}
w.Write([]byte(rc.ToJson()))
diff --git a/api4/command_test.go b/api4/command_test.go
index 42c77e7b7..6ef0f4d27 100644
--- a/api4/command_test.go
+++ b/api4/command_test.go
@@ -423,6 +423,14 @@ func TestExecuteCommand(t *testing.T) {
cmdPosted := false
for _, post := range posts.Posts {
if strings.Contains(post.Message, "test command response") {
+ if post.Type != "custom_test" {
+ t.Fatal("wrong type set in slash command post")
+ }
+
+ if post.Props["someprop"] != "somevalue" {
+ t.Fatal("wrong prop set in slash command post")
+ }
+
cmdPosted = true
break
}
diff --git a/app/command.go b/app/command.go
index 0763e24c7..1eaf304ad 100644
--- a/app/command.go
+++ b/app/command.go
@@ -239,6 +239,8 @@ func (a *App) HandleCommandResponse(command *model.Command, args *model.CommandA
post.RootId = args.RootId
post.ParentId = args.ParentId
post.UserId = args.UserId
+ post.Type = response.Type
+ post.Props = response.Props
if !builtIn {
post.AddProp("from_webhook", "true")
diff --git a/app/webhook.go b/app/webhook.go
index 380839cdc..9531cba10 100644
--- a/app/webhook.go
+++ b/app/webhook.go
@@ -105,10 +105,10 @@ func (a *App) TriggerWebhook(payload *model.OutgoingWebhookPayload, hook *model.
l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.event_post.error"), err.Error())
} else {
defer CloseBody(resp)
- respProps := model.MapFromJson(resp.Body)
+ webhookResp := model.OutgoingWebhookResponseFromJson(resp.Body)
- if text, ok := respProps["text"]; ok {
- if _, err := a.CreateWebhookPost(hook.CreatorId, channel, text, respProps["username"], respProps["icon_url"], post.Props, post.Type); err != nil {
+ if webhookResp != nil && webhookResp.Text != nil {
+ if _, err := a.CreateWebhookPost(hook.CreatorId, channel, *webhookResp.Text, webhookResp.Username, webhookResp.IconURL, webhookResp.Props, webhookResp.Type); err != nil {
l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.create_post.error"), err)
}
}
diff --git a/model/command_response.go b/model/command_response.go
index d17bd00e3..f5c628b71 100644
--- a/model/command_response.go
+++ b/model/command_response.go
@@ -20,6 +20,8 @@ type CommandResponse struct {
Text string `json:"text"`
Username string `json:"username"`
IconURL string `json:"icon_url"`
+ Type string `json:"type"`
+ Props StringInterface `json:"props"`
GotoLocation string `json:"goto_location"`
Attachments []*SlackAttachment `json:"attachments"`
}
diff --git a/model/outgoing_webhook.go b/model/outgoing_webhook.go
index 59408c24e..14c6f2269 100644
--- a/model/outgoing_webhook.go
+++ b/model/outgoing_webhook.go
@@ -45,6 +45,14 @@ type OutgoingWebhookPayload struct {
FileIds string `json:"file_ids"`
}
+type OutgoingWebhookResponse struct {
+ Text *string `json:"text"`
+ Username string `json:"username"`
+ IconURL string `json:"icon_url"`
+ Props StringInterface `json:"props"`
+ Type string `json:"type"`
+}
+
func (o *OutgoingWebhookPayload) ToJSON() string {
b, err := json.Marshal(o)
if err != nil {
@@ -112,6 +120,26 @@ func OutgoingWebhookListFromJson(data io.Reader) []*OutgoingWebhook {
}
}
+func (o *OutgoingWebhookResponse) ToJson() string {
+ b, err := json.Marshal(o)
+ if err != nil {
+ return ""
+ } else {
+ return string(b)
+ }
+}
+
+func OutgoingWebhookResponseFromJson(data io.Reader) *OutgoingWebhookResponse {
+ decoder := json.NewDecoder(data)
+ var o OutgoingWebhookResponse
+ err := decoder.Decode(&o)
+ if err == nil {
+ return &o
+ } else {
+ return nil
+ }
+}
+
func (o *OutgoingWebhook) IsValid() *AppError {
if len(o.Id) != 26 {
diff --git a/model/outgoing_webhook_test.go b/model/outgoing_webhook_test.go
index 431b1f6c1..2412271b5 100644
--- a/model/outgoing_webhook_test.go
+++ b/model/outgoing_webhook_test.go
@@ -176,3 +176,16 @@ func TestOutgoingWebhookTriggerWordStartsWith(t *testing.T) {
t.Fatal("Should return false")
}
}
+
+func TestOutgoingWebhookResponseJson(t *testing.T) {
+ o := OutgoingWebhookResponse{}
+ o.Text = new(string)
+ *o.Text = "some text"
+
+ json := o.ToJson()
+ ro := OutgoingWebhookResponseFromJson(strings.NewReader(json))
+
+ if *o.Text != *ro.Text {
+ t.Fatal("Text does not match")
+ }
+}