Building Alexa Skills with Node.js

dahuongbrown1

New member
#Alexaskills #node.js #echo #Aws #smarthome ## Xây dựng kỹ năng Alexa với Node.js

Alexa Kỹ năng là một cách mạnh mẽ để mở rộng khả 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 ra những trải nghiệm 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 kiểm tra thời tiết.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 với Node.js.

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

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

* Một thiết bị Echo Amazon
* Môi trường phát triển Node.js
* Bộ [Kỹ năng Alexa CLI] (https://developer.amazon.com/en-us/...nodejs/setup-alexa-skills-kit-sdk-nodejs.html)

## Bắt đầu

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 sử dụng lệnh sau:

`` `
NPM init
`` `

Điều này sẽ tạo ra một thư mục mới có tên là `My-skill` với một tệp có tên là` pack.json`.Tệp `pack.json` được sử dụng để xác định các phụ thuộc cho dự án của bạn.

Tiếp theo, bạn cần cài đặt Bộ Kỹ năng Alexa CLI.Bạn có thể làm điều này bằng cách sử dụng lệnh sau:

`` `
NPM Cài đặt--Save @Alexa/Ask-SDK
`` `

Bộ Kỹ năng Alexa CLI cung cấp một số công cụ mà bạn có thể sử dụng để phát triển các kỹ năng của Alexa.

## Tạo bản kê khai kỹ năng

Bước tiếp theo là tạo ra bảng kê khai kỹ năng.Biên bản kỹ năng là một tệp JSON xác định cấu hình cho kỹ năng của bạn.Bạn có thể tạo bảng kê khai kỹ năng bằng lệnh sau:

`` `
Ask-sdk mới của tôi
`` `

Điều này sẽ tạo một tệp mới có tên là `Skill.json` trong thư mục` my-skill`.Tệp `Skill.json` chứa các thông tin sau:

* Tên của kỹ năng
* Tên gọi của kỹ năng
* Mô tả của kỹ năng
* Các ngôn ngữ được hỗ trợ của kỹ năng

## Viết mã kỹ năng

Bước tiếp theo là viết mã cho kỹ năng của bạn.Mã kỹ năng chịu trách nhiệm xử lý các yêu cầu được gửi từ Alexa.Bạn có thể viết mã kỹ năng bằng bất kỳ ngôn ngữ lập trình nào bạn muốn.Trong hướng dẫn này, chúng tôi sẽ sử dụng Node.js.

Sau đây là một ví dụ về một mã kỹ năng đơn giản đáp ứng với "thời tiết là gì?"lời yêu cầu:

`` `
const Alexa = Yêu cầu ('@Alexa/Ask-SDK');

Const Skill = new Alexa.SkillBuilders.Custom ()
.AddRequestHandlers (
Alexa.RequestHandlers.IntentRequestHandler mới
CanHandle (HandlerInput) {
return HandlerInput.Request.Intent.Name === 'getWeatherIntent';
},
Xử lý (HandlerInput) {
Const phát biểu = `Nhiệt độ hiện tại ở San Francisco là 65 độ Fahrenheit.`;
Trả về HandlerInput.ResponseBuilder
.Speak (lời nói)
.withsimplecard ('thời tiết', lời nói)
.kết thúc();
},
}),
)
.xây dựng();

Kỹ năng.Handler.Process (Sự kiện, Bối cảnh, gọi lại);
`` `

Mã này sử dụng lớp `Alexa.RequestHandlers.IntEntRequestHandler` để tạo một trình xử lý cho ý định của` getweatherintent`.Người xử lý có được nhiệt độ hiện tại ở San Francisco và trả về một phản hồi bao gồm một tin nhắn nói và một thẻ đơn giản.

## Kiểm tra kỹ năng

Khi bạn đã viết mã kỹ năng, bạn có thể kiểm tra nó bằng trình mô phỏng kỹ năng Alexa.Alexa Skill Simulator là một công cụ dựa trên web cho phép bạn kiểm tra các kỹ năng của mình mà không cần thiết bị Amazon Echo.

Để kiểm tra kỹ năng của bạn, hãy mở trình mô phỏng Alexa Skill trong trình duyệt web và nhấp vào nút "Tạo kỹ năng".Nhập tên của kỹ năng của bạn và nhấp vào nút "Tạo".

Tiếp theo, nhấp vào tab "Kiểm tra" và nhập văn bản sau vào trường "Ý định":

`` `
Thời tiết ra sao?
`` `

Nhấp vào nút "Gửi" và trình giả lập kỹ năng Alexa sẽ gửi "
=======================================
#Alexaskills #node.js #echo #Aws #smarthome ## Building Alexa Skills With Node.js

Alexa Skills are a powerful way to extend the capabilities of your Amazon Echo device. With Alexa Skills, you can create voice-activated experiences that can do anything from playing music to checking the weather. In this tutorial, we'll show you how to build an Alexa Skill with Node.js.

## Prerequisites

To follow along with this tutorial, you'll need the following:

* An Amazon Echo device
* A Node.js development environment
* The [Alexa Skills Kit CLI](https://developer.amazon.com/en-US/docs/alexa/alexa-skills-kit-sdk-for-nodejs/setup-alexa-skills-kit-sdk-for-nodejs.html)

## Getting Started

The first step is to create a new Node.js project. You can do this using the following command:

```
npm init
```

This will create a new directory called `my-skill` with a file called `package.json`. The `package.json` file is used to define the dependencies for your project.

Next, you need to install the Alexa Skills Kit CLI. You can do this using the following command:

```
npm install --save @alexa/ask-sdk
```

The Alexa Skills Kit CLI provides a number of tools that you can use to develop Alexa Skills.

## Creating the Skill Manifest

The next step is to create the skill manifest. The skill manifest is a JSON file that defines the configuration for your skill. You can create the skill manifest using the following command:

```
ask-sdk new my-skill
```

This will create a new file called `skill.json` in the `my-skill` directory. The `skill.json` file contains the following information:

* The skill's name
* The skill's invocation name
* The skill's description
* The skill's supported languages

## Writing the Skill Code

The next step is to write the code for your skill. The skill code is responsible for handling the requests that are sent from Alexa. You can write the skill code in any programming language that you want. In this tutorial, we'll be using Node.js.

The following is an example of a simple skill code that responds to the "What's the weather?" request:

```
const Alexa = require('@alexa/ask-sdk');

const skill = new Alexa.SkillBuilders.custom()
.addRequestHandlers(
new Alexa.RequestHandlers.IntentRequestHandler({
canHandle(handlerInput) {
return handlerInput.request.intent.name === 'GetWeatherIntent';
},
handle(handlerInput) {
const speech = `The current temperature in San Francisco is 65 degrees Fahrenheit.`;
return handlerInput.responseBuilder
.speak(speech)
.withSimpleCard('Weather', speech)
.end();
},
}),
)
.build();

skill.handler.process(event, context, callback);
```

This code uses the `Alexa.RequestHandlers.IntentRequestHandler` class to create a handler for the `GetWeatherIntent` intent. The handler gets the current temperature in San Francisco and returns a response that includes a spoken message and a simple card.

## Testing the Skill

Once you've written the skill code, you can test it using the Alexa Skills Simulator. The Alexa Skills Simulator is a web-based tool that allows you to test your skills without having an Amazon Echo device.

To test your skill, open the Alexa Skills Simulator in a web browser and click the "Create Skill" button. Enter the name of your skill and click the "Create" button.

Next, click the "Test" tab and enter the following text in the "Intent" field:

```
What's the weather?
```

Click the "Submit" button and the Alexa Skills Simulator will send the "What's
 
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