-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.go
169 lines (139 loc) · 4.72 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
"os/signal"
"regexp"
"time"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/rs/zerolog"
)
func main() {
app := &application{
logger: zerolog.New(
zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC822,
},
).With().Timestamp().Logger(),
}
app.config = loadConfig()
app.logger.Info().Str("config", fmt.Sprint(app.config)).Msg("")
setupGracefulShutdown(app)
// Create a new mattermost client.
app.mattermostClient = model.NewAPIv4Client(app.config.mattermostServer.String())
// Login.
app.mattermostClient.SetToken(app.config.mattermostToken)
if user, resp, err := app.mattermostClient.GetUser("me", ""); err != nil {
app.logger.Fatal().Err(err).Msg("Could not log in")
} else {
app.logger.Debug().Interface("user", user).Interface("resp", resp).Msg("")
app.logger.Info().Msg("Logged in to mattermost")
app.mattermostUser = user
}
// Find and save the bot's team to app struct.
if team, resp, err := app.mattermostClient.GetTeamByName(app.config.mattermostTeamName, ""); err != nil {
app.logger.Fatal().Err(err).Msg("Could not find team. Is this bot a member ?")
} else {
app.logger.Debug().Interface("team", team).Interface("resp", resp).Msg("")
app.mattermostTeam = team
}
// Find and save the talking channel to app struct.
if channel, resp, err := app.mattermostClient.GetChannelByName(
app.config.mattermostChannel, app.mattermostTeam.Id, "",
); err != nil {
app.logger.Fatal().Err(err).Msg("Could not find channel. Is this bot added to that channel ?")
} else {
app.logger.Debug().Interface("channel", channel).Interface("resp", resp).Msg("")
app.mattermostChannel = channel
}
// Send a message (new post).
sendMsgToTalkingChannel(app, "Hi! I am a bot.", "")
// Listen to live events coming in via websocket.
listenToEvents(app)
}
func setupGracefulShutdown(app *application) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
if app.mattermostWebSocketClient != nil {
app.logger.Info().Msg("Closing websocket connection")
app.mattermostWebSocketClient.Close()
}
app.logger.Info().Msg("Shutting down")
os.Exit(0)
}
}()
}
func sendMsgToTalkingChannel(app *application, msg string, replyToId string) {
// Note that replyToId should be empty for a new post.
// All replies in a thread should reply to root.
post := &model.Post{}
post.ChannelId = app.mattermostChannel.Id
post.Message = msg
post.RootId = replyToId
if _, _, err := app.mattermostClient.CreatePost(post); err != nil {
app.logger.Error().Err(err).Str("RootID", replyToId).Msg("Failed to create post")
}
}
func listenToEvents(app *application) {
var err error
failCount := 0
for {
app.mattermostWebSocketClient, err = model.NewWebSocketClient4(
fmt.Sprintf("ws://%s", app.config.mattermostServer.Host+app.config.mattermostServer.Path),
app.mattermostClient.AuthToken,
)
if err != nil {
app.logger.Warn().Err(err).Msg("Mattermost websocket disconnected, retrying")
failCount += 1
// TODO: backoff based on failCount and sleep for a while.
continue
}
app.logger.Info().Msg("Mattermost websocket connected")
app.mattermostWebSocketClient.Listen()
for event := range app.mattermostWebSocketClient.EventChannel {
// Launch new goroutine for handling the actual event.
// If required, you can limit the number of events beng processed at a time.
go handleWebSocketEvent(app, event)
}
}
}
func handleWebSocketEvent(app *application, event *model.WebSocketEvent) {
// Ignore other channels.
if event.GetBroadcast().ChannelId != app.mattermostChannel.Id {
return
}
// Ignore other types of events.
if event.EventType() != model.WebsocketEventPosted {
return
}
// Since this event is a post, unmarshal it to (*model.Post)
post := &model.Post{}
err := json.Unmarshal([]byte(event.GetData()["post"].(string)), &post)
if err != nil {
app.logger.Error().Err(err).Msg("Could not cast event to *model.Post")
}
// Ignore messages sent by this bot itself.
if post.UserId == app.mattermostUser.Id {
return
}
// Handle however you want.
handlePost(app, post)
}
func handlePost(app *application, post *model.Post) {
app.logger.Debug().Str("message", post.Message).Msg("")
app.logger.Debug().Interface("post", post).Msg("")
if matched, _ := regexp.MatchString(`(?:^|\W)hello(?:$|\W)`, post.Message); matched {
// If post has a root ID then its part of thread, so reply there.
// If not, then post is independent, so reply to the post.
if post.RootId != "" {
sendMsgToTalkingChannel(app, "I replied in an existing thread.", post.RootId)
} else {
sendMsgToTalkingChannel(app, "I just replied to a new post, starting a chain.", post.Id)
}
return
}
}