目次

alexa

since 2020-08-13

すごい広島 with Python [41] で紹介。

スキルの開発をしたい。amazon.co.jp のアカウントはある。そのアカウントに紐づいた alexa デバイスの実機もある。

アカウントの準備

https://developer.amazon.com/ja/blogs/alexa/post/9f852a38-3a44-48bd-b78f-22050269d7c7/hamaridokoro

Alexa-Hosted Python

https://developer.amazon.com/ja/blogs/alexa/post/31c9fd71-f34f-49fc-901f-d74f4f20e28d/alexatraining-firstskill

https://developer.amazon.com/ja-JP/docs/alexa/hosted-skills/build-a-skill-end-to-end-using-an-alexa-hosted-skill.html

実機でスキルが有効かどうか確認

音声認識

「はじめてのAlexaスキル開発」(技術評論社)Part 2 / Chapter 3 の例を参考にしている。

これを Python でやってみる。

https://developer.amazon.com/ja-JP/docs/alexa/custom-skills/handle-requests-sent-by-alexa.html

下記を HelloWorld に書き足す。

# 最初のほうに下記を追加
from ask_sdk_core.utils import get_slot_value
 
# 途中に追加
class OrderJuiceHandler(AbstractRequestHandler):
    """Handler for OrderJuice."""
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_intent_name("OrderJuice")(handler_input)
 
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        juice_type_value = get_slot_value(handler_input=handler_input, slot_name="JuiceType")
        speak_output = "ジュースの注文を受け付けました。"
        if juice_type_value:
            speak_output = juice_type_value + speak_output
 
        return (
            handler_input.response_builder
                .speak(speak_output)
                # .ask("add a reprompt if you want to keep the session open for the user to respond")
                .response
        )
 
# sb.add_request_handler(HelloWorldIntentHandler()) の次の行に下記を追加
sb.add_request_handler(OrderJuiceHandler())

これで以下の会話ができる

例1

例2