I believe it's all working
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
type Model interface {
|
||||
SendMessage(msg BotMessage)
|
||||
GetSlackAdminDMId() string
|
||||
GetSlackLatency() time.Duration
|
||||
GetBytes(path []string) ([]byte, error)
|
||||
SetBytes(path []string, val []byte) error
|
||||
GetString(path []string) (string, error)
|
||||
SetString(path []string, val string) error
|
||||
GetInt(path []string) (int, error)
|
||||
SetInt(path []string, val int) error
|
||||
}
|
||||
|
||||
type Message interface {
|
||||
GetSource() string
|
||||
GetDestination() string
|
||||
GetType() string
|
||||
GetTarget() string
|
||||
GetText() string
|
||||
}
|
||||
|
||||
type PluginState interface {
|
||||
Name() string
|
||||
Initialize(Model) error
|
||||
ProcessMessage(BotMessage) bool
|
||||
ProcessRTMEvent(*slack.RTMEvent) bool
|
||||
Run()
|
||||
Exit()
|
||||
}
|
@@ -9,6 +9,7 @@ import (
|
||||
const (
|
||||
MsgSrcApp = "app"
|
||||
MsgSrcSlack = "slack"
|
||||
MsgTpStatus = "status"
|
||||
MsgTpControl = "control"
|
||||
MsgTpError = "error"
|
||||
MsgTpMessage = "message"
|
||||
|
@@ -22,8 +22,8 @@ type BotModel struct {
|
||||
incomingSlackMessages chan *slack.MessageEvent
|
||||
otherRTMEvents chan *slack.RTMEvent
|
||||
|
||||
messageWatchers []func(msg BotMessage) bool
|
||||
rtmWatchers []func(event *slack.RTMEvent) bool
|
||||
messageWatchers map[string]func(msg BotMessage) bool
|
||||
rtmWatchers map[string]func(event *slack.RTMEvent) bool
|
||||
}
|
||||
|
||||
func NewBotModel(debug bool) (*BotModel, error) {
|
||||
@@ -31,9 +31,10 @@ func NewBotModel(debug bool) (*BotModel, error) {
|
||||
m := new(BotModel)
|
||||
m.debug = debug
|
||||
m.messages = make(chan BotMessage, 100)
|
||||
m.messageWatchers = make([]func(msg BotMessage) bool, 1)
|
||||
m.AddMessageWatcher(m.DebugMessageWatcher)
|
||||
m.AddRTMWatcher(m.DebugRTMWatcher)
|
||||
m.messageWatchers = make(map[string]func(msg BotMessage) bool)
|
||||
m.rtmWatchers = make(map[string]func(even *slack.RTMEvent) bool)
|
||||
m.AddMessageWatcher("debug", m.DebugMessageWatcher)
|
||||
m.AddRTMWatcher("debug", m.DebugRTMWatcher)
|
||||
m.db, err = boltease.Create("helperbot.db", 0600, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -63,26 +64,32 @@ func (m *BotModel) SendMessage(msg BotMessage) {
|
||||
m.messages <- msg
|
||||
}
|
||||
|
||||
func (m *BotModel) AddMessageWatcher(watcher func(msg BotMessage) bool) {
|
||||
m.messageWatchers = append(m.messageWatchers, watcher)
|
||||
func (m *BotModel) AddMessageWatcher(name string, watcher func(msg BotMessage) bool) {
|
||||
m.messageWatchers[name] = watcher
|
||||
}
|
||||
func (m *BotModel) AddRTMWatcher(watcher func(event *slack.RTMEvent) bool) {
|
||||
m.rtmWatchers = append(m.rtmWatchers, watcher)
|
||||
func (m *BotModel) AddRTMWatcher(name string, watcher func(event *slack.RTMEvent) bool) {
|
||||
m.rtmWatchers[name] = watcher
|
||||
}
|
||||
|
||||
func (m *BotModel) ProcessMessageChannel() {
|
||||
fmt.Println(">> ProcessMessageChannel")
|
||||
msg := <-m.messages
|
||||
for _, v := range m.messageWatchers {
|
||||
fmt.Println(">>>> Received Message", msg)
|
||||
for k, v := range m.messageWatchers {
|
||||
fmt.Printf(">>>> Message Watcher [%s]\n", k)
|
||||
// Pass the message to the watcher
|
||||
if v(msg) {
|
||||
if v != nil && v(msg) {
|
||||
// if a watcher returns true, the message was consumed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
func (m *BotModel) ProcessRTMChannel() {
|
||||
fmt.Println(">> ProcessRTMChannel")
|
||||
msg := <-m.otherRTMEvents
|
||||
for _, v := range m.rtmWatchers {
|
||||
fmt.Println(">>>> Received Message", msg)
|
||||
for k, v := range m.rtmWatchers {
|
||||
fmt.Printf(">>>> RTM Watcher [%s]\n", k)
|
||||
// Pass the event to the watcher
|
||||
if v(msg) {
|
||||
// if a watcher returns true, the message was consumed
|
||||
|
@@ -12,13 +12,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
keyPluginDir = "plugin_dir"
|
||||
keySlackToken = "slack.token"
|
||||
keySlackAdminDmId = "slack.admin_dm_id"
|
||||
)
|
||||
|
||||
func (m *BotModel) GetPluginDir() string { return viper.GetString(keyPluginDir) }
|
||||
|
||||
/* Slack Config Functions */
|
||||
func (m *BotModel) getSlackToken() string { return viper.GetString(keySlackToken) }
|
||||
func (m *BotModel) setSlackToken(token string) error {
|
||||
|
14
models/plugin_model.go
Normal file
14
models/plugin_model.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
type PluginState interface {
|
||||
Name() string
|
||||
Initialize(*BotModel) error
|
||||
ProcessMessage(BotMessage) bool
|
||||
ProcessRTMEvent(*slack.RTMEvent) bool
|
||||
Run()
|
||||
Exit()
|
||||
}
|
Reference in New Issue
Block a user