" 10/27 探索未來對話式科技 " ☁ 親手帶你做智慧語音助理:輕鬆上手 Amazon Lex 工作坊 ☁
Import the package we need.
import json
import random
import requests
""" Helpers to call jService (https://jservice.io/) """
def get_question(category):
id = {
'Pop Music' : 2,
'Sport' : 81,
'Geography' : 158
}
try:
payload = { 'id' : str(id[category]) }
url = 'https://jservice.io/api/category'
r = requests.get(url, params=payload)
questions = r.json()['clues']
q = random.choice(questions)
return q['question'] + ' → ' + q['answer']
except:
return 'Oops! Our question set is out of service. Please try it later.'
""" Main handler """
def lambda_handler(event, context):
intent_name = event['interpretations'][0]['intent']['name']
slots = event['interpretations'][0]['intent']['slots']
category = slots['CategoryName']['value']['interpretedValue']
message = get_question(category)
response = {
'sessionState' : {
'dialogAction' : {
'type' : 'Close'
},
'intent' : {
'name' : intent_name,
'state' : 'Fulfilled'
}
},
'messages': [
{
'contentType' : 'PlainText',
'content' : message
}
]
}
return response
AWS Educate Cloud Ambassador, Technical Support
📩 E-mail: [email protected]
Hugo ChunHo Lin
🧳 Linkedin: Hugo ChunHo Lin
👨🏻💻 GitHub: 1chooo
Released under MIT by AWS Educate TW, Hugo ChunHo Lin.
This software can be modified and reused without restriction. The original license must be included with any copies of this software. If a significant portion of the source code is used, please provide a link back to this repository.