Building Alexa Skills with Python + Flask-Ask

#Alexaskills #Python #flask-sask #alexaskillkit #voiceinterface

## Xây dựng các kỹ năng Alexa với Python và Flask-Ask

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 với Python và Flask-Ack.

### Đ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
* Một máy tính với Python 3 được cài đặt
* Thư viện-Bà

### Bắt đầu

Bước đầu tiên là tạo một dự án bình 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:

`` `
$ mkdir my_skill
$ cd my_skill
$ python3 -m venv venv
$ Nguồn venv/bin/kích hoạt
$ Pip Cài đặt Flask-Ask
`` `

Điều này sẽ tạo ra một thư mục mới có tên là `my_skill` và cài đặt thư viện pha lê vào môi trường ảo của bạn.

### Tạo tệp định nghĩa kỹ năng

Bước tiếp theo là tạo một tệp định nghĩa kỹ năng.Tệp này sẽ xác định tên của kỹ năng của bạn, tên gọi và ý định mà kỹ năng của bạn sẽ hỗ trợ.Bạn có thể tạo tệp định nghĩa kỹ năng bằng cách chạy lệnh sau:

`` `
$ Touch Skill.json
`` `

Mở tệp `skill.json` trong trình soạn thảo văn bản yêu thích của bạn và thêm mã sau:

`` `
{
"Skill_id": "Amzn1.ask.skill. <Your_skill_id>",
"Tên": "Kỹ năng của tôi",
"Phiên bản": "1.0",
"Ý định": [
{
"Tên": "HelloWorldIntent",
"khe": [],
"Cách nói": ["Xin chào", "Hi", "Bạn có khỏe không"]
}
]
}
`` `

Mã này xác định một kỹ năng với tên "Kỹ năng của tôi" và tên gọi "Hello World".Kỹ năng hỗ trợ một ý định duy nhất gọi là "HelloWorldIntent".Ý định này không có vị trí và chấp nhận các cách nói sau: "Xin chào", "Hi" và "Bạn có khỏe".

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

Bước tiếp theo là viết mã cho kỹ năng của bạn.Bạn có thể làm điều này bằng cách tạo một tệp mới có tên là `Skill.py`.Mở tệp `Skill.py` trong trình soạn thảo văn bản yêu thích của bạn và thêm mã sau:

`` `
bình nhập bình
Từ flask_ask nhập hỏi, yêu cầu, phiên

app = flask.flask (__ name__)
Hỏi = Ask (Ứng dụng)

@Ask.intent ('HelloWorldIntent')
def hello_world ():
"" "Trả lời ý định của HelloWorldent." ""

# Nhận tên người dùng từ phiên.
name = session.get ('name')

# Nếu tên của người dùng không có trong phiên, hãy hỏi họ tên của họ.
Nếu không tên:
Bài phát biểu = "Tên của bạn là gì?"
Trả về Ask.Response (bài phát biểu = lời nói, nên_end_session = false)

# Nếu không, chào người dùng theo tên.
Bài phát biểu = "Xin chào, {}!". Định dạng (tên)
Trả về Ask.Response (lời nói = lời nói, nên_end_session = true)

Nếu __name__ == '__main__':
app.run (gỡ lỗi = true)
`` `

Mã này xác định một hàm gọi là `hello_world ()` xử lý ý định của `HelloworldIntent`.Hàm có tên của người dùng từ phiên và sau đó chào người dùng bằng tên.

### Chạy kỹ năng

Để chạy kỹ năng, bạn có thể sử dụng lệnh sau:

`` `
$ Flask chạy
`` `

Điều này sẽ bắt đầu một máy chủ phát triển Flask trên cổng 5000. Sau đó, bạn có thể mở Trình mô phỏng bộ kỹ năng Alexa và kiểm tra kỹ năng của bạn.

### Triển khai kỹ năng cho Amazon

Khi bạn đã kiểm tra kỹ năng của mình và hài lòng với kết quả
=======================================
#Alexaskills #Python #flask-ASK #alexaskillkit #voiceinterface

## Building Alexa Skills with Python and Flask-ASK

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'll show you how to build an Alexa Skill with Python and Flask-ASK.

### Prerequisites

To follow this tutorial, you will need the following:

* An Amazon Echo device
* A computer with Python 3 installed
* The Flask-ASK library

### Getting Started

The first step is to create a new Flask project. You can do this by running the following command in your terminal:

```
$ mkdir my_skill
$ cd my_skill
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install flask-ask
```

This will create a new directory called `my_skill`, and install the Flask-ASK library into your virtual environment.

### Creating the Skill Definition File

The next step is to create a skill definition file. This file will define the name of your skill, the invocation name, and the intents that your skill will support. You can create the skill definition file by running the following command:

```
$ touch skill.json
```

Open the `skill.json` file in your favorite text editor and add the following code:

```
{
"skill_id": "amzn1.ask.skill.<YOUR_SKILL_ID>",
"name": "My Skill",
"version": "1.0",
"intents": [
{
"name": "HelloWorldIntent",
"slots": [],
"utterances": ["hello", "hi", "how are you"]
}
]
}
```

This code defines a skill with the name "My Skill" and the invocation name "hello world". The skill supports a single intent called "HelloWorldIntent". This intent has no slots and accepts the following utterances: "hello", "hi", and "how are you".

### Writing the Skill Code

The next step is to write the code for your skill. You can do this by creating a new file called `skill.py`. Open the `skill.py` file in your favorite text editor and add the following code:

```
import flask
from flask_ask import Ask, request, session

app = flask.Flask(__name__)
ask = Ask(app)

@ask.intent('HelloWorldIntent')
def hello_world():
"""Responds to the HelloWorldIntent intent."""

# Get the user's name from the session.
name = session.get('name')

# If the user's name is not in the session, ask them for their name.
if not name:
speech = "What is your name?"
return ask.response(speech=speech, should_end_session=False)

# Otherwise, greet the user by name.
speech = "Hello, {}!".format(name)
return ask.response(speech=speech, should_end_session=True)

if __name__ == '__main__':
app.run(debug=True)
```

This code defines a function called `hello_world()` that handles the `HelloWorldIntent` intent. The function gets the user's name from the session and then greets the user by name.

### Running the Skill

To run the skill, you can use the following command:

```
$ flask run
```

This will start a Flask development server on port 5000. You can then open the Alexa Skills Kit Simulator and test your skill.

### Deploying the Skill to Amazon

Once you have tested your skill and are satisfied with the results
 
Làm cách nào tôi có thể sử dụng Flask-Aking để tạo kỹ năng Alexa tùy chỉnh cho phép người dùng chơi một trò chơi tic-tac-toe?
 
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