Reputation: 21
I have this code:
bot.on('conversationUpdate', (message) => {
if (message.membersAdded) {
message.membersAdded.forEach((identity) => {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, 'start');
}
});
}
});
bot.dialog('start', [
(session) => {
var msg = new builder.Message(session);
msg.attachments([
new builder.HeroCard(session)
.title('test')
.buttons([{ title: 'testButton', type: 'imBack', value: 'testButton' }])
]);
builder.Prompts.choice(session, msg, ['testButton']);
},
(session, results) => {
session.send('Reached 2nd function!');
console.dir(results);
var message = results.response.entity;
session.beginDialog('anotherDialog', message);
}
]);
It works fine by using Bot Framework Emulator. Bot Framework Emulator Result
However, It doesn't reach 2nd function in the waterfall steps by using Web Chat(Azure Console). Test in Web Chat Result
What is the difference of behavior between Bot Framework Emulator and Web Chat?
And what should I modify in the code?
Do you have any idea?
Upvotes: 2
Views: 374
Reputation: 12264
I understand that what you want to do is have the bot start the conversation instead of waiting for the user to say something, which is a very common objective. Unfortunately this is not exactly an easy task with built-in functionality, but fortunately there is a blog post explaining how to do it. The blog post is taken from a workaround posted in a GitHub issue that's linked to in the one Fei Han linked.
The gist is that conversationUpdate
events don't contain enough information to allow for bot state and so dialogs and prompts and such shouldn't be spawned from the event handler. You can get around this by generating your own event in your client-side code. Of course this probably wouldn't help you when testing in the Azure portal.
In general you should expect there to be many differences between the different channels, especially when it comes to the nature of the events produced by the channels. conversationUpdate
is a particularly contentious event, and it's known to behave differently in Bot Emulator from the other channels. From the blog post (emphasis mine):
If you’re using WebChat or directline, the bot’s ConversationUpdate is sent when the conversation is created and the user sides’ ConversationUpdate is sent when they first send a message. When ConversationUpdate is initially sent, there isn’t enough information in the message to construct the dialog stack. The reason that this appears to work in the emulator, is that the emulator simulates a sort of pseudo DirectLine, but both conversationUpdates are resolved at the same time in the emulator, and this is not the case for how the actual service performs.
If you want to avoid writing client code and you're sure your bot is only going to be used in channels that support the conversationUpdate
event, I may have another workaround for you. Even though the blog post is clear that you shouldn't be using conversationUpdate
, it may still be acceptable in cases when you just need to send a single message. You could simulate a prompt by sending a single message in your event handler and then behaving as though you're following up on that message in your root dialog. Here's a proof of concept:
bot.on('conversationUpdate', (message) => {
if (message.membersAdded) {
message.membersAdded.forEach((identity) => {
if (identity.id === message.address.bot.id) {
var msg = new builder.Message()
.address(message.address)
.attachments([
new builder.HeroCard()
.title('test')
.buttons([{ title: 'testButton', type: 'imBack', value: 'testButton' }])
]);
bot.send(msg);
}
});
}
});
bot.dialog('/', function (session) {
if (session.message.text == "testButton") {
session.send('Reached 2nd function!');
session.beginDialog('/getStarted');
} else {
builder.Prompts.choice(session, "I didn’t understand. Please choose an option from the list.", ['testButton']);
}
});
Note that this proof of concept is far from robust. Since the root dialog is likely to be accessed from many different places in a real bot, you'll probably want to put a condition in there to make sure it only responds to the intro prompt one time and you'll also probably want to spawn other dialogs.
Upvotes: 0