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 ¶
- type Entity
- type EntityTokens
- type EntityType
- type Handler
- type Printf
- type Request
- type RequestMarkup
- type RequestMeta
- type RequestNLU
- type RequestPayload
- type RequestPayloadType
- type RequestSession
- type Responder
- type Response
- type ResponseButton
- type ResponseCard
- type ResponseCardButton
- type ResponseCardFooter
- type ResponseCardHeader
- type ResponseCardItem
- type ResponseCardItemsList
- type ResponseCardType
- type ResponsePayload
- type ResponseSession
- type YandexDateTime
- type YandexFio
- type YandexGeo
- type YandexNumber
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) YandexNumber ¶
func (e *Entity) YandexNumber() *YandexNumber
YandexNumber extracts integer or float number from entity, or nil.
type EntityTokens ¶
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 ¶
NewHandler creates new handler with given responder and default timeout (3s). Exported fields of the returned object can be changed before usage.
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 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 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 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 ResponseCardFooter ¶
type ResponseCardFooter struct {}
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"` }
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 YandexGeo ¶
type YandexGeo struct { Country string City string Street string HouseNumber string Airport string }
YandexGeo represents extracted address.
type YandexNumber ¶
YandexNumber represents extracted integer or float number.