mirror of https://github.com/velour/catbase.git
initial implementation and POC to getting reactions pushed into slack via catbase
This commit is contained in:
parent
e45c335bd3
commit
1bf6997144
|
@ -61,6 +61,10 @@ func (b *bot) SendAction(channel, message string) {
|
|||
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.
|
||||
func (b *bot) checkHelp(channel string, parts []string) {
|
||||
if len(parts) == 1 {
|
||||
|
|
|
@ -17,6 +17,7 @@ type Bot interface {
|
|||
AddHandler(string, Handler)
|
||||
SendMessage(string, string)
|
||||
SendAction(string, string)
|
||||
React(string, string, msg.Message)
|
||||
MsgReceived(msg.Message)
|
||||
EventReceived(msg.Message)
|
||||
Filter(msg.Message, string) string
|
||||
|
@ -30,6 +31,7 @@ type Connector interface {
|
|||
|
||||
SendMessage(channel, message string)
|
||||
SendAction(channel, message string)
|
||||
React(string, string, msg.Message)
|
||||
Serve()
|
||||
|
||||
Who(string) []string
|
||||
|
|
|
@ -19,4 +19,5 @@ type Message struct {
|
|||
Action bool
|
||||
Time time.Time
|
||||
Host string
|
||||
AdditionalData map[string]string
|
||||
}
|
||||
|
|
|
@ -98,6 +98,10 @@ func (i *Irc) SendAction(channel, message string) {
|
|||
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() {
|
||||
if i.eventReceived == nil || i.messageReceived == nil {
|
||||
log.Fatal("Missing an event handler")
|
||||
|
|
2
main.go
2
main.go
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/velour/catbase/plugins/dice"
|
||||
"github.com/velour/catbase/plugins/fact"
|
||||
"github.com/velour/catbase/plugins/leftpad"
|
||||
"github.com/velour/catbase/plugins/reaction"
|
||||
"github.com/velour/catbase/plugins/reminder"
|
||||
"github.com/velour/catbase/plugins/rss"
|
||||
"github.com/velour/catbase/plugins/stats"
|
||||
|
@ -60,6 +61,7 @@ func main() {
|
|||
b.AddHandler("babbler", babbler.New(b))
|
||||
b.AddHandler("zork", zork.New(b))
|
||||
b.AddHandler("rss", rss.New(b))
|
||||
b.AddHandler("reaction", reaction.New(b))
|
||||
// catches anything left, will always return true
|
||||
b.AddHandler("factoid", fact.New(b))
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -82,6 +82,12 @@ type slackMessage struct {
|
|||
} `json:"error"`
|
||||
}
|
||||
|
||||
type slackReaction struct {
|
||||
Reaction string `json:"name"`
|
||||
Channel string `json:"channel"`
|
||||
Timestamp float64 `json:"timestamp"`
|
||||
}
|
||||
|
||||
type rtmStart struct {
|
||||
Ok bool `json:"ok"`
|
||||
Error string `json:"error"`
|
||||
|
@ -131,6 +137,19 @@ func (s *Slack) SendAction(channel, message string) {
|
|||
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) {
|
||||
var msg []byte
|
||||
m := slackMessage{}
|
||||
|
@ -212,6 +231,9 @@ func (s *Slack) buildMessage(m slackMessage) msg.Message {
|
|||
Action: isAction,
|
||||
Host: string(m.Id),
|
||||
Time: tstamp,
|
||||
AdditionalData: map[string]string{
|
||||
"RAW_SLACK_TIMESTAMP": m.Ts,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue