Update to use nlopes-slack

This commit is contained in:
Brian Buller 2019-11-21 17:45:04 -06:00
parent 8909975cce
commit 52e0fca780
9 changed files with 77 additions and 113 deletions

View File

@ -23,12 +23,11 @@ func NewApp() (*App, error) {
} }
a.DebugMode = DebugMode a.DebugMode = DebugMode
a.running = true
err := a.initialize() err := a.initialize()
if err != nil { if err != nil {
a.running = false
return nil, err return nil, err
} }
a.running = true
go a.MonitorSlackMessages() go a.MonitorSlackMessages()
return a, nil return a, nil
@ -39,6 +38,10 @@ func (a *App) initialize() error {
if a.m, err = NewBotModel(); err != nil { if a.m, err = NewBotModel(); err != nil {
return err return err
} }
bt, bterr := a.m.GetString([]string{"config", "plugin_dir"})
if bterr == nil {
fmt.Println(bt)
}
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
// Load up the plugins // Load up the plugins
@ -65,12 +68,10 @@ func (a *App) initialize() error {
a.m.setSlackAdminDMId(strings.TrimSpace(slackDMid)) a.m.setSlackAdminDMId(strings.TrimSpace(slackDMid))
} }
fmt.Println("Creating Slack Model")
if err = a.m.NewSlack(); err != nil { if err = a.m.NewSlack(); err != nil {
return err return err
} }
fmt.Println("Starting gofunc watchMessageChannel")
go a.watchMessageChannel() go a.watchMessageChannel()
return nil return nil
} }
@ -79,10 +80,7 @@ func (a *App) watchMessageChannel() {
for a.running { for a.running {
msg := <-a.m.messages msg := <-a.m.messages
slackMsg := msg.GetMessage() slackMsg := msg.GetMessage()
if slackMsg.Type != "user_typing" { if slackMsg.Type == "control" && slackMsg.Text == "quit" {
fmt.Println(slackMsg)
}
if slackMsg.Type == "control" && slackMsg.Name == "quit" {
a.running = false a.running = false
break break
} else if msg.GetDestination() == "error" { } else if msg.GetDestination() == "error" {
@ -95,4 +93,5 @@ func (a *App) watchMessageChannel() {
v.State.ProcessMessage(msg) v.State.ProcessMessage(msg)
} }
} }
close(a.m.messages)
} }

View File

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"plugin" "plugin"
"strings"
"git.bullercodeworks.com/brian/helperbot" "git.bullercodeworks.com/brian/helperbot"
) )
@ -24,22 +25,26 @@ func (a *App) LoadPluginsFromDirectory(dir string) error {
os.Exit(1) os.Exit(1)
} }
for _, f := range files { for _, f := range files {
if !strings.HasSuffix(f.Name(), ".so") {
fmt.Printf("Skipping file (%s)\n", f.Name())
continue
}
p, err := plugin.Open(dir + f.Name()) p, err := plugin.Open(dir + f.Name())
if err != nil { if err != nil {
fmt.Printf("Error loading plugin (%s)\n", f.Name()) fmt.Println(fmt.Sprintf("Error loading plugin (%s)\n", f.Name()))
fmt.Println(err.Error()) fmt.Println(err.Error())
os.Exit(1) os.Exit(1)
} }
hp, err := NewHelperPlugin(p) hp, err := NewHelperPlugin(p)
if err != nil { if err != nil {
fmt.Printf("Error loading plugin (%s)\n", f.Name()) fmt.Println(fmt.Sprintf("Error loading plugin (%s)\n", f.Name()))
fmt.Println(err.Error()) fmt.Println(err.Error())
os.Exit(1) os.Exit(1)
} }
hp.State.Initialize(a.m) hp.State.Initialize(a.m)
hp.State.Run() hp.State.Run()
fmt.Printf("Plugin Loaded (%s)\n", f.Name())
a.plugins = append(a.plugins, *hp) a.plugins = append(a.plugins, *hp)
fmt.Printf("Plugin loaded: %s\n", f.Name())
} }
return nil return nil
} }

View File

