summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Gerus <arachnist@i.am-a.cat>2016-06-13 03:17:51 +0200
committerRobert Gerus <arachnist@i.am-a.cat>2016-06-13 03:17:51 +0200
commitefa9bc2e8b3c08d8a8e32fc5518a96904ff09ee4 (patch)
tree5d01d4648002417852842961ff6fc036b41ffe00
parentdc8ec49130991dbf2bb2bfc63bddcb87eb577b62 (diff)
downloadgorepost-efa9bc2e8b3c08d8a8e32fc5518a96904ff09ee4.tar.gz
gorepost-efa9bc2e8b3c08d8a8e32fc5518a96904ff09ee4.tar.bz2
gorepost-efa9bc2e8b3c08d8a8e32fc5518a96904ff09ee4.zip
Hello, this is frog.
-rw-r--r--bot/frog.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/bot/frog.go b/bot/frog.go
new file mode 100644
index 0000000..ea5afbd
--- /dev/null
+++ b/bot/frog.go
@@ -0,0 +1,51 @@
+// Copyright 2015 Robert S. Gerus. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package bot
+
+import (
+ "encoding/json"
+ "fmt"
+ "math/rand"
+ "strings"
+
+ "github.com/arachnist/gorepost/irc"
+)
+
+type tip struct {
+ Number int `json:"number"`
+ Tip string `json:"tip"`
+}
+
+type tips struct {
+ Tips []tip `json:"tips"`
+}
+
+func frog(output func(irc.Message), msg irc.Message) {
+ var values tips
+
+ if strings.Split(msg.Trailing, " ")[0] != ":frog" {
+ return
+ }
+
+ data, err := httpGet("http://frog.tips/api/1/tips/")
+ if err != nil {
+ output(reply(msg, fmt.Sprint("error:", err)))
+ return
+ }
+
+ err = json.Unmarshal(data, &values)
+ if err != nil {
+ output(reply(msg, fmt.Sprint("error:", err)))
+ return
+ }
+
+ tip := values.Tips[rand.Intn(len(values.Tips))]
+
+ output(reply(msg, fmt.Sprintf("frog tip #%d: %s", tip.Number, tip.Tip)))
+}
+
+func init() {
+ addCallback("PRIVMSG", "frog", frog)
+}