【AWS re:Invent 2016】 Amazon Echo Dotでブログ投稿数を読み上げるcustom skillの作成 ③ Lambda function作成編

この記事は公開されてから半年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Amazon Echo Dotにて弊社ブログの投稿数を取得するAmazon Echo(dot)用のcustom skillを作成します。

  1. 開封の義
  2. Amazon Echo Dot初期設定編
  3. Amazon DeveloperConsoleを有効化
  4. Lambda function作成 <- 今回はこちら
  5. Amazon DeveloperConsoleよりAlexa Skillを登録

※Amazon EchoやAmazon Tap, Echo dotは日本の技適を通っていませんので、日本国内での利用は行なえません、ご注意下さい

今回の記事では、custom skill作成例として弊社ブログ記事の投稿数を取得 -> 発話を行います。
Amazonの提供しているサンプルコードをベースとした、わかりやすい動作概要解説につきまして最下段に記載のリンク先を参考にさせて頂きました。

対象として利用出来るブログシステム

WordPressにJSON API pluginがインストール済みで、Core(get系の機能)が有効化されているもの
(何らかのJsonを返してくれるAPIであれば比較的簡単な修正で対応出来ると思います)

※作成を進める上で警告が出ますが、リージョンをeu-west1 もしくは us-east1(バージニア北部) にて実施する必要があります。

新規Lambda functionの作成

Select blueprint

AWSマネジメントコンソールにログイン後、Lambdaを選択して下さい。
blueprintとして 「alexa-skills-kit-color-expert-python」を選択しますが、数が多いため Filter に 「alexa」と入力すると選択が楽です。

01-create_new_function

Configure triggers

Alexa Skills Kit を選択します。

02-config_triggers

ここで選択後、下記のような画面となる場合はリージョンを再設定し初めから実行して下さい。

02-config_triggers_warn

Configure function

こちらでコードと動作設定を行います、コードは下段に記載しております。

入力項目

  • Name, Description : 任意の名前を入力
  • Lambda function code : 最下段に記載のコードを入力
  • Lambda funtcion handler role : 既に存在する lambda_basic_execution を指定します
    下記に記載しておりますが、存在しない場合「Create a custom role」を選択するとIAM管理画面が開き、作成を半自動で行えます。
  • Timeout : APIリクエストの返却される時間に余裕を持った秒数を選択します、こちらでは10秒を指定しています。
03-config_function-_create_iam

IAMロールの半自動生成

Create a custom role を選択後、自動で開かれたIAM管理画面にて許可を押すとRoleが生成されます。

10-create_iam_policy

Lambdaコード

ほぼblueprintですが、追加・変更・削除した部分は下記となります。
ロジックについては、外部URLを叩き取得したJSONをパースしているのみです。

  • 削除
    もともと持っているサンプルとしての機能、関数部分を削除
  • 変更
    on_intent 中の intent_name を変更
  • 追加
    先頭部分 DATE / URLを追加
    get_blog_count() 関数を追加

コード全体

※URI部分を自身のWordpress+JSON API導入済みのWebサイト等に変更しなければ動作しません。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
&quot;&quot;&quot;
return BlogCount from WordPress (using JSON API plugin)</p>
<p>Based on <a href="http://amzn.to/1LzFrj6">http://amzn.to/1LzFrj6</a>
&quot;&quot;&quot;</p>
<p>from <strong>future</strong> import print_function
from urllib2 import urlopen
import json
import time</p>
<p>DATE = time.strftime(&quot;%Y%m%d&quot;)
<h1>--------------- Helpers that build all of the responses ----------------------</h1>
<p>def build_speechlet_response(title, output, reprompt_text, should_end_session):
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'card': {
'type': 'Simple',
'title': &quot;SessionSpeechlet - &quot; + title,
'content': &quot;SessionSpeechlet - &quot; + output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': should_end_session
}</p>
<p>def build_response(session_attributes, speechlet_response):
return {
'version': '1.0',
'sessionAttributes': session_attributes,
'response': speechlet_response
}</p>
<h1>--------------- Functions that control the skill's behavior ------------------</h1>
<p>def get_welcome_response():
&quot;&quot;&quot; If we wanted to initialize the session to have some attributes we could
add those here
&quot;&quot;&quot;</p>
[crayon-68d0e417db50a879085786/]
<p>def handle_session_end_request():
card_title = &quot;Session Ended&quot;
speech_output = &quot;Thank you for trying Blog articles counter!&quot;</p>
[crayon-68d0e417db511488591015/]
<p>def get_blog_count():
card_title = &quot;Blog articles counter&quot;
session_attributes = {}</p>
[crayon-68d0e417db512665628689/]
<h1>--------------- Events ------------------</h1>
<p>def on_session_started(session_started_request, session):
&quot;&quot;&quot; Called when the session starts &quot;&quot;&quot;</p>
[crayon-68d0e417db513196441471/]
<p>def on_launch(launch_request, session):
&quot;&quot;&quot; Called when the user launches the skill without specifying what they
want
&quot;&quot;&quot;</p>
[crayon-68d0e417db514526962791/]
<p>def on_intent(intent_request, session):
&quot;&quot;&quot; Called when the user specifies an intent for this skill &quot;&quot;&quot;</p>
[crayon-68d0e417db515811518670/]
<p>def on_session_ended(session_ended_request, session):
&quot;&quot;&quot; Called when the user ends the session.</p>
[crayon-68d0e417db516867322151/]
<h1>--------------- Main handler ------------------</h1>
<p>def lambda_handler(event, context):
&quot;&quot;&quot; Route the incoming request based on type (LaunchRequest, IntentRequest,
etc.) The JSON body of the request is provided in the event parameter.
&quot;&quot;&quot;
print(&quot;event.session.application.applicationId=&quot; +
event['session']['application']['applicationId'])</p>
[crayon-68d0e417db517229606154/]
<p>

テスト

登録したLambda functionが適切に動作するかテストします。

作成したLambda functionを選択

20-test_lambda

Configure test event イベント指定Windowを開く

21-configure_test

発行するイベントを入力

22-configure_test

intent name を HowManyBlogsIntent として Lambda functionを呼び出し結果を取得・表示するための物です。

結果確認

コントロールパネル下段のExecution resultにresponseとしてoutputSpeechに 「XX Blog posts Today」と表示されればテストOKです!

23-configure_test

※こちらの内容をAmazon echo (dot)が喋ります

次回はいよいよAlexa Skillを登録し、Amazon echo (dot)を喋らせましょう!

この連載を通じて出来上がるもの

単独で動作しますが、自分の発音の英語を5回に1度程しか聞き取ってくれないため(悲)、PCにて喋らせております。

参考動画

PC - 「Alexa, ask blog get count」
Amazon Echo Dot - 「16 Blog posts Today」

この間に自分の作成したLambda funtcionを通っていると考えると感激モノですね!

動作概要解説で参考にさせて頂いたページ

公式ドキュメント

投稿者プロフィール

takashi
Japan AWS Ambassadors 2023, 2024
開発会社での ASP型WEBサービス企画 / 開発 / サーバ運用 を経て
2010年よりスカイアーチネットワークスに在籍しております

機械化/効率化/システム構築を軸に人に喜んで頂ける物作りが大好きです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


Time limit is exhausted. Please reload CAPTCHA.