From a14a2b2e617f66ac548e0af070459264e028fd65 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 25 Mar 2016 12:25:00 -0400 Subject: [PATCH] Yup. Leftpad. --- main.go | 2 + plugins/leftpad/leftpad.go | 92 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 plugins/leftpad/leftpad.go diff --git a/main.go b/main.go index 681362b..69b04d9 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "github.com/velour/catbase/plugins/dice" "github.com/velour/catbase/plugins/downtime" "github.com/velour/catbase/plugins/fact" + "github.com/velour/catbase/plugins/leftpad" "github.com/velour/catbase/plugins/talker" "github.com/velour/catbase/plugins/your" "github.com/velour/catbase/slack" @@ -43,6 +44,7 @@ func main() { // b.AddHandler(plugins.NewTestPlugin(b)) b.AddHandler("admin", admin.NewAdminPlugin(b)) // b.AddHandler("first", plugins.NewFirstPlugin(b)) + b.AddHandler("leftpad", leftpad.New(b)) b.AddHandler("downtime", downtime.NewDowntimePlugin(b)) b.AddHandler("talker", talker.NewTalkerPlugin(b)) b.AddHandler("dice", dice.NewDicePlugin(b)) diff --git a/plugins/leftpad/leftpad.go b/plugins/leftpad/leftpad.go new file mode 100644 index 0000000..533f6a8 --- /dev/null +++ b/plugins/leftpad/leftpad.go @@ -0,0 +1,92 @@ +// © 2016 the CatBase Authors under the WTFPL license. See AUTHORS for the list of authors. + +// Leftpad contains the plugin that allows the bot to pad messages +package leftpad + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "strings" + + "github.com/velour/catbase/bot" +) + +type LeftpadPlugin struct { + bot *bot.Bot +} + +// New creates a new LeftpadPlugin with the Plugin interface +func New(bot *bot.Bot) *LeftpadPlugin { + p := LeftpadPlugin{ + bot: bot, + } + return &p +} + +type leftpadResp struct { + Str string +} + +func (p *LeftpadPlugin) Message(message bot.Message) bool { + if !message.Command { + return false + } + + parts := strings.Split(message.Body, " ") + if len(parts) > 3 && parts[0] == "leftpad" { + padchar := parts[1] + length := parts[2] + text := parts[3:][0] + url := fmt.Sprintf("https://api.left-pad.io/?str=%s&len=%s&ch=%s", + text, + length, + padchar, + ) + log.Printf("Requesting leftpad url: %s", url) + resp, err := http.Get(url) + if err != nil { + p.bot.SendMessage(message.Channel, err.Error()) + return true + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + p.bot.SendMessage(message.Channel, "I can't leftpad right now :(") + log.Printf("Error decoding leftpad: %s", err) + return true + } + r := leftpadResp{} + err = json.Unmarshal(body, &r) + if err != nil { + p.bot.SendMessage(message.Channel, "I can't leftpad right now :(") + log.Printf("Error decoding leftpad: %s", err) + return true + } + p.bot.SendMessage(message.Channel, r.Str) + return true + } + + return false +} + +func (p *LeftpadPlugin) Event(e string, message bot.Message) bool { + return false +} + +func (p *LeftpadPlugin) BotMessage(message bot.Message) bool { + return false +} + +func (p *LeftpadPlugin) LoadData() { +} + +func (p *LeftpadPlugin) Help(e string, m []string) { +} + +func (p *LeftpadPlugin) RegisterWeb() *string { + // nothing to register + return nil +}