webhooks should work now

This commit is contained in:
an 2018-04-22 15:42:09 +02:00
parent 87857b4509
commit 827167fa46
1 changed files with 13 additions and 21 deletions

View File

@ -3,6 +3,7 @@
package modules
import (
"bufio"
"encoding/json"
"flag"
"fmt"
@ -13,8 +14,9 @@ import (
)
var (
webhookPort = flag.String("webhook_port", "8080", "Webhook listener port")
webhookChan = flag.String("webhook_chan", "", "Channel to post into")
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() {
@ -23,23 +25,16 @@ func init() {
}
func webhookRun() {
xlog.Info("webhook listener started")
http.HandleFunc("/webhook", webhookHandleHTTP)
xlog.Fatal("%v", http.ListenAndServe(":8080", nil))
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) {
tok := strings.Split(m.Text, " ")
if len(tok) < 1 {
return
}
switch tok[0] {
case "!webhook-add":
if len(tok) > 1 {
http.HandleFunc("/webhook/"+tok[1], webhookHandleHTTP)
}
default:
}
}
func webhookHandleHTTP(w http.ResponseWriter, r *http.Request) {
@ -49,11 +44,8 @@ func webhookHandleHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("got webhook payload: ")
SayCh <- fmt.Sprintf("%s\n%v", data["channel"], data["text"])
for k, v := range data {
fmt.Printf("%s : %v\n", k, v)
if k == "text" {
SayCh <- fmt.Sprintf("%s\n%v", *webhookChan, v)
}
xlog.Debug("%s : %v\n", k, v)
}
}