【问题标题】:Microsoft BotFramework-WebChat is getting two welcome messagesMicrosoft BotFramework-WebChat 收到两条欢迎消息
【发布时间】:2019-01-30 15:05:00
【问题描述】:

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

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

Bot 框架模拟器不会发生这种情况,它只显示一条欢迎消息。

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

window.WebChat.renderWebChat({
   directLine: window.WebChat.createDirectLine({ token }),
   store,
   styleOptions,
   userID: guid(),
}, document.getElementById('webchat'));

为什么会这样?为什么网络频道会为用户发送两个“加入”事件?

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

} 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)
        }
    }
}

}

【问题讨论】:

    标签: botframework


    【解决方案1】:

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

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

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

    【讨论】:

    • 我正在查看使用 ConversationUpdate 的 github.com/Microsoft/BotBuilder-Samples/blob/master/samples/… 他们的模式看起来与您发布的博客非常不同。谢谢你的建议,我想我能在这方面取得一些进展
    • 谢谢,只有用户有一个“实体”键......所以检查“if (typeof turnContext.activity.entities != 'undefined') {”就足以确定用户加入是人类。这更符合 Microsoft 示例 github.com/Microsoft/BotBuilder-Samples/tree/master/samples/… 中给出的方法
    • 很棒的发现,安迪!使用 ConversationUpdate 或自定义事件都是可能的,但是使用 ConversationUpdate 无法发送自定义参数,例如浏览器语言或登录用户的名称。而且 ConversationUpdate 有更多限制,例如无法在第一条消息中使用提示。
    猜你喜欢
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多