package module
Version: v0.0.0-...-d933019 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2017 License: Apache-2.0 Imports: 6 Imported by: 2


GoDoc Build Status Go Report Card


This package provides an easy way to handle webhook requests for api.ai fulfillments.

You can find more in the fulfillment docs.

This is totally experimental for now, so please file let me know what you think about it. File issues!


This is a simple example fo an application handling a single intent called number, which it simply doubles as a result.

package main

import (


func main() {
	h := apiai.NewHandler()
	h.Register("double", doubleHandler)
	log.Fatal(http.ListenAndServe("", h))

func doubleHandler(ctx context.Context, req *apiai.Request) (*apiai.Response, error) {
	num, err := strconv.Atoi(req.Param("number"))
	if err != nil {
		return nil, fmt.Errorf("could not parse number %q: %v", req.Param("number"), err)
	return &apiai.Response{
		Speech: fmt.Sprintf("%d times two equals %d", num, 2*num),
	}, nil

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.



Package apiai provides an easy way to handle webhooks coming from api.ai, as described in the documentation: https://api.ai/docs/fulfillment



This section is empty.


This section is empty.


func HTTPRequest

func HTTPRequest(ctx context.Context) *http.Request

HTTPRequest returns the HTTP request associated to the given context or nil.


type Handler

type Handler struct {
	// contains filtered or unexported fields

Handler provides an easy way to route and handle requests by intent.

func NewHandler

func NewHandler() *Handler

NewHandler returns a new empty handler.

func (*Handler) Register

func (h *Handler) Register(intent string, handler IntentHandler)

Register registers the handler for a given intent.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type IntentHandler

type IntentHandler func(ctx context.Context, req *Request) (*Response, error)

IntentHandler handles an intent.

type Request

type Request struct {
	Lang   string `json:"lang"`
	Status struct {
		ErrorType string `json:"errorType"`
		Code      int    `json:"code"`
	} `json:"status"`
	Timestamp time.Time `json:"timestamp"`
	SessionID string    `json:"sessionId"`
	Result    struct {
		Parameters    map[string]string `json:"parameters"`
		Contexts      []struct{}        `json:"contexts"` // TODO
		ResolvedQuery string            `json:"resolvedQuery"`
		Source        string            `json:"source"`
		Score         float64           `json:"score"`
		Speech        string            `json:"speech"`
		Fulfillment   struct {
			Messages []struct {
			} `json:"messages"`
			Speech string `json:"speech"`
		} `json:"fulfillment"`
		ActionIncomplete bool   `json:"actionIncomplete"`
		Action           string `json:"action"`
		Metadata         struct {
			IntentID                  string `json:"intentId"`
			WebhookForSlotFillingUsed bool   `json:"webhookForSlotFillingUsed,string"`
			IntentName                string `json:"intentName"`
			WebhookUsed               bool   `json:"webhookUsed,string"`
		} `json:"metadata"`
	} `json:"result"`
	ID              string `json:"id"`
	OriginalRequest struct {
		Source string                 `json:"source"`
		Data   map[string]interface{} `json:"data"` // TODO
	} `json:"originalRequest"`

A Request contains all of the information to an intent invocation.

func (*Request) Param

func (req *Request) Param(name string) string

Param returns the value associated to the given parameter name.

type Response

type Response struct {
	Speech      string `json:"speech"`
	DisplayText string `json:"displayText"`

A Response is what an intent responds after an invokation.

Source Files


Path Synopsis
This example shows how to create a simple chat bot that reads a number from the requests and doubles it.
This example shows how to create a simple chat bot that reads a number from the requests and doubles it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL