Alice Skills Examples
repository·master·Indexed 41 days ago
https://github.com/yandex/alice-skillsPractical examples and templates for developing skills for Alice, Yandex's voice assistant, using the Yandex Dialogs platform. Includes implementations in Node.js and Python for deployment on Vercel, Yandex Cloud Functions, Google Cloud, and Azure, featuring webhook examples using Express and Flask.
What's inside Alice Skills
- This repository contains examples of using Yandex Dialogs to create skills for Alice, Yandex's voice assistant. For instructions on how to deploy and publish these skills, refer to the official Yandex Dialogs documentation.
Deploy a Python Alice Skill to Vercel
masterTo deploy a Python-based Alice skill using Vercel, follow the official deployment instructions provided in the Yandex Dialogs documentation. This project serves as a Python example for creating Alice skills.Deploy a Python Alice Skill
masterTo deploy a Python-based Alice skill, follow the official deployment instructions provided in the Yandex Dialogs documentation. This repository provides a Python example (buy-elephant) that can be used as a template for building your own skill.Deploy a Node.js Alice Skill to Vercel
masterTo deploy a Node.js-based Alice skill using Vercel, follow the official deployment instructions provided in the Yandex Dialogs documentation. This project serves as a Node.js example for creating Alice skills.Deploy the Morning Show skill template to Yandex Cloud Functions
masterThis template is designed to be deployed as a Yandex Cloud Function using a Python 3.7 runtime. The skill provides content for the 'Morning Show' based on the current date, pulling data from a static CSV file. If no content is found for the current date, it returns a random entry from the file.
To deploy this template, follow these steps:
- Prepare Content: Add your desired content to the
data/data.csvfile. - Create Cloud Function: Create a new cloud function in the Yandex Cloud console.
- Upload Code: Copy all files from the
show-templatedirectory into the function editor. - Set Runtime: Ensure the runtime environment is set to
python/3.7. - Configure Entry Point: Set the function's entry point method to
skill.handle_dialog_yacloud. - Deploy Version: Create a new version of the function.
- Register Skill: Create a new skill on the Yandex Dialogs platform and set the Backend to the Yandex Cloud function you just created.
- Prepare Content: Add your desired content to the
Handle Alice Skill dialog logic
masterThe core logic of an Alice Skill is typically implemented in a handler function (e.g.,
handle_dialog(req, res)) that processes the incoming request and modifies the response object in place.Common logic patterns include:
- New User Detection: Check
req['session']['new']to initialize user-specific state or provide a welcome message. - Intent Recognition: Inspect
req['request']['original_utterance']to determine user intent (e.g., checking if the user said 'ладно' or 'куплю'). - Session Management: Use a storage mechanism (like a dictionary or database) keyed by
req['session']['user_id']to maintain state across turns. - Response Construction: Set
res['response']['text']for speech andres['response']['buttons']for UI suggestions.
- New User Detection: Check
Implement an Alice Skill endpoint with Express
masterTo deploy a parrot-style Alice skill to Google Cloud, use an Express.js server that listens for POST requests on the root path (
/). The server must parse JSON bodies and respond with a specific JSON structure containingversion,session, andresponse(which includestextandend_session).This implementation demonstrates how to handle different command scenarios to test session and version persistence:
no text: Returns an empty string fortext.no version: Omits theversionfield from the response.no session: Omits thesessionfield from the response.end session: Setsend_session: truein the response.- Default: Echoes the command text back to the user and keeps the session active.
const express = require('express'); const port = process.env.PORT || 3000; const app = express(); app.use(express.json()); app.post('/', function (req, res) { // Logic to handle req.body.request.command and return Alice-compatible JSON if (req.body.request.command == "no text") { res.json({ version: req.body.version, session: req.body.session, response: { text: "", end_session: false, }, }); } else { // ... other command handlers } }); app.listen(port);Implement an Alice Skill webhook with Flask
masterTo deploy an Alice Skill as a web service (e.g., on Google Cloud), you can use a Flask application to handle incoming POST requests from Yandex. The webhook must receive a JSON payload containing the
version,session, andrequestobjects, and return a JSON response containing theversion,session, and aresponseobject.Key requirements for the response object:
version: Must match the version received in the request.session: Must match the session received in the request.response: Containstext(the speech output),buttons(suggested replies), andend_session(boolean to close the session).
from flask import Flask, request import json app = Flask(__name__) @app.route("/", methods=['POST']) def main(): req_data = request.json response = { "version": req_data['version'], "session": req_data['session'], "response": { "end_session": False } } # Logic to populate response['response'] goes here return json.dumps(response, ensure_ascii=False, indent=2)Implement an Alice Skill webhook using Flask
masterTo deploy an Alice skill to a web service like Azure, you can use a Flask application to handle incoming POST requests from Yandex. The webhook must receive a JSON payload containing the
version,session, andrequestobjects, and return a JSON response containing theversion,session, andresponseobjects.Key requirements for the response structure:
version: Must match the version received in the request.session: Must match the session received in the request.response: Must containtext(the speech response) and optionallybuttonsorend_session.
from flask import Flask, request import json app = Flask(__name__) @app.route("/", methods=['POST']) def main(): # request.json contains the Alice request payload request_data = request.json response = { "version": request_data['version'], "session": request_data['session'], "response": { "end_session": False } } # Logic to populate response['response'] goes here # handle_dialog(request_data, response) return json.dumps(response, ensure_ascii=False, indent=2)Deploy a Parrot skill to Vercel/Now
masterThis entrypoint is designed for deploying an Alice skill as an asynchronous serverless function on Vercel (formerly Now). It uses the
micropackage to handle JSON parsing.When the function is invoked, it extracts
request,session, andversionfrom the incoming JSON request. It then returns a response containing the sameversionandsessionto maintain the dialogue state, along with aresponseobject.To prevent the dialogue from closing immediately, set
response.end_sessiontofalse. To respond to the user, useresponse.text(e.g., echoing therequest.original_utterance).const { json } = require('micro'); module.exports = async (req, res) => { const { request, session, version } = await json(req); res.end(JSON.stringify({ version, session, response: { text: request.original_utterance || 'Hello!', end_session: false, }, })); };Implement the Alice skill handler for Universal Translator
masterThe
handler(event, context)function serves as the entrypoint for the Universal Translator skill when deployed to Yandex Cloud. It processes incoming Alice requests and manages the translation state.Input Structure
The
eventobject is expected to follow the Yandex Dialogs request format, specifically requiring:event['request']['nlu']['intents']: A dictionary of detected intents (e.g.,translate_main,translate_ellipsis,exit,YANDEX.HELP,YANDEX.REPEAT).event['request']['command']: The raw command string.event['state']['session']['translate']: A dictionary containing the current translation state (text,lang_from,lang_to).event['state']['session']['last_phrase']: The text of the last response sent to the user.
Output Structure
The handler must return a dictionary compatible with the Yandex Dialogs response format:
{ "version": "string", "session": { ... }, "response": { "text": "string", "end_session": "true" | "false" }, "session_state": { "translate": { "text": "...", "lang_from": "...", "lang_to": "..." }, "last_phrase": "string" } }def handler(event, context): # ... implementation logic ... return { 'version': event['version'], 'session': event['session'], 'response': { 'text': text, 'end_session': end_session }, 'session_state': {'translate': translate_state, 'last_phrase': text} }Manage translation state with do_translate
masterThe
do_translate(form, translate_state)function handles the core logic of extracting translation parameters from an intent and performing the translation.Parameters
form: The intent object containingslots. Expected slots arephrase(the text to translate),from(source language), andto(target language).translate_state: A dictionary representing the persistent session state used to accumulate missing parameters across multiple turns (ellipses).
Logic Flow
- Extraction: It pulls
phrase,from, andtofrom theform['slots']. - Language Normalization: It uses
lang_to_codeto convert language names into ISO-like codes. - State Accumulation: It updates
translate_statewith any newly provided information. - Validation:
- If
textis missing, it asks for the text. - If the text is Russian-like but
lang_tois missing, it asks for the target language. - If the text is not Russian-like but
lang_fromis missing, it asks for the source language.
- If
- Execution: Once all parameters are present, it calls
translate(**translate_state).
Returns
Returns a tuple of
(response_text, updated_translate_state).def do_translate(form, translate_state): # ... logic to extract slots and update state ... tran_error, tran_result = translate(**translate_state) text = tran_error or tran_result return text, translate_state