Alexaを使った実店舗の注文システムを作りたいと考えています。
お弁当屋さんに来たお客さんがAlexaに注文を話し、それを厨房に伝えるシステムです
イメージとしては以下のようになります。
客 「注文」//Alexaのインテントトリガー
Alexa「注文をどうぞ」//レスポンス
客 「○○を一つ○○の大盛りを2つ・・・」//関数(注文内容を厨房のタブレットにslackを使って表示
「~以上で」//注文を終わらせる文言
Alexa「注文は(注文内容繰り返し)でよろしかったでしょうか」
客 「はい」
Alexa「出来上がりまで少々お待ちください」//終了
Alexaに話したことをslackに表示させることはできたのですが、大盛りや弁当の個数、注文を終わらせるまでループ処理をさせる、といったところでつまずいております。なにかヒントをいただければ幸いです。
python
1#coding : utf-8 2""" 3This sample demonstrates a simple skill built with the Amazon Alexa Skills Kit. 4The Intent Schema, Custom Slots, and Sample Utterances for this skill, as well 5as testing instructions are located at http://amzn.to/1LzFrj6 6 7For additional samples, visit the Alexa Skills Kit Getting Started guide at 8http://amzn.to/1LGWsLG 9""" 10 11from __future__ import print_function 12import urllib.parse 13import urllib.request 14 15 16# --------------- Helpers that build all of the responses ---------------------- 17 18def build_speechlet_response(title, output, reprompt_text, should_end_session): 19 return { 20 'outputSpeech': { 21 'type': 'PlainText', 22 'text': output 23 }, 24 'card': { 25 'type': 'Simple', 26 'title': "SessionSpeechlet - " + title, 27 'content': "SessionSpeechlet - " + output 28 }, 29 'reprompt': { 30 'outputSpeech': { 31 'type': 'PlainText', 32 'text': reprompt_text 33 } 34 }, 35 'shouldEndSession': should_end_session 36 } 37 38 39def build_response(session_attributes, speechlet_response): 40 return { 41 'version': '1.0', 42 'sessionAttributes': session_attributes, 43 'response': speechlet_response 44 } 45 46 47# --------------- Functions that control the skill's behavior ------------------ 48 49def get_welcome_response(): 50 """ If we wanted to initialize the session to have some attributes we could 51 add those here 52 """ 53 54 session_attributes = {} 55 card_title = "Welcome" 56 speech_output = "注文をどうぞ" 57 reprompt_text = "もう一度おねがいします" 58 should_end_session = False 59 return build_response(session_attributes, build_speechlet_response( 60 card_title, speech_output, reprompt_text, should_end_session)) 61 62 63def handle_session_end_request(): 64 card_title = "Session Ended" 65 speech_output = "Thank you for trying the Alexa Skills Kit sample. " \ 66 "Have a nice day! " 67 # Setting this to true ends the session and exits the skill. 68 should_end_session = True 69 return build_response({}, build_speechlet_response( 70 card_title, speech_output, None, should_end_session)) 71 72 73def take_memo(intent, session): 74 try: 75 memo = intent["slots"]["contents"].get("value") 76 url = ''//ここにwebhookを使って作成したURLを入れる 77 params = urllib.parse.urlencode({"payload": {"text":":memo:"+memo}}).encode("utf-8") 78 f = urllib.request.urlopen(url, params) 79 speech_output = "注文を受け付けました" 80 except: 81 speech_output = "失敗しました" 82 83 session_attributes = {} 84 reprompt_text = None 85 should_end_session = True 86 return build_response(session_attributes, build_speechlet_response( 87 intent['name'], speech_output, reprompt_text, should_end_session)) 88 89 90# --------------- Events ------------------ 91 92def on_session_started(session_started_request, session): 93 """ Called when the session starts """ 94 95 print("on_session_started requestId=" + session_started_request['requestId'] 96 + ", sessionId=" + session['sessionId']) 97 98 99def on_launch(launch_request, session): 100 """ Called when the user launches the skill without specifying what they 101 want 102 """ 103 104 print("on_launch requestId=" + launch_request['requestId'] + 105 ", sessionId=" + session['sessionId']) 106 # Dispatch to your skill's launch 107 return get_welcome_response() 108 109 110def on_intent(intent_request, session): 111 """ Called when the user specifies an intent for this skill """ 112 113 print("on_intent requestId=" + intent_request['requestId'] + 114 ", sessionId=" + session['sessionId']) 115 116 intent = intent_request['intent'] 117 intent_name = intent_request['intent']['name'] 118 119 # Dispatch to your skill's intent handlers 120 if intent_name == "Memointent": 121 return take_memo(intent, session) 122 elif intent_name == "AMAZON.HelpIntent": 123 return get_welcome_response() 124 elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent": 125 return handle_session_end_request() 126 else: 127 raise ValueError("Invalid intent") 128 129 130def on_session_ended(session_ended_request, session): 131 """ Called when the user ends the session. 132 133 Is not called when the skill returns should_end_session=true 134 """ 135 print("on_session_ended requestId=" + session_ended_request['requestId'] + 136 ", sessionId=" + session['sessionId']) 137 # add cleanup logic here 138 139 140# --------------- Main handler ------------------ 141 142def lambda_handler(event, context): 143 """ Route the incoming request based on type (LaunchRequest, IntentRequest, 144 etc.) The JSON body of the request is provided in the event parameter. 145 """ 146 print("event.session.application.applicationId=" + 147 event['session']['application']['applicationId']) 148 149 """ 150 Uncomment this if statement and populate with your skill's application ID to 151 prevent someone else from configuring a skill that sends requests to this 152 function. 153 """ 154 # if (event['session']['application']['applicationId'] != 155 # "amzn1.echo-sdk-ams.app.[unique-value-here]"): 156 # raise ValueError("Invalid Application ID") 157 158 if event['session']['new']: 159 on_session_started({'requestId': event['request']['requestId']}, 160 event['session']) 161 162 if event['request']['type'] == "LaunchRequest": 163 return on_launch(event['request'], event['session']) 164 elif event['request']['type'] == "IntentRequest": 165 return on_intent(event['request'], event['session']) 166 elif event['request']['type'] == "SessionEndedRequest": 167 return on_session_ended(event['request'], event['session'])