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 dbda11878b9175b2d6460c3e7d323f7cff9c2ad9
parent 905fcc7c3812190fd35246413ceaed5fcf509fb3
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sat,  2 Jun 2018 15:02:36 +0200

Convert resp to latin1 encoding

Diffstat:
Mgo.mod | 1+
MwsListener/server.go | 20+++++++++++++++++++-
2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/go.mod b/go.mod @@ -1,6 +1,7 @@ module github.com/OpenHomeAuto/AiListener require ( + github.com/bjarneh/latinx v0.0.0-20120329061922-4dfe9ba2a293 github.com/gorilla/mux v1.6.2 github.com/leboncoin/dialogflow-go-webhook v1.0.0 ) diff --git a/wsListener/server.go b/wsListener/server.go @@ -3,6 +3,7 @@ package wsListener import ( "encoding/json" "fmt" + "github.com/bjarneh/latinx" "github.com/gorilla/mux" df "github.com/leboncoin/dialogflow-go-webhook" "log" @@ -45,9 +46,26 @@ func DFHandler(w http.ResponseWriter, r *http.Request) { return } + // fetch converter for desired charset + converter := latinx.Get(latinx.ISO_8859_1) + + // encode a UTF-8 stream as ISO_8859_1 + dffb, err := json.Marshal(dff) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + log.Println("[WSERROR] failed to marshal json: " + err.Error()) + return + } + latin1resp, _, err := converter.Encode(dffb) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + log.Println("[WSERROR] failed to convert json to latin1: " + err.Error()) + return + } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(dff) + w.Write(latin1resp) default: w.WriteHeader(http.StatusOK) }