diff --git a/bot/bot.go b/bot/bot.go index 1232dab..886eafb 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -6,6 +6,8 @@ import ( "regexp" "strings" + "github.com/kr/pretty" + "github.com/mattermost/mattermost-server/v5/model" ) @@ -178,6 +180,55 @@ 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) { + if event.Event != model.WEBSOCKET_EVENT_POSTED { + return + } + println("responding to channel msg") + + post := model.PostFromJson(strings.NewReader(event.Data["post"].(string))) + if post == nil { + return + } + + pretty.Print(post) + + // FIXME find out what channel it came from so can respond appropriately + + // ignore my events + 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 { + 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 { + 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 { + 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 { + b.SendMsgToDebuggingChannel("Yes I'm running", post.Id) + return + } + + b.SendMsgToDebuggingChannel("I did not understand you!", post.Id) + } func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {