Add a ping/pong message to slack.

There was an error with buffers ending in \x00 unmarshaling the message when it was short:
{"type": "pong"}

My solution was to just include a timestamp to make the message large enough to fill the
initial buffer size. Perfect.
This commit is contained in:
Chris Sexton 2018-06-27 08:41:23 -04:00
parent 01056f1956
commit ae4e28c5be
2 changed files with 37 additions and 18 deletions

View File

@ -27,7 +27,6 @@ import (
"github.com/velour/catbase/plugins/rpgORdie" "github.com/velour/catbase/plugins/rpgORdie"
"github.com/velour/catbase/plugins/rss" "github.com/velour/catbase/plugins/rss"
"github.com/velour/catbase/plugins/sisyphus" "github.com/velour/catbase/plugins/sisyphus"
"github.com/velour/catbase/plugins/stats"
"github.com/velour/catbase/plugins/talker" "github.com/velour/catbase/plugins/talker"
"github.com/velour/catbase/plugins/tell" "github.com/velour/catbase/plugins/tell"
"github.com/velour/catbase/plugins/twitch" "github.com/velour/catbase/plugins/twitch"
@ -55,12 +54,9 @@ func main() {
b := bot.New(c, client) b := bot.New(c, client)
// b.AddHandler(plugins.NewTestPlugin(b))
b.AddHandler("admin", admin.New(b)) b.AddHandler("admin", admin.New(b))
b.AddHandler("stats", stats.New(b))
b.AddHandler("first", first.New(b)) b.AddHandler("first", first.New(b))
b.AddHandler("leftpad", leftpad.New(b)) b.AddHandler("leftpad", leftpad.New(b))
// b.AddHandler("downtime", downtime.New(b))
b.AddHandler("talker", talker.New(b)) b.AddHandler("talker", talker.New(b))
b.AddHandler("dice", dice.New(b)) b.AddHandler("dice", dice.New(b))
b.AddHandler("picker", picker.New(b)) b.AddHandler("picker", picker.New(b))

View File

@ -18,12 +18,13 @@ import (
"strings" "strings"
// "sync/atomic" // "sync/atomic"
"time" "time"
"context"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user" "github.com/velour/catbase/bot/user"
"github.com/velour/catbase/config" "github.com/velour/catbase/config"
"golang.org/x/net/websocket" "github.com/velour/chat/websocket"
) )
type Slack struct { type Slack struct {
@ -316,7 +317,7 @@ func (s *Slack) React(channel, reaction string, message msg.Message) bool {
"channel": {channel}, "channel": {channel},
"timestamp": {message.AdditionalData["RAW_SLACK_TIMESTAMP"]}}) "timestamp": {message.AdditionalData["RAW_SLACK_TIMESTAMP"]}})
if err != nil { if err != nil {
log.Println("reaction failed: %s", err) log.Printf("reaction failed: %s", err)
return false return false
} }
return checkReturnStatus(resp) return checkReturnStatus(resp)
@ -330,7 +331,7 @@ func (s *Slack) Edit(channel, newMessage, identifier string) bool {
"text": {newMessage}, "text": {newMessage},
"ts": {identifier}}) "ts": {identifier}})
if err != nil { if err != nil {
log.Println("edit failed: %s", err) log.Printf("edit failed: %s", err)
return false return false
} }
return checkReturnStatus(resp) return checkReturnStatus(resp)
@ -355,8 +356,8 @@ func (s *Slack) populateEmojiList() {
} }
type EmojiListResponse struct { type EmojiListResponse struct {
OK bool `json:ok` OK bool `json:"ok"`
Emoji map[string]string `json:emoji` Emoji map[string]string `json:"emoji"`
} }
var list EmojiListResponse var list EmojiListResponse
@ -367,16 +368,30 @@ func (s *Slack) populateEmojiList() {
s.emoji = list.Emoji s.emoji = list.Emoji
} }
func (s *Slack) ping(ctx context.Context) {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
ping := map[string]interface{}{"type": "ping", "time": time.Now().UnixNano()}
if err := s.ws.Send(context.TODO(), ping); err != nil {
panic(err)
}
}
}
}
func (s *Slack) receiveMessage() (slackMessage, error) { func (s *Slack) receiveMessage() (slackMessage, error) {
var msg []byte
m := slackMessage{} m := slackMessage{}
err := websocket.Message.Receive(s.ws, &msg) err := s.ws.Recv(context.TODO(), &m)
if err != nil { if err != nil {
log.Println("Error decoding WS message") log.Println("Error decoding WS message")
return m, err panic(fmt.Errorf("%v\n%v", m, err))
} }
err2 := json.Unmarshal(msg, &m) return m, nil
return m, err2
} }
// I think it's horseshit that I have to do this // I think it's horseshit that I have to do this
@ -390,6 +405,11 @@ func slackTStoTime(t string) time.Time {
func (s *Slack) Serve() error { func (s *Slack) Serve() error {
s.connect() s.connect()
s.populateEmojiList() s.populateEmojiList()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go s.ping(ctx)
for { for {
msg, err := s.receiveMessage() msg, err := s.receiveMessage()
if err != nil && err == io.EOF { if err != nil && err == io.EOF {
@ -422,6 +442,7 @@ func (s *Slack) Serve() error {
case "user_typing": case "user_typing":
case "reconnect_url": case "reconnect_url":
case "desktop_notification": case "desktop_notification":
case "pong":
// squeltch this stuff // squeltch this stuff
continue continue
default: default:
@ -588,8 +609,8 @@ func (s *Slack) markChannelAsRead(slackChanId string) error {
func (s *Slack) connect() { func (s *Slack) connect() {
token := s.config.Slack.Token token := s.config.Slack.Token
url := fmt.Sprintf("https://slack.com/api/rtm.connect?token=%s", token) u := fmt.Sprintf("https://slack.com/api/rtm.connect?token=%s", token)
resp, err := http.Get(url) resp, err := http.Get(u)
if err != nil { if err != nil {
return return
} }
@ -614,9 +635,11 @@ func (s *Slack) connect() {
s.url = "https://slack.com/api/" s.url = "https://slack.com/api/"
s.id = rtm.Self.ID s.id = rtm.Self.ID
s.markAllChannelsRead() // This is hitting the rate limit, and it may not be needed
//s.markAllChannelsRead()
s.ws, err = websocket.Dial(rtm.URL, "", s.url) rtmURL, _ := url.Parse(rtm.URL)
s.ws, err = websocket.Dial(context.TODO(), rtmURL)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }