alice

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2019 License: MIT Imports: 8 Imported by: 0

README

alice

Actions Codecov Go Report Card GoDoc

Package alice provides helpers for developing skills for Alice virtual assistant via Yandex.Dialogs platform.

Documentation

Overview

Package alice provides helpers for developing skills for Alice virtual assistant via Yandex.Dialogs platform.

See https://alice.yandex.ru for general information about Alice. See https://tech.yandex.ru/dialogs/alice/ for information about Yandex.Dialogs platform. See https://tech.yandex.ru/dialogs/alice/doc/ for technical documentation.

Example

See more examples in "examples" directory.

Example
package main

import (
	"context"
	"log"
	"net/http"

	"github.com/AlekSi/alice"
)

func main() {
	h := alice.NewHandler(func(ctx context.Context, request *alice.Request) (*alice.ResponsePayload, error) {
		return &alice.ResponsePayload{
			Text:       "Bye!",
			EndSession: true,
		}, nil
	})

	h.Errorf = log.Printf
	http.Handle("/", h)

	const addr = "127.0.0.1:8080"
	log.Printf("Listening on http://%s ...", addr)
	log.Fatal(http.ListenAndServe(addr, nil))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entity

type Entity struct {
	Tokens EntityTokens `json:"tokens"`
	Type   EntityType   `json:"type"`
	Value  interface{}  `json:"value"`
}

Entity represents NLU-extracted named entity. See https://yandex.ru/dev/dialogs/alice/doc/nlu-docpage/.

func (*Entity) YandexDateTime

func (e *Entity) YandexDateTime() *YandexDateTime

YandexDateTime extracts date and/or time from entity, or nil.

func (*Entity) YandexFio

func (e *Entity) YandexFio() *YandexFio

YandexFio extracts full name from entity, or nil.

func (*Entity) YandexGeo

func (e *Entity) YandexGeo() *YandexGeo

YandexGeo extracts address from entity, or nil.

func (*Entity) YandexNumber

func (e *Entity) YandexNumber() *YandexNumber

YandexNumber extracts integer or float number from entity, or nil.

type EntityTokens

type EntityTokens struct {
	Start int `json:"start"`
	End   int `json:"end"`
}

EntityTokens represents the place of extracted named entity in tokens slice.

type EntityType

type EntityType string

EntityType represents entity type.

const (
	EntityYandexFio      EntityType = "YANDEX.FIO"
	EntityYandexGeo      EntityType = "YANDEX.GEO"
	EntityYandexDateTime EntityType = "YANDEX.DATETIME"
	EntityYandexNumber   EntityType = "YANDEX.NUMBER"
)

Entity types.

type Handler

type Handler struct {
	Timeout time.Duration // responder's timeout
	Errorf  Printf        // error logger

	// debugging options
	Debugf        Printf // debug logger
	Indent        bool   // indent requests and responses
	StrictDecoder bool   // disallow unexpected fields in requests
	// contains filtered or unexported fields
}

Handler accepts Yandex.Dialogs requests, decodes them, handles "ping" requests itself, and delegates other requests to responder.

func NewHandler

func NewHandler(r Responder) *Handler

NewHandler creates new handler with given responder and default timeout (3s). Exported fields of the returned object can be changed before usage.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler interface.

type Printf

type Printf func(format string, a ...interface{})

Printf is a log.Printf-like function that can be used for logging.

type Request

type Request struct {
	Version string         `json:"version"`
	Meta    RequestMeta    `json:"meta"`
	Request RequestPayload `json:"request"`
	Session RequestSession `json:"session"`
}

https://tech.yandex.ru/dialogs/alice/doc/protocol-docpage/#request

type RequestMarkup

type RequestMarkup struct {
	DangerousContext bool `json:"dangerous_context"`
}

type RequestMeta

type RequestMeta struct {
	Locale     string                 `json:"locale"`
	Timezone   string                 `json:"timezone"`
	ClientID   string                 `json:"client_id"`
	Interfaces map[string]interface{} `json:"interfaces"`
}

func (RequestMeta) HasScreen

func (m RequestMeta) HasScreen() bool

HasScreen returns true if user's device has screen.

type RequestNLU

type RequestNLU struct {
	Tokens   []string `json:"tokens"`
	Entities []Entity `json:"entities"`
}

type RequestPayload

type RequestPayload struct {
	Command           string             `json:"command"`
	OriginalUtterance string             `json:"original_utterance"`
	Type              RequestPayloadType `json:"type"`
	Markup            RequestMarkup      `json:"markup"`
	Payload           interface{}        `json:"payload,omitempty"`
	NLU               RequestNLU         `json:"nlu"`
}

type RequestPayloadType

type RequestPayloadType string
const (
	SimpleUtterance RequestPayloadType = "SimpleUtterance"
	ButtonPressed   RequestPayloadType = "ButtonPressed"
)

type RequestSession

type RequestSession struct {
	New       bool   `json:"new"`
	MessageID int    `json:"message_id"`
	SessionID string `json:"session_id"`
	SkillID   string `json:"skill_id"`
	UserID    string `json:"user_id"`
}

type Responder

type Responder func(ctx context.Context, request *Request) (*ResponsePayload, error)

Responder is a function that should be implemented to handle Yandex.Dialogs requests.

Passed context is derived from HTTP request's context with added handler's timeout. It is canceled when the request is canceled (see https://golang.org/pkg/net/http/#Request.Context) or on timeout.

Only response payload can be returned; other response fields (session, version) will be set automatically. If error is returned, it is logged with error logger, and 500 Internal server error is sent in response.

type Response

type Response struct {
	Response ResponsePayload `json:"response"`
	Session  ResponseSession `json:"session"`
	Version  string          `json:"version"`
}

Response represents main response object. See https://tech.yandex.ru/dialogs/alice/doc/protocol-docpage/#response

type ResponseButton

type ResponseButton struct {
	Title   string      `json:"title"`
	Payload interface{} `json:"payload,omitempty"`
	URL     string      `json:"url,omitempty"`
	Hide    bool        `json:"hide,omitempty"`
}

type ResponseCard

type ResponseCard struct {
	Type ResponseCardType `json:"type"`

	// For BigImage type.
	*ResponseCardItem `json:",omitempty"`

	// For ItemsList type.
	*ResponseCardItemsList `json:",omitempty"`
}

ResponseCard contains response card.

type ResponseCardButton

type ResponseCardButton struct {
	Text    string      `json:"text"`
	URL     string      `json:"url,omitempty"`
	Payload interface{} `json:"payload,omitempty"`
}

type ResponseCardFooter

type ResponseCardFooter struct {
	Text   string              `json:"text"`
	Button *ResponseCardButton `json:"button,omitempty"`
}

type ResponseCardHeader

type ResponseCardHeader struct {
	Text string `json:"text"`
}

type ResponseCardItem

type ResponseCardItem struct {
	ImageID     string              `json:"image_id,omitempty"`
	Title       string              `json:"title,omitempty"`
	Description string              `json:"description,omitempty"`
	Button      *ResponseCardButton `json:"button,omitempty"`
}

type ResponseCardItemsList

type ResponseCardItemsList struct {
	Header *ResponseCardHeader `json:"header,omitempty"`
	Items  []ResponseCardItem  `json:"items,omitempty"`
	Footer *ResponseCardFooter `json:"footer,omitempty"`
}

type ResponseCardType

type ResponseCardType string
const (
	BigImage  ResponseCardType = "BigImage"
	ItemsList ResponseCardType = "ItemsList"
)

type ResponsePayload

type ResponsePayload struct {
	Text       string           `json:"text"`
	Tts        string           `json:"tts,omitempty"`
	Card       *ResponseCard    `json:"card,omitempty"`
	Buttons    []ResponseButton `json:"buttons,omitempty"`
	EndSession bool             `json:"end_session"`
}

ResponsePayload contains response payload.

type ResponseSession

type ResponseSession struct {
	SessionID string `json:"session_id"`
	MessageID int    `json:"message_id"`
	UserID    string `json:"user_id"`
}

ResponseSession contains response session.

type YandexDateTime

type YandexDateTime struct {
	Year   int
	Month  int
	Day    int
	Hour   int
	Minute int

	YearIsRelative   bool
	MonthIsRelative  bool
	DayIsRelative    bool
	HourIsRelative   bool
	MinuteIsRelative bool
}

YandexDateTime represents extracted date and/or time.

type YandexFio

type YandexFio struct {
	FirstName      string
	PatronymicName string
	LastName       string
}

YandexFio represents extracted full name.

type YandexGeo

type YandexGeo struct {
	Country     string
	City        string
	Street      string
	HouseNumber string
	Airport     string
}

YandexGeo represents extracted address.

type YandexNumber

type YandexNumber struct {
	json.Number
}

YandexNumber represents extracted integer or float number.

Directories

Path Synopsis
Package speaker provides various sounds and effects.
Package speaker provides various sounds and effects.

Jump to

Keyboard shortcuts

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