Creating a skill for Alice and a website with authorization
In this tutorial, you will develop a skill for Alice
The project's data is stored in a Yandex Object Storage bucket and Yandex Managed Service for YDB database. Yandex Cloud Functions process requests; Yandex API Gateway enables service interaction.
To deploy a project:
- Get your cloud ready.
- Set up your environment.
- Create resources.
- Set the project variables.
- Deploy the project.
- Register a skill for Alice.
- Test the skill.
If you no longer need the resources you created, delete them.
Get your cloud ready
Sign up in Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or register a new account. - On the Yandex Cloud Billing
page, make sure you have a linked billing account with anACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders.
Required paid resources
The infrastructure support costs include:
- Fee for the number of function calls, computing resources allocated for the functions, and outbound traffic (see Yandex Cloud Functions pricing).
- Fee for YDB operations and data storage (see Yandex Managed Service for YDB pricing).
- Fee for storing data in a bucket (see Yandex Object Storage pricing).
- Fee for the number of requests to the API gateway and outbound traffic (see Yandex API Gateway pricing).
- Fee for logging operations and data storage in a log group (see Yandex Cloud Logging pricing).
Set up your environment
- Install Windows Subsystem for Linux (WSL)
to use Linux. - Run WSL (Ubuntu by default).
- Configure the environment as described in this guide for Linux.
Note
If you are using a distribution other than Ubuntu, install the specified software using your package manager.
-
Clone the yc-serverless-alice-shareable-todolist
repository:git clone https://github.com/yandex-cloud-examples/yc-serverless-alice-shareable-todolist.git
-
Install and configure the following:
-
Optionally, to finalize the project, additionally install:
Create resources
Create a bucket
Create a bucket to store static data in:
- Go to the Yandex Cloud management console
and select the folder where you will perform the operations. - On the folder page, click Create resource and select Bucket.
- In the ** Name** field, enter a name for the bucket consistent with the naming conventions:
- In the Object read access, Object listing access, and Read access to settings fields, select
Restricted
. - Specify the maximum size of the bucket in GB.
- Click Create bucket.
Create an API gateway
Create an API gateway for interaction between services:
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select API Gateway.
- Click Create API gateway.
- In the Name field, enter
gate-1
. - Click Create.
- Wait for the API gateway status to change to
Active
. - Select the created API gateway.
- Find the ID and Default domain fields and save their values. You will need them to configure the skill.
Create a database
Create a YDB database in Serverless mode:
-
In the management console
, select the folder where you created the bucket. -
Select Managed Service for YDB.
-
Click Create a database.
-
Specify a Name for the database. Follow these naming requirements:
- It must be from 2 to 63 characters long.
- It can only contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
-
Under Database type, select
Serverless
. -
Click Create a database.
-
Wait for the database status to change to
Running
. -
Select the created database.
-
Under Connection, find the Endpoint field and save its value. You will need it when creating functions.
Create an app
Create
-
Go to the Yandex OAuth website
and log in. -
Click Create app.
-
Specify the appropriate app name and upload an icon.
-
Under Platforms, select
Web services
and click Save and continue. -
Under Platforms, specify the following URIs in the Redirect URI field:
https://social.yandex.net/broker/redirect
<API_gateway_service_domain>/receive-token
.
Leave the Suggest Hostname field blank.
-
Click Save and continue.
-
Under Permissions, select
Access to user avatar
and click Save and continue. -
Under Email address, specify your email address and click Save and continue.
-
Verify the data and click Everything is correct. Create the app.
You can read more about the service features in the Yandex.OAuth documentation
Set the project variables
Configure the project using the values you got when creating your resources.
Create the secrets file
Create a file named secure-config.json
with secrets:
-
To create a file from the
secure-config-template.json
template, navigate to theyc-serverless-alice-shareable-todolist
project files folder and run this command:cp secure-config-template.json secure-config.json
-
Open the
secure-config.json
file innano
:nano secure-config.json
-
Substitute these variable values into the file:
oauth_secret
: Password of the app (Client Secret) registered in Yandex OAuth .hash
: Base64-encoded random 64-byte string, e.g.,qrJagO5NVwOj0FeTmgYSwUN+XXkiQJMWifvrklF53wT55q80Xk8vmEB3kxhtpDnA1WDC893Z9Bh6QcqK********
. You can generate a random value in your terminal usingopenssl rand -base64 64 | tr -d '\n'
.block
: Base64-encoded random 32-byte string, e.g.,uwk0duFgn2nYyfu2VzJe+MnWKWQrfKaiZijI********
. You can generate a random value in your terminal usingopenssl rand -base64 32 | tr -d '\n'
.
You can generate random values at generate.plus
.
Create the configuration file
Create a file named variables.json
with project deployment configuration:
-
To create a file from the
variables-template.json
template, navigate to the project files folder and run this command:cp variables-template.json variables.json
-
Open the
variables.json
file innano
:nano variables.json
-
Substitute these variable values into the file:
folder-id
: Cloud folder ID.domain
:gate-1
API gateway service domain withouthttps://
, e.g.,d5dbo25bol8n********.apigw.yandexcloud.net
.oauth-client-id
: ClientID field value of the app registered in Yandex.OAuth .database
: Database location, the second part of the previously saved Endpoint field value (following/?database=
), e.g.,/ru-central1/r1gra875baom********/g5n22e7ejfr1********
.database-endpoint
: First part of the previously saved Endpoint field value (followinggrpcs://
and preceding/?database=
), e.g.,ydb.serverless.yandexcloud.net:2135
.yc-profile
: Yandex Cloud CLI profile name.secure-config-path
: Path to thesecure-config.json
secrets file.storage-bucket
: Name of the bucket you created for storing static data.gateway-id
: API gatewaygate-1
ID.
Deploy your project
Transfer the project files to Yandex Cloud and update the configuration.
Apply a data schema
To create tables in the database, navigate to the project files folder and run this command:
./upload_ydb_schema.sh
Get an OAuth token
Get an OAuth token:
- Go to Yandex.OAuth
. Before issuing a token, the service may request data access. - Save the new token: you will need it for code uploading.
Upload the backend code to Cloud Functions
Use Terraform to automate your operations. Before you start, initialize it.
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
-
Run the following command in the folder with the
app.tf
configuration file:terraform init
Note
If you encounter a
permission denied
error during Terraform provider initialization, run thesudo chown $(whoami) ~/yc-serverless-alice-shareable-todolist
command to edit current user's access permissions for the project folder. -
After successful initialization, create resources by providing the OAuth token for Yandex Cloud authorization:
terraform apply -var-file ./variables.json -var yc-token=<OAuth_token>
As a result, Terraform will automatically create or update the required resources.
Upload the frontend code to Object Storage
To deploy the frontend web app, build the static files and upload them to Object Storage.
-
Before compiling static files, make sure you have Node.js and the npm package manager installed.
-
Navigate to the
yc-serverless-alice-shareable-todolist/frontend
directory and run the following command:npm run build
If during the compilation you encounter the
-
ERR_OSSL_EVP_UNSUPPORTED
error, create aNODE_OPTIONS
variable:export NODE_OPTIONS=--openssl-legacy-provider
-
npm ERR! Could not resolve dependency
, run the command:npm install --legacy-peer-deps && npm install --force
Result:
> todolist@0.1.0 build > react-scripts build Creating an optimized production build... Compiled successfully. File sizes after gzip: 75.93 KB build/static/js/2.84be0fca.chunk.js 23.26 KB build/static/css/2.ef9168ec.chunk.css 2.63 KB build/static/js/main.d9e069c9.chunk.js 776 B build/static/js/runtime-main.676997b0.js 402 B build/static/css/main.e5cbab88.chunk.css The project was built assuming it is hosted at /. You can control this with the homepage field in your package.json. The build folder is ready to be deployed. You may serve it with a static server: npm install -g serve serve -s build
-
-
To upload the files to Object Storage, run this command:
cd ../ ./upload_static.sh
Result:
./upload_static.sh upload: frontend/build/robots.txt to s3://frontent-statics/robots.txt upload: frontend/build/manifest.json to s3://frontent-statics/manifest.json upload: frontend/build/static/css/main.e5cbab88.chunk.css.map to s3://frontent-statics/static/css/main.e5cbab88.chunk.css.map upload: frontend/build/index.html to s3://frontent-statics/index.html upload: frontend/build/asset-manifest.json to s3://frontent-statics/asset-manifest.json upload: frontend/build/static/js/2.84be0fca.chunk.js.LICENSE.txt to s3://frontent-statics/static/js/2.84be0fca.chunk.js.LICENSE.txt upload: frontend/build/static/css/main.e5cbab88.chunk.css to s3://frontent-statics/static/css/main.e5cbab88.chunk.css upload: frontend/build/static/js/main.d9e069c9.chunk.js to s3://frontent-statics/static/js/main.d9e069c9.chunk.js upload: frontend/build/static/js/2.84be0fca.chunk.js to s3://frontent-statics/static/js/2.84be0fca.chunk.js upload: frontend/build/static/js/runtime-main.676997b0.js to s3://frontent-statics/static/js/runtime-main.676997b0.js upload: frontend/build/static/js/runtime-main.676997b0.js.map to s3://frontent-statics/static/js/runtime-main.676997b0.js.map upload: frontend/build/static/js/main.d9e069c9.chunk.js.map to s3://frontent-statics/static/js/main.d9e069c9.chunk.js.map upload: frontend/build/static/css/2.ef9168ec.chunk.css to s3://frontent-statics/static/css/2.ef9168ec.chunk.css upload: frontend/build/static/css/2.ef9168ec.chunk.css.map to s3://frontent-statics/static/css/2.ef9168ec.chunk.css.map upload: frontend/build/static/js/2.84be0fca.chunk.js.map to s3://frontent-statics/static/js/2.84be0fca.chunk.js.map
Update the API gateway configuration
To upload the current specification to API Gateway, run this command:
./update_gateway.sh
Result:
done (2s)
id: d5dsj4st2g81********
folder_id: b1g86q4m5vej********
created_at: "2024-10-22T16:01:54.777Z"
name: gate-1
status: ACTIVE
domain: d5dm1lba80md********.i9******.apigw.yandexcloud.net
connectivity:
network_id: enp3srbi9u49********
log_options:
folder_id: b1g86q4m5vej********
execution_timeout: 300s
Register a skill for Alice
Create a dialog
- Go to Yandex Dialogs
and log in to the console. - Click Create dialog and select the
Skill
dialog type. - In the Skill name field, specify
To-do lists
. - Under Backend, enable Yandex Cloud function and from the list select the
todolist-alice
function you created earlier in Cloud Functions. - Enable Use data storage in the skill.
- Fill in the required fields under Publication in folder. Configure the other settings as needed. For example, you can specify various word forms to trigger the skill, select a voice, or set access type.
- Click Save at the bottom of the page.
For more information, see the Yandex Dialogs documentation
Configure authorization on the Alice page
-
Go to the Account linking tab.
-
Enter the following:
- Application ID:
ClientID
field value of the app registered in Yandex.OAuth . - Application secret:
Client secret
field value of the app registered in Yandex.OAuth . - Authorization URL:
https://oauth.yandex.com/authorize
. - Get token URL:
https://oauth.yandex.com/token
. - Refresh token URL:
https://oauth.yandex.com/token
.
- Application ID:
-
Click Save.
To learn more about OAuth 2.0, see RFC 6749
Add intents
-
Go to the Settings tab.
-
Go to the Intents tab and click Create.
-
Add intents for every action possible in the dialog. The intents you make reside are in the project's
yc-serverless-alice-shareable-todolist/intents
folder. -
Enter the following:
- Name: Any name you want displayed in the interface.
- ID: Intent ID equal to the name of the file in the
intents
folder. - Grammar: Grammar text equal to the contents of the file in the
intents
folder.
-
Click Save.
-
To complete creating the dialog, click Submit for moderation on the right side of the page.
To learn more about intents, see Alice's skills
Test the skill
To debug the skill, use the Testing tab in the Yandex Dialogs
Navigate to the Testing tab. You will see a chat with Alice on the left and an interaction log in JSON format on the right.
Dialog example:
Let me help you with your lists!
Hi Alice. Create a Groceries list.
Done, I created a Groceries list.
Add milk to Groceries.
Done, I added "milk" to Groceries.
Add bread.
What list should I add "bread" to?
Groceries.
Done, I added "bread" to Groceries.
Add eggs.
What list should I add "eggs" to?
Groceries.
Done, I added "eggs" to Groceries.
Alice, tell me what's on the Groceries list.
Groceries:
1. Milk
2. Bread
3. Eggs
To start a dialog, use any device or service supported by Alice
In your browser, follow the URL in the Default domain field of your gate-1
API gateway and log in. The My lists page will open. In any of the lists, you can add or remove items and grant other users access to the list.
How to delete the resources you created
To stop paying for the resources you created:
- Delete the bucket.
- Delete the database.
- Delete the functions.
- Delete the API gateway.
- Delete the log group.