summaryrefslogtreecommitdiff
path: root/handler.go
blob: e0d40b8880bd7e38288f30edd032980b93d8bf14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)
	}
}