-
-
Save steven-miller/00ef0daa59f47ed014f3c8ca267b7db3 to your computer and use it in GitHub Desktop.
Bot lambda function that uses external site search and returns results for user to select from
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
exports.main = (event, callback) => { | |
//Your code goes here. | |
if (event.userMessage.quickReply === null) { | |
const requestUrl = `https://www.govplanet.com/jsp/s/search/autocomplete.do?regex=${event.userMessage.message}` | |
request(requestUrl, function (error, response, body) { | |
const data = JSON.parse(body); | |
const quickReplies = []; | |
for (var i = 0; i < data.terms.autocomplete.length; i = i + 2) { | |
let rawValue = data.terms.autocomplete[i]; | |
let sliceLoc = rawValue.indexOf('|'); | |
var option = {}; | |
if (sliceLoc !== -1) { | |
option = { | |
value: rawValue.substring(0, sliceLoc), | |
label: rawValue.substring(0, sliceLoc) | |
} | |
} else { | |
option = { | |
value: rawValue, | |
label: rawValue | |
} | |
} | |
quickReplies.push(option); | |
} | |
let responseJson = { | |
"botMessage": "Perfect! Which category best represents what you're looking for?", | |
"quickReplies": quickReplies, | |
"responseExpected": true | |
} | |
callback(responseJson); | |
}); | |
} else { | |
let responseJson = { | |
"botMessage": `Great! So you're looking to sell a ${event.userMessage.quickReply.quickReplies[0].label}`, | |
"customState": { | |
"category": event.userMessage.quickReply.quickReplies[0].label | |
}, | |
"responseExpected": false | |
} | |
callback(responseJson); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment