首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Microsoft BotFramework-WebChat收到两条欢迎消息

Microsoft BotFramework-WebChat收到两条欢迎消息
EN

Stack Overflow用户
提问于 2019-01-30 23:05:00
回答 1查看 735关注 0票数 0

我使用的是基于https://github.com/Microsoft/BotFramework-WebChat/blob/master/samples/15.d.backchannel-send-welcome-event/index.html的代码

当我加载网页时,我收到了两条欢迎消息。查看我的机器人的控制台输出,我可以看到两个会话更新正在发生。

这不会发生在Bot框架仿真器中,它只显示一条欢迎消息。

我的代码与示例的唯一不同之处在于渲染:

代码语言:javascript
运行
复制
window.WebChat.renderWebChat({
   directLine: window.WebChat.createDirectLine({ token }),
   store,
   styleOptions,
   userID: guid(),
}, document.getElementById('webchat'));

为什么会出现这种情况呢?为什么web通道要为用户发送两个“加入”事件?

我处理会话更新的代码如下所示:

代码语言:javascript
运行
复制
} else if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
if (DEBUG) { console.log("ConversationUpdate"); }

// Do we have any new members added to the conversation?
if (turnContext.activity.membersAdded.length !== 0) {
    // Iterate over all new members added to the conversation
    for (var idx in turnContext.activity.membersAdded) {
        console.log(turnContext.activity.membersAdded);
        // Greet anyone that was not the target (recipient) of this message
        // the 'bot' is the recipient for events from the channel,
        // turnContext.activity.membersAdded == turnContext.activity.recipient.Id indicates the
        // bot was added to the conversation.
        if (turnContext.activity.membersAdded[idx].id != turnContext.activity.recipient.id) {
            if (DEBUG) {console.log("Starting MASTER_DIALOG");}
            const user = await this.userProfile.get(turnContext, {});
            user.id = this.guid();
            await this.userProfile.set(turnContext, user);
            await this.userState.saveChanges(turnContext);
            return await dialogContext.beginDialog(MASTER_DIALOG)
        }
    }
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-31 02:04:10

不建议使用ConversationUpdate事件发送欢迎消息。阅读有关how to properly send a greeting message的更多信息。

每个连接将有两个ConversationUpdate事件。一个用于机器人加入对话,另一个用于(人类)用户加入对话。在您当前的代码中,您正在迭代所有新成员,其中您必须过滤掉机器人本身。

更好的选择是使用使用反向通道发送的自定义事件。在您提到的example中,您已经拥有了此功能。它会向你的机器人发送一个新的事件webchat/join,默认情况下甚至包括浏览器语言。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54443597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档