Skip to content

Instantly share code, notes, and snippets.

@dbabbs
Created November 28, 2017 17:29
Show Gist options
  • Save dbabbs/b4a6ef7328a367ac9213008edfdd8fb0 to your computer and use it in GitHub Desktop.
Save dbabbs/b4a6ef7328a367ac9213008edfdd8fb0 to your computer and use it in GitHub Desktop.
//Node modules & setup
const http = require('http');
const express = require('express');
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const bodyParser = require('body-parser');
const request = require('request');
//HERE credentials
const hereID = 'YOUR-HERE-ID';
const hereCode = 'YOUR-HERE-CODE';
//Twilio credentials:
const twilioSid = 'YOUR-TWILIO-SID';
const twilioToken = 'YOUR-TWILIO-TOKEN';
const client = require('twilio')(twilioSid, twilioToken);
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
var places = [];
app.post('/sms', (req, res) => {
const twiml = new MessagingResponse();
var incoming = req.body.Body;
if (incoming.length > 1) {
if (!incoming.includes('near')) { //Making sure it is in correct format
twiml.message('Please send a message with a place name and address. Example: "Mexican food near 701 Pike Street Seattle"');
res.writeHead(200, {
'Content-Type': 'text/xml'
});
res.end(twiml.toString());
return;
}
places = [] //clear places in case user gives two requests in a row without diving into additional info
var searchQuery = incoming.split(' near ')[0];
//Extract the search query
var locationString = incoming.split(' near ')[1]; //Extract the location
var geocodeURL = 'https://geocoder.cit.api.here.com/6.2/geocode.json' +
'?app_id=' + hereID +
'&app_code=' + hereCode +
'&searchtext=' + locationString;
request.get(geocodeURL, (error, response, body) => {
let geocodeJson = JSON.parse(body);
if (geocodeJson.Response.View[0] == null) {
//Can't find placeResults
twiml.message('No search results found for "' + searchQuery +'" in the area. Please try again.');
res.writeHead(200, {
'Content-Type': 'text/xml'
});
res.end(twiml.toString());
return;
}
var coordinates = {
lat: geocodeJson.Response.View[0].Result[0].Location.DisplayPosition.Latitude,
long: geocodeJson.Response.View[0].Result[0].Location.DisplayPosition.Longitude
};
var placesURL = 'https://places.cit.api.here.com/places/v1/autosuggest' +
'?at=' + coordinates.lat + ',' + coordinates.long +
'&q=' + searchQuery.replace(/ /g, '+') +
'&app_id=' + hereID +
'&tf=plain' +
'&app_code=' + hereCode;
request.get(placesURL, (error, response, body) => {
let placesJson = JSON.parse(body);
var placeResults = placesJson.results;
var resultAmount = Math.min(3, placeResults.length)
var responseMessage = 'Here are the ' + resultAmount + ' closest ' + searchQuery +
' places to you: \n';
for (i = 0; i < resultAmount; i++) {
if (placeResults[i].resultType != 'category') {
places.push({
name: placeResults[i].title,
category: placeResults[i].category,
address: placeResults[i].vicinity
});
responseMessage += '(' + places.length + ') ' + placeResults[i].title + '\n';
} else {
resultAmount++; //means that resultType was a category
}
}
twiml.message(responseMessage + '\nReply with # to learn more information');
res.writeHead(200, {
'Content-Type': 'text/xml'
});
res.end(twiml.toString());
});
});
} else if (places.length > 0 && incoming.length == 1) { //Reply
twiml.message(places[parseInt(incoming) - 1].name + ' is located at ' + places[parseInt(incoming) - 1].address);
res.writeHead(200, {
'Content-Type': 'text/xml'
});
res.end(twiml.toString());
places = []; //Empty places to restart
}
});
http.createServer(app).listen(1337, () => {
console.log('Express server listening on port 1337');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment