-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbothelper.go
207 lines (178 loc) · 5.48 KB
/
mbothelper.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package mbothelper
import (
"log"
"os"
"os/signal"
"fmt"
"github.com/mattermost/platform/model"
)
type BotConfig struct {
MattermostServer string
MattermostWSURL string
Listen string
BotName string
UserEmail string
UserName string
UserPassword string
UserLastname string
UserFirstname string
TeamName string
LogChannel string
MainChannel string
StatusChannel string
PluginsDirectory string
Plugins []string
PluginsConfig map[string]BotConfigPlugin
}
type BotConfigPlugin struct {
PluginName string
Handler string
Watcher string
MentionHandler string
HelpHandler string
PathPatterns []string
PluginConfig string
Channels map[string]*model.Channel
}
var config BotConfig
var client *model.Client4
var webSocketClient *model.WebSocketClient
var BotUser *model.User
var BotTeam *model.Team
var DebuggingChannel *model.Channel
var MainChannel *model.Channel
var StatusChannel *model.Channel
func InitMbotHelper(botConfig BotConfig, client4 *model.Client4) {
config = botConfig
client = client4
}
func MakeSureServerIsRunning() {
if props, resp := client.GetOldClientConfig(""); resp.Error != nil {
log.Println("There was a problem pinging the Mattermost server. Are you sure it's running?")
PrintError(resp.Error)
os.Exit(1)
} else {
log.Println("Server detected and is running version " + props["Version"])
}
}
func LoginAsTheBotUser() {
if user, resp := client.Login(config.UserEmail, config.UserPassword); resp.Error != nil {
log.Println("There was a problem logging into the Mattermost server. Are you sure ran the setup steps from the README.md?")
PrintError(resp.Error)
os.Exit(1)
} else {
BotUser = user
}
}
func UpdateTheBotUserIfNeeded() {
if BotUser.FirstName != config.UserFirstname || BotUser.LastName != config.UserLastname || BotUser.Username != config.UserName {
BotUser.FirstName = config.UserFirstname
BotUser.LastName = config.UserLastname
BotUser.Username = config.UserName
if user, resp := client.UpdateUser(BotUser); resp.Error != nil {
log.Println("We failed to update the Sample Bot user")
PrintError(resp.Error)
os.Exit(1)
} else {
BotUser = user
log.Println("Looks like this might be the first run so we've updated the bots account settings")
}
}
}
func FindBotTeam() {
if team, resp := client.GetTeamByName(config.TeamName, ""); resp.Error != nil {
log.Printf("We failed to get the initial load or we do not appear to be a member of the team '%v'", config.TeamName)
PrintError(resp.Error)
os.Exit(1)
} else {
BotTeam = team
}
}
func CreateBotDebuggingChannelIfNeeded() {
if rchannel, resp := client.GetChannelByName(config.LogChannel, BotTeam.Id, ""); resp.Error != nil {
log.Println("We failed to get the channels")
PrintError(resp.Error)
} else {
DebuggingChannel = rchannel
return
}
// Looks like we need to create the logging channel
channel := &model.Channel{}
channel.Name = config.LogChannel
channel.DisplayName = "Debugging Channel for bot"
channel.Purpose = "This is used for logging bot debug messages"
channel.Type = model.CHANNEL_OPEN
channel.TeamId = BotTeam.Id
if rchannel, resp := client.CreateChannel(channel); resp.Error != nil {
log.Println("We failed to create the channel " + config.LogChannel)
PrintError(resp.Error)
} else {
DebuggingChannel = rchannel
log.Println("Looks like this might be the first run so we've created the channel " + config.LogChannel)
}
}
func JoinChannel(channel string, teamId string, userId string) *model.Channel {
if rchannel, resp := client.GetChannelByName(channel, teamId, ""); resp.Error != nil {
log.Printf("Failed to get the channel by name: %s", channel)
PrintError(resp.Error)
} else {
if _, resp := client.AddChannelMember(rchannel.Id, userId); resp.Error != nil {
log.Printf("Failed to join ourselves to the channel: %s", channel)
PrintError(resp.Error)
}
return rchannel
}
return nil
}
func SendMsgToChannel(msg string, replyToId string, channelId string) {
post := &model.Post{}
post.ChannelId = channelId
post.Message = msg
post.RootId = replyToId
if _, resp := client.CreatePost(post); resp.Error != nil {
SendMsgToDebuggingChannel(fmt.Sprintf("Failed to send a message to the channel: %s", channelId), "")
}
}
func SendMsgToDebuggingChannel(msg string, replyToId string) {
post := &model.Post{}
post.ChannelId = DebuggingChannel.Id
post.Message = msg
post.RootId = replyToId
if _, resp := client.CreatePost(post); resp.Error != nil {
log.Println("We failed to send a message to the logging channel")
PrintError(resp.Error)
}
}
func ReplyToUser(msg string, userId string) {
// we need a direct channel to the other user
channel, response := client.CreateDirectChannel(userId, BotUser.Id)
if response != nil {
log.Printf("response was not nil: %v", response)
}
newPost := model.Post{
UserId: userId,
ChannelId: channel.Id,
Message: msg,
}
r, e := client.CreatePost(&newPost)
if e != nil {
log.Printf("Couldn't make post: %s", e)
}
log.Printf("Post created: %v", r)
}
func PrintError(err *model.AppError) {
log.Printf("\tError Details:\n\t\t%v\n\t\t%v\n\t\t%v", err.Message, err.Id, err.DetailedError)
}
func SetupGracefulShutdown() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
if webSocketClient != nil {
webSocketClient.Close()
}
SendMsgToDebuggingChannel("_"+config.BotName+" has **stopped** running_", "")
os.Exit(0)
}
}()
}