I think things are pretty good

This commit is contained in:
Brian Buller 2023-12-01 10:57:15 -06:00
parent ff58d1ddf4
commit 0fc78d4b4c
8 changed files with 65 additions and 69 deletions

View File

@ -2,12 +2,8 @@
helperbot: helperbot:
go build -o build/helperbot *.go go build -o build/helperbot *.go
plugins:
./buildplugins.sh
package: package:
go build -o build/helperbot *.go go build -o build/helperbot *.go
./buildplugins.sh
cp helperbot.service build/ cp helperbot.service build/
cd build && tar -zcvf helperbot.tgz * cd build && tar -zcvf helperbot.tgz *

View File

@ -9,8 +9,7 @@ import (
) )
type App struct { type App struct {
debug bool debug bool
Running bool
m *models.BotModel m *models.BotModel
@ -28,7 +27,6 @@ func NewApp(debugMode bool) (*App, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
a.Running = true
go a.watchMessageChannel() go a.watchMessageChannel()
return a, err return a, err
} }
@ -54,6 +52,7 @@ func (a *App) initialize() error {
if a.m, err = models.NewBotModel(a.debug); err != nil { if a.m, err = models.NewBotModel(a.debug); err != nil {
return err return err
} }
a.m.Running = true
// Load up the plugins // Load up the plugins
a.plugins = append(a.plugins, &plugins.StatsState{}, &plugins.AoCState{}) a.plugins = append(a.plugins, &plugins.StatsState{}, &plugins.AoCState{})
@ -69,6 +68,9 @@ func (a *App) initialize() error {
return err return err
} }
func (a *App) Stop() { a.m.Running = false }
func (a *App) IsRunning() bool { return a.m.Running }
func (a *App) SendQuitMessage() { a.m.SendMessage(models.QuitMessage) } func (a *App) SendQuitMessage() { a.m.SendMessage(models.QuitMessage) }
func (a *App) setupMessageWatchers() { func (a *App) setupMessageWatchers() {
@ -76,7 +78,7 @@ func (a *App) setupMessageWatchers() {
if msg.Is(models.QuitMessage) { if msg.Is(models.QuitMessage) {
// App is shutting down // App is shutting down
fmt.Println("Received QuitMessage") fmt.Println("Received QuitMessage")
a.Running = false a.m.Running = false
} else if msg.IsError() { } else if msg.IsError() {
// Received an Error Message // Received an Error Message
fmt.Printf("ERROR: %s\n", msg) fmt.Printf("ERROR: %s\n", msg)
@ -89,14 +91,13 @@ func (a *App) setupMessageWatchers() {
}) })
for _, v := range a.plugins { for _, v := range a.plugins {
fmt.Println("Setting up watchers for", v.Name())
a.m.AddMessageWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessMessage) a.m.AddMessageWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessMessage)
a.m.AddRTMWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessRTMEvent) a.m.AddRTMWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessRTMEvent)
} }
} }
func (a *App) watchMessageChannel() { func (a *App) watchMessageChannel() {
for a.Running { for a.m.Running {
a.m.ProcessMessageChannel() a.m.ProcessMessageChannel()
} }
} }

View File

@ -1,14 +0,0 @@
#!/bin/bash
mkdir -p build/plugins
cd plugins_src
for i in `ls`; do
echo "Building plugin: $i"
cd "$i"
go build -buildmode=plugin "$i.go"
if [ $? -eq 0 ]; then
mv *.so ../../build/plugins
fi
cd ..
done
cd ..

View File

