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.
我要做的是在用户第一次到达时,在他们输入任何东西之前,向用户显示一个问候语,然后在此之后返回。我正在使用“conversationUpdate”事件为我的用户触发一个问候信息。我无法理解的是,为什么它似乎会触发两次,因为只有在用户加入时才应该触发一次conversationUpdate事件。
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
谢谢,艾德
2 个解决方案
#1
2
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加入会话时触发。
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);
}
});
}
});
});
#2
1
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, '/');
}
});
}
});
#1
2
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加入会话时触发。
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);
}
});
}
});
});
#2
1
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, '/');
}
});
}
});