@ -3,7 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
slack "git.bullercodeworks.com/brian/go-slack" "github.com/nlopes/slack"
) )
func GetMessageJson(msg *slack.Message) string { func GetMessageJson(msg *slack.Message) string {

View File

@ -7,7 +7,7 @@ import (
"syscall" "syscall"
"time" "time"
slack "git.bullercodeworks.com/brian/go-slack" "github.com/nlopes/slack"
) )
var DebugMode = false var DebugMode = false
@ -26,6 +26,11 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// Monitor the Advent of Code Boards
//go m.MonitorAoCBoards()
// Monitor incoming Slack messages
//go m.MonitorSlackMessages()
// Set up a channel to intercept Ctrl+C for graceful shutdowns // Set up a channel to intercept Ctrl+C for graceful shutdowns
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM) signal.Notify(c, os.Interrupt, syscall.SIGTERM)
@ -33,11 +38,15 @@ func main() {
<-c <-c
// Save the changes when the app quits // Save the changes when the app quits
fmt.Println("\nFinishing up...") fmt.Println("\nFinishing up...")
a.m.messages <- NewBotMessage("main", "main", slack.Message{Type: "control", Name: "quit"}) msg := slack.Message{}
msg.Type = "control"
msg.Text = "quit"
a.m.messages <- NewBotMessage("main", "main", msg)
}() }()
for a.running { for a.running {
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
} }
fmt.Println("Model has stopped running")
fmt.Println("Done") fmt.Println("Done")
os.Exit(0) os.Exit(0)
} }

View File

@ -1,6 +1,6 @@
package main package main
import slack "git.bullercodeworks.com/brian/go-slack" import "github.com/nlopes/slack"
// This message type is for communications over the messages channel // This message type is for communications over the messages channel
type BotMessage struct { type BotMessage struct {

View File

@ -6,9 +6,10 @@ import (
"os" "os"
"strings" "strings"
slack "git.bullercodeworks.com/brian/go-slack" goslack "git.bullercodeworks.com/brian/go-slack"
"git.bullercodeworks.com/brian/helperbot" "git.bullercodeworks.com/brian/helperbot"
"github.com/br0xen/boltease" "github.com/br0xen/boltease"
"github.com/nlopes/slack"
) )
type BotModel struct { type BotModel struct {
@ -16,7 +17,7 @@ type BotModel struct {
messages chan helperbot.Message messages chan helperbot.Message
slack *slack.Slack slack *goslack.Slack
cache map[string][]byte cache map[string][]byte
} }
@ -30,8 +31,6 @@ func NewBotModel() (*BotModel, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
m.db.MkBucketPath([]string{"slack", "users"})
m.db.MkBucketPath([]string{"slack", "channels"})
return m, nil return m, nil
} }
@ -93,6 +92,9 @@ func (m *BotModel) GetString(path []string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if len(bts) == 0 {
return "", nil
}
return string(bts), nil return string(bts), nil
} }

View File

@ -1,11 +1,10 @@
package main package main
import ( import (
slack "git.bullercodeworks.com/brian/go-slack" goslack "git.bullercodeworks.com/brian/go-slack"
"github.com/nlopes/slack"
) )
// TODO: Look into switching to: https://github.com/nlopes/slack
/* DB Functions */ /* DB Functions */
func (m *BotModel) setSlackToken(token string) error { func (m *BotModel) setSlackToken(token string) error {
return m.SetBytes([]string{"slack", "config", "token"}, []byte(token)) return m.SetBytes([]string{"slack", "config", "token"}, []byte(token))
@ -30,18 +29,16 @@ func (m *BotModel) NewSlack() error {
if err != nil { if err != nil {
return err return err
} }
if m.slack, err = slack.CreateSlack(token); err != nil { if m.slack, err = goslack.CreateSlack(token); err != nil {
return err return err
} }
m.slack.StartRTM()
return nil return nil
} }
func (a *App) MonitorSlackMessages() { func (a *App) MonitorSlackMessages() {
for a.running { for msg := range a.m.slack.IncomingMessages {
msg, err := a.m.slack.GetMessage() a.m.SendMessage("slack", "main", slack.Message(*msg))
if err == nil {
a.m.SendMessage("slack", "main", msg)
}
} }
} }
@ -50,10 +47,7 @@ func (m *BotModel) SendSlackChannelMessage(msg *slack.Message) error {
return m.SendSlackAdminMessage(msg) return m.SendSlackAdminMessage(msg)
} }
// Send message to slack channel // Send message to slack channel
var err error m.slack.PostMessage(msg)
if err = m.slack.PostMessage(*msg); err != nil {
return err
}
return nil return nil
} }
@ -64,8 +58,6 @@ func (m *BotModel) SendSlackAdminMessage(msg *slack.Message) error {
if err != nil { if err != nil {
return err return err
} }
if err = m.slack.PostMessage(*msg); err != nil { m.slack.PostMessage(msg)
return err
}
return nil return nil
} }

View File

