server.go (4887B)
1 package wsListener 2 3 import ( 4 "fmt" 5 df "github.com/OpenHomeAuto/dialogflow-go-webhook" 6 "github.com/bjarneh/latinx" 7 "github.com/gorilla/mux" 8 "github.com/pquerna/ffjson/ffjson" 9 "io/ioutil" 10 "log" 11 "net/http" 12 "time" 13 ) 14 15 func Main() { 16 r := mux.NewRouter() 17 r.HandleFunc("/", HomeHandler).Methods("GET") 18 r.HandleFunc("/dialogflow", DFHandler).Methods("POST") 19 http.ListenAndServe(":400", r) 20 } 21 22 func HomeHandler(w http.ResponseWriter, r *http.Request) { 23 w.WriteHeader(http.StatusOK) 24 fmt.Fprintf(w, "Home") 25 } 26 27 func DFHandler(w http.ResponseWriter, r *http.Request) { 28 start := time.Now() 29 30 dfr := &df.Request{} 31 32 if r.Body == nil { 33 http.Error(w, "Please send a request body", http.StatusNotAcceptable) 34 log.Println("[WSERROR] request body missing") 35 return 36 } 37 defer r.Body.Close() 38 body, err := ioutil.ReadAll(r.Body) 39 if err != nil { 40 http.Error(w, err.Error(), http.StatusInternalServerError) 41 log.Println("[WSERROR] failed to convert to byte: " + err.Error()) 42 return 43 } 44 err = dfr.UnmarshalJSON(body) 45 if err != nil { 46 http.Error(w, err.Error(), http.StatusInternalServerError) 47 log.Println("[WSERROR] failed to UnmarshalJSON: " + err.Error()) 48 return 49 } 50 51 switch dfr.QueryResult.Action { 52 case "play_music": 53 dff := playMusicDispatch(dfr, w) 54 if dff == nil { 55 log.Println("[WSERROR] empty dff at play_music ") 56 return 57 } 58 59 // fetch converter for desired charset 60 converter := latinx.Get(latinx.ISO_8859_1) 61 62 // encode a UTF-8 stream as ISO_8859_1 63 dffb, err := ffjson.Marshal(dff) 64 if err != nil { 65 http.Error(w, err.Error(), http.StatusInternalServerError) 66 log.Println("[WSERROR] failed to marshal json: " + err.Error()) 67 return 68 } 69 latin1resp, _, err := converter.Encode(dffb) 70 if err != nil { 71 http.Error(w, err.Error(), http.StatusInternalServerError) 72 log.Println("[WSERROR] failed to convert json to latin1: " + err.Error()) 73 return 74 } 75 76 w.Header().Set("Content-Type", "application/json") 77 w.WriteHeader(http.StatusOK) 78 w.Write(latin1resp) 79 elapsed := time.Since(start) 80 log.Printf("Took %s", elapsed) 81 return 82 default: 83 w.WriteHeader(http.StatusNotImplemented) 84 log.Println("[WSINFO] default resp") 85 elapsed := time.Since(start) 86 log.Printf("Took %s", elapsed) 87 return 88 } 89 90 } 91 92 func playMusicDispatch(dfr *df.Request, w http.ResponseWriter) *df.Fulfillment { 93 start := time.Now() 94 type MusicParams struct { 95 Artist string `json:"music-artist"` 96 Room string `json:"room"` 97 Genre string `json:"MusikGenre"` 98 Title string `json:"title"` 99 } 100 101 var p MusicParams 102 var resp *df.Fulfillment 103 104 // Retrieve the params of the request 105 if err := dfr.GetParams(&p); err != nil { 106 http.Error(w, err.Error(), http.StatusInternalServerError) 107 log.Println("[WSERROR] failed to get parmas: " + err.Error()) 108 return nil 109 } 110 log.Printf("REQUEST: %+v\n", dfr.QueryResult) 111 log.Printf("PARAMS: %+v\n", p) 112 113 if p.Artist != "" { 114 // Send back a fulfillment 115 resp = &df.Fulfillment{ 116 FulfillmentMessages: df.Messages{ 117 df.ForGoogle(df.SingleSimpleResponse("Musik wird gestartet!", "Musik wird gestartet!")), 118 df.ForGoogle( 119 &df.BasicCard{ 120 Title: fmt.Sprintf("Musik von %s", p.Artist), 121 Subtitle: "Künstler", 122 FormattedText: "Musik wird gestartet...", 123 Image: nil, 124 Buttons: nil, 125 }, 126 ), 127 {RichMessage: df.Text{Text: []string{"Musik wird gestartet!"}}}, 128 }, 129 } 130 } else if p.Genre != "" { 131 // Send back a fulfillment 132 resp = &df.Fulfillment{ 133 FulfillmentMessages: df.Messages{ 134 df.ForGoogle(df.SingleSimpleResponse("Musik wird gestartet!", "Musik wird gestartet!")), 135 df.ForGoogle( 136 &df.BasicCard{ 137 Title: fmt.Sprintf("%s", p.Genre), 138 Subtitle: "Genre", 139 FormattedText: "Musik wird gestartet...", 140 Image: nil, 141 Buttons: nil, 142 }, 143 ), 144 {RichMessage: df.Text{Text: []string{"Musik wird gestartet!"}}}, 145 }, 146 } 147 } else if p.Title != "" { 148 // Send back a fulfillment 149 resp = &df.Fulfillment{ 150 FulfillmentMessages: df.Messages{ 151 df.ForGoogle(df.SingleSimpleResponse(fmt.Sprintf("%s wird gestartet...", p.Title), fmt.Sprintf("%s wird gestartet", p.Title))), 152 df.ForGoogle( 153 &df.BasicCard{ 154 Title: fmt.Sprintf("%s", p.Title), 155 Subtitle: "Song", 156 FormattedText: fmt.Sprintf("%s wird gestartet...", p.Title), 157 Image: nil, 158 Buttons: nil, 159 }, 160 ), 161 {RichMessage: df.Text{Text: []string{fmt.Sprintf("%s wird gestartet...", p.Title)}}}, 162 }, 163 } 164 } else { 165 // Send back a fulfillment 166 resp = &df.Fulfillment{ 167 FulfillmentMessages: df.Messages{ 168 df.ForGoogle(df.SingleSimpleResponse("Musik wird gestartet!", "Musik wird gestartet!")), 169 {RichMessage: df.Text{Text: []string{"Musik wird gestartet!"}}}, 170 }, 171 } 172 } 173 elapsed := time.Since(start) 174 log.Printf("playMusicDispatch Took %s", elapsed) 175 176 return resp 177 }