package models import ( "fmt" "time" "git.bullercodeworks.com/brian/boltease" "github.com/slack-go/slack" ) type BotModel struct { debug bool db *boltease.DB messages chan BotMessage slackApiToken string slackApi *slack.Client slackRTM *slack.RTM slackRTMLatency time.Duration incomingSlackMessages chan *slack.MessageEvent otherRTMEvents chan *slack.RTMEvent messageWatchers map[string]func(msg BotMessage) bool rtmWatchers map[string]func(event *slack.RTMEvent) bool Running bool } func NewBotModel(debug bool) (*BotModel, error) { var err error m := new(BotModel) m.debug = debug m.messages = make(chan BotMessage, 100) 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 } if err = m.NewSlack(); err != nil { return nil, err } go m.monitorSlackMessages() return m, nil } func (m *BotModel) DebugMessageWatcher(msg BotMessage) bool { if m.debug { fmt.Printf("Received Message: %s\n", msg) } return false } func (m *BotModel) DebugRTMWatcher(event *slack.RTMEvent) bool { if m.debug { fmt.Printf("RTMEvent: %s\n", event) } return false } func (m *BotModel) SendMessage(msg BotMessage) { if m.debug { fmt.Println("Sending:", msg) } m.messages <- msg } func (m *BotModel) AddMessageWatcher(name string, watcher func(msg BotMessage) bool) { m.messageWatchers[name] = watcher } func (m *BotModel) AddRTMWatcher(name string, watcher func(event *slack.RTMEvent) bool) { m.rtmWatchers[name] = watcher } func (m *BotModel) ProcessMessageChannel() { msg := <-m.messages for _, v := range m.messageWatchers { // Pass the message to the watcher if v != nil && v(msg) { // if a watcher returns true, the message was consumed break } } } func (m *BotModel) ProcessRTMChannel() { msg := <-m.otherRTMEvents for _, v := range m.rtmWatchers { // Pass the event to the watcher if v(msg) { // if a watcher returns true, the message was consumed break } } }