question

O. Smith avatar image
O. Smith asked

Simple Wolfram Alpha example app using Python

I just spent an hour writing a really basic Wolfram Alpha app for Echo and thought I'd share the results since I haven't seen any Python examples yet. It's not complicated if you read the docs but maybe it will save somebody a bit of time. First of all, I have an EC2 instance with a domain name pointing to it already, so that's where I'm running the app. Example usage: Alexa, open Alpha and ask "How big is a blue whale?" Response is: "Blue whale - Length - 82 to 100 feet." Following the "Getting Started" SDK doc, I made a private key and a self-signed certificate and put them in ~/echo on my EC2 instance. I also made an app in the AWS Echo console. The endpoint is https://mydomain.com/echo. I just have one intent: { "intents": [ { "intent": "AskQuestion", "slots": [ { "name": "Question", "type": "LITERAL" } ] } ] } I got errors whenever I tried to save that text in the AWS console, so that threw me for a while but eventually I figured out that it was actually saving and just giving me an error anyway. The error seems to have gone away now - sorry, can't remember what it said exactly - something about not being able to build the intent schema. The sample utterances are: AskQuestion and ask {one|Question} AskQuestion and ask {one two|Question} AskQuestion and ask {one two three|Question} AskQuestion and ask {one two three four|Question} AskQuestion and ask {one two three four five|Question} AskQuestion and ask {one two three four five six|Question} AskQuestion and ask {one two three four five six seven|Question} AskQuestion and ask {one two three four five six seven eight|Question} AskQuestion and ask {one two three four five six seven eight nine|Question} AskQuestion and ask {one two three four five six seven eight nine ten|Question} I read in another post here that this is the way to get an arbitrary sentence from the user. Utterances can't have wildcards, so you have to have one replaceable token for each word. The way I did it allows you to ask a question up to ten words long. Now create a developer account on Wolfram Alpha and an app at https://developer.wolframalpha.com/portal/myapps/. Note the app ID. HTTPS needs to be enabled in the security group for your EC2 instance, so go to the AWS console and add an inbound rule for HTTPS on port 443. Now we can set up the web server. I'm using cherrypy just because I know it won't need much setup. Here's my cherrypy.config: [global] server.socket_host = '0.0.0.0' server.socket_port = 443 log.error_file = '/home/ubuntu/logs/cherrypy.error.log' log.access_file = '/home/ubuntu/logs/cherrypy.access.log' server.ssl_module = 'builtin' server.ssl_certificate = 'certificate.pem' server.ssl_private_key = 'private-key.pem' And here's app.py (sorry, the forum won't indent the code correctly so you'll have to figure it out yourself): import json import urllib import urllib2 import re import cherrypy APP_ID = 'your-wolfram-alpha-app-id' p = re.compile(' (.*?)&lt;/plaintext&gt;', re.DOTALL) class EchoAlpha(object): @cherrypy.expose @cherrypy.tools.json_in() @cherrypy.tools.json_out() def echo(self): question = cherrypy.request.json['request']['intent']['slots']['Question']['value'] print question answer = urllib2.urlopen(' <a href="http://api.wolframalpha.com/v2/query?input=%s&amp;appid=%s" target="_blank">http://api.wolframalpha.com/v2/query?input=%s&amp;appid=%s</a>' % (urllib.quote(question), APP_ID)).read() results = p.findall(answer) if len(results) == 0: speech = 'Sorry, I don\'t know.' elif len(results) == 1: speech = results[0] else: speech = ' '.join(results[:2]) print speech response = dict(version = '1.0', response = dict(outputSpeech = dict(type = 'PlainText', text = speech))) return response cherrypy.tree.mount(EchoAlpha(), '/') Then you can run it with a command like: sudo /usr/bin/python /usr/local/bin/cherryd -c cherrypy.config -i app This example just grabs the first two pieces of plaintext that Wolfram Alpha returns and sends it to your Echo. It will only work with the most basic of questions - parsing the data more correctly would be a much bigger job. But even this can give you some neat answers, for example: Alexa, open Alpha and ask "When is sunset in Portland, Oregon?" Answer: Sunset - Portland, Oregon - Today 7:18 PM PDT. Alexa, open Alpha and ask "What are the planets in the solar system?" Answer: Planets - Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune - Total 8 Alexa, open Alpha and ask "What is the circumference of Jupiter?" Answer: Jupiter - Equatorial circumference 279,120 miles. Alexa, open Alpha and ask "Which planets have rings?" Answer: Ringed planets - Jupiter, Saturn, Uranus, Neptune Etc, etc. Feedback/issues: I have to be super precise when saying "Alexa, open alpha and ask" or Echo fails thinking I said "open alphabet" or something. I might have to change it to something else. The "Alexa, open app" syntax is not ideal for user experience but I get why that decision was made and I'm not sure what would be better. Wolfram Alpha takes a while to return results and sometimes Echo times out. Echo won't accept text that is too long. I wish more things were allowed - I initially wanted to make an app that would record me practicing guitar and play it back to me, but that's not possible with the SDK. I want somebody to make Spotify integration but that's definitely not possible with the SDK. :( As I understand it, writing texts and emails is not allowed also. Otherwise, this was fun and super easy to do - thanks! </plaintext>
alexa skills kitdebugging
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

O. Smith avatar image
O. Smith answered
Thinking about this a bit more, I think a more natural command syntax would be: Alexa, ask Wolfram "What is the atomic weight of mercury?"
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

rgr@amazon avatar image
rgr@amazon answered
O.Smith, Thanks you very much for the feedback, Your Wolfram Alpha sample is very cool, thank you for posting this in the forums. This is super helpful to other developers and the Echo SDK Team as we strive to make the SDK Better!! Your example, -> Alexa, ask Wolfram "What is the atomic weight of mercury?” is very insightful, we will incorporate your feedback to as we progress on improving the Echo SDK!! Thanks again!!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.