apiai: github.com/campoy/apiai Index | Files | Directories

package apiai

import "github.com/campoy/apiai"

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


Package Files


func HTTPRequest Uses

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

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

type Handler Uses

type Handler struct {
    // contains filtered or unexported fields

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

func NewHandler Uses

func NewHandler() *Handler

NewHandler returns a new empty handler.

func (*Handler) Register Uses

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

Register registers the handler for a given intent.

func (*Handler) ServeHTTP Uses

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

type IntentHandler Uses

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

IntentHandler handles an intent.

type Request Uses

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 Uses

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

Param returns the value associated to the given parameter name.

type Response Uses

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

A Response is what an intent responds after an invokation.


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

Package apiai imports 6 packages (graph) and is imported by 1 packages. Updated 2018-02-12. Refresh now. Tools for package owners.