add more bot commands
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Jeffrey Paul 2020-09-08 19:15:17 -07:00
parent 5d4e43cca8
commit 1239c9e1f3
1 changed files with 44 additions and 16 deletions

View File

@ -1,14 +1,15 @@
package bot package bot
//import "github.com/kr/pretty"
import ( import (
"fmt"
"github.com/mattermost/mattermost-server/v5/model"
"os" "os"
"os/signal" "os/signal"
"regexp" "regexp"
"strings" "strings"
"syscall"
"github.com/kr/pretty" "time"
"github.com/mattermost/mattermost-server/v5/model"
) )
type Bot struct { type Bot struct {
@ -24,6 +25,7 @@ type Bot struct {
TeamName string TeamName string
Version string Version string
WebsocketURL string WebsocketURL string
StartupUnixTime int64
client *model.Client4 client *model.Client4
webSocketClient *model.WebSocketClient webSocketClient *model.WebSocketClient
botUser *model.User botUser *model.User
@ -42,6 +44,8 @@ func New(options ...func(s *Bot)) *Bot {
func (b *Bot) Main() int { func (b *Bot) Main() int {
println(b.BotName) println(b.BotName)
b.StartupUnixTime = time.Now().Unix()
b.SetupGracefulShutdown() b.SetupGracefulShutdown()
b.client = model.NewAPIv4Client(b.APIURL) b.client = model.NewAPIv4Client(b.APIURL)
@ -152,8 +156,8 @@ 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 = "LSV Serious Callers Only"
channel.Purpose = "This is used as a test channel for logging bot debug messages" channel.Purpose = "Bot Channel"
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 {
@ -171,7 +175,7 @@ func (b *Bot) SendMsgToChannel(msg string, replyToId string, channelId string) {
post.Message = msg post.Message = msg
post.RootId = replyToId post.RootId = replyToId
if _, resp := b.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 channel")
PrintError(resp.Error) PrintError(resp.Error)
} }
} }
@ -190,14 +194,27 @@ func (b *Bot) SendMsgToDebuggingChannel(msg string, replyToId string) {
} }
func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) { func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) {
b.HandleMsgFromDebuggingChannel(event)
b.HandleMsgFromAnyChannel(event)
}
func (b *Bot) HandleMsgFromAnyChannel(event *model.WebSocketEvent) { b.HandleMsgFromDebuggingChannel(event)
if event.Event != model.WEBSOCKET_EVENT_POSTED { if event.Event != model.WEBSOCKET_EVENT_POSTED {
return return
} }
post := model.PostFromJson(strings.NewReader(event.Data["post"].(string)))
if post == nil {
return
}
// check to see if we have been addressed
if matched, _ := regexp.MatchString(`^`+b.BotName+`\s+`, post.Message); matched {
b.SendMsgToDebuggingChannel("i have been addressed in channel "+post.ChannelId, "")
b.HandleMsgFromAnyChannel(event)
}
}
func (b *Bot) HandleMsgFromAnyChannel(event *model.WebSocketEvent) {
println("responding to channel msg") println("responding to channel msg")
post := model.PostFromJson(strings.NewReader(event.Data["post"].(string))) post := model.PostFromJson(strings.NewReader(event.Data["post"].(string)))
@ -205,9 +222,7 @@ func (b *Bot) HandleMsgFromAnyChannel(event *model.WebSocketEvent) {
return return
} }
pretty.Print(post) //pretty.Print(post)
channel := post.ChannelId
// ignore my events // ignore my events
if post.UserId == b.botUser.Id { if post.UserId == b.botUser.Id {
@ -216,11 +231,18 @@ func (b *Bot) HandleMsgFromAnyChannel(event *model.WebSocketEvent) {
// 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 {
b.SendMsgToChannel("Yes I'm running", "", channel) b.SendMsgToChannel("Yes I'm running", "", post.ChannelId)
return return
} }
//b.SendMsgToDebuggingChannel("I did not understand you!", post.Id) if matched, _ := regexp.MatchString(`(?:^|\W)uptime(?:$|\W)`, post.Message); matched {
uptime := time.Now().Unix() - b.StartupUnixTime
msg := fmt.Sprintf("running: uptime %d seconds", uptime)
b.SendMsgToChannel(msg, "", post.ChannelId)
return
}
b.SendMsgToChannel("I did not understand your command, sorry", post.Id, post.ChannelId)
} }
@ -245,6 +267,12 @@ func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
return return
} }
if matched, _ := regexp.MatchString(`(?:^|\W)shutdown(?:$|\W)`, post.Message); matched {
b.SendMsgToDebuggingChannel("i will now exit", post.Id)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
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 {
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id) b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)