poserver
Version:
Server for JD Bot
124 lines (113 loc) • 4.23 kB
JavaScript
/**
* Created by tomdaley on 10/2/16.
*/
/**
* Q U A L I F Y J U R I S D I C T I O N
*
* Make sure our user is trying to use us for a case that we are qualified to handle.
*/
/**
* @namespace
* @property {object} results
* @property {object} results.response
* @property {number} results.response.index
* @property {string} results.response.entity
*/
var prompts = require("./../const/prompts");
var builder = require('botbuilder');
var ConfirmDialog = require("./ConfirmDialog");
const library = new builder.Library('qualifyJurisdiction');
library.dialog('/',
[
//Ask if this case satisfies our national jurisdictional limit OR skip if we already know it's a US case.
function (session, args, next)
{
if (!session.userData.hasOwnProperty("jurisdiction")) session.userData.jurisdiction = {};
if (!session.userData.jurisdiction.hasOwnProperty("country") || session.userData.jurisdiction.country != "US")
{
ConfirmDialog.confirm(session, {
prompt : prompts.askJurisdictionCountry,
explainMessage: "We only handle cases in the US at this time."
});
}
else
{
next({response: true});
}
},
//Process the users response to the COUNTRY question. End the conversation if the user is seeking help with a
//case in a country we don't work in.
//Use these ISO Alpha-2 Country Codes: http://www.nationsonline.org/oneworld/country_code_list.htm
function (session, results, next)
{
if (results.hasOwnProperty("navigation"))
{
switch (results.navigation)
{
case "back":
case "quit":
case "restart":
session.endConversation(prompts.sayHiToBegin);
break;
}
}
else if (results.response)
{
session.userData.jurisdiction.country = "US";
next();
}
else
{
session.send(prompts.sayJurisdictionCountryLimit);
session.endConversation();
}
},
//Ask if this case satisfies our state jurisdictional limit OR skip if we already know it's a TX case.
function (session, results, next)
{
if (!session.userData.jurisdiction.hasOwnProperty("state") || session.userData.jurisdiction.state != "TX")
{
ConfirmDialog.confirm(session, {
prompt : prompts.askJurisdictionState,
explainMessage: "We only handle cases in Texas at this time."
});
}
else
{
next({response: true});
}
},
//Process the user's response to the STATE question. End the conversation if the user is seeking help with a
//case in a state outside of our jurisdiction.
function (session, results)
{
if (results.hasOwnProperty("navigation"))
{
switch (results.navigation)
{
case "back":
session.userData.jurisdiction.country = "";
session.replaceDialog("/");
break;
case "quit":
case "restart":
session.endConversation(prompts.sayHiToBegin);
break;
}
}
else if (results.response)
{
session.userData.jurisdiction.state = "TX";
session.endDialogWithResult({
"country": session.userData.jurisdiction.country,
"state" : session.userData.jurisdiction.state
});
}
else
{
session.send(prompts.sayJurisdictionStateLimit);
session.endConversation();
}
}
]);
module.exports = library;