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 (
|
||||
"os"
|
||||
@ -10,16 +10,18 @@ import (
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
botName string
|
||||
apiURL string
|
||||
websocketURL string
|
||||
accountEmail string
|
||||
accountPassword string
|
||||
accountUsername string
|
||||
accountFirstname string
|
||||
accountLastname string
|
||||
teamName string
|
||||
debuggingChannelName string
|
||||
APIURL string
|
||||
AccountEmail string
|
||||
AccountFirstname string
|
||||
AccountLastname string
|
||||
AccountPassword string
|
||||
AccountUsername string
|
||||
BotName string
|
||||
Buildarch string
|
||||
DebuggingChannelName string
|
||||
TeamName string
|
||||
Version string
|
||||
WebsocketURL string
|
||||
client *model.Client4
|
||||
webSocketClient *model.WebSocketClient
|
||||
botUser *model.User
|
||||
@ -35,12 +37,12 @@ func New(options ...func(s *Bot)) *Bot {
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Bot) Main() {
|
||||
println(b.botName)
|
||||
func (b *Bot) Main() int {
|
||||
println(b.BotName)
|
||||
|
||||
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
|
||||
b.MakeSureServerIsRunning()
|
||||
@ -62,11 +64,11 @@ func (b *Bot) Main() {
|
||||
|
||||
// Lets create a bot channel for logging debug messages into
|
||||
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!
|
||||
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 {
|
||||
println("We failed to connect to the web socket")
|
||||
PrintError(err)
|
||||
@ -85,6 +87,7 @@ func (b *Bot) Main() {
|
||||
|
||||
// You can block forever with
|
||||
select {}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (b *Bot) MakeSureServerIsRunning() {
|
||||
@ -98,7 +101,7 @@ func (b *Bot) MakeSureServerIsRunning() {
|
||||
}
|
||||
|
||||
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?")
|
||||
PrintError(resp.Error)
|
||||
os.Exit(1)
|
||||
@ -108,10 +111,10 @@ func (b *Bot) LoginAsTheBotUser() {
|
||||
}
|
||||
|
||||
func (b *Bot) UpdateTheBotUserIfNeeded() {
|
||||
if b.botUser.FirstName != b.accountFirstname || b.botUser.LastName != b.accountLastname || b.botUser.Username != b.accountUsername {
|
||||
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.LastName = b.AccountLastname
|
||||
b.botUser.Username = b.AccountUsername
|
||||
|
||||
if user, resp := b.client.UpdateUser(b.botUser); resp.Error != nil {
|
||||
println("We failed to update the Bot user account")
|
||||
@ -125,9 +128,9 @@ func (b *Bot) UpdateTheBotUserIfNeeded() {
|
||||
}
|
||||
|
||||
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("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)
|
||||
os.Exit(1)
|
||||
} else {
|
||||
@ -136,7 +139,7 @@ func (b *Bot) FindBotTeam() {
|
||||
}
|
||||
|
||||
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")
|
||||
PrintError(resp.Error)
|
||||
} else {
|
||||
@ -146,17 +149,17 @@ func (b *Bot) CreateBotDebuggingChannelIfNeeded() {
|
||||
|
||||
// Looks like we need to create the logging channel
|
||||
channel := &model.Channel{}
|
||||
channel.Name = b.debuggingChannelName
|
||||
channel.Name = b.DebuggingChannelName
|
||||
channel.DisplayName = "Debugging For Bot"
|
||||
channel.Purpose = "This is used as a test channel for logging bot debug messages"
|
||||
channel.Type = model.CHANNEL_OPEN
|
||||
channel.TeamId = b.botTeam.Id
|
||||
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)
|
||||
} else {
|
||||
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.SendMsgToDebuggingChannel("_"+b.botName+" has **stopped** running_", "")
|
||||
b.SendMsgToDebuggingChannel("_"+b.BotName+" has **stopped** running_", "")
|
||||
os.Exit(0)
|
||||
}
|
||||
}()
|
||||
|
@ -1,15 +1,16 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "git.eeqj.de/sneak/sco/bot"
|
||||
|
||||
// Documentation for the Go driver can be found
|
||||
// at https://godoc.org/github.com/mattermost/platform/model#Client
|
||||
var Version string
|
||||
var Buildarch string
|
||||
|
||||
func main() {
|
||||
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