flokati/modules/webhook.go

57 lines
1.2 KiB
Go
Raw Normal View History

2018-04-22 09:28:18 +00:00
// vi:ts=4:sts=4:sw=4:noet:tw=72
package modules
import (
2018-04-22 13:42:09 +00:00
"bufio"
2018-04-22 09:28:18 +00:00
"encoding/json"
2018-04-22 09:39:52 +00:00
"flag"
2018-04-22 09:28:18 +00:00
"fmt"
"net/http"
2018-04-22 13:44:35 +00:00
"os"
2018-04-22 12:21:58 +00:00
"strings"
2018-04-22 09:28:18 +00:00
"git.dnix.de/an/xlog"
)
2018-04-22 09:39:52 +00:00
var (
2018-04-22 13:42:09 +00:00
webhookPort = flag.String("webhook_port", "8080", "Webhook listener port")
webhookToken = flag.String("webhook_token", "token.txt", "Token file")
2018-04-22 09:39:52 +00:00
)
2018-04-22 09:28:18 +00:00
func init() {
MsgFuncs["webhook"] = webhookHandleMessage
RunFuncs["webhook"] = webhookRun
}
func webhookRun() {
2018-04-22 13:44:35 +00:00
file, err := os.Open(*webhookToken)
if err != nil {
xlog.Fatal(err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
2018-04-22 13:42:09 +00:00
for scanner.Scan() {
tok := strings.Split(scanner.Text(), " ")
http.HandleFunc("/webhook/"+tok[0], webhookHandleHTTP)
}
xlog.Info("Webhook listener started")
xlog.Fatal("%v", http.ListenAndServe(":"+*webhookPort, nil))
2018-04-22 09:28:18 +00:00
}
2018-04-22 11:54:39 +00:00
func webhookHandleMessage(m *Message) {
}
2018-04-22 11:54:06 +00:00
2018-04-22 09:28:18 +00:00
func webhookHandleHTTP(w http.ResponseWriter, r *http.Request) {
data := make(map[string]interface{})
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2018-04-22 14:24:34 +00:00
SayCh <- fmt.Sprintf("%s\n[%s] %v", data["channel"], data["username"], data["text"])
2018-04-22 09:31:15 +00:00
for k, v := range data {
2018-04-22 13:42:09 +00:00
xlog.Debug("%s : %v\n", k, v)
2018-04-22 09:28:18 +00:00
}
}