Message Boards Message Boards

11
|
14789 Views
|
2 Replies
|
14 Total Likes
View groups...
Share
Share this post:

What I did at a hackathon: text to Wolfram|Alpha via SMS API

Posted 9 years ago

I was at a hackathon recently and I won the best use of wolfram technologies prize. I basically made a texting service, and one of the features that I implemented was the ability to ask questions, which the service would then answer by querying wolfram alpha. I kinda don't want to post the number here cause it'll cost me a lot of money if it gets out, but it was pretty cool.

I used the Wolfram Alpha API, Google App Engine as a backend (with Python), and Twilio as the SMS API.

Here's a picture of a sample question message that queried Wolfram Alpha

Update:

Here's some code that y'all might find interesting:

  1. This is a class that searches for queries on wolfram alpha. Basically you create a wolfram class and pass in your ID and then you can call search on a query.

    import urllib2
    import urllib
    import httplib
    from xml.etree import ElementTree as etree
    import sys
    
    class wolfram(object):
        def __init__(self, appid):
            self.appid = appid
            self.base_url = 'http://api.wolframalpha.com/v2/query?'
            self.headers = {'User-Agent':None}
    
        def _get_xml(self, ip):
            url_params = {'input':ip, 'appid':self.appid}
            data = urllib.urlencode(url_params)
            req = urllib2.Request(self.base_url, data, self.headers)
            xml = urllib2.urlopen(req).read()
            return xml
    
        def _xmlparser(self, xml):
            data_dics = {}
            tree = etree.fromstring(xml)
            #retrieving every tag with label 'plaintext'
            for e in tree.findall('pod'):
                for item in [ef for ef in list(e) if ef.tag=='subpod']:
                    for it in [i for i in list(item) if i.tag=='plaintext']:
                        if it.tag=='plaintext':
                            data_dics[e.get('title')] = it.text
            return data_dics
    
        def search(self, ip):
            xml = self._get_xml(ip)
            result_dics = self._xmlparser(xml)
            for key, value in result_dics.iteritems(): 
                val = result_dics[key]
                try: 
                    result_dics[key] = val.encode('ascii', 'ignore')
                except Exception: 
                    pass 
            valList = result_dics.values()
            keyList = result_dics.keys()
            if 'Result' in result_dics.keys():
                return result_dics['Result']
            elif 'Response' in result_dics.keys():
                return result_dics['Response']
            elif self.checkForString('Weather forecast for', result_dics):
                return result_dics[keyList[self.getIndexOfString('Weather forecast for', result_dics)]]
            elif self.checkForString('Results', result_dics):
                return result_dics[keyList[self.getIndexOfString('Results', result_dics)]]
            elif self.checkForString('Corporate', result_dics):
                return result_dics[keyList[self.getIndexOfString('Corporate', result_dics)]]
            elif self.checkForString('Basic', result_dics):
                return result_dics[keyList[self.getIndexOfString('Basic', result_dics)]]
            elif self.checkForString('IP address', result_dics):
                return result_dics[keyList[self.getIndexOfString('IP address', result_dics)]]
            elif self.checkForString('date', result_dics):
                return result_dics[keyList[self.getIndexOfString('date', result_dics)]]
            else: 
                return "I don't understand. Sorry! Type COMMANDS to see the list of available commands."
    
        def checkForString(self, checkStr, myDict):
            keyList = myDict.keys()
            for key in keyList:
                if checkStr in key: 
                    return True
            return False
    
        def getIndexOfString(self, checkStr, myDict):
            keyList = myDict.keys()
            for x in range(len(keyList)):
                    if checkStr in keyList[x]: 
                        return x 
    
  2. This is some code that calls the Twilio API to create a reply to a query. Twilio is a REST API that lets you set up SMS projects pretty easily. Here's some code that replies to an SMS. What this does is create an XML page that the Twilio API can call from your project. Basically you create a REST API that twilio can call and post the TwiML for twilio to use and send. In my case I used Google App Engine and webapp2, but you can use flask or other web APIs.

    twilio_resp = twilio.twiml.Response()    
    twilio_resp.sms(reply)
    self.response.out.write(str(twilio_resp) + "\n")
    

(reply is the string that is you query from Wolfram Alpha)

POSTED BY: Nathaniel Ostrer
2 Replies
Posted 9 years ago

Interesting use of tech. Like it.

POSTED BY: Raj Shah

This is very interesting, Nathaniel! Keep the number secret, but could you please explain how you did it? Perhaps post some code, a walk through main steps, so other folks could try it out?

POSTED BY: Vitaliy Kaurov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract