Skip to content
This repository has been archived by the owner on May 31, 2023. It is now read-only.

Commit

Permalink
Add parsing of attachment fallback text
Browse files Browse the repository at this point in the history
  • Loading branch information
dpb587-pivotal committed Apr 20, 2020
1 parent 740c0c6 commit b553c69
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cmd/delegatebot/service/slack/message_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,29 @@ func (p *MessageParser) ParseMessage(msg slack.Msg) (*message.Message, error) {
Text: msg.Text,
}

// include attachments
for _, attachment := range msg.Attachments {
if attachment.Fallback == "" {
continue
}

incoming.Text = fmt.Sprintf("%s\n\n---\n\n%s", incoming.Text, attachment.Fallback)
}

if msg.Channel[0] == 'D' { // TODO better way to detect if this is our bot DM?
matches := reChannelMention.FindStringSubmatch(msg.Text)
matches := reChannelMention.FindStringSubmatch(incoming.Text)
if len(matches) > 0 {
incoming.InterruptTarget = matches[1]
}

incoming.OriginType = message.DirectMessageOriginType

return incoming, nil
} else if !p.reMention.MatchString(msg.Text) {
} else if !p.reMention.MatchString(incoming.Text) {
return nil, nil
}

matches := p.reChannelMention.FindStringSubmatch(msg.Text)
matches := p.reChannelMention.FindStringSubmatch(incoming.Text)
if len(matches) > 0 {
incoming.InterruptTarget = matches[1]
}
Expand Down
15 changes: 15 additions & 0 deletions cmd/delegatebot/service/slack/message_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ var _ = Describe("MessageParser", func() {
Expect(res).To(BeNil())
})

It("parses attachment fallback", func() {
msg.Attachments = []slack.Attachment{
{
Fallback: msg.Text,
},
}

msg.Text = "something else entirely"

res, err := subject.ParseMessage(msg)
Expect(err).NotTo(HaveOccurred())
Expect(res).ToNot(BeNil())
Expect(res.Text).To(Equal("something else entirely\n\n---\n\nhelp me, <@U1234567> you're my only hope."))
})

Context("direct messages", func() {
BeforeEach(func() {
msg.Channel = "D1234567"
Expand Down

0 comments on commit b553c69

Please # to comment.