Merge pull request #63 from velour/reactions

initial implementation and POC to getting reactions pushed into slack…
This commit is contained in:
Chris Sexton 2017-06-17 16:27:45 -04:00 committed by GitHub
commit f30955cdef
7 changed files with 81 additions and 0 deletions

View File

@ -61,6 +61,10 @@ func (b *bot) SendAction(channel, message string) {
b.conn.SendAction(channel, message) b.conn.SendAction(channel, message)
} }
func (b *bot) React(channel, reaction string, message msg.Message) {
b.conn.React(channel, reaction, message)
}
// Checks to see if the user is asking for help, returns true if so and handles the situation. // Checks to see if the user is asking for help, returns true if so and handles the situation.
func (b *bot) checkHelp(channel string, parts []string) { func (b *bot) checkHelp(channel string, parts []string) {
if len(parts) == 1 { if len(parts) == 1 {

View File

@ -17,6 +17,7 @@ type Bot interface {
AddHandler(string, Handler) AddHandler(string, Handler)
SendMessage(string, string) SendMessage(string, string)
SendAction(string, string) SendAction(string, string)
React(string, string, msg.Message)
MsgReceived(msg.Message) MsgReceived(msg.Message)
EventReceived(msg.Message) EventReceived(msg.Message)
Filter(msg.Message, string) string Filter(msg.Message, string) string
@ -30,6 +31,7 @@ type Connector interface {
SendMessage(channel, message string) SendMessage(channel, message string)
SendAction(channel, message string) SendAction(channel, message string)
React(string, string, msg.Message)
Serve() Serve()
Who(string) []string Who(string) []string

View File

@ -19,4 +19,5 @@ type Message struct {
Action bool Action bool
Time time.Time Time time.Time
Host string Host string
AdditionalData map[string]string
} }

View File

@ -98,6 +98,10 @@ func (i *Irc) SendAction(channel, message string) {
i.SendMessage(channel, message) i.SendMessage(channel, message)
} }
func (i *Irc) React(channel, reaction string, message msg.Message) {
//we're not goign to do anything because it's IRC
}
func (i *Irc) Serve() { func (i *Irc) Serve() {
if i.eventReceived == nil || i.messageReceived == nil { if i.eventReceived == nil || i.messageReceived == nil {
log.Fatal("Missing an event handler") log.Fatal("Missing an event handler")

View File

@ -16,6 +16,7 @@ import (
"github.com/velour/catbase/plugins/dice" "github.com/velour/catbase/plugins/dice"
"github.com/velour/catbase/plugins/fact" "github.com/velour/catbase/plugins/fact"
"github.com/velour/catbase/plugins/leftpad" "github.com/velour/catbase/plugins/leftpad"
"github.com/velour/catbase/plugins/reaction"
"github.com/velour/catbase/plugins/reminder" "github.com/velour/catbase/plugins/reminder"
"github.com/velour/catbase/plugins/rss" "github.com/velour/catbase/plugins/rss"
"github.com/velour/catbase/plugins/stats" "github.com/velour/catbase/plugins/stats"
@ -60,6 +61,7 @@ func main() {
b.AddHandler("babbler", babbler.New(b)) b.AddHandler("babbler", babbler.New(b))
b.AddHandler("zork", zork.New(b)) b.AddHandler("zork", zork.New(b))
b.AddHandler("rss", rss.New(b)) b.AddHandler("rss", rss.New(b))
b.AddHandler("reaction", reaction.New(b))
// catches anything left, will always return true // catches anything left, will always return true
b.AddHandler("factoid", fact.New(b)) b.AddHandler("factoid", fact.New(b))

View File

@ -0,0 +1,46 @@
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package reaction
import (
"time"
"math/rand"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
)
type ReactionPlugin struct {
Bot bot.Bot
}
func New(bot bot.Bot) *ReactionPlugin {
rand.Seed(time.Now().Unix())
return &ReactionPlugin{
Bot: bot,
}
}
func (p *ReactionPlugin) Message(message msg.Message) bool {
if rand.Intn(100) == 0 {
p.Bot.React(message.Channel, "+1", message)
}
return false
}
func (p *ReactionPlugin) Help(channel string, parts []string) {
}
func (p *ReactionPlugin) Event(kind string, message msg.Message) bool {
return false
}
func (p *ReactionPlugin) BotMessage(message msg.Message) bool {
return false
}
func (p *ReactionPlugin) RegisterWeb() *string {
return nil
}

View File

@ -82,6 +82,12 @@ type slackMessage struct {
} `json:"error"` } `json:"error"`
} }
type slackReaction struct {
Reaction string `json:"name"`
Channel string `json:"channel"`
Timestamp float64 `json:"timestamp"`
}
type rtmStart struct { type rtmStart struct {
Ok bool `json:"ok"` Ok bool `json:"ok"`
Error string `json:"error"` Error string `json:"error"`
@ -131,6 +137,19 @@ func (s *Slack) SendAction(channel, message string) {
s.SendMessageType(channel, "message", "me_message", "_"+message+"_") s.SendMessageType(channel, "message", "me_message", "_"+message+"_")
} }
func (s *Slack) React(channel, reaction string, message msg.Message) {
log.Printf("Reacting in %s: %s", channel, reaction)
resp, err := http.PostForm("https://slack.com/api/reactions.add",
url.Values{ "token": {s.config.Slack.Token},
"name": {reaction},
"channel": {channel},
"timestamp": {message.AdditionalData["RAW_SLACK_TIMESTAMP"]}})
if err != nil {
log.Printf("Error sending Slack reaction: %s", err)
}
log.Print(resp)
}
func (s *Slack) receiveMessage() (slackMessage, error) { func (s *Slack) receiveMessage() (slackMessage, error) {
var msg []byte var msg []byte
m := slackMessage{} m := slackMessage{}
@ -212,6 +231,9 @@ func (s *Slack) buildMessage(m slackMessage) msg.Message {
Action: isAction, Action: isAction,
Host: string(m.Id), Host: string(m.Id),
Time: tstamp, Time: tstamp,
AdditionalData: map[string]string{
"RAW_SLACK_TIMESTAMP": m.Ts,
},
} }
} }