go dialogflow fulfillment

Writing DialogFlow fulfillment servers in Go

SEAN K.H. LIAO

go dialogflow fulfillment

Writing DialogFlow fulfillment servers in Go

DialogFlow, unified backend chatbot interface powered by ai

Fulfillment

Calling your backend servers based on front chatbot input

Go

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

Webhook Request Response

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

minimal code

 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