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
This commit is contained in:
Zeger-Jan van de Weg
2017-01-09 15:25:02 +01:00
committed by Harrison Healey
parent 1020081130
commit 12662d0c87
3 changed files with 69 additions and 55 deletions

View File

@@ -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)