Building Alexa Skills with ASK SDK + Node.js

ngomaitam.khanh

New member
#Alexaskills #asksdk #node.js #voiceinterface #Chatbots

## Giới thiệu

Alexa Kỹ năng là một cách mạnh mẽ để mở rộng chức năng của thiết bị Amazon Echo của bạn.Với các kỹ năng của Alexa, bạn có thể tạo các ứng dụng kích hoạt bằng giọng nói có thể làm bất cứ điều gì từ chơi nhạc đến nhận tin tức.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách xây dựng một kỹ năng Alexa bằng cách sử dụng SDK và Node.js.

## Điều kiện tiên quyết

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Một thiết bị Echo Amazon
* Tài khoản nhà phát triển Amazon
* Node.js được cài đặt trên máy tính của bạn
* Ask SDK cho Node.js

## Bắt đầu

Bước đầu tiên là tạo ra một kỹ năng Alexa mới trong bảng điều khiển nhà phát triển Amazon.Để làm điều này, hãy truy cập [Cổng thông tin nhà phát triển Alexa Kỹ năng] (https://developer.amazon.com/alexa/console/ask) và nhấp vào nút ** Tạo kỹ năng **.

Trong phần Thông tin kỹ năng, nhập tên cho kỹ năng của bạn và một mô tả ngắn.Bạn cũng có thể chọn một danh mục cho kỹ năng của bạn.

Trong phần Tên gọi, nhập cụm từ mà người dùng sẽ sử dụng để kích hoạt kỹ năng của bạn.Ví dụ: bạn có thể nhập "Alexa, mở kỹ năng của tôi".

Nhấp vào nút ** Tạo kỹ năng ** để tạo kỹ năng của bạn.

## Xây dựng kỹ năng

Bây giờ bạn đã tạo ra kỹ năng của mình, bạn có thể bắt đầu xây dựng nó.Bước đầu tiên là tạo một dự án Node.js mới.Bạn có thể làm điều này bằng cách chạy lệnh sau trong thiết bị đầu cuối của bạn:

`` `
NPM init
`` `

Điều này sẽ tạo ra một thư mục mới gọi là `my-skill`.Bên trong thư mục này, hãy tạo một tệp mới có tên là `index.js`.Tệp này sẽ chứa mã cho kỹ năng của bạn.

## Thêm SDK Ask Ask

Bước tiếp theo là thêm SDK Ask vào dự án của bạn.Bạn có thể làm điều này bằng cách chạy lệnh sau:

`` `
NPM Cài đặt Ask-SDK
`` `

Điều này sẽ cài đặt Ask SDK cho Node.js.

## Tạo trình xử lý kỹ năng

Bước tiếp theo là tạo bộ xử lý kỹ năng.Trình xử lý kỹ năng là điểm vào chính cho kỹ năng của bạn.Nó chịu trách nhiệm xử lý các yêu cầu từ Alexa Voice Service (AVS).

Để tạo trình xử lý kỹ năng, hãy thêm mã sau vào tệp `index.js` của bạn:

`` `
const {SkillClient, SkillBuilders} = Yêu cầu ('Ask-sdk');

const skillBuilder = skillBuilders.newskillBuilder ();

const skillHandler = skillBuilder.AddRequestHandlers (
[
{
CanHandle (HandlerInput) {
return HandlerInput.Request.Type === 'LaunchRequest';
},
Xử lý (HandlerInput) {
Trả về HandlerInput.ResponseBuilder
.Speak ('Chào mừng đến với kỹ năng của tôi!')
.nhận được phản ứng();
},
},
{
CanHandle (HandlerInput) {
return HandlerInput.Request.Type === 'IntentRequest' &&
HandlerInput.Request.Intent.Name === 'MyInTent';
},
Xử lý (HandlerInput) {
Trả về HandlerInput.ResponseBuilder
.Speak ('Bạn đã kích hoạt ý định của tôi!')
.nhận được phản ứng();
},
},
]
).xây dựng();

const Skill = new SkillClient ({skillid: 'your_skill_id'});

Kỹ năng.RegisterHandlers (SkillHandler);

Kỹ năng.start ();
`` `

Mã này tạo ra một người xây dựng kỹ năng mới và thêm hai trình xử lý yêu cầu vào nó.Trình xử lý yêu cầu đầu tiên xử lý sự kiện `LaunchRequest`, được kích hoạt khi người dùng lần đầu tiên mở kỹ năng của bạn.Trình xử lý yêu cầu thứ hai xử lý sự kiện `IntentRequest`, được kích hoạt khi người dùng gọi kỹ năng của bạn với một ý định cụ thể.

## Kiểm tra kỹ năng

Bây giờ bạn đã tạo kỹ năng của mình, bạn có thể kiểm tra nó bằng trình giả lập Alexa.Để làm điều này, hãy truy cập [Cổng thông tin nhà phát triển Kỹ năng Alexa] (https://developer.amazon.com/alexa/console/ask) và nhấp vào tab ** test **.

Trong tab kiểm tra, bạn có thể nhập một cụm từ để kích hoạt kỹ năng của mình và
=======================================
#Alexaskills #asksdk #node.js #voiceinterface #Chatbots

## Introduction

Alexa Skills are a powerful way to extend the functionality of your Amazon Echo device. With Alexa Skills, you can create voice-activated applications that can do anything from playing music to getting the news. In this tutorial, we will show you how to build an Alexa Skill using the Ask SDK and Node.js.

## Prerequisites

To follow this tutorial, you will need the following:

* An Amazon Echo device
* An Amazon Developer account
* Node.js installed on your computer
* The Ask SDK for Node.js

## Getting Started

The first step is to create a new Alexa Skill in the Amazon Developer Console. To do this, go to the [Alexa Skills Kit Developer Portal](https://developer.amazon.com/alexa/console/ask) and click the **Create Skill** button.

In the Skill Information section, enter a name for your skill and a short description. You can also choose a category for your skill.

In the Invocation Name section, enter the phrase that users will use to activate your skill. For example, you could enter "Alexa, open my skill".

Click the **Create Skill** button to create your skill.

## Building the Skill

Now that you have created your skill, you can start building it. The first step is to create a new Node.js project. You can do this by running the following command in your terminal:

```
npm init
```

This will create a new directory called `my-skill`. Inside this directory, create a new file called `index.js`. This file will contain the code for your skill.

## Adding the Ask SDK

The next step is to add the Ask SDK to your project. You can do this by running the following command:

```
npm install ask-sdk
```

This will install the Ask SDK for Node.js.

## Creating the Skill Handler

The next step is to create the skill handler. The skill handler is the main entry point for your skill. It is responsible for handling requests from the Alexa Voice Service (AVS).

To create the skill handler, add the following code to your `index.js` file:

```
const {SkillsClient, SkillBuilders} = require('ask-sdk');

const skillBuilder = SkillBuilders.newSkillBuilder();

const skillHandler = skillBuilder.addRequestHandlers(
[
{
canHandle(handlerInput) {
return handlerInput.request.type === 'LaunchRequest';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak('Welcome to my skill!')
.getResponse();
},
},
{
canHandle(handlerInput) {
return handlerInput.request.type === 'IntentRequest' &&
handlerInput.request.intent.name === 'MyIntent';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak('You have triggered my intent!')
.getResponse();
},
},
]
).build();

const skill = new SkillsClient({skillId: 'YOUR_SKILL_ID'});

skill.registerHandlers(skillHandler);

skill.start();
```

This code creates a new skill builder and adds two request handlers to it. The first request handler handles the `LaunchRequest` event, which is triggered when the user first opens your skill. The second request handler handles the `IntentRequest` event, which is triggered when the user invokes your skill with a specific intent.

## Testing the Skill

Now that you have created your skill, you can test it using the Alexa Simulator. To do this, go to the [Alexa Skills Kit Developer Portal](https://developer.amazon.com/alexa/console/ask) and click the **Test** tab.

In the Test tab, you can enter a phrase to activate your skill and
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock