interim
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Jeffrey Paul 2020-09-08 16:13:15 -07:00
parent 597bd0140b
commit 08bb2191c9
3 changed files with 59 additions and 53 deletions

View File

@ -31,6 +31,9 @@ fmt:
docker-build:
docker build -t $(IMAGENAME) .
run: go-get
cd cmd/$(FN) && go run
build: ./$(FN)
go-get:

View File

@ -10,20 +10,21 @@ import (
)
type Bot struct {
botName string
apiURL string
websocketURL string
accountEmail string
accountPassword string
accountFirstname string
accountLastname string
teamName string
logChannelName string
client *model.Client4
webSocketClient *model.WebSocketClient
botUser *model.User
botTeam *model.Team
debuggingChannel *model.Channel
botName string
apiURL string
websocketURL string
accountEmail string
accountPassword string
accountUsername string
accountFirstname string
accountLastname string
teamName string
debuggingChannelName string
client *model.Client4
webSocketClient *model.WebSocketClient
botUser *model.User
botTeam *model.Team
debuggingChannel *model.Channel
}
func New(options ...func(s *Bot)) *Bot {
@ -35,7 +36,7 @@ func New(options ...func(s *Bot)) *Bot {
}
func (b *Bot) Main() {
println(b.BotName)
println(b.botName)
b.SetupGracefulShutdown()
@ -61,9 +62,10 @@ func (b *Bot) Main() {
// Lets create a bot channel for logging debug messages into
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!
var err *model.AppError
b.webSocketClient, err = model.NewWebSocketClient4(b.websocketURL, b.client.AuthToken)
if err != nil {
println("We failed to connect to the web socket")
@ -86,7 +88,7 @@ func (b *Bot) Main() {
}
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?")
PrintError(resp.Error)
os.Exit(1)
@ -96,88 +98,88 @@ func (b *Bot) MakeSureServerIsRunning() {
}
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?")
PrintError(resp.Error)
os.Exit(1)
} else {
botUser = user
b.botUser = user
}
}
func (b *Bot) UpdateTheBotUserIfNeeded() {
if botUser.FirstName != USER_FIRST || botUser.LastName != USER_LAST || botUser.Username != USER_NAME {
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 {
b.botUser.FirstName = b.accountFirstname
b.botUser.LastName = b.accountLastname
b.botUser.Username = b.accountUsername
if user, resp := client.UpdateUser(botUser); resp.Error != nil {
println("We failed to update the Sample Bot user")
if user, resp := b.client.UpdateUser(b.botUser); resp.Error != nil {
println("We failed to update the Bot user account")
PrintError(resp.Error)
os.Exit(1)
} else {
botUser = user
b.botUser = user
println("Looks like this might be the first run so we've updated the bots account settings")
}
}
}
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("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)
os.Exit(1)
} else {
botTeam = team
b.botTeam = team
}
}
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")
PrintError(resp.Error)
} else {
debuggingChannel = rchannel
b.debuggingChannel = rchannel
return
}
// Looks like we need to create the logging channel
channel := &model.Channel{}
channel.Name = CHANNEL_LOG_NAME
channel.DisplayName = "Debugging For Sample Bot"
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 = botTeam.Id
if rchannel, resp := client.CreateChannel(channel); resp.Error != nil {
println("We failed to create the channel " + CHANNEL_LOG_NAME)
channel.TeamId = b.botTeam.Id
if rchannel, resp := b.client.CreateChannel(channel); resp.Error != nil {
println("We failed to create the channel " + b.debuggingChannelName)
PrintError(resp.Error)
} else {
debuggingChannel = rchannel
println("Looks like this might be the first run so we've created the channel " + CHANNEL_LOG_NAME)
b.debuggingChannel = rchannel
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) {
post := &model.Post{}
post.ChannelId = debuggingChannel.Id
post.ChannelId = b.debuggingChannel.Id
post.Message = msg
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")
PrintError(resp.Error)
}
}
func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) {
HandleMsgFromDebuggingChannel(event)
b.HandleMsgFromDebuggingChannel(event)
}
func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
// 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
}
@ -192,36 +194,36 @@ func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
if post != nil {
// ignore my events
if post.UserId == botUser.Id {
if post.UserId == b.botUser.Id {
return
}
// if you see any word matching 'alive' then respond
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
}
// if you see any word matching 'up' then respond
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
}
// if you see any word matching 'running' then respond
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
}
// if you see any word matching 'hello' then respond
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
}
}
SendMsgToDebuggingChannel("I did not understand you!", post.Id)
b.SendMsgToDebuggingChannel("I did not understand you!", post.Id)
}
func PrintError(err *model.AppError) {
@ -236,11 +238,11 @@ func (b *Bot) SetupGracefulShutdown() {
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
if webSocketClient != nil {
webSocketClient.Close()
if b.webSocketClient != nil {
b.webSocketClient.Close()
}
SendMsgToDebuggingChannel("_"+SAMPLE_NAME+" has **stopped** running_", "")
b.SendMsgToDebuggingChannel("_"+b.botName+" has **stopped** running_", "")
os.Exit(0)
}
}()

View File

@ -8,7 +8,8 @@ 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
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()
}