ailistener-old

The central Server which transports webhook events from the Dialogflow bot to the local nodes
git clone git://archive.git.mtrnord.blog/OpenHomeAuto/ailistener-old.git
Log | Files | Refs | README | LICENSE

commit a4b759ac01d67c2fb8ca56a33ac9933aaf0fe170
parent f06760d01fdbad9025de89e6aaffc37effe1a8cd
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sat,  2 Jun 2018 10:20:25 +0200

Add most basic webhook handler

Diffstat:
Mgo.mod | 5+++++
Mmain.go | 4+++-
AwsListener/server.go | 34++++++++++++++++++++++++++++++++++
3 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/go.mod b/go.mod @@ -1 +1,6 @@ module github.com/OpenHomeAuto/AiListener + +require ( + github.com/gorilla/mux v1.6.2 + github.com/leboncoin/dialogflow-go-webhook v1.0.0 +) diff --git a/main.go b/main.go @@ -1,5 +1,7 @@ package main -func main() { +import "github.com/OpenHomeAuto/AiListener/wsListener" +func main() { + wsListener.Main() } diff --git a/wsListener/server.go b/wsListener/server.go @@ -0,0 +1,34 @@ +package wsListener + +import ( + "encoding/json" + "fmt" + "github.com/gorilla/mux" + df "github.com/leboncoin/dialogflow-go-webhook" + "log" + "net/http" +) + +func Main() { + r := mux.NewRouter() + r.HandleFunc("/", HomeHandler).Methods("GET") + r.HandleFunc("/dialogflow", DFHandler).Methods("POST") + http.ListenAndServe(":80", r) +} + +func HomeHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, "Home") +} + +func DFHandler(w http.ResponseWriter, r *http.Request) { + var dfr *df.Request + + decoder := json.NewDecoder(r.Body) + err := decoder.Decode(dfr) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + w.WriteHeader(http.StatusAccepted) + log.Printf("%v", dfr) +}