This commit is contained in:
parent
08bb2191c9
commit
209391c963
59
bot/bot.go
59
bot/bot.go
@ -1,4 +1,4 @@
|
|||||||
package sco
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
@ -10,16 +10,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
botName string
|
APIURL string
|
||||||
apiURL string
|
AccountEmail string
|
||||||
websocketURL string
|
AccountFirstname string
|
||||||
accountEmail string
|
AccountLastname string
|
||||||
accountPassword string
|
AccountPassword string
|
||||||
accountUsername string
|
AccountUsername string
|
||||||
accountFirstname string
|
BotName string
|
||||||
accountLastname string
|
Buildarch string
|
||||||
teamName string
|
DebuggingChannelName string
|
||||||
debuggingChannelName string
|
TeamName string
|
||||||
|
Version string
|
||||||
|
WebsocketURL string
|
||||||
client *model.Client4
|
client *model.Client4
|
||||||
webSocketClient *model.WebSocketClient
|
webSocketClient *model.WebSocketClient
|
||||||
botUser *model.User
|
botUser *model.User
|
||||||
@ -35,12 +37,12 @@ func New(options ...func(s *Bot)) *Bot {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) Main() {
|
func (b *Bot) Main() int {
|
||||||
println(b.botName)
|
println(b.BotName)
|
||||||
|
|
||||||
b.SetupGracefulShutdown()
|
b.SetupGracefulShutdown()
|
||||||
|
|
||||||
b.client = model.NewAPIv4Client(b.apiURL)
|
b.client = model.NewAPIv4Client(b.APIURL)
|
||||||
|
|
||||||
// Lets test to see if the mattermost server is up and running
|
// Lets test to see if the mattermost server is up and running
|
||||||
b.MakeSureServerIsRunning()
|
b.MakeSureServerIsRunning()
|
||||||
@ -62,11 +64,11 @@ 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("_"+b.botName+" 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
|
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")
|
||||||
PrintError(err)
|
PrintError(err)
|
||||||
@ -85,6 +87,7 @@ func (b *Bot) Main() {
|
|||||||
|
|
||||||
// You can block forever with
|
// You can block forever with
|
||||||
select {}
|
select {}
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) MakeSureServerIsRunning() {
|
func (b *Bot) MakeSureServerIsRunning() {
|
||||||
@ -98,7 +101,7 @@ func (b *Bot) MakeSureServerIsRunning() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) LoginAsTheBotUser() {
|
func (b *Bot) LoginAsTheBotUser() {
|
||||||
if user, resp := b.client.Login(b.accountEmail, b.accountPassword); 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)
|
||||||
@ -108,10 +111,10 @@ func (b *Bot) LoginAsTheBotUser() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) UpdateTheBotUserIfNeeded() {
|
func (b *Bot) UpdateTheBotUserIfNeeded() {
|
||||||
if b.botUser.FirstName != b.accountFirstname || b.botUser.LastName != b.accountLastname || b.botUser.Username != b.accountUsername {
|
if b.botUser.FirstName != b.AccountFirstname || b.botUser.LastName != b.AccountLastname || b.botUser.Username != b.AccountUsername {
|
||||||
b.botUser.FirstName = b.accountFirstname
|
b.botUser.FirstName = b.AccountFirstname
|
||||||
b.botUser.LastName = b.accountLastname
|
b.botUser.LastName = b.AccountLastname
|
||||||
b.botUser.Username = b.accountUsername
|
b.botUser.Username = b.AccountUsername
|
||||||
|
|
||||||
if user, resp := b.client.UpdateUser(b.botUser); resp.Error != nil {
|
if user, resp := b.client.UpdateUser(b.botUser); resp.Error != nil {
|
||||||
println("We failed to update the Bot user account")
|
println("We failed to update the Bot user account")
|
||||||
@ -125,9 +128,9 @@ func (b *Bot) UpdateTheBotUserIfNeeded() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) FindBotTeam() {
|
func (b *Bot) FindBotTeam() {
|
||||||
if team, resp := b.client.GetTeamByName(b.teamName, ""); 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 '" + b.teamName + "'")
|
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 {
|
||||||
@ -136,7 +139,7 @@ func (b *Bot) FindBotTeam() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) CreateBotDebuggingChannelIfNeeded() {
|
func (b *Bot) CreateBotDebuggingChannelIfNeeded() {
|
||||||
if rchannel, resp := b.client.GetChannelByName(b.debuggingChannelName, b.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 {
|
||||||
@ -146,17 +149,17 @@ func (b *Bot) CreateBotDebuggingChannelIfNeeded() {
|
|||||||
|
|
||||||
// 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 = b.debuggingChannelName
|
channel.Name = b.DebuggingChannelName
|
||||||
channel.DisplayName = "Debugging For 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 = b.botTeam.Id
|
channel.TeamId = b.botTeam.Id
|
||||||
if rchannel, resp := b.client.CreateChannel(channel); resp.Error != nil {
|
if rchannel, resp := b.client.CreateChannel(channel); resp.Error != nil {
|
||||||
println("We failed to create the channel " + b.debuggingChannelName)
|
println("We failed to create the channel " + b.DebuggingChannelName)
|
||||||
PrintError(resp.Error)
|
PrintError(resp.Error)
|
||||||
} else {
|
} else {
|
||||||
b.debuggingChannel = rchannel
|
b.debuggingChannel = rchannel
|
||||||
println("Looks like this might be the first run so we've created the channel " + b.debuggingChannelName)
|
println("Looks like this might be the first run so we've created the channel " + b.DebuggingChannelName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,7 +245,7 @@ func (b *Bot) SetupGracefulShutdown() {
|
|||||||
b.webSocketClient.Close()
|
b.webSocketClient.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
b.SendMsgToDebuggingChannel("_"+b.botName+" has **stopped** running_", "")
|
b.SendMsgToDebuggingChannel("_"+b.BotName+" has **stopped** running_", "")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
|
||||||
// See License.txt for license information.
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
import "git.eeqj.de/sneak/sco/bot"
|
import "git.eeqj.de/sneak/sco/bot"
|
||||||
|
|
||||||
// Documentation for the Go driver can be found
|
var Version string
|
||||||
// at https://godoc.org/github.com/mattermost/platform/model#Client
|
var Buildarch string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mybot := bot.New(func(b *bot.Bot) {
|
mybot := bot.New(func(b *bot.Bot) {
|
||||||
b.botName = "LSV Serious Callers Only"
|
b.BotName = "LSV Serious Callers Only"
|
||||||
|
b.Version = Version
|
||||||
|
b.Buildarch = Buildarch
|
||||||
})
|
})
|
||||||
mybot.Main()
|
os.Exit(mybot.Main())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user