DialogFlow, unified backend chatbot interface powered by ai
Calling your backend servers based on front chatbot input
First thing you notice, there is only an SDK for client side Go, (sending user text to DialogFlow and getting the response), and nothing for server side fulfillment.
No worries, everything is standardized
The godoc for the message types can be found here,
But the webhook is called with json, and that it proto
It includes json tags, but the json we're getting is encoded with the json parameter in the protobuf tag, jsonpb to the rescue
1package main
2
3import (
4 "net/http"
5
6 "github.com/golang/protobuf/jsonpb"
7 pb "google.golang.org/genproto/googleapis/cloud/dialogflow/v2"
8)
9
10func main() {
11 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
12 req := &pb.WebhookRequest{}
13 err := jsonpb.Unmarshal(r.Body, req)
14 if err != nil {
15 // handle error
16 }
17 // Do something with req
18
19 res := &pb.&pb.WebhookResponse{FulfillmentText:"I am fulfilled!"}
20
21 m := &jsonpb.Marshaler{}
22 err = m.Marshal(w, res)
23 if err != nil {
24 // handle error
25 }
26 })
27}
28