From 12662d0c877b585c96d35b91fea4b6e99fcba749 Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Mon, 9 Jan 2017 15:25:02 +0100 Subject: Slack format for Slash command messages (#4999) * Slash commands accept Slack format Until this commit the slash commands only accepted 'text' properties. For better styling however, Slack formatting support was added. However, ephemeral messages are not supported, and only text will be displayed. * Allow emphemeral Slack messages --- api/post.go | 103 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 36 deletions(-) (limited to 'api/post.go') diff --git a/api/post.go b/api/post.go index 2052636bb..8d6fa0b02 100644 --- a/api/post.go +++ b/api/post.go @@ -175,10 +175,6 @@ func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post } func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIconUrl string, props model.StringInterface, postType string) (*model.Post, *model.AppError) { - // parse links into Markdown format - linkWithTextRegex := regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) - text = linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})") - post := &model.Post{UserId: c.Session.UserId, ChannelId: channelId, Message: text, Type: postType} post.AddProp("from_webhook", "true") @@ -199,38 +195,7 @@ func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIc if len(props) > 0 { for key, val := range props { if key == "attachments" { - if list, success := val.([]interface{}); success { - // parse attachment links into Markdown format - for i, aInt := range list { - attachment := aInt.(map[string]interface{}) - if aText, ok := attachment["text"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["text"] = aText - list[i] = attachment - } - if aText, ok := attachment["pretext"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["pretext"] = aText - list[i] = attachment - } - if fVal, ok := attachment["fields"]; ok { - if fields, ok := fVal.([]interface{}); ok { - // parse attachment field links into Markdown format - for j, fInt := range fields { - field := fInt.(map[string]interface{}) - if fValue, ok := field["value"].(string); ok { - fValue = linkWithTextRegex.ReplaceAllString(fValue, "[${2}](${1})") - field["value"] = fValue - fields[j] = field - } - } - attachment["fields"] = fields - list[i] = attachment - } - } - } - post.AddProp(key, list) - } + createSlackPost(post, val) } else if key != "override_icon_url" && key != "override_username" && key != "from_webhook" { post.AddProp(key, val) } @@ -244,6 +209,72 @@ func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIc return post, nil } +func CreateCommandPost(c *Context, post *model.Post, response *model.CommandResponse) { + post.Message = response.Text + post.UserId = c.Session.UserId + post.CreateAt = model.GetMillis() + + if response.Attachments != nil { + createSlackPost(post, response.Attachments) + } + + switch response.ResponseType { + case model.COMMAND_RESPONSE_TYPE_IN_CHANNEL: + if _, err := CreatePost(c, post, true); err != nil { + c.Err = model.NewLocAppError("command", "api.command.execute_command.save.app_error", nil, "") + } + case model.COMMAND_RESPONSE_TYPE_EPHEMERAL: + if response.Text == "" { + return + } + + post.ParentId = "" + SendEphemeralPost(c.TeamId, c.Session.UserId, post) + } +} + +// This method only parses and processes the attachments, +// all else should be set in the post which is passed +func createSlackPost(post *model.Post, attachments interface{}) { + post.Type = model.POST_SLACK_ATTACHMENT + + // parse links into Markdown format + linkWithTextRegex := regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) + post.Message = linkWithTextRegex.ReplaceAllString(post.Message, "[${2}](${1})") + + if list, success := attachments.([]interface{}); success { + for i, aInt := range list { + attachment := aInt.(map[string]interface{}) + if aText, ok := attachment["text"].(string); ok { + aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") + attachment["text"] = aText + list[i] = attachment + } + if aText, ok := attachment["pretext"].(string); ok { + aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") + attachment["pretext"] = aText + list[i] = attachment + } + if fVal, ok := attachment["fields"]; ok { + if fields, ok := fVal.([]interface{}); ok { + // parse attachment field links into Markdown format + for j, fInt := range fields { + field := fInt.(map[string]interface{}) + if fValue, ok := field["value"].(string); ok { + fValue = linkWithTextRegex.ReplaceAllString(fValue, "[${2}](${1})") + field["value"] = fValue + fields[j] = field + } + } + attachment["fields"] = fields + list[i] = attachment + } + } + } + post.AddProp("attachments", list) + } +} + func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) { tchan := Srv.Store.Team().Get(c.TeamId) cchan := Srv.Store.Channel().Get(post.ChannelId, true) -- cgit v1.2.3-1-g7c22