This commit is contained in:
parent
597bd0140b
commit
08bb2191c9
3
Makefile
3
Makefile
@ -31,6 +31,9 @@ fmt:
|
|||||||
docker-build:
|
docker-build:
|
||||||
docker build -t $(IMAGENAME) .
|
docker build -t $(IMAGENAME) .
|
||||||
|
|
||||||
|
run: go-get
|
||||||
|
cd cmd/$(FN) && go run
|
||||||
|
|
||||||
build: ./$(FN)
|
build: ./$(FN)
|
||||||
|
|
||||||
go-get:
|
go-get:
|
||||||
|
104
bot/bot.go
104
bot/bot.go
@ -10,20 +10,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
botName string
|
botName string
|
||||||
apiURL string
|
apiURL string
|
||||||
websocketURL string
|
websocketURL string
|
||||||
accountEmail string
|
accountEmail string
|
||||||
accountPassword string
|
accountPassword string
|
||||||
accountFirstname string
|
accountUsername string
|
||||||
accountLastname string
|
accountFirstname string
|
||||||
teamName string
|
accountLastname string
|
||||||
logChannelName string
|
teamName string
|
||||||
client *model.Client4
|
debuggingChannelName string
|
||||||
webSocketClient *model.WebSocketClient
|
client *model.Client4
|
||||||
botUser *model.User
|
webSocketClient *model.WebSocketClient
|
||||||
botTeam *model.Team
|
botUser *model.User
|
||||||
debuggingChannel *model.Channel
|
botTeam *model.Team
|
||||||
|
debuggingChannel *model.Channel
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(options ...func(s *Bot)) *Bot {
|
func New(options ...func(s *Bot)) *Bot {
|
||||||
@ -35,7 +36,7 @@ func New(options ...func(s *Bot)) *Bot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) Main() {
|
func (b *Bot) Main() {
|
||||||
println(b.BotName)
|
println(b.botName)
|
||||||
|
|
||||||
b.SetupGracefulShutdown()
|
b.SetupGracefulShutdown()
|
||||||
|
|
||||||
@ -61,9 +62,10 @@ func (b *Bot) Main() {
|
|||||||
|
|
||||||
// Lets create a bot channel for logging debug messages into
|
// Lets create a bot channel for logging debug messages into
|
||||||
b.CreateBotDebuggingChannelIfNeeded()
|
b.CreateBotDebuggingChannelIfNeeded()
|
||||||
b.SendMsgToDebuggingChannel("_"+SAMPLE_NAME+" has **started** running_", "")
|
b.SendMsgToDebuggingChannel("_"+b.botName+" has **started** running_", "")
|
||||||
|
|
||||||
// Lets start listening to some channels via the websocket!
|
// Lets start listening to some channels via the websocket!
|
||||||
|
var err *model.AppError
|
||||||
b.webSocketClient, err = model.NewWebSocketClient4(b.websocketURL, b.client.AuthToken)
|
b.webSocketClient, err = model.NewWebSocketClient4(b.websocketURL, b.client.AuthToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("We failed to connect to the web socket")
|
println("We failed to connect to the web socket")
|
||||||
@ -86,7 +88,7 @@ func (b *Bot) Main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) MakeSureServerIsRunning() {
|
func (b *Bot) MakeSureServerIsRunning() {
|
||||||
if props, resp := client.GetOldClientConfig(""); resp.Error != nil {
|
if props, resp := b.client.GetOldClientConfig(""); resp.Error != nil {
|
||||||
println("There was a problem pinging the Mattermost server. Are you sure it's running?")
|
println("There was a problem pinging the Mattermost server. Are you sure it's running?")
|
||||||
PrintError(resp.Error)
|
PrintError(resp.Error)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -96,88 +98,88 @@ func (b *Bot) MakeSureServerIsRunning() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) LoginAsTheBotUser() {
|
func (b *Bot) LoginAsTheBotUser() {
|
||||||
if user, resp := client.Login(USER_EMAIL, USER_PASSWORD); resp.Error != nil {
|
if user, resp := b.client.Login(b.accountEmail, b.accountPassword); resp.Error != nil {
|
||||||
println("There was a problem logging into the Mattermost server. Are you sure ran the setup steps from the README.md?")
|
println("There was a problem logging into the Mattermost server. Are you sure ran the setup steps from the README.md?")
|
||||||
PrintError(resp.Error)
|
PrintError(resp.Error)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else {
|
} else {
|
||||||
botUser = user
|
b.botUser = user
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) UpdateTheBotUserIfNeeded() {
|
func (b *Bot) UpdateTheBotUserIfNeeded() {
|
||||||
if botUser.FirstName != USER_FIRST || botUser.LastName != USER_LAST || botUser.Username != USER_NAME {
|
if b.botUser.FirstName != b.accountFirstname || b.botUser.LastName != b.accountLastname || b.botUser.Username != b.accountUsername {
|
||||||
botUser.FirstName = USER_FIRST
|
b.botUser.FirstName = b.accountFirstname
|
||||||
botUser.LastName = USER_LAST
|
b.botUser.LastName = b.accountLastname
|
||||||
botUser.Username = USER_NAME
|
b.botUser.Username = b.accountUsername
|
||||||
|
|
||||||
if user, resp := client.UpdateUser(botUser); resp.Error != nil {
|
if user, resp := b.client.UpdateUser(b.botUser); resp.Error != nil {
|
||||||
println("We failed to update the Sample Bot user")
|
println("We failed to update the Bot user account")
|
||||||
PrintError(resp.Error)
|
PrintError(resp.Error)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else {
|
} else {
|
||||||
botUser = user
|
b.botUser = user
|
||||||
println("Looks like this might be the first run so we've updated the bots account settings")
|
println("Looks like this might be the first run so we've updated the bots account settings")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) FindBotTeam() {
|
func (b *Bot) FindBotTeam() {
|
||||||
if team, resp := client.GetTeamByName(TEAM_NAME, ""); resp.Error != nil {
|
if team, resp := b.client.GetTeamByName(b.teamName, ""); resp.Error != nil {
|
||||||
println("We failed to get the initial load")
|
println("We failed to get the initial load")
|
||||||
println("or we do not appear to be a member of the team '" + TEAM_NAME + "'")
|
println("or we do not appear to be a member of the team '" + b.teamName + "'")
|
||||||
PrintError(resp.Error)
|
PrintError(resp.Error)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else {
|
} else {
|
||||||
botTeam = team
|
b.botTeam = team
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) CreateBotDebuggingChannelIfNeeded() {
|
func (b *Bot) CreateBotDebuggingChannelIfNeeded() {
|
||||||
if rchannel, resp := client.GetChannelByName(CHANNEL_LOG_NAME, botTeam.Id, ""); resp.Error != nil {
|
if rchannel, resp := b.client.GetChannelByName(b.debuggingChannelName, b.botTeam.Id, ""); resp.Error != nil {
|
||||||
println("We failed to get the channels")
|
println("We failed to get the channels")
|
||||||
PrintError(resp.Error)
|
PrintError(resp.Error)
|
||||||
} else {
|
} else {
|
||||||
debuggingChannel = rchannel
|
b.debuggingChannel = rchannel
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks like we need to create the logging channel
|
// Looks like we need to create the logging channel
|
||||||
channel := &model.Channel{}
|
channel := &model.Channel{}
|
||||||
channel.Name = CHANNEL_LOG_NAME
|
channel.Name = b.debuggingChannelName
|
||||||
channel.DisplayName = "Debugging For Sample Bot"
|
channel.DisplayName = "Debugging For Bot"
|
||||||
channel.Purpose = "This is used as a test channel for logging bot debug messages"
|
channel.Purpose = "This is used as a test channel for logging bot debug messages"
|
||||||
channel.Type = model.CHANNEL_OPEN
|
channel.Type = model.CHANNEL_OPEN
|
||||||
channel.TeamId = botTeam.Id
|
channel.TeamId = b.botTeam.Id
|
||||||
if rchannel, resp := client.CreateChannel(channel); resp.Error != nil {
|
if rchannel, resp := b.client.CreateChannel(channel); resp.Error != nil {
|
||||||
println("We failed to create the channel " + CHANNEL_LOG_NAME)
|
println("We failed to create the channel " + b.debuggingChannelName)
|
||||||
PrintError(resp.Error)
|
PrintError(resp.Error)
|
||||||
} else {
|
} else {
|
||||||
debuggingChannel = rchannel
|
b.debuggingChannel = rchannel
|
||||||
println("Looks like this might be the first run so we've created the channel " + CHANNEL_LOG_NAME)
|
println("Looks like this might be the first run so we've created the channel " + b.debuggingChannelName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) SendMsgToDebuggingChannel(msg string, replyToId string) {
|
func (b *Bot) SendMsgToDebuggingChannel(msg string, replyToId string) {
|
||||||
post := &model.Post{}
|
post := &model.Post{}
|
||||||
post.ChannelId = debuggingChannel.Id
|
post.ChannelId = b.debuggingChannel.Id
|
||||||
post.Message = msg
|
post.Message = msg
|
||||||
|
|
||||||
post.RootId = replyToId
|
post.RootId = replyToId
|
||||||
|
|
||||||
if _, resp := client.CreatePost(post); resp.Error != nil {
|
if _, resp := b.client.CreatePost(post); resp.Error != nil {
|
||||||
println("We failed to send a message to the logging channel")
|
println("We failed to send a message to the logging channel")
|
||||||
PrintError(resp.Error)
|
PrintError(resp.Error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) {
|
func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) {
|
||||||
HandleMsgFromDebuggingChannel(event)
|
b.HandleMsgFromDebuggingChannel(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
|
func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
|
||||||
// If this isn't the debugging channel then lets ingore it
|
// If this isn't the debugging channel then lets ingore it
|
||||||
if event.Broadcast.ChannelId != debuggingChannel.Id {
|
if event.Broadcast.ChannelId != b.debuggingChannel.Id {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,36 +194,36 @@ func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
|
|||||||
if post != nil {
|
if post != nil {
|
||||||
|
|
||||||
// ignore my events
|
// ignore my events
|
||||||
if post.UserId == botUser.Id {
|
if post.UserId == b.botUser.Id {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// if you see any word matching 'alive' then respond
|
// if you see any word matching 'alive' then respond
|
||||||
if matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched {
|
if matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched {
|
||||||
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
|
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// if you see any word matching 'up' then respond
|
// if you see any word matching 'up' then respond
|
||||||
if matched, _ := regexp.MatchString(`(?:^|\W)up(?:$|\W)`, post.Message); matched {
|
if matched, _ := regexp.MatchString(`(?:^|\W)up(?:$|\W)`, post.Message); matched {
|
||||||
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
|
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// if you see any word matching 'running' then respond
|
// if you see any word matching 'running' then respond
|
||||||
if matched, _ := regexp.MatchString(`(?:^|\W)running(?:$|\W)`, post.Message); matched {
|
if matched, _ := regexp.MatchString(`(?:^|\W)running(?:$|\W)`, post.Message); matched {
|
||||||
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
|
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// if you see any word matching 'hello' then respond
|
// if you see any word matching 'hello' then respond
|
||||||
if matched, _ := regexp.MatchString(`(?:^|\W)hello(?:$|\W)`, post.Message); matched {
|
if matched, _ := regexp.MatchString(`(?:^|\W)hello(?:$|\W)`, post.Message); matched {
|
||||||
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
|
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SendMsgToDebuggingChannel("I did not understand you!", post.Id)
|
b.SendMsgToDebuggingChannel("I did not understand you!", post.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrintError(err *model.AppError) {
|
func PrintError(err *model.AppError) {
|
||||||
@ -236,11 +238,11 @@ func (b *Bot) SetupGracefulShutdown() {
|
|||||||
signal.Notify(c, os.Interrupt)
|
signal.Notify(c, os.Interrupt)
|
||||||
go func() {
|
go func() {
|
||||||
for _ = range c {
|
for _ = range c {
|
||||||
if webSocketClient != nil {
|
if b.webSocketClient != nil {
|
||||||
webSocketClient.Close()
|
b.webSocketClient.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
SendMsgToDebuggingChannel("_"+SAMPLE_NAME+" has **stopped** running_", "")
|
b.SendMsgToDebuggingChannel("_"+b.botName+" has **stopped** running_", "")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -8,7 +8,8 @@ import "git.eeqj.de/sneak/sco/bot"
|
|||||||
// Documentation for the Go driver can be found
|
// Documentation for the Go driver can be found
|
||||||
// at https://godoc.org/github.com/mattermost/platform/model#Client
|
// at https://godoc.org/github.com/mattermost/platform/model#Client
|
||||||
func main() {
|
func main() {
|
||||||
sco := bot.New(func(b *bot.Bot) {
|
mybot := bot.New(func(b *bot.Bot) {
|
||||||
|
b.botName = "LSV Serious Callers Only"
|
||||||
})
|
})
|
||||||
sco.Main()
|
mybot.Main()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user