import urllib
import urllib2
import re
import json
def lambda_handler(event, context):
if (event['session']['application']['applicationId'] != "amzn1.ask.skill.dae3422d-d32d-xxxx-xxxx-e2dc1986608e"):
raise ValueError("Invalid Application ID")
url = 'http://api.wolframalpha.com/v2/query'
values = {'appid' : 'XXXXXX-XXXXXXXXXX',
'input' : event['request']['intent']['slots']['Problem']['value']}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
response = response.read()
response = str(re.findall(r'<plaintext>(.*)?</plaintext>', response))
session_attributes = {}
card_title = "Alexa My Math Teacher"
speech_output = "The answer is " + response
reprompt_text = ""
should_end_session = True
return build_response(session_attributes, build_speechlet_response(
card_title, speech_output, reprompt_text, should_end_session))
def build_speechlet_response(title, output, reprompt_text, should_end_session):
return {
"outputSpeech": {
"type": "PlainText",
"text": output
},
"card": {
"type": "Simple",
"title": title,
"content": output
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": reprompt_text
}
},
"shouldEndSession": should_end_session
}
def build_response(session_attributes, speechlet_response):
return {
"version": "1.0",
"sessionAttributes": session_attributes,
"response": speechlet_response
}
Comments