-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
112 lines (94 loc) · 2.06 KB
/
message.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package bot
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/nlopes/slack"
)
type Message struct {
*slack.MessageEvent
bot *Bot
isBot checkFunc
isIM checkFunc
isMentioned checkFunc
}
type checkFunc func(*Message) bool
type MessageMock interface {
GetUser() *slack.User
GetChannel() *slack.Channel
OpenIMChannel() string
}
func (bot *Bot) newMessage(event *slack.MessageEvent) *Message {
return &Message{
event,
bot,
isBot,
isIM,
isMentioned,
}
}
func isBot(message *Message) bool {
return message.User == "USLACKBOT" || message.BotID != ""
}
func (message *Message) IsBot() bool {
return message.isBot(message)
}
func isIM(message *Message) bool {
return message.Channel[0:1] == "D"
}
func (message *Message) IsIM() bool {
return message.isIM(message)
}
func isMentioned(message *Message) bool {
return strings.Contains(message.Text, "<@"+message.bot.id+">")
}
func (message *Message) IsMentioned() bool {
return message.isMentioned(message)
}
func (message *Message) ToJson() string {
messageJson, err := json.Marshal(message)
if err != nil {
fmt.Println(err)
return "Invalid json"
}
return string(messageJson)
}
func (message *Message) GetUser() *slack.User {
if message.bot == nil {
return nil
}
if message.bot.slackApi == nil {
return nil
}
user, error := message.bot.slackApi.GetUserInfo(message.User)
if error != nil {
log.Print(error)
}
return user
}
func (message *Message) GetChannel() *slack.Channel {
if message.bot.slackApi == nil {
return nil
}
channel, error := message.bot.slackApi.GetChannelInfo(message.Channel)
if error != nil {
log.Print(error)
}
return channel
}
func (message *Message) Respond(messageString string) {
message.bot.SendMessage(message.Channel, messageString)
}
func (message *Message) OpenIMChannel(user string) string {
return message.bot.OpenIMChannel(user)
}
func (bot *Bot) MockMessage(event *slack.MessageEvent, isBot checkFunc, isIM checkFunc, isMentioned checkFunc) *Message {
return &Message{
event,
bot,
isBot,
isIM,
isMentioned,
}
}