From 34e2404e8bb68d281be77a174c6f7df3393d03b8 Mon Sep 17 00:00:00 2001 From: Scott Kiesel Date: Tue, 31 Oct 2017 06:22:36 -0400 Subject: [PATCH] hacked poc for edits as an animation mechanism --- bot/bot.go | 6 +- bot/handlers.go | 6 ++ bot/interfaces.go | 3 + bot/mock.go | 2 + irc/irc.go | 4 ++ main.go | 2 + plugins/rpgORdie/rpgORdie.go | 61 +++++++++++++++++++ plugins/rpgORdie/rpgORdie_test.go | 4 ++ slack/slack.go | 98 +++++++++++++++++++++++++++---- 9 files changed, 175 insertions(+), 11 deletions(-) create mode 100644 plugins/rpgORdie/rpgORdie.go create mode 100644 plugins/rpgORdie/rpgORdie_test.go diff --git a/bot/bot.go b/bot/bot.go index 873d361..2507586 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -110,6 +110,10 @@ func (b *bot) DB() *sqlx.DB { return b.db } +func (b *bot) Conn() Connector { + return b.conn +} + // Create any tables if necessary based on version of DB // Plugins should create their own tables, these are only for official bot stuff // Note: This does not return an error. Database issues are all fatal at this stage. @@ -145,7 +149,7 @@ func (b *bot) migrateDB() { // Adds a constructed handler to the bots handlers list func (b *bot) AddHandler(name string, h Handler) { - b.plugins[strings.ToLower(name)] = h + b.plugins[name] = h b.pluginOrdering = append(b.pluginOrdering, name) if entry := h.RegisterWeb(); entry != nil { b.httpEndPoints[name] = *entry diff --git a/bot/handlers.go b/bot/handlers.go index e28dd65..9df5896 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -23,6 +23,8 @@ func (b *bot) MsgReceived(msg msg.Message) { // msg := b.buildMessage(client, inMsg) // do need to look up user and fix it + log.Println(msg.User.Name) + if strings.HasPrefix(msg.Body, "help ") && msg.Command { parts := strings.Fields(strings.ToLower(msg.Body)) b.checkHelp(msg.Channel, parts) @@ -65,6 +67,10 @@ func (b *bot) React(channel, reaction string, message msg.Message) { b.conn.React(channel, reaction, message) } +func (b *bot) Edit(channel, newMessage, identifier string) { + b.conn.Edit(channel, newMessage, identifier) +} + func (b *bot) GetEmojiList() map[string]string { return b.conn.GetEmojiList() } diff --git a/bot/interfaces.go b/bot/interfaces.go index 23e1185..d476642 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -13,11 +13,13 @@ type Bot interface { Config() *config.Config DBVersion() int64 DB() *sqlx.DB + Conn() Connector Who(string) []user.User AddHandler(string, Handler) SendMessage(string, string) SendAction(string, string) React(string, string, msg.Message) + Edit(string, string, string) MsgReceived(msg.Message) EventReceived(msg.Message) Filter(msg.Message, string) string @@ -34,6 +36,7 @@ type Connector interface { SendMessage(channel, message string) SendAction(channel, message string) React(string, string, msg.Message) + Edit(string, string, string) GetEmojiList() map[string]string Serve() error diff --git a/bot/mock.go b/bot/mock.go index 6b809f0..997fe5f 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -25,6 +25,7 @@ type MockBot struct { func (mb *MockBot) Config() *config.Config { return &mb.Cfg } func (mb *MockBot) DBVersion() int64 { return 1 } func (mb *MockBot) DB() *sqlx.DB { return mb.db } +func (mb *MockBot) Conn() Connector { return nil } func (mb *MockBot) Who(string) []user.User { return []user.User{} } func (mb *MockBot) AddHandler(name string, f Handler) {} func (mb *MockBot) SendMessage(ch string, msg string) { @@ -40,6 +41,7 @@ func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Mess func (mb *MockBot) CheckAdmin(nick string) bool { return false } func (mb *MockBot) React(channel, reaction string, message msg.Message) {} +func (mb *MockBot) Edit(channel, newMessage, identifier string) {} func (mb *MockBot) GetEmojiList() map[string]string { return make(map[string]string) } func (mb *MockBot) RegisterFilter(s string, f func(string) string) {} diff --git a/irc/irc.go b/irc/irc.go index 5ff2b07..da2a806 100644 --- a/irc/irc.go +++ b/irc/irc.go @@ -103,6 +103,10 @@ func (i *Irc) React(channel, reaction string, message msg.Message) { //we're not goign to do anything because it's IRC } +func (i *Irc) Edit(channel, newMessage, identifier string) { + //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) diff --git a/main.go b/main.go index f576aca..069a27f 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ import ( "github.com/velour/catbase/plugins/leftpad" "github.com/velour/catbase/plugins/reaction" "github.com/velour/catbase/plugins/reminder" + "github.com/velour/catbase/plugins/rpgORdie" "github.com/velour/catbase/plugins/rss" "github.com/velour/catbase/plugins/stats" "github.com/velour/catbase/plugins/talker" @@ -69,6 +70,7 @@ func main() { b.AddHandler("emojifyme", emojifyme.New(b)) b.AddHandler("twitch", twitch.New(b)) b.AddHandler("inventory", inventory.New(b)) + b.AddHandler("rpgORdie", rpgORdie.New(b)) // catches anything left, will always return true b.AddHandler("factoid", fact.New(b)) diff --git a/plugins/rpgORdie/rpgORdie.go b/plugins/rpgORdie/rpgORdie.go new file mode 100644 index 0000000..7201c1b --- /dev/null +++ b/plugins/rpgORdie/rpgORdie.go @@ -0,0 +1,61 @@ +package rpgORdie + +import ( + "strings" + // "log" + "time" + + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/slack" +) + +type RPGPlugin struct { + Bot bot.Bot + Slack *slack.Slack //nasty +} + +func New(b bot.Bot) *RPGPlugin { + return &RPGPlugin{ + Bot: b, + Slack: b.Conn().(*slack.Slack), //oh boy, this is just filthy + } +} + +func (p *RPGPlugin) Message(message msg.Message) bool { + if strings.ToLower(message.Body) == "start rpg" { + p.Bot.SendMessage(message.Channel, "I'll edit this.") + ts := p.Slack.GetLastMessageId() + + time.Sleep(2 * time.Second) + + edited := "" + for i := 0; i <= 5; i++ { + p.Bot.Edit(message.Channel, edited, ts) + edited += ":fire:" + time.Sleep(2 * time.Second) + } + p.Bot.Edit(message.Channel, "HECK YES", ts) + } + return false +} + +func (p *RPGPlugin) LoadData() { + +} + +func (p *RPGPlugin) Help(channel string, parts []string) { + p.Bot.SendMessage(channel, "Go find a walkthrough or something.") +} + +func (p *RPGPlugin) Event(kind string, message msg.Message) bool { + return false +} + +func (p *RPGPlugin) BotMessage(message msg.Message) bool { + return false +} + +func (p *RPGPlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/rpgORdie/rpgORdie_test.go b/plugins/rpgORdie/rpgORdie_test.go new file mode 100644 index 0000000..42db0f4 --- /dev/null +++ b/plugins/rpgORdie/rpgORdie_test.go @@ -0,0 +1,4 @@ +package rpgORdie + +import ( +) diff --git a/slack/slack.go b/slack/slack.go index 5f885bb..22c5c3a 100644 --- a/slack/slack.go +++ b/slack/slack.go @@ -15,7 +15,7 @@ import ( "regexp" "strconv" "strings" - "sync/atomic" + // "sync/atomic" "time" "github.com/velour/catbase/bot" @@ -38,6 +38,8 @@ type Slack struct { emoji map[string]string + lastMessageId string + eventReceived func(msg.Message) messageReceived func(msg.Message) } @@ -160,6 +162,7 @@ func New(c *config.Config) *Slack { lastRecieved: time.Now(), users: make(map[string]string), emoji: make(map[string]string), + lastMessageId: "", } } @@ -172,17 +175,47 @@ func (s *Slack) RegisterMessageReceived(f func(msg.Message)) { } func (s *Slack) SendMessageType(channel, messageType, subType, message string) error { - m := slackMessage{ - ID: atomic.AddUint64(&idCounter, 1), - Type: messageType, - SubType: subType, - Channel: channel, - Text: message, - } - err := websocket.JSON.Send(s.ws, m) + resp, err := http.PostForm("https://slack.com/api/chat.postMessage", + url.Values{"token": {s.config.Slack.Token}, + "channel": {channel}, + "text": {message}, + }) + if err != nil { - log.Printf("Error sending Slack message: %s", err) + log.Printf("Error sending Slack reaction: %s", err) } + + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + log.Fatalf("Error reading Slack API body: %s", err) + } + + log.Println(string(body)) + + type MessageResponse struct { + OK bool `json:ok` + Channel string `json:channel` + Timestamp string `json:ts` + } + + var mr MessageResponse + err = json.Unmarshal(body, &mr) + if err != nil { + log.Fatalf("Error parsing message response: %s", err) + } + + bodyAsString := string(body) + + //what the fuck + if strings.Contains(bodyAsString, "\"ts\":\"") { + mr.Timestamp = strings.Split(strings.Split(bodyAsString, "\"ts\":\"")[1], "\"")[0] + } + + s.lastMessageId = mr.Timestamp + + log.Println(mr) + return err } @@ -209,6 +242,46 @@ func (s *Slack) React(channel, reaction string, message msg.Message) { log.Print(resp) } +func (s* Slack) GetLastMessageId() string { + return s.lastMessageId +} + +func (s* Slack) PrintHistory(channel string, howMany int) { + resp, err := http.PostForm("https://slack.com/api/channels.history", + url.Values{"token": {s.config.Slack.Token}, + "channel": {channel}, + "count": {fmt.Sprintf("%d", howMany)}}) + if err != nil { + log.Printf("Error getting slack history: %s", err) + } + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + log.Fatalf("Error reading Slack API body: %s", err) + } + + log.Println(string(body)) +} + +func (s *Slack) Edit(channel, newMessage, identifier string) { + log.Printf("Editing in (%s) %s: %s", identifier, channel, newMessage) + resp, err := http.PostForm("https://slack.com/api/chat.update", + url.Values{"token": {s.config.Slack.Token}, + "channel": {channel}, + "text": {newMessage}, + "ts": {identifier}}) + if err != nil { + log.Printf("Error sending Slack reaction: %s", err) + } + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + log.Fatalf("Error reading Slack API body: %s", err) + } + + log.Println(string(body)) +} + func (s *Slack) GetEmojiList() map[string]string { return s.emoji } @@ -275,6 +348,11 @@ func (s *Slack) Serve() error { case "message": if !msg.Hidden { m := s.buildMessage(msg) + + log.Println() + log.Println(m) + log.Println() + if m.Time.Before(s.lastRecieved) { log.Printf("Ignoring message: %+v\nlastRecieved: %v msg: %v", msg.ID, s.lastRecieved, m.Time) } else {