Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for message attachments #575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions bridge/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,16 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) {
msg = strings.TrimLeft(msg, "*")
msg = strings.TrimRight(msg, "*")
msg = "\x01ACTION " + msg + " \x01"
} else if data.Type == "slack_attachment" {
attachmentMsg := parseSlackAttachmentMsg(data.Attachments())
if attachmentMsg == "" {
break
}
if msg == "" {
msg = attachmentMsg
} else {
msg += attachmentMsg
}
} else if data.Type == "custom_matterpoll" {
pollMsg := parseMatterpollToMsg(data.Attachments())
if pollMsg == "" {
Expand Down Expand Up @@ -1503,6 +1513,50 @@ func parseMatterpollToMsg(attachments []*model.SlackAttachment) string {
return msg
}

func parseSlackAttachmentMsg(attachments []*model.SlackAttachment) string {
msg := ""
for _, attachment := range attachments {
prefix := "| "
// XXX: Figure out how to use mIRC codes here without it being
// stripped further down. With that, also support hex color codes.
if attachment.Color == "danger" {
prefix = "\033[31m| \033[0m"
} else if attachment.Color == "good" {
prefix = "\033[32m| \033[0m"
}

if attachment.Text == "" {
continue
}
if attachment.AuthorName != "" {
msg += prefix + attachment.AuthorName
if attachment.AuthorLink != "" {
msg += " (" + attachment.AuthorLink + ")"
}
msg += "\n"
}
if attachment.Title != "" {
msg += prefix + attachment.Title
if attachment.TitleLink != "" {
msg += attachment.TitleLink
}
msg += "\n"
}
lines := strings.Split(attachment.Text, "\n")
for _, text := range lines {
msg += prefix + text + "\n"
}
if attachment.ImageURL != "" {
msg += prefix + attachment.ImageURL + "\n"
}
for _, field := range attachment.Fields {
msg += prefix + field.Title + ": " + fmt.Sprintf("%s", field.Value) + "\n"
}
}

return msg
}

func (m *Mattermost) GetLastSentMsgs() []string {
data := make([]string, 0)

Expand Down
Loading