Pkg.go.dev is a new destination for Go discovery & docs. Check it out at pkg.go.dev/github.com/jprobinson/dialogflow and share your feedback.

package dialogflow

import "github.com/jprobinson/dialogflow"

Package dialogflow is a framework for building services that respond to DialogFlow fulfillment webhooks.

auth.go dialogflow.go service.go

❖ func Run(svc FulfillmentService, audience string) error

Run will register a service with kit and run the server. Call this in your main function.

The service will register the webhook on the path "/fulfillment" so make sure to configure your fulfillment webhook to point at something like: https://example.appspot.com/fulfillment

❖ type EventInput struct { Name string `json:"name"` LanguageCode string `json:"languageCode"` Parameters map[string]interface{} `json:"parameters"` }

EventInput will hold all inbound input information.

❖ type FulfillmentMessage struct { Text []string `json:"text"` }

FulfillmentMessage is the most basic way to respond to intents.

❖ type FulfillmentPayload struct { Google RichFulfillmentPayload `json:"google"` FulfillmentMessage `json:"facebook"` Slack FulfillmentMessage `json:"slack"` }

FulfillmentPayload allows users to respond differently to alternate platforms.

❖ type FulfillmentResponse struct { FulfillmentText string `json:"fulfillmentText"` FulfillmentMessages []FulfillmentMessage `json:"fulfillmentMessages"` Source string `json:"source"` Payload *FulfillmentPayload `json:"payload"` OutputContexts []OutputContext `json:"outputContexts"` FollowupEventInput *EventInput `json:"followupEventInput"` }

FulfillmentResponse contains all the fields required for responding to a Dialogflow intent. For more information about how to fill this struct, see the documentation at: https://dialogflow.com/docs/fulfillment/how-it-works.

❖ type FulfillmentService interface { // Intents returns a mapping of intent display name IntentHandler. Intents () map[string]IntentHandler // Middleware can be used for things like adding generic responses or // authentication. Middleware (endpoint.Endpoint) endpoint.Endpoint }

FulfillmentService returns a IntentHandler for each intent in their DialogFlow.

❖ type Intent struct { Name string `json:"name"` DisplayName string `json:"displayName"` }

Intent contains the name that matched the user's query.

❖ type IntentHandler func(context.Context, *Request) (*FulfillmentResponse, error)

IntentHandler encapsulates the logic for a single action for a Dialogflow intent. For more information about the request and response, see the DialogFlow documentation for fulfillment: https://dialogflow.com/docs/fulfillment/how-it-works

❖ type OutputContext struct { Name string `json:"name"` LifespanCount int `json:"lifespanCount"` Parameters map[string]interface{} `json:"parameters"` }

OutputContext holds information for output contexts within fulfillment requests and responses.

❖ type QueryResult struct { LanguageCode string `json:"languageCode"` QueryText string `json:"queryText"` FulfillmentText string `json:"fulfillmentText"` Action string `json:"action"` AllRequiredParamsPresent bool `json:"allRequiredParamsPresent"` Parameters map[string]interface{} `json:"parameters"` OutputContexts []OutputContext `json:"outputContexts"` Intent Intent `json:"intent"` FulfillmentMessages []struct { Text struct { Text []string `json:"text"` } `json:"text"` } `json:"fulfillmentMessages"` IntentDetectionConfidence float64 `json:"intentDetectionConfidence"` DiagnosticInfo interface{} `json:"diagnosticInfo"` }

QueryResult contains the result of the conversation query or event processing.

❖ type Request struct { ResponseID string `json:"responseId"` Session string `json:"session"` QueryResult QueryResult `json:"queryResult"` OriginalDetectIntentRequest struct { Source string `json:"source"` Version string `json:"version"` Payload struct { IsInSandbox bool `json:"isInSandbox"` Surface struct { Capabilities []interface{} `json:"capabilities"` } `json:"surface"` Inputs []interface{} `json:"inputs"` User struct { UserID string `json:"userId"` IDToken string `json:"idToken"` Email string `json:"email"` Name string `json:"name"` GivenName string `json:"given_name"` FamilyName string `json:"family_name"` Locale string `json:"locale"` LastSeen string `json:"lastSeen"` } `json:"user"` Conversation interface{} `json:"conversation"` AvailableSurfaces []interface{} `json:"availableSurfaces"` } `json:"payload"` } `json:"originalDetectIntentRequest"` }

Request contains all the inbound information from a Dialogflow fulfillment request. The QueryResult field contains most of the needed information. For more information about how to fill this struct, see the documentation at: https://dialogflow.com/docs/fulfillment/how-it-works.

GetParam is a convenience method for getting a parameter from QueryResult.Parameters. If the parameter does not exist, an error will be returned.

GetStringParam is a convenience method for getting a parameter from QueryResult.Parameters. If the parameter does not exist or is not a string, an error will be returned.

❖ type RichFulfillmentPayload struct { ExpectUserResponse bool `json:"expectUserResponse"` RichResponse struct { Items []interface{} `json:"items"` } `json:"richResponse"` }

RichFulfillmentPayload can be used for rich responses to Google Actions.