@ -1,6 +1,6 @@
package helperbot package helperbot
import slack "git.bullercodeworks.com/brian/go-slack" import "github.com/nlopes/slack"
type Model interface { type Model interface {
SendMessage(src, dest string, message slack.Message) SendMessage(src, dest string, message slack.Message)

View File

@ -14,8 +14,8 @@ import (
"time" "time"
aoc "git.bullercodeworks.com/brian/go-adventofcode" aoc "git.bullercodeworks.com/brian/go-adventofcode"
slack "git.bullercodeworks.com/brian/go-slack"
"git.bullercodeworks.com/brian/helperbot" "git.bullercodeworks.com/brian/helperbot"
"github.com/nlopes/slack"
) )
/* Plugin State */ /* Plugin State */
@ -76,11 +76,7 @@ func (s *AoCState) ProcessMessage(m helperbot.Message) {
case 'D': case 'D':
admin, err := s.model.GetSlackAdminDMId() admin, err := s.model.GetSlackAdminDMId()
if err != nil { if err != nil {
s.model.SendMessage(s.Name(), "error", slack.Message{ s.SendAdminIdError()
Type: "error",
Text: "Error getting Admin DM Id",
Time: time.Now(),
})
return return
} }
if slackMsg.Channel == admin { if slackMsg.Channel == admin {
@ -129,11 +125,8 @@ func (s *AoCState) runLoop() {
} }
time.Sleep(time.Minute) time.Sleep(time.Minute)
} }
s.model.SendMessage(s.Name(), "main", slack.Message{
Type: "status", s.model.SendMessage(s.Name(), "main", s.BuildMessage("status", "done", ""))
Text: "done",
Time: time.Now(),
})
} }
func (s *AoCState) ProcessDirectMessage(slackMsg slack.Message) { func (s *AoCState) ProcessDirectMessage(slackMsg slack.Message) {
@ -183,30 +176,18 @@ func (s *AoCState) DoHelpCmd(slackMsg slack.Message) {
txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n", txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n",
"-- WiP --", "-- WiP --",
) )
s.model.SendMessage(s.Name(), "slack", slack.Message{ s.SendSlackMessage(txt, slackMsg.Channel)
Type: "message",
Channel: slackMsg.Channel,
Text: txt,
})
} }
func (s *AoCState) DoHelpAdminCmd(slackMsg slack.Message) { func (s *AoCState) DoHelpAdminCmd(slackMsg slack.Message) {
txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n", txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n",
"-- WiP --", "-- WiP --",
) )
s.model.SendMessage(s.Name(), "slack", slack.Message{ s.SendSlackMessage(txt, slackMsg.Channel)
Type: "message",
Channel: slackMsg.Channel,
Text: txt,
})
} }
func (s *AoCState) DoPingCmd(slackMsg slack.Message) { func (s *AoCState) DoPingCmd(slackMsg slack.Message) {
s.model.SendMessage(s.Name(), "slack", slack.Message{ s.SendSlackMessage(":christmas_tree: PONG :christmas_tree:", slackMsg.Channel)
Type: "message",
Channel: slackMsg.Channel,
Text: ":christmas_tree: PONG :christmas_tree:",
})
} }
func (s *AoCState) DoSessionCmd(slackMsg slack.Message) { func (s *AoCState) DoSessionCmd(slackMsg slack.Message) {
@ -215,39 +196,23 @@ func (s *AoCState) DoSessionCmd(slackMsg slack.Message) {
// Set the session cookie // Set the session cookie
admin, err := s.model.GetSlackAdminDMId() admin, err := s.model.GetSlackAdminDMId()
if err != nil { if err != nil {
s.model.SendMessage(s.Name(), "error", slack.Message{ s.SendAdminIdError()
Type: "error",
Text: "Error getting Admin DM Id",
Time: time.Now(),
})
return return
} }
aocSession := msgPts[2] aocSession := msgPts[2]
s.setAoCSessionCookie(strings.TrimSpace(aocSession)) s.setAoCSessionCookie(strings.TrimSpace(aocSession))
s.sessionCookie = aocSession s.sessionCookie = aocSession
s.sessionNeedsUpdate = false s.sessionNeedsUpdate = false
s.model.SendMessage(s.Name(), "slack", slack.Message{ s.SendSlackMessage(":christmas_tree: New Session: "+s.sessionCookie, admin)
Type: "message",
Channel: admin,
Text: ":christmas_tree: New Session: " + s.sessionCookie,
})
} else if len(msgPts) == 2 && msgPts[1] == "session" { } else if len(msgPts) == 2 && msgPts[1] == "session" {
// Print the session cookie // Print the session cookie
admin, err := s.model.GetSlackAdminDMId() admin, err := s.model.GetSlackAdminDMId()
if err != nil { if err != nil {
s.model.SendMessage(s.Name(), "error", slack.Message{ s.SendAdminIdError()
Type: "error",
Text: "Error getting Admin DM Id",
Time: time.Now(),
})
return return
} }
// We only send the session cookie to the admin // We only send the session cookie to the admin
s.model.SendMessage(s.Name(), "slack", slack.Message{ s.SendSlackMessage(":christmas_tree: session: "+s.sessionCookie, admin)
Type: "message",
Channel: admin,
Text: ":christmas_tree: session: " + s.sessionCookie,
})
} }
} }
@ -275,11 +240,7 @@ func (s *AoCState) DoTopCmd(slackMsg slack.Message) {
} else { } else {
txt = s.DoTopForYear(yr) txt = s.DoTopForYear(yr)
} }
s.model.SendMessage(s.Name(), "slack", slack.Message{ s.SendSlackMessage(txt, slackMsg.Channel)
Type: "message",
Channel: slackMsg.Channel,
Text: txt,
})
} }
func (s *AoCState) DoTopForAll() (string, error) { func (s *AoCState) DoTopForAll() (string, error) {
@ -379,30 +340,18 @@ func (s *AoCState) AoCBoardCheckAndUpdate(yr int) {
if err != nil { if err != nil {
admin, adminErr := s.model.GetSlackAdminDMId() admin, adminErr := s.model.GetSlackAdminDMId()
if adminErr != nil { if adminErr != nil {
s.model.SendMessage(s.Name(), "error", slack.Message{ s.SendAdminIdError()
Type: "error",
Text: "Error getting Admin DM Id",
Time: time.Now(),
})
return return
} }
if err.Error() == "Invalid Session Cookie" { if err.Error() == "Invalid Session Cookie" {
s.sessionNeedsUpdate = true s.sessionNeedsUpdate = true
} }
s.model.SendMessage(s.Name(), "slack", slack.Message{ s.SendSlackMessage(fmt.Sprintf(":warning: AoC Error processing leaderboard (%d) - %s", yr, err.Error()), admin)
Type: "message",
Channel: admin,
Text: fmt.Sprintf(":warning: AoC Error processing leaderboard (%d) - %s", yr, err.Error()),
})
return return
} }
msg := slack.Message{ s.model.SendMessage(s.Name(), "main",
Type: "success", s.BuildMessage("success", fmt.Sprintf("Received leaderboard (%d)", yr), ""))
Text: fmt.Sprintf("Received leaderboard (%d)", yr),
Time: time.Now(),
}
s.model.SendMessage(s.Name(), "main", msg)
// Compare the new leaderboard to the saved one // Compare the new leaderboard to the saved one
for _, v := range l.Members { for _, v := range l.Members {
mbr, err := s.getMember(l.Event, v.ID) mbr, err := s.getMember(l.Event, v.ID)
@ -410,11 +359,7 @@ func (s *AoCState) AoCBoardCheckAndUpdate(yr int) {
continue continue
} }
if mbr.Stars != v.Stars { if mbr.Stars != v.Stars {
s.model.SendMessage(s.Name(), "slack", slack.Message{ s.SendSlackMessage(":christmas_tree: "+v.Name+" now has "+strconv.Itoa(v.Stars)+" stars! :christmas_tree:", channelId)
Type: "message",
Channel: channelId,
Text: ":christmas_tree: " + v.Name + " now has " + strconv.Itoa(v.Stars) + " stars! :christmas_tree:",
})
} }
} }
// Save the leaderboard to the db // Save the leaderboard to the db
@ -499,11 +444,7 @@ func (s *AoCState) saveLeaderboard(l *aoc.Leaderboard) error {
} }
for _, v := range l.Members { for _, v := range l.Members {
if err = s.saveMember(l.Event, &v); err != nil { if err = s.saveMember(l.Event, &v); err != nil {
s.model.SendMessage(s.Name(), "error", slack.Message{ s.model.SendMessage(s.Name(), "error", s.BuildMessage("error", fmt.Sprintf("Error Saving Member (%s)", v.Name), ""))
Type: "error",
Text: fmt.Sprintf("Error Saving Member (%s)", v.Name),
Time: time.Now(),
})
} }
} }
return nil return nil
@ -579,3 +520,19 @@ func (s *AoCState) getMember(event string, memberId string) (*aoc.Member, error)
mbr.GlobalScore, err = strconv.Atoi(wrk) mbr.GlobalScore, err = strconv.Atoi(wrk)
return mbr, nil return mbr, nil
} }
func (s *AoCState) SendAdminIdError() {
s.model.SendMessage(s.Name(), "error", s.BuildMessage("error", "Error getting Admin DM Id", ""))
}
func (s *AoCState) SendSlackMessage(text, dest string) {
s.model.SendMessage(s.Name(), "slack", s.BuildMessage("message", text, dest))
}
func (s *AoCState) BuildMessage(tp, text, ch string) slack.Message {
ret := slack.Message{}
ret.Type = tp
ret.Text = text
ret.Channel = ch
return ret
}