Parse incoming messages to see if we can annoy eaburns with emoji word replacements

This commit is contained in:
skkiesel 2017-07-24 15:09:27 -04:00
parent 8728d96639
commit c6e1769f0f
6 changed files with 151 additions and 1 deletions

View File

@ -65,6 +65,10 @@ func (b *bot) React(channel, reaction string, message msg.Message) {
b.conn.React(channel, reaction, message) b.conn.React(channel, reaction, message)
} }
func (b *bot) GetEmojiList() map[string]string {
return b.conn.GetEmojiList()
}
// 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

@ -23,6 +23,7 @@ type Bot interface {
Filter(msg.Message, string) string Filter(msg.Message, string) string
LastMessage(string) (msg.Message, error) LastMessage(string) (msg.Message, error)
CheckAdmin(string) bool CheckAdmin(string) bool
GetEmojiList() map[string]string
} }
type Connector interface { type Connector interface {
@ -32,6 +33,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) React(string, string, msg.Message)
GetEmojiList() map[string]string
Serve() Serve()
Who(string) []string Who(string) []string

View File

@ -102,6 +102,11 @@ func (i *Irc) React(channel, reaction string, message msg.Message) {
//we're not goign to do anything because it's IRC //we're not goign to do anything because it's IRC
} }
func (i *Irc) GetEmojiList() map[string]string {
//we're not goign to do anything because it's IRC
return make(map[string]string)
}
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

@ -14,6 +14,7 @@ import (
"github.com/velour/catbase/plugins/beers" "github.com/velour/catbase/plugins/beers"
"github.com/velour/catbase/plugins/counter" "github.com/velour/catbase/plugins/counter"
"github.com/velour/catbase/plugins/dice" "github.com/velour/catbase/plugins/dice"
"github.com/velour/catbase/plugins/emojifyme"
"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/reaction"
@ -62,6 +63,7 @@ func main() {
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)) b.AddHandler("reaction", reaction.New(b))
b.AddHandler("emojifyme", emojifyme.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,102 @@
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package emojifyme
import (
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strings"
"time"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
)
type EmojifyMePlugin struct {
Bot bot.Bot
GotBotEmoji bool
Emoji map[string]string
}
func New(bot bot.Bot) *EmojifyMePlugin {
rand.Seed(time.Now().Unix())
resp, err := http.Get("https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json")
if err != nil {
log.Fatalf("Error generic emoji list: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatalf("Error generic emoji list body: %s", err)
}
type Emoji struct {
Aliases []string `json:aliases`
}
var emoji []Emoji
err = json.Unmarshal(body, &emoji)
if err != nil {
log.Fatalf("Error parsing emoji list: %s", err)
}
emojiMap := map[string]string{}
for _, e := range emoji {
for _, alias := range e.Aliases {
emojiMap[alias] = alias
}
}
return &EmojifyMePlugin{
Bot: bot,
GotBotEmoji: false,
Emoji: emojiMap,
}
}
func (p *EmojifyMePlugin) Message(message msg.Message) bool {
if !p.GotBotEmoji {
p.GotBotEmoji = true
emojiMap := p.Bot.GetEmojiList()
for e := range emojiMap {
p.Emoji[e] = e
}
}
if rand.Intn(10) == 0 {
tokens := strings.Fields(strings.ToLower(message.Body))
sendMessage := false
for i, token := range tokens {
if _, ok := p.Emoji[token]; ok {
sendMessage = true
tokens[i] = ":" + token + ":"
}
}
if sendMessage {
modified := strings.Join(tokens, " ")
p.Bot.SendMessage(message.Channel, modified)
return true
}
}
return false
}
func (p *EmojifyMePlugin) Help(channel string, parts []string) {
}
func (p *EmojifyMePlugin) Event(kind string, message msg.Message) bool {
return false
}
func (p *EmojifyMePlugin) BotMessage(message msg.Message) bool {
return false
}
func (p *EmojifyMePlugin) RegisterWeb() *string {
return nil
}

View File

@ -34,6 +34,8 @@ type Slack struct {
users map[string]string users map[string]string
emoji map[string]string
eventReceived func(msg.Message) eventReceived func(msg.Message)
messageReceived func(msg.Message) messageReceived func(msg.Message)
} }
@ -101,6 +103,7 @@ func New(c *config.Config) *Slack {
return &Slack{ return &Slack{
config: c, config: c,
users: make(map[string]string), users: make(map[string]string),
emoji: make(map[string]string),
} }
} }
@ -150,6 +153,37 @@ func (s *Slack) React(channel, reaction string, message msg.Message) {
log.Print(resp) log.Print(resp)
} }
func (s *Slack) GetEmojiList() map[string]string {
return s.emoji
}
func (s *Slack) populateEmojiList() {
resp, err := http.PostForm("https://slack.com/api/emoji.list",
url.Values{ "token": {s.config.Slack.Token}})
if err != nil {
log.Printf("Error retrieving emoji list from Slack: %s", err)
return
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatalf("Error reading Slack API body: %s", err)
}
type EmojiListResponse struct {
OK bool `json:ok`
Emoji map[string]string `json:emoji`
}
var list EmojiListResponse
err = json.Unmarshal(body, &list)
if err != nil {
log.Fatalf("Error parsing emoji list: %s", err)
}
s.emoji = list.Emoji
}
func (s *Slack) receiveMessage() (slackMessage, error) { func (s *Slack) receiveMessage() (slackMessage, error) {
var msg []byte var msg []byte
m := slackMessage{} m := slackMessage{}
@ -166,6 +200,7 @@ func (s *Slack) receiveMessage() (slackMessage, error) {
func (s *Slack) Serve() { func (s *Slack) Serve() {
s.connect() s.connect()
s.populateEmojiList()
for { for {
msg, err := s.receiveMessage() msg, err := s.receiveMessage()
if err != nil && err == io.EOF { if err != nil && err == io.EOF {