pubsub: add counter pub

This commit is contained in:
Chris Sexton 2021-11-15 20:34:09 -05:00 committed by Chris Sexton
parent 4f9b50f47a
commit 3343a98802
5 changed files with 46 additions and 11 deletions

View File

@ -3,10 +3,14 @@
package bot package bot
import ( import (
"bytes"
"database/sql" "database/sql"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"net/http"
"reflect" "reflect"
"regexp" "regexp"
"strconv" "strconv"
@ -263,3 +267,25 @@ func (b *bot) selfSaid(conn Connector, channel, message string, action bool) {
} }
} }
} }
// PubToASub sends updates to subscribed URLs
func (b *bot) PubToASub(subject string, payload interface{}) {
key := fmt.Sprintf("pubsub.%s.url", subject)
subs := b.config.GetArray(key, []string{})
if len(subs) == 0 {
return
}
encodedBody, _ := json.Marshal(struct {
Payload interface{} `json:"payload"`
}{payload})
body := bytes.NewBuffer(encodedBody)
for _, url := range subs {
r, err := http.Post(url, "text/json", body)
if err != nil {
log.Error().Err(err).Msg("")
} else {
body, _ := ioutil.ReadAll(r.Body)
log.Debug().Msgf("Response body: %s", string(body))
}
}
}

View File

@ -172,6 +172,9 @@ type Bot interface {
// Check valid password // Check valid password
CheckPassword(secret, password string) bool CheckPassword(secret, password string) bool
// PubToASub publishes a message to any subscribers
PubToASub(subject string, payload interface{})
} }
// Connector represents a server connection to a chat service // Connector represents a server connection to a chat service

View File

@ -118,11 +118,12 @@ func NewMockBot() *MockBot {
return &b return &b
} }
func (mb *MockBot) GetPluginNames() []string { return nil } func (mb *MockBot) GetPluginNames() []string { return nil }
func (mb *MockBot) RefreshPluginBlacklist() error { return nil } func (mb *MockBot) RefreshPluginBlacklist() error { return nil }
func (mb *MockBot) RefreshPluginWhitelist() error { return nil } func (mb *MockBot) RefreshPluginWhitelist() error { return nil }
func (mb *MockBot) GetWhitelist() []string { return []string{} } func (mb *MockBot) GetWhitelist() []string { return []string{} }
func (mb *MockBot) OnBlacklist(ch, p string) bool { return false } func (mb *MockBot) OnBlacklist(ch, p string) bool { return false }
func (mb *MockBot) URLFormat(title, url string) string { return title + url } func (mb *MockBot) URLFormat(title, url string) string { return title + url }
func (mb *MockBot) CheckPassword(secret, password string) bool { return true } func (mb *MockBot) CheckPassword(secret, password string) bool { return true }
func (mb *MockBot) ListenAndServe() {} func (mb *MockBot) ListenAndServe() {}
func (mb *MockBot) PubToASub(subject string, payload interface{}) {}

View File

@ -160,7 +160,7 @@ func (p *CounterPlugin) handleCounterAPI(w http.ResponseWriter, r *http.Request)
} }
type Update struct { type Update struct {
Who string Who string `json:"who"`
What string What string `json:"what"`
Amount int Amount int `json:"amount"`
} }

View File

@ -344,6 +344,11 @@ func New(b bot.Bot) *CounterPlugin {
b.Register(cp, bot.Help, cp.help) b.Register(cp, bot.Help, cp.help)
cp.registerWeb() cp.registerWeb()
RegisterUpdate(func(r bot.Request, u Update) {
log.Debug().Msgf("Publishing update %v", u)
b.PubToASub("counter", u)
})
return cp return cp
} }