yandex-dialogs-sdk

Note: this is an open-source project. It is not affiliated with Yandex LLC.
Tiny zen library to create skills for Yandex.Alice
yandex-dialogs-sdk — Telegram chat, if you need help
Install SDK
npm i yandex-dialogs-sdk --save
To enable debug mode run DEBUG=yandex-dialogs-sdk node YOUR_APP.js
Videotutorials
Getting Started
const { Alice, Reply, Markup } = require('yandex-dialogs-sdk')
const alice = new Alice();
const M = Markup;
alice.command('', async ctx => Reply.text('Look, what i can!'));
alice.command('Give a piece of advice', async ctx =>
Reply.text('Make const not var'),
);
alice.command(
['What is trending now?', 'Watch films', 'Whats in the theatre?'],
ctx => {
return {
text: `What about 50 Angry Men?`,
buttons: [M.button('Buy ticket'), M.button('What else?')],
};
},
);
alice.command(/(https?:\/\/[^\s]+)/g, ctx => Reply.text('Matched a link!'));
alice.any(async ctx => Reply.text(`I don't understand`));
const server = alice.listen(3001, '/');
Handle non-trivial scenarios
const { Alice, Scene, Stage } = require('yandex-dialogs-sdk')
const stage = new Stage();
const alice = new Alice();
const SCENE_AT_BAR = 'SCENE_AT_BAR';
const atBar = new Scene(SCENE_AT_BAR);
atBar.command('show menu', ctx =>
Reply.text('only vodka here', {
buttons: ['buy vodka', 'go away'],
}),
);
atBar.command('buy vodka', ctx => Reply.text(`you're dead`));
atBar.command('go away', ctx => {
ctx.leave();
return Reply.text('as you want');
});
atBar.any(ctx => Reply.text(`no money no honey`));
stage.addScene(atBar);
alice.use(stage.getMiddleware());
alice.command('i want some drinks', ctx => {
ctx.enter(SCENE_AT_BAR);
return Reply.text('lets go into pub', {
buttons: ['show menu', 'go away'],
});
});
A lot of examples in folder ./examples
API
Alice
const { Alice } = require('yandex-dialogs-sdk')
alice.command - Set handler for command
alice.command('text', ctx => null)
alice.command(/regex/ig, ctx => null)
alice.command(['array', 'of', 'strings'], ctx => null)
alice.command(ctx => true || false, ctx => null)
Images Api
To use this API you have to provide your auth data.
More info
const alice = new Alice({
oAuthToken: OAUTH_TOKEN,
skillId: SKILL_ID
});
alice.imagesApi.uploadImageByUrl - Upload image by URL
const image = await alice.imagesApi.uploadImageByUrl(IMAGE_URL);
alice.imagesApi.uploadImageFile - Upload image by File Buffer (Not implemented yet).
alice.imagesApi.getImages - Get all uploaded images
const images = await alice.imagesApi.getImages();
Context
[ctx.data] - object with request
[ctx.message] — shortcut for ctx.data.request.command
[ctx.originalUtterance] - shortcut for ctx.data.request.original_utterance
[ctx.sessionId] — shortcut for ctx.data.session.session_id
[ctx.messageId] — shortcut for ctx.data.session.message_id
[ctx.userId] — shortcut for ctx.data.session.user_id
[ctx.payload] — shortcut for ctx.data.request.payload
[ctx.enter] - enters session
const { Scene } = require('yandex-dialogs-sdk')
const scene = new Scene('scene-name')
ctx.enter('scene-name')
[ctx.leave] - goes to main dialog
const { Scene } = require('yandex-dialogs-sdk')
const scene = new Scene('scene-name')
ctx.leave()
enter/leave example
Stage
const { Stage } = require('yandex-dialogs-sdk')
const stage = new Stage()
stage.addScene - adds scene to stage
stage.removeScene - removes scene from stage
stage.getMiddleware - returns stage middleware
full scene example
Reply
const { Reply } = require('yandex-dialogs-sdk')
IMAGE_ID = '213044/d13b0d86a41daf9de232'
EXTRA_PARAMS = {
tts: 'Hi the+re',
buttons: ['one', Markup.button('two')],
end_session: true
}
alice.any(ctx => Reply.text('text'), EXTRA_PARAMS)
Reply.bigImageCard - One big image
Reply.bigImageCard('text', {
image_id: IMAGE_ID,
title: string,
description: string,
button: M.button('click'),
}, EXTRA_PARAMS)
Reply.itemsListCard - Gallery
Reply.itemsListCard('text', [IMAGE_ID, IMAGE_ID], EXTRA_PARAMS);
Reply.itemsListCard('test', {
header: 'header',
footer: {
text: 'test',
button: Markup.button('button'),
},
items: [
IMAGE_ID,
{ image_id: IMAGE_ID, title: 'title', description: 'description' },
],
});
Markup
const { Markup } = require('yandex-dialogs-sdk')
const M = Markup
M.button('string')
M.button({
title: string;
url: string;
payload: object;
hide: boolean;
})
🔨 Built with SDK
CONTRIBUTING
git clone
npm install && npm run test && npm run dev
Typescript will be compiled into ./dist
Contributors
Thanks all these awesome people for this product.
Phil Romanov © MIT 2018