Two-Way Messaging with Wolfram|Alpha
We’ve just released ‘receiving SMS messages’ as a beta function and what better way to celebrate the launch of this often requested feature than by doing a small tech demo?
For this demo I will present a small API project that allow us to receive a question via SMS, then query Wolfram Alpha for a reply and then respond via SMS. Wolfram Alpha is an online service that answers factual queries directly by computing the answer from externally sourced “curated data”.
What follows is a quick tutorial on how to do this yourself, explaining the overall steps along the way for a better understanding of the tech involved. If you just want to try it out, and you got a Danish mobile subscription, text wolfram, followed by your question to 1204. I.e. send e.g. “wolfram temperature copenhagen” to 1204.
We can’t make any guarantees about the availability of this Wolfram Alpha tech demo since there are limits to the number of API requests etc. However, you can easily make your own setup with the instructions below.
Also please note that Wolfram Alpha needs to think about your query, so it might take a while to fetch your answer.
For this demo you’ll need a Wolfram Alpha APP ID. You get this free, by signing up to WolframAlpha.
You’ll also need a GatewayAPI account, which you can get on this site for free. However, in order to receive an SMS, you’ll need a short code and/or keyword to which the user sends the SMS. This short code and keyword is leased to you, so when we receive a SMS on the specific short code, with the specific keyword, we know where to deliver the SMS.
You can either lease a keyword on a shared short code, such as 1204, or you can lease an entire short code, e.g. +4560575797. Your account should already be set up with a keyword that starts with ‘test-‘ for testing purposes.
Contact us via the live chat if you need a new short code and/or keyword.
In order to deliver SMS messages to your service, your service/webhook needs to be made available on the internet so we can contact it when a new SMS arrives.
Normally, this means that you have to deploy your code to a webserver somewhere which means that testing suddenly takes ages. However, a free service makes this really easy and quick: ngrok, which gives you a HTTP(S) endpoint on the internet where all requests are forwarded to your local server for easy webhook development.
We are also going to use Flask, a Python based micro framework, which is very suitable for this task. I hope that the code samples below are easy to follow even for non-Python devs. As always if you have any questions, contact us via live chat or email@example.com.
Download and unzip ngrok.
Install deps with:
Open wolfram.py in your favourite editor and paste the following.
On the command line where you unzipped ngrok. Run ./ngrok http 5000. You will get a ngrok.io https url.
Run env FLASK_APP=wolfram.py flask run this will start a Flask server on localhost port 5000. Ngrok will forward requests to Flask, confirm this by visiting the ngrok.io URL in your webbrowser. You should get a page that reads “Send me an SMS”.
Receiving SMS messages
The ngrok URL you got earlier must be configured as a web hook, and a keyword must be assigned to the web hook, so we will know to contact it when receiving an SMS.
Open the GatewayAPI Dashboard, go to Settings -> Web Hooks, add a new web hook and enter the ngrok URL in “Web hook URL” and click save. Then open the ‘Keywords’ tab and assign a keyword to the web hook. GatewayAPI will now call your web hook whenever an SMS arrives for your keyword on your shortcode.
At the time of writing this post, only REST web hooks support receiving SMS messages. Kannel etc. will follow later.
Now we need to extend the Flask API with a route that receives the SMS messages.
Now we get a variable named query, which contains everything the user wrote after the keyword itself. This will be our input to WolframAlpha.
Query Wolfram Alpha
Now we look up the query with Wolfram Alpha and format the reply. Replace “APP-ID-GOES-HERE” with your APP ID from Wolfram Alpha.
Send the reply
All that is left now is to send the reply from Wolfram Alpha back to the user via SMS. Sending an SMS is easy with GatewayAPI. On our dashboard you can find pre-configured code samples for most languages. Select the Python example and copy paste your API key and secret.
GatewayAPI will keep calling your web hook until you send a 2xx reply to let us know the SMS was received, so remember to return a reply at the end.
Wolfram Alpha contains many good examples on what to ask it, including topics as Web and Computers, Video Games and Arts and Media.
Our two-way messaging feature is at the time of writing in the beta stage, and there are a number of improvements we will implement in the months that follow. Expect latency to improve and more shortcodes to be made available etc.
Complete code sample
Same as above, but included here in full.