Merge pull request #68 from velour/emojifyme

Ready... Set... EmojifyMe.
This commit is contained in:
Chris Sexton 2017-07-25 09:51:25 -04:00 committed by GitHub
commit 8c90bde6fa
7 changed files with 155 additions and 3 deletions

View File

@ -65,6 +65,10 @@ func (b *bot) React(channel, reaction string, message msg.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.
func (b *bot) checkHelp(channel string, parts []string) {
if len(parts) == 1 {

View File

@ -23,6 +23,7 @@ type Bot interface {
Filter(msg.Message, string) string
LastMessage(string) (msg.Message, error)
CheckAdmin(string) bool
GetEmojiList() map[string]string
}
type Connector interface {
@ -32,6 +33,7 @@ type Connector interface {
SendMessage(channel, message string)
SendAction(channel, message string)
React(string, string, msg.Message)
GetEmojiList() map[string]string
Serve()
Who(string) []string

View File

@ -38,7 +38,9 @@ func (mb *MockBot) EventReceived(msg msg.Message) {}
func (mb *MockBot) Filter(msg msg.Message, s string) string { return "" }
func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil }
func (mb *MockBot) CheckAdmin(nick string) bool { return false }
func (mb *MockBot) React(string, string, msg.Message) {}
func (mb *MockBot) React(channel, reaction string, message msg.Message) {}
func (mb *MockBot) GetEmojiList() map[string]string { return make(map[string]string) }
func NewMockBot() *MockBot {
db, err := sqlx.Open("sqlite3_custom", ":memory:")

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
}
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() {
if i.eventReceived == nil || i.messageReceived == nil {
log.Fatal("Missing an event handler")

View File

@ -14,6 +14,7 @@ import (
"github.com/velour/catbase/plugins/beers"
"github.com/velour/catbase/plugins/counter"
"github.com/velour/catbase/plugins/dice"
"github.com/velour/catbase/plugins/emojifyme"
"github.com/velour/catbase/plugins/fact"
"github.com/velour/catbase/plugins/leftpad"
"github.com/velour/catbase/plugins/reaction"
@ -62,6 +63,7 @@ func main() {
b.AddHandler("zork", zork.New(b))
b.AddHandler("rss", rss.New(b))
b.AddHandler("reaction", reaction.New(b))
b.AddHandler("emojifyme", emojifyme.New(b))
// catches anything left, will always return true
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
emoji map[string]string
eventReceived func(msg.Message)
messageReceived func(msg.Message)
}
@ -101,6 +103,7 @@ func New(c *config.Config) *Slack {
return &Slack{
config: c,
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)
}
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) {
var msg []byte
m := slackMessage{}
@ -166,6 +200,7 @@ func (s *Slack) receiveMessage() (slackMessage, error) {
func (s *Slack) Serve() {
s.connect()
s.populateEmojiList()
for {
msg, err := s.receiveMessage()
if err != nil && err == io.EOF {