summaryrefslogtreecommitdiff
path: root/handler.go
diff options
context:
space:
mode:
Diffstat (limited to 'handler.go')
-rw-r--r--handler.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/handler.go b/handler.go
new file mode 100644
index 0000000..e0d40b8
--- /dev/null
+++ b/handler.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+ "encoding/json"
+ "log"
+ "net/http"
+)
+
+func IndexHandler(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(400)
+ w.Write([]byte("Wrong URL."))
+}
+
+func WebhookFailureHandler(w http.ResponseWriter, r *http.Request) {
+ log.Println("Getting not-Post request on /webhook")
+ w.WriteHeader(400)
+ w.Write([]byte("Accepts only POST requests."))
+}
+
+func WebhookHandler(w http.ResponseWriter, r *http.Request) {
+ decoder := json.NewDecoder(r.Body)
+ var hook Webhook
+ err := decoder.Decode(&hook)
+ if err != nil {
+ log.Print(err)
+ w.WriteHeader(400)
+ w.Write([]byte("Wrong JSON."))
+ return
+ }
+ if hook.Join != "" {
+ con.Join("#" + hook.Join)
+ con.Privmsg("#"+hook.Join, hook.Message)
+ con.Part("#" + hook.Join)
+ if hook.Target != "" {
+ con.Privmsg(hook.Target, hook.Message)
+ }
+ } else {
+ con.Privmsg(hook.Target, hook.Message)
+ }
+}