Need Help Calling Internal RESTful API
I know this is possible but how do I go about calling an internal REST endpoint and add that data to session.userData.XX via node.js?
I have a dialog in which I ask the user their name and then ask if they would like to retrieve an internal token and if they say 'yes' I will then call an internal REST service to get the token. How do I go about adding this into my dialog/routines.
Thanks, matt
I think I might have this...the code below appears to work but not sure if it is done according to best practices. Sorry the code did not format properly. Could someone take a look and let me know? Thanks, Matt
bot.dialog('/', [ //Name Input function (session) { builder.Prompts.text(session, "Hello...I am your friendly Spartan Bot...Can I get your name before proceeding?");
}, function (session,results,next) { session.sendTyping('Getting Data'); SpartanBotToken(function (err,resultset) { var TokenResults = resultset; var TokenResultsString = JSON.stringify(TokenResults); var TokenResultsJson = JSON.parse(TokenResultsString); var UserSpartanToken = TokenResultsJson.result; session.userData.Token = UserSpartanToken; next(); });
}, //Yes or No input function (session, results) { console.log(session) session.userData.name = results.response;
builder.Prompts.choice(session, "Hi " + results.response + ", Would you like to build a database?", ["YES", "NO"]);
}, ...etc...more code
I'd structure things more like this but you're on the right path:
bot.dialog('/', [
function (session, args, next) {
if (!session.userData.name) {
// Prompt user for name
builder.Prompts.text(session, "Hello...I am your friendly Spartan Bot...Can I get your name before proceeding?");
} else {
next();
}
},
function (session, results) {
if (results.response) {
// Save users name
session.userData.Name = results.response;
}
// Fetch token
session.sendTyping();
SpartanBotToken(function (err, resultset) {
// Save token
var TokenResults = resultset;
var TokenResultsString = JSON.stringify(TokenResults);
var TokenResultsJson = JSON.parse(TokenResultsString);
var UserSpartanToken = TokenResultsJson.result;
session.userData.Token = UserSpartanToken;
// Prompt to build database
builder.Prompts.confirm(session, "Hi " + session.UserData.name + ", Would you like to build a database?");
});
},
function (session, results) {
if (results.response) {
// ... build database ...
} else {
session.endDialog();
}
}
]);
Excellent, thank you for the help. Much appreciated. I am going to close this issue.