Reputation: 143
I've been looking at this for hours but I'm not getting anywhere...
What I am trying to do is show the user a greeting when they first arrive, before they type anything, then echo back after this. I am using the 'conversationUpdate' event to trigger a greeting message for my user. What I can't understand is why it seems to trigger twice, as the conversationUpdate event should only be fired once when the user joins.
var restify = require('restify');
var builder = require('botbuilder');
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var server = require('restify').createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
server.post('/api/messages', connector.listen());
var bot = new builder.UniversalBot(connector);
bot.dialog('/', function (session) {
var message = null;
if (session.message.text == ''){
message = 'hello and welcome!';
session.send(message);
} else{
message = session.message.text;
session.send("You said: %s", message);
}
});
bot.on('conversationUpdate', function (message) {
if (message.membersAdded && message.membersAdded.length > 0) {
bot.beginDialog(message.address, '/');
}
});
Thanks, Ed
Upvotes: 3
Views: 1241
Reputation: 143
bot.on('conversationUpdate', function (message) {
if (message.membersAdded && message.membersAdded.length > 0) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, '/');
}
});
}
});
Upvotes: 1
Reputation: 111
It triggers twice, one for user joining conversation, and other for bot joining conversation, so you should check when you want to fire an action. In the next example, it triggers when bot joins conversation.
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
var msg = new builder.Message().address(message.address);
msg.text("Hail User");
bot.send(msg);
}
});
}
});
Upvotes: 2