SUSI.AI has a capability to tell the basic information about a location, it is asked for. Along with the basic information about that place, it shows a map (i.e. open street map) pointing to that location. The task at hand is to inculcate this “location” feature to the SUSI.AI messenger bots. The SUSI Tweetbot and SUSI Fbbot are used as examples in this blog.
Let’s first check on what response do we get, when a person asks a query like “where is london” to the SUSI API. Along with the basic information about that location, the result as shown below has the type of reply (i.e. map), latitude, longitude and a link to the open street map.
"actions": [ { "type": "answer", "language": "en", "expression": "Ludhiana is a city and a municipal corporation in Ludhiana district in the Indian state of Punjab, and is the largest city north of Delhi." }, { "type": "anchor", "link": "https://www.openstreetmap.org/#map=13/30.912040570244187/75.85379021980509", "text": "Here is a map", "language": "en" }, { "type": "map", "latitude": "30.912040570244187", "longitude": "75.85379021980509", "zoom": "13", "language": "en" } ]
The response for a location type query has these 3 main parts:
- Clickable static map image.
- A basic information of the place asked about.
- The link i.e. when the static map image is clicked it should redirect to the corresponding map location.
Let’s try to make up with the 1st part of the response i.e. Static map image.
The map quest API is used to result in a static map image of the location. We need an API key to access the map quest API, which can be requested from their developer site.
Along with that we need the latitude and longitude of the location at hand.
The SUSI API’s response helps us to get the latitude value:
// if body represents the response object var lat = body.answers[0].actions[2].latitude;
And the longitude value:
var lon = body.answers[0].actions[2].longitude;
Using the three values that are API key, latitude and longitude, the static image is rendered by this link:
var static_image_url = "https://open.mapquestapi.com/staticmap/v4/getmap?key=API_KEY&size=600,400&zoom=13¢er="+lat+","+lon;
The second part is, basic information about the place asked, can be fetched from:
// if body is the JSON response object from SUSI API var mapMessage = body.answers[0].actions[0].expression;
The link to the map location can be easily fetched from the SUSI API’s response:
var link = body.answers[0].actions[1].link;
As all the three parts are ready, let’s look on how to render them on the SUSI.AI bot’s screen.
Facebook:
Sending a generic template message object:
message = { "type":"template", "payload":{ "template_type":"generic", "elements":[{ "title": mapMessage, "image_url": static_image_url, "Item_url": link }] } }; sendTextMessage(sender, message, 1);
Twitter:
The Twitter API does not need a static image of the map to be rendered. It does that work for us. We just need to send an event to the Twitter API with the message data object constituting of the map message, the latitude and longitude values:
"message_data": { "text": mapMessage, "attachment": { "type": "location", "location": { "type": "shared_coordinate", "shared_coordinate": { "coordinates": { "type": "Point", "coordinates": [lon, lat] } } } } }
Resources:
- Speed up customer service with quick replies and welcome messages by Ian Cairns from Twitter blog.
- Drive discovery of bots and other customer experiences in direct messages by By Travis Lull from Twitter blog.
- By Seth Rosenberg from Facebook developers blog – Link Ads to Messenger, Enhanced Mobile Websites, Payments and More.