This repository has been archived by the owner on May 31, 2023. It is now read-only.
/
message_parser.go
80 lines (67 loc) · 2.34 KB
/
message_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package slack
import (
"fmt"
"regexp"
"strings"
"github.com/dpb587/slack-delegate-bot/cmd/delegatebot/message"
"github.com/nlopes/slack"
)
type MessageParser struct {
self *slack.UserDetails
reMention *regexp.Regexp
reChannelMention *regexp.Regexp
}
func NewMessageParser(self *slack.UserDetails) *MessageParser {
return &MessageParser{
self: self,
reMention: regexp.MustCompile(fmt.Sprintf(`<@%s>`, regexp.QuoteMeta(self.ID))),
reChannelMention: regexp.MustCompile(fmt.Sprintf(`<#([^|]+)\|([^>]+)>\s+<@%s>`, regexp.QuoteMeta(self.ID))),
}
}
func (p *MessageParser) ParseMessage(msg slack.Msg) (*message.Message, error) {
if msg.Type != "message" {
return nil, nil
} else if msg.SubType == "message_deleted" {
// no sense responding to deleted message notifications
return nil, nil
} else if msg.SubType == "group_topic" || strings.Contains(msg.Text, "set the channel topic: ") {
// no sense responding to a reference in the topic
// trivia: slack doesn't support topic threads, but still allows bots to
// respond which means you get mentioned, but the browser app doesn't
// render the thread in New Threads so you can't mark it as read unless you
// use the mobile app (which happens to show it as -1 replies).
return nil, nil
} else if msg.User == p.self.ID {
// avoid accidentally talking to ourselves into a recursive DoS
return nil, nil
}
incoming := &message.Message{
Origin: msg.Channel,
OriginType: message.ChannelOriginType,
InterruptTarget: msg.Channel,
Timestamp: convertSlackTimestamp(msg.Timestamp),
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(incoming.Text)
if len(matches) > 0 {
incoming.InterruptTarget = matches[1]
}
incoming.OriginType = message.DirectMessageOriginType
return incoming, nil
} else if !p.reMention.MatchString(incoming.Text) {
return nil, nil
}
matches := p.reChannelMention.FindStringSubmatch(incoming.Text)
if len(matches) > 0 {
incoming.InterruptTarget = matches[1]
}
return incoming, nil
}