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 package modules
import ( import (
"bufio"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
@ -15,6 +16,7 @@ import (
var ( var (
webhookPort = flag.String("webhook_port", "8080", "Webhook listener port") webhookPort = flag.String("webhook_port", "8080", "Webhook listener port")
webhookChan = flag.String("webhook_chan", "", "Channel to post into") webhookChan = flag.String("webhook_chan", "", "Channel to post into")
webhookToken = flag.String("webhook_token", "token.txt", "Token file")
) )
func init() { func init() {
@ -23,23 +25,16 @@ func init() {
} }
func webhookRun() { func webhookRun() {
xlog.Info("webhook listener started") scanner := bufio.NewScanner(*webhook_token)
http.HandleFunc("/webhook", webhookHandleHTTP) for scanner.Scan() {
xlog.Fatal("%v", http.ListenAndServe(":8080", nil)) 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 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) { 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) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
fmt.Println("got webhook payload: ") SayCh <- fmt.Sprintf("%s\n%v", data["channel"], data["text"])
for k, v := range data { for k, v := range data {
fmt.Printf("%s : %v\n", k, v) xlog.Debug("%s : %v\n", k, v)
if k == "text" {
SayCh <- fmt.Sprintf("%s\n%v", *webhookChan, v)
}
} }
} }