Data validation before next dialog waterfall step in Bot Framework - node.js

Have simple waterfall dialog:
SendMessageDialog = [
function (session) {
builder.Prompts.time(session, "Enter dates?");
},
function (session, results) {
session.conversationData.start = builder.EntityRecognizer.resolveTime([results.response]).toISOString();
if(typeof results.response.resolution.end != "undefined")
session.conversationData.end = results.response.resolution.end.toISOString();
}
];
Bot successfully recognizes time in different formats, and if format is invalid makes default prompt to user proposing to re-enter data like:
I didn't understand. Please choose an option from the list.
In Prompts option I can only change this default retryPrompt message. What if I need additional validation like:
User enters a date, but the date isn't valid because of business
logic (in the past, unavailable)
User enters a location, so need to check against list of available locations (perhaps from an api call)
Check number range after Prompts.number()
etc
Is there is an easy way to add additional validation to retry same waterfall step and ask user to re-enter data? How to implement this? Is there a workable code for BotBuilder 3.9?
There are some examples exist to make some validations with LUIS API calls, however they work only on next waterfall step. Goal not to go to the next step until correct data entered - is it possible? Thanks!

Right after the question was asked, had found how-to Create custom prompts to validate input:
Result code:
[
function (session) {
// Call start/end time prompt dialog
session.beginDialog("DatePromptDialog");
},
...
]
DatePromptDialog = [
function (session, args) {
var options = { retryPrompt: "I didn’t recognize dates you entered. Please try again using format: start - end dates" };
if (args && args.reprompt && args.endTimeMissed) {
builder.Prompts.time(session, "Please specify both start - end times:", options);
} else if (args && args.reprompt && args.dateInPast){
builder.Prompts.time(session, "That date seems to be in the past! Please enter a valid date.", options);
} else {
builder.Prompts.time(session, "Enter dates?", options);
}
},
function (session, results) {
var args = {};
delete session.conversationData.start; // Clear previous values
delete session.conversationData.end;
// Get start time
session.conversationData.start = builder.EntityRecognizer.resolveTime([results.response]).toISOString();
// Get duration end time if available
if(typeof results.response.resolution.end != "undefined")
session.conversationData.end = results.response.resolution.end.toISOString();
else {
args.endTimeMissed = true;
args.reprompt = true;
}
// Convert dates from string
var currDate = new Date(); // Current date
var startDate = new Date(session.conversationData.start);
var endDate = new Date(session.conversationData.end);
if(startDate < currDate || endDate < currDate) {
args.dateInPast = true;
args.reprompt = true;
}
if (args.reprompt) {
// Repeat the dialog
session.replaceDialog('DatePromptDialog', args);
} else {
// Success
session.endDialog();
}
}
];

Related

discord.js - Random user ID from reactions under message

