UNPKG

poserver

Version:
91 lines (78 loc) 3.51 kB
/** * Created by tomdaley on 10/29/16. */ var builder = require('botbuilder'); var prompts = require("./../const/prompts"); var Util = require("./../JdBotUtil"); var LIBNAME = "getCounty"; const library = new builder.Library(LIBNAME); /** * /getCounty - Prompt user for a county name. * * This dialog will prompt for a county name and then try to validate the county within the jurisdiction country and state. * If the county unambiguously validates, then we move on. If it validates as a partial match (e.g. when the user enters * "ColXlin" and Google finds "Collin", prompt the user to verify that's the county he or she wanted. If the county * cannot be found at all, the tell the user that wasn't even fucking close and reprompt. */ library.dialog("/", [ //Prompt for county name function (session) { session.userData.state = "getCounty/"; builder.Prompts.text(session, prompts.askCounty); }, //Process user entry. Look it up using Google's map api. If exact match found, keep going. If partial match found, //ask for verification. If no match found, reprompt. function (session, results, next) { session.userData.lastResults = results; if (Util.getStopWord(results.response) === null) { session.userData.case = {}; Util.lookupCounty(session.userData.jurisdiction.country, session.userData.jurisdiction.state, results.response) .then(function (result) { var address = result.address; //We found a partial or exact mach if (address.hasOwnProperty("county")) { session.userData.case.county = address.county; session.userData.case.fullJurisdiction = address.formatted_address; if (address.partialMatch) builder.Prompts.confirm(session, session.gettext(prompts.askVerifyCounty, {county: address.formatted_address})); else next({response: true}); } //We found no match at all. else { session.send(session.gettext(Util.getRandomElement(prompts.sayCantFindCounty), {county: results.response})); session.replaceDialog("/"); } }) .catch(function (address) { var data = { county: results.response, state: session.userData.jurisdiction.state, country: session.userData.jurisdiction.country }; session.send(session.gettext(prompts.sayCantFindCounty, data)); session.replaceDialog("/"); }); } }, function (session, results, next) { if (results.response) next(); else session.replaceDialog("/"); }, function (session) { session.replaceDialog(Util.getNextDialog(session, session.userData.lastResults)); } ]); module.exports = library;