【问题标题】:Azure Web Chat - Messages appear on same side in Chat WindowAzure 网络聊天 - 消息显示在聊天窗口的同一侧
【发布时间】:2019-09-19 08:24:50
【问题描述】:

我有 2 个带有 WebChat 的 HTML。尝试从一个 WebChat 聊天到另一个 WebChat 时,消息显示在同一侧,类似于:

我读到您必须在网络聊天中输入userID,但这并没有帮助。有人对如何修复它有建议吗?

这是一个网络聊天:

    <div id="WebChatWindowTest" role="main">
      <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
      <script>
         const styleOptions = {
            rootHeight: '450px',
            rootWidth: '100%',
            bubbleBackground: 'rgba(0, 0, 255, .1)',
            bubbleFromUserBackground: 'rgba(0, 255, 0, .1)',
            hideUploadButton: true,

         };

         window.WebChat.renderWebChat(
            {
               directLine: window.WebChat.createDirectLine({

                  token: '' + loginToken
               }),
        userID: 'MyOtherID',
        username: 'MyOtherName',



               styleOptions
            },
            document.getElementById('WebChatWindowTest')
         );

      </script>
</div>

这是第二个网络聊天:

        <div id="WebChatWindow" role="main">
      <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
      <script>
         const styleOptions = {
            rootHeight: '450px',
            rootWidth: '100%',
            bubbleBackground: 'rgba(0, 0, 255, .1)',
            bubbleFromUserBackground: 'rgba(0, 255, 0, .1)',
            hideUploadButton: true
         };


         window.WebChat.renderWebChat(
            {
               directLine: window.WebChat.createDirectLine({
                  secret: token
               }),
        userID: 'MyID',
        username: 'MyName',

               styleOptions
            },
            document.getElementById('WebChatWindow')
         );
      </script>
</div>

两个 HTML 的 Token 相同。

【问题讨论】:

  • 我假设你已经看过这个 Stack Overflow question?你的机器人 id 是什么,你用什么作为用户 id?您是否尝试过从 renderWebChat 选项中删除用户 ID?您是在令牌请求中添加 id 还是仅使用网络聊天密码?您是否使用移交中间件在用户之间转发消息?
  • 在 HTML 中,机器人 id 看起来类似于“MyBotName@T7I8paygF0Q”。一个 HTML 中的用户 ID 是“MyID”,另一个是“MyOtherID”。当我从“renderWebChat”中删除用户 ID 时,用户 ID 看起来类似于“r_1568903975”。我没有在我的令牌中添加 id。我生成带有 POST 到“directline.botframework.com/v3/directline/tokens/generate”的令牌。我不使用中间件。
  • 您如何将活动从一个对话发送到另一个对话?
  • 两个 HTML 的标记是相同的。它并不是真正将消息从一个对话发送到另一个对话。这更像是向一个对话发送消息,而我猜这两个 HTML 都在看同一个聊天。我对这个不好,应该提到它。
  • 您希望机器人管理对话,还是希望所有内容都在您的单个网页中流动?

标签: azure botframework azure-bot-service direct-line-botframework web-chat


【解决方案1】:

由于两个对话共享相同的令牌,因此网络聊天会将来自两个用户的所有消息显示为单个用户。我建议您使用两个不同的令牌创建两个对话,而不是使用单个令牌。然后,您可以链接两个对话并从机器人管理对话流。

网络聊天 v4

生成两个不同的标记 - 每个响应都有一个标记和一个对话 ID。然后创建两个自定义存储中间件,将另一个对话的 id 添加到每个传出活动的通道数据中。此 ID 将在机器人中用于查找对话引用以将消息转发给其他用户。

查看Piggyback Data on Every Outgoing Activity 网络聊天示例,了解有关添加自定义频道数据的更多详细信息。

const res1 = await fetch('/directline/token', { method: 'POST' });
const { token: token1, conversationId: conversationId1 } = await res1.json();

const res2 = await fetch('/directline/token', { method: 'POST' });
const { token: token2, conversationId: conversationId2 } = await res2.json();

const store1 = window.WebChat.createStore(
  {},
  () => next => action => {
    if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
      action = window.simpleUpdateIn(action, ['payload', 'activity', 'channelData', 'conversationId'], () => conversationId2);
    }
    return next(action);
  }
);

const store2 = window.WebChat.createStore(
  {},
  () => next => action => {
    if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
      action = window.simpleUpdateIn(action, ['payload', 'activity', 'channelData', 'conversationId'], () => conversationId1);
    }
    return next(action);
  }
);

window.WebChat.renderWebChat({
    directLine: window.WebChat.createDirectLine({ token: token1 }),
    store: store1
  },
  document.getElementById('webchat1')
);

window.WebChat.renderWebChat({
    directLine: window.WebChat.createDirectLine({ token: token2 }),
    store: store2
  },
  document.getElementById('webchat2')
);

BotFramework SDK v4(节点)

当为每个用户创建对话时,Direct Line 将向机器人发送对话更新事件。此事件将触发onMemberAdded 活动处理程序,您可以在其中捕获对话引用。请注意,您应该在生成 Direct Line 令牌时向请求添加用户 ID,以便在用户向机器人发送消息之前触发 onMembersAdded 处理程序。查看这个 GitHub Issue 了解更多详情。

来自每个用户的传入消息将触发onMessage 处理程序。在处理程序中,从客户端添加的通道数据中检索对话 ID。使用 id 检索会话引用并将消息作为主动消息发送到其他会话。

为简单起见,此示例将引用保存在字典中;但是,您应该在生产中管理持久存储中的对话引用。

const { ActivityHandler, TurnContext } = require('botbuilder');

class EchoBot extends ActivityHandler {
    constructor() {
        super();

        // For simplicity, we are just saving the conversation references in an object.
        // In production, they should be maintained and managed in a persistent database structure.
        this.conversationReferences = {};

        // See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
        this.onMessage(async (context, next) => {
          const { activity: { channelData: { conversationId }, text }} = context;
          const reference = this.conversationReferences[conversationId];

          if (reference) {
            context.adapter.continueConversation(reference, context => {
              context.sendActivity(text);
            });
          }
          await next();
        });

        this.onMembersAdded(async (context, next) => {

            const membersAdded = context.activity.membersAdded;
            for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
                if (membersAdded[cnt].id !== context.activity.recipient.id) {
                  // Save the conversation reference when a new user is added.
                  const reference = TurnContext.getConversationReference(context.activity);
                  const { conversation: { id } } = reference;
                  this.conversationReferences[id] = reference;
                }
            }
            await next();
        });
    }
}

屏幕截图

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多