add more bot commands
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
5d4e43cca8
commit
1239c9e1f3
60
bot/bot.go
60
bot/bot.go
|
@ -1,14 +1,15 @@
|
|||
package bot
|
||||
|
||||
//import "github.com/kr/pretty"
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"os"
|
||||
"os/signal"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/kr/pretty"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
|
@ -24,6 +25,7 @@ type Bot struct {
|
|||
TeamName string
|
||||
Version string
|
||||
WebsocketURL string
|
||||
StartupUnixTime int64
|
||||
client *model.Client4
|
||||
webSocketClient *model.WebSocketClient
|
||||
botUser *model.User
|
||||
|
@ -42,6 +44,8 @@ func New(options ...func(s *Bot)) *Bot {
|
|||
func (b *Bot) Main() int {
|
||||
println(b.BotName)
|
||||
|
||||
b.StartupUnixTime = time.Now().Unix()
|
||||
|
||||
b.SetupGracefulShutdown()
|
||||
|
||||
b.client = model.NewAPIv4Client(b.APIURL)
|
||||
|
@ -152,8 +156,8 @@ func (b *Bot) CreateBotDebuggingChannelIfNeeded() {
|
|||
// Looks like we need to create the logging channel
|
||||
channel := &model.Channel{}
|
||||
channel.Name = b.DebuggingChannelName
|
||||
channel.DisplayName = "Debugging For Bot"
|
||||
channel.Purpose = "This is used as a test channel for logging bot debug messages"
|
||||
channel.DisplayName = "LSV Serious Callers Only"
|
||||
channel.Purpose = "Bot Channel"
|
||||
channel.Type = model.CHANNEL_OPEN
|
||||
channel.TeamId = b.botTeam.Id
|
||||
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.RootId = replyToId
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -190,14 +194,27 @@ func (b *Bot) SendMsgToDebuggingChannel(msg string, replyToId string) {
|
|||
}
|
||||
|
||||
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 {
|
||||
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")
|
||||
|
||||
post := model.PostFromJson(strings.NewReader(event.Data["post"].(string)))
|
||||
|
@ -205,9 +222,7 @@ func (b *Bot) HandleMsgFromAnyChannel(event *model.WebSocketEvent) {
|
|||
return
|
||||
}
|
||||
|
||||
pretty.Print(post)
|
||||
|
||||
channel := post.ChannelId
|
||||
//pretty.Print(post)
|
||||
|
||||
// ignore my events
|
||||
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 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
|
||||
}
|
||||
|
||||
//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
|
||||
}
|
||||
|
||||
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 matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched {
|
||||
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
|
||||
|
|
Loading…
Reference in New Issue