forked from an/flokati
52 lines
1.2 KiB
Go
52 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"
|
|
"strings"
|
|
|
|
"git.dnix.de/an/xlog"
|
|
)
|
|
|
|
var (
|
|
webhookPort = flag.String("webhook_port", "8080", "Webhook listener port")
|
|
webhookChan = flag.String("webhook_chan", "", "Channel to post into")
|
|
webhookToken = flag.String("webhook_token", "token.txt", "Token file")
|
|
)
|
|
|
|
func init() {
|
|
MsgFuncs["webhook"] = webhookHandleMessage
|
|
RunFuncs["webhook"] = webhookRun
|
|
}
|
|
|
|
func webhookRun() {
|
|
scanner := bufio.NewScanner(*webhook_token)
|
|
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\n%v", data["channel"], data["text"])
|
|
for k, v := range data {
|
|
xlog.Debug("%s : %v\n", k, v)
|
|
}
|
|
}
|