@ -7,4 +7,3 @@ rmE build/aoc-util
rmE build/helperbot rmE build/helperbot
rmE build/helperbot.service rmE build/helperbot.service
rmE build/helperbot.tgz rmE build/helperbot.tgz
rm build/plugins/*

View File

@ -36,9 +36,9 @@ var rootCmd = &cobra.Command{
// Save the changes when the app quits // Save the changes when the app quits
fmt.Println("\nFinishing up...") fmt.Println("\nFinishing up...")
a.SendQuitMessage() a.SendQuitMessage()
a.Running = false a.Stop()
}() }()
for a.Running { for a.IsRunning() {
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
} }
fmt.Println("Done") fmt.Println("Done")

View File

@ -24,6 +24,8 @@ type BotModel struct {
messageWatchers map[string]func(msg BotMessage) bool messageWatchers map[string]func(msg BotMessage) bool
rtmWatchers map[string]func(event *slack.RTMEvent) bool rtmWatchers map[string]func(event *slack.RTMEvent) bool
Running bool
} }
func NewBotModel(debug bool) (*BotModel, error) { func NewBotModel(debug bool) (*BotModel, error) {
@ -60,7 +62,9 @@ func (m *BotModel) DebugRTMWatcher(event *slack.RTMEvent) bool {
} }
func (m *BotModel) SendMessage(msg BotMessage) { func (m *BotModel) SendMessage(msg BotMessage) {
fmt.Println("Sending:", msg) if m.debug {
fmt.Println("Sending:", msg)
}
m.messages <- msg m.messages <- msg
} }
@ -72,11 +76,8 @@ func (m *BotModel) AddRTMWatcher(name string, watcher func(event *slack.RTMEvent
} }
func (m *BotModel) ProcessMessageChannel() { func (m *BotModel) ProcessMessageChannel() {
fmt.Println(">> ProcessMessageChannel")
msg := <-m.messages msg := <-m.messages
fmt.Println(">>>> Received Message", msg) for _, v := range m.messageWatchers {
for k, v := range m.messageWatchers {
fmt.Printf(">>>> Message Watcher [%s]\n", k)
// Pass the message to the watcher // Pass the message to the watcher
if v != nil && v(msg) { if v != nil && v(msg) {
// if a watcher returns true, the message was consumed // if a watcher returns true, the message was consumed
@ -85,11 +86,8 @@ func (m *BotModel) ProcessMessageChannel() {
} }
} }
func (m *BotModel) ProcessRTMChannel() { func (m *BotModel) ProcessRTMChannel() {
fmt.Println(">> ProcessRTMChannel")
msg := <-m.otherRTMEvents msg := <-m.otherRTMEvents
fmt.Println(">>>> Received Message", msg) for _, v := range m.rtmWatchers {
for k, v := range m.rtmWatchers {
fmt.Printf(">>>> RTM Watcher [%s]\n", k)
// Pass the event to the watcher // Pass the event to the watcher
if v(msg) { if v(msg) {
// if a watcher returns true, the message was consumed // if a watcher returns true, the message was consumed

View File

@ -1,9 +1,7 @@
#!/bin/bash #!/bin/bash
rm helperbot.tgz rm helperbot.tgz
./buildplugins.sh
cd cmd cd cmd
go build -o helperbot go build -o helperbot
mv plugins build/ tar -zcvf ../helperbot.tgz helperbot helperbot.service plugins
#tar -zcvf ../helperbot.tgz helperbot helperbot.service plugins cd ..
#cd ..

View File

@ -21,6 +21,7 @@ type AoCState struct {
boardId string boardId string
sessionCookie string sessionCookie string
sessionNeedsUpdate bool sessionNeedsUpdate bool
lastUpdate time.Time
aoc *aoc.AoC aoc *aoc.AoC
lastYear int lastYear int
@ -112,41 +113,28 @@ func (s *AoCState) runLoop() {
if err != nil { if err != nil {
return return
} }
// Don't do anything until we've done an initial update of all earlier years for s.model.Running {
for _, yr := range s.GetListOfAoCYears() {
if yr == s.GetLatestYear() {
continue
}
if !s.sessionNeedsUpdate {
fmt.Printf("Startup: Checking if board needs update (%d)\n", yr)
s.AoCSilentBoardCheckAndUpdate(yr)
}
}
for {
_, err := s.getChannelId() _, err := s.getChannelId()
// This plugin fails without a channel id // This plugin fails without a channel id
if err != nil { if err != nil {
return return
} }
for _, yr := range s.GetListOfAoCYears() {
if !s.sessionNeedsUpdate { if !s.sessionNeedsUpdate {
if s.GetLatestYear() != s.lastYear { if s.GetLatestYear() != s.lastYear {
// Latest year changed. Grab that board first. // Latest year changed. Update it.
s.lastYear = s.GetLatestYear() s.lastYear = s.GetLatestYear()
admin := s.model.GetSlackAdminDMId() admin := s.model.GetSlackAdminDMId()
if admin == "" { if admin == "" {
s.SendAdminIdError() s.SendAdminIdError()
return return
}
s.SendSlackMessage(fmt.Sprintf(":christmas_tree: AoC Set latest leaderboard to %d", s.lastYear), admin)
s.AoCBoardCheckAndUpdate(s.lastYear)
time.Sleep(time.Minute)
} }
s.AoCBoardCheckAndUpdate(yr) s.SendSlackMessage(fmt.Sprintf(":christmas_tree: AoC Set latest leaderboard to %d", s.lastYear), admin)
time.Sleep(time.Minute)
} }
s.AoCBoardCheckAndUpdate(s.lastYear)
s.lastUpdate = time.Now()
} }
time.Sleep(time.Minute) time.Sleep(time.Minute * 15)
} }
s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcApp, "status", "done", "")) s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcApp, "status", "done", ""))
@ -186,6 +174,9 @@ func (s *AoCState) ProcessAdminDirectMessage(m models.BotMessage) bool {
case "session": case "session":
s.DoSessionCmd(m) s.DoSessionCmd(m)
return true return true
case "status":
s.DoStatusCmd(m)
return true
} }
return false return false
} }
@ -204,7 +195,11 @@ func (s *AoCState) ProcessChannelMessage(m models.BotMessage) bool {
func (s *AoCState) DoHelpCmd(m models.BotMessage) bool { func (s *AoCState) DoHelpCmd(m models.BotMessage) bool {
txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n", txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n",
"-- WiP --", "• help - Print this message\n",
"• ping - PONG! Check if the service is running\n",
"• top [year] - Print the top 5 users for the passed year\n",
" If no year is passed, the current year is used\n",
" Passing 'all' considers all years\n",
) )
s.SendSlackMessage(txt, m.Target) s.SendSlackMessage(txt, m.Target)
return true return true
@ -212,7 +207,13 @@ func (s *AoCState) DoHelpCmd(m models.BotMessage) bool {
func (s *AoCState) DoHelpAdminCmd(m models.BotMessage) bool { func (s *AoCState) DoHelpAdminCmd(m models.BotMessage) bool {
txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n", txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n",
"-- WiP --", "• help - Print this message\n",
"• ping - PONG! Check if the service is running\n",
"• top [year] - Print the top 5 users for the passed year\n",
" If no year is passed, the current year is used\n",
" Passing 'all' considers all years\n",
"• session [token] - Print or set the session cookie\n",
"• status - Print the status of the service\n",
) )
s.SendSlackMessage(txt, m.Target) s.SendSlackMessage(txt, m.Target)
return true return true
@ -250,6 +251,23 @@ func (s *AoCState) DoSessionCmd(m models.BotMessage) bool {
return true return true
} }
func (s *AoCState) DoStatusCmd(m models.BotMessage) bool {
admin := s.model.GetSlackAdminDMId()
if admin == "" {
s.SendAdminIdError()
return true
}
txt := ":christmas_tree: Advent of Code Status :christmas_tree:"
txt = fmt.Sprintf(
"%s\nLast Update: %s\nNext Update In: %s", txt,
s.lastUpdate.Format(time.RFC3339),
s.lastUpdate.Add(time.Minute*15).Sub(time.Now()).Round(time.Second),
)
s.SendSlackMessage(txt, m.Target)
return false
}
func (s *AoCState) DoTopCmd(m models.BotMessage) bool { func (s *AoCState) DoTopCmd(m models.BotMessage) bool {
msgPts := strings.Fields(m.Text) msgPts := strings.Fields(m.Text)
var err error var err error