皆さんこんにちは。
GMOアドマーケティングで顧客対応や開発・RPAを担当しているR.Aです。
以前、Alexaのスキル開発について執筆を行いましたが、AWS環境での設定方法について記述していませんでしたので、今回はLambdaを使用し、Alexaスキルを動かす方法を記載したいと思います。
①AWSのLambda画面で関数の作成
まず、Lambdaでスキルを動かす為に、初期設定の関数の作成を行っていきます。
Lambda画面へログインを行い、「関数の作成」をクリックし、作成画面を開きます。
設定プリセットから作成というものもありますが、今回はシンプルに作成したい為、
「一から作成」を選択し、画像のように必要事項を記入し作成ボタンをクリックします。
前回はAlexaスキルをRubyで作成しましたが、今回はPythonで作成してみたいと思います。
なお、ロケーションは「東京」で作成をしてください。東京にする事でレスポンスが早くなるなどが期待できます。
②Alexaスキルの実装
作成すると関数のホーム画面に遷移します。
画面上部にデザインボックスがありその中に「+トリガーを追加」というボタンがありますので、
こちらをクリックし、「Alexa Skills Kit」を検索します。
選択するとスキルIDの入力画面が表示されますので、Alexaスキル作成画面よりスキルIDをコピーし、ペーストします。
次に関数に処理を記述していきます。
内容としては、以前記述したものをPythonで書き換えたものなので、詳細は割愛します。
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import urllib.request from html.parser import HTMLParser import re class BaseSpeech: def __init__(self, speech_text, should_end_session, session_attributes=None): if session_attributes is None: session_attributes = {} self._response = { 'version': '1.0', 'sessionAttributes': session_attributes, 'response': { 'outputSpeech': { 'type': 'PlainText', 'text': speech_text }, 'shouldEndSession': should_end_session, }, } self.speech_text = speech_text self.should_end_session = should_end_session self.session_attributes = session_attributes def build(self): return self._response class OneSpeech(BaseSpeech): def __init__(self, speech_text, session_attributes=None): super().__init__(speech_text, True, session_attributes) class QuestionSpeech(BaseSpeech): def __init__(self, speech_text, session_attributes=None): super().__init__(speech_text, False, session_attributes) def reprompt(self, text): reprompt = { 'outputSpeech': { 'type': 'PlainText', 'text': text } } self._response['response']['reprompt'] = reprompt return self class Parser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.url = "" # html要素からtableを抽出 def code_conversion(url,transformword): htmldata = urllib.request.Request(url) with urllib.request.urlopen(htmldata) as res: body = res.read().decode('UTF-8') root = lxml.html.fromstring(body) status = root.xpath('//table') return replyshaping(buslist,transformword) #発音音声を作成 def replyshaping(buslist,transformword): return OneSpeech(buslist).build() if not buslist: return OneSpeech(transformword + "のバスは、野沢龍雲寺近辺には来ていないようです。").build() pattern = '/(1[0-9])+/' for index, bustime in enumerate(buslist): if re.match(pattern,bustime): buslist[index] = bustime else: buslist[index] = bustime[-4:-1] if len(buslist) == 1: ask = transformword + "のバスは、" + buslist[0] + "ちです。" else: ask = transformword + "のバスは、" + buslist[0] + "ちです。その次のバスは、" + buslist[1] + "ちです。" return OneSpeech(ask).build() def welcome(): return QuestionSpeech('はい、野沢龍雲寺からの東急バスを調べます。').build() def retry(): return QuestionSpeech('もう一度言ってください。').build() #終了時の処理 def bye(): return OneSpeech('終了します。').build() def lambda_handler(event, context): # リクエストの種類を取得 request = event['request'] request_type = request['type'] if request_type == 'LaunchRequest': return welcome() # 処理内容の分岐 バスの検索だったら行き先を格納 elif request_type == 'IntentRequest': intent_name = request['intent']['name'] if intent_name == 'TokyuBusIntent': intent_slots = request['intent']['slots'] if len(intent_slots['station']) == 2: city = "その他" else: city = intent_slots['station']['value'] #渋谷か目黒を格納 if city == '目黒' or city == '中目黒' or city == 'めぐろ' or city == 'なかめぐろ': url = 'http://tokyu.bus-location.jp/blsys/navi?VID=lsc&EID=nt&FID=&SID=&PRM=&SCT=2&DSN=%E8%A5%BF%E6%BE%84%E5%AF%BA&ASN=%E7%9B%AE%E9%BB%92%E9%A7%85%E5%89%8D&FDSN=0&FASN=0&DSMK=2687&ASMK=2534' searchword = '黒09・中目01' transformword = '黒09' return code_conversion(url,transformword) elif city == '渋谷' or city == 'しぶや': url = 'http://tokyu.bus-location.jp/blsys/navi?VID=lsc&EID=nt&FID=&SID=&PRM=&SCT=2&DSN=%E8%A5%BF%E6%BE%84%E5%AF%BA&ASN=%E6%B8%8B%E8%B0%B7%E9%A7%85&FDSN=0&FASN=0&DSMK=2687&ASMK=2336' searchword = '渋32' transformword = '渋32' return code_conversion(url,transformword) else: return retry() elif intent_name =='HelloWorldIntent': return HelloWorld() elif intent_name == 'AMAZON.HelpIntent': return welcome() elif intent_name == 'AMAZON.CancelIntent' or intent_name == 'AMAZON.StopIntent': return bye() |
③AlexaスキルとLambdaの紐付け
最後にAlexaのエンドポイントに今回作成したLambdaのARNを記入します。
コピー元のARNはLambdaの上部に、コピー先のエンドポイントは、開発画面 -> ビルド -> エンドポイントにあります。
上記まで記入が完了したら動作テストを行ってみます。
無事に動作し、動作確認が行えました。
Amazon Echo でも喋らせてみた所問題なく処理してくれました。
こちらでスタンダードなAlexaスキルを作ることが出来ました。
今後はもう少し実用的なスキルを作り、収益化も出来たらいいなぁと考えております。