flokati/modules/webhook.go

57 lines
1.2 KiB
Go

// vi:ts=4:sts=4:sw=4:noet:tw=72
package modules
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"
"git.dnix.de/an/xlog"
)
var (
webhookPort = flag.String("webhook_port", "8080", "Webhook listener port")
webhookToken = flag.String("webhook_token", "token.txt", "Token file")
)
func init() {
MsgFuncs["webhook"] = webhookHandleMessage
RunFuncs["webhook"] = webhookRun
}
func webhookRun() {
file, err := os.Open(*webhookToken)
if err != nil {
xlog.Fatal(err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
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))
}
func webhookHandleMessage(m *Message) {
}
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
}
SayCh <- fmt.Sprintf("*%s*: %s\n%v", data["username"], data["channel"], data["text"])
for k, v := range data {
xlog.Debug("%s : %v\n", k, v)
}
}