summaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
authorZeger-Jan van de Weg <ZJvandeWeg@users.noreply.github.com>2017-01-09 15:25:02 +0100
committerHarrison Healey <harrisonmhealey@gmail.com>2017-01-09 09:25:02 -0500
commit12662d0c877b585c96d35b91fea4b6e99fcba749 (patch)
treec8ed79a11d3e8191434afc955f5c598538d0f985 /api
parent10200811308c5b857572ed2f66cb510b797a301d (diff)
downloadchat-12662d0c877b585c96d35b91fea4b6e99fcba749.tar.gz
chat-12662d0c877b585c96d35b91fea4b6e99fcba749.tar.bz2
chat-12662d0c877b585c96d35b91fea4b6e99fcba749.zip
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
Diffstat (limited to 'api')
-rw-r--r--api/command.go19
-rw-r--r--api/post.go103
2 files changed, 68 insertions, 54 deletions
diff --git a/api/command.go b/api/command.go
index 842d67843..9c8f60be5 100644
--- a/api/command.go
+++ b/api/command.go
@@ -134,7 +134,6 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) {
return
} else {
team = tr.Data.(*model.Team)
-
}
var user *model.User
@@ -247,23 +246,7 @@ func handleResponse(c *Context, w http.ResponseWriter, response *model.CommandRe
}
}
- if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL {
- post.Message = response.Text
- post.UserId = c.Session.UserId
- 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 && response.Text != "" {
- post.Message = response.Text
- post.CreateAt = model.GetMillis()
- post.UserId = c.Session.UserId
- post.ParentId = ""
- SendEphemeralPost(
- c.TeamId,
- c.Session.UserId,
- post,
- )
- }
+ CreateCommandPost(c, post, response)
w.Write([]byte(response.ToJson()))
}
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)