I want to get random user ID from users with reaction under some message, but almost always when I'm trying to get all users with reaction it returns No Winner even if I reacted
Code:
setTimeout(()=> {
// msg.reactions.removeAll
if(msg.reactions.cache.get("👍").users.cache.filter(user => !user.bot).size > 0) {
const winner = msg.reactions.cache.get("👍").users.cache.filter(user => !user.client).random().id
message.channel.send(`Winner: #<${winner}>`)
} else {
message.channel.send("No winner.")
}
}, time-Date.now())
I had to add
Intents.FLAGS.GUILD_MESSAGE_REACTIONS to my intents.
I modified your code and it's working. You get No winner result every time because of user.client. You should use user.bot.
Here's a code I modified:
setTimeout(()=> {
const reaction = message.reactions.cache.get("👍")
const reactionUsers = reaction.users.cache.filter(user => !user.bot)
if(reactionUsers.size > 0){
const winner = reactionUsers.random()
const winnerId = winner.id
message.channel.send(`Winner: <#${winnerId}>`)
} else {
message.channel.send(`No winner`)
}
}, 10000)
Change 1: I used user.bot instead of user.client
Change 2: I assigned all variables before using them (It's more readable for me).
Change 3: #<${winnerId}> is not correct. Use <#${winnerId}> instead. (# should be inside of <> tag)

Delete filled in details after restart

I'm trying to let a person fill in some details and return an overview of the details. There is an option to restart the conversation (look at code) but when the conversation is restarted and the person fill in some new details, it will show the old details of the first filled in details.
How can i fix this problem ?
bot.dialog('overview', function (session, options) {
if (session.message && session.message.value) {
if(session.message.value.actions == "Accept"){
}
return;
}
var overview_msg = require('./cards/overview.json');
var date = new Date();
overview_msg.attachments[0].content.body[0].items[1].columns[1].items[0].text = overview_msg.attachments[0].content.body[0].items[1].columns[1].items[0].text.replace(/{{name}}/,nameGuest)
overview_msg.attachments[0].content.body[0].items[1].columns[1].items[1].text = overview_msg.attachments[0].content.body[0].items[1].columns[1].items[1].text.replace(/{{date}}/,date.toDateString() +' ' + date.toLocaleTimeString());
overview_msg.attachments[0].content.body[1].items[1].facts[0].value = overview_msg.attachments[0].content.body[1].items[1].facts[0].value.replace(/{{email}}/, mailGuest);
overview_msg.attachments[0].content.body[1].items[1].facts[1].value = overview_msg.attachments[0].content.body[1].items[1].facts[1].value.replace(/{{phone}}/, phoneGuest);
overview_msg.attachments[0].content.body[1].items[1].facts[2].value = overview_msg.attachments[0].content.body[1].items[1].facts[2].value.replace(/{{extra}}/, numberPeople);
overview_msg.attachments[0].content.body[1].items[1].facts[3].value = overview_msg.attachments[0].content.body[1].items[1].facts[3].value.replace(/{{lunch}}/, lunchGuest);
overview_msg.attachments[0].content.body[1].items[1].facts[3].value = overview_msg.attachments[0].content.body[1].items[1].facts[3].value.replace(/{{allergy}}/, lunchAllergyGuest);
overview_msg.attachments[0].content.body[1].items[1].facts[3].value = overview_msg.attachments[0].content.body[1].items[1].facts[3].value.replace(/{{vegan}}/, lunchVegan);
session.send(overview_msg);
bot.dialog('restart', function (session) {
session.beginDialog('overview');
}).triggerAction({matches: /restart|quit/i});
I think it may relate to how you define variables nameGuest, mailGuest, phoneGuest, etc which are not shown in your code snippet.
For getting values from Input.Text of adaptive-card, you can try the following code snippet:
bot.dialog('form', [
(session, args, next) => {
let card = require('./card.json');
if (session.message && session.message.value) {
next(session.message.value)
} else {
var msg = new builder.Message(session)
.addAttachment(card);
session.send(msg);
}
},
(session, results) => {
// Get the User input data here
session.send(JSON.stringify(results));
}
]).triggerAction({
matches: ['form', 'Action.Submit']
})
Yes i managed to get it done.
Instead of replacing the value in the json i referred to the variable.
example:
overview_msg.attachments[0].content.body[1].items[1].facts[0‌​].value = VARIABLE

Botframework Prompt dialogs until user finishes

I'm creating a chat bot for slack using Microsoft's botbuilder and LUIS.
Is there a way to keep using builder.Prompts.text() to keep asking the user if there are anymore information the user wants to put, like a for or while loop? For example I want to keep on asking the user an undefined number of times if there's a key the user wants to save and only stop when the user types done and then I will have an equal number of builder.Prompts.text() to ask the user for the values to put in each of those keys.
function (session, results, next) {
builder.Prompts.text(session, "Another key to put?");
},
function (session, results, next) {
builder.Prompts.text(session, "Value to put?");
}
It doesn't seem like I can create some sort of loop with an array that saves each key with its value, I'm not sure how to approach this.
Thanks.
What you're looking for is session.replaceDialog(); there is an example labeled 'basics-loops' on the GitHub repo for the SDK. To loop through prompts, one has to create a small dialog with the desired prompts and have the dialog restart automatically via session.replaceDialog() or session.beginDialog().
I've built a chatbot that receives key-value pairs in the scenario you specified above. The code excerpt below is the final step in my 'Loop' dialog.
function (session, results) {
var value = results.response ? results.response : null,
key = session.dialogData.key;
var pairs = session.userData.kVPairs;
var newPair = {};
newPair[key] = value;
if (key && value) {
session.userData.kVPairs.push(newPair);
console.log(pairs[pairs.length - 1]);
}
session.send('latest key-value pair added, { %s : %s }', key, value);
session.replaceDialog('Loop');
}
session.replaceDialog('Loop') is incorporated at the end of this waterfall step and takes the Id of the new dialog. The method can also take optional arguments to pass to the new dialog.
Note: While not applicable here, the difference between replaceDialog and beginDialog/endDialog is semi-obvious, when you use beginDialog, the new dialog is added to the stack. When you end that child dialog, you will be returned to the original/parent dialog. replaceDialog will end the current dialog and begin the new one.
You may use replacedialog to loop the user:
bot.dialog("/getUserKeys", [
function (session, args, next) {
session.dialogData.keys = args && args.keys ? args.keys : [];
builder.Prompts.text(session, "Another key to put?");
},
function (session, results, next) {
if (results.response === "none") {
session.endDialogWithResult({response: { keys: session.DialogData.keys }});
return;
}
session.dialogData.keys[session.dialogData.keys.length] = results.response;
session.replaceDialog("/getUserKeys", { keys: session.DialogData.keys });
}
]);

how to stop bot to not move forward unless entity is resolves

var intent = args.intent;
var number = builder.EntityRecognizer.findEntity(intent.entities, 'builtin.numer');
when i use findentity it move forward if the answer is correct or not how can i use entity resolve on that which are not builtin entites
var location1 = builder.EntityRecognizer.findEntity(intent.entities, 'Location');
var time = builder.EntityRecognizer.resolveTime(intent.entities);
when i use resolve time it ask againand again unless entity is resolve;
var alarm = session.dialogData.alarm = {
number: number ? number.entity : null,
timestamp: time ? time.getTime() : null,
location1: location1? location1.entity :null
};
/* if (!number & !location1 time)
{} */
// Prompt for number
if (!alarm.number) {
builder.Prompts.text(session, 'how many people you are');
} else {
next();
}
},
function (session, results, next) {
var alarm = session.dialogData.alarm;
if (results.response) {
alarm.number = results.response;
}
I believe I've already answered this question on StackOverflow: "Botframework Prompt dialogs until user finishes".
You'll need to create a mini-dialog, that will have at least two waterfall steps. Your first step will take any args and check/set them as the potential value your chatbot is waiting for. It'll prompt the user to verify that these are the correct values. If no args were passed in, or the data was not valid, the user will be prompted to supply the value the chatbot is waiting for.
The second step will take the user's response to the first step and either set the value into a session data object (like session.userData or session.conversationData) or restart the dialog using session.replaceDialog() or session.beginDialog().
In your main dialog you'll modify the step where you employ your EntityRecognizers to include an if-statement that begins your mini-dialog. To trigger the if-statement, you could use the same design as shown in this GitHub example or in your code. This code might look like below:
var location1 = builder.EntityRecognizer.findEntity(intent.entities, 'Location');
session.userData.location1 = location1 ? location1.entity : null;
if(!session.userData.location1) {
session.beginDialog('<get-location-dialog>');
}

Validate In-Line Edits in Netsuite

I need to validate inline editing in NetSuite.
I already have a Client Script in place that works great when editing the record normally.
I tried adding a User Event script that on the before save function that validates the record, but it appears this is ignored with inline editing.
Has anybody ran into this before?
Any insight you can provide would be helpful. Thanks!
Edits:
The relevant code from the UE script:
function beforeSubmit(type){
if (type == "create" || type == "edit" || type == "xedit") {
var status = nlapiGetContext().getSetting("SCRIPT", "...");
var amount = Number(nlapiGetContext().getSetting("SCRIPT", "..."));
var nr = nlapiGetNewRecord();
var entitystatus = nr.getFieldValue("entitystatus");
var projectedtotal = Number(nr.getFieldValue("projectedtotal"));
if (entitystatus == status && projectedtotal >= amount) {
var statusText = nr.getFieldText("entitystatus");
var message = "ERROR...";
throw nlapiCreateError("...", message, true);
}
}
}
This applies to the opportunity record.
The field being validated is Projected Total with id projectedtotal.
My mistake, I misunderstood how xedit handled nlapiGetNewRecord(). Calling nlapiGetNewRecord when in xedit only returns the edited fields, not the entire record. Thus, the if statement was never true in xedit mode, because either the amount or the status would be null (it was very unlikely the user would edit both at the same time, and validation relies on both these fields' values).
I edited the code to lookup the field value if it is not present in the new record. Now everything works as expected!
Thanks everyone for the help!
For reference, the corrected code is below.
function beforeSubmit(type){
if (type == "create" || type == "edit" || type == "xedit") {
var status = nlapiGetContext().getSetting("SCRIPT", "...");
var amount = Number(nlapiGetContext().getSetting("SCRIPT", "..."));
var nr = nlapiGetNewRecord();
//Attempt to get values normally
var entitystatus = nr.getFieldValue("entitystatus");
var projectedtotal = Number(nr.getFieldValue("projectedtotal"));
var id = nr.getId();
//If values were null, it's likely they were not edited and
//thus not present in nr. Look them up.
if(!entitystatus){
entitystatus = nlapiLookupField("opportunity", id, "entitystatus");
}
if(!projectedtotal){
projectedtotal = Number(nlapiLookupField("opportunity", id, "projectedtotal"));
}
if (entitystatus == status && projectedtotal >= amount) {
var message = "ERROR...";
throw nlapiCreateError("101", message, true);
}
}
}
In your user event are you checking the value of the type parameter. For inline editing, the value of the type is 'xedit'.

Resources