Microsoft BotFramework-WebChat 滚动问题

Microsoft BotFramework-WebChat scrolling issues

我正在使用 microsoft/BotFramework-WebChat,但我无法正确滚动它。

通常当机器人响应时,用户被迫手动滚动到聊天记录的底部。

我找不到任何关于钩子的文档,可以让我调用 API 来滚动它。

有没有办法让聊天 window 自动滚动?

HTML:

<div id="bot-button" style="display:none" >
    <p id="need-help" class="triangle-isosceles">Hey!  Need any help?</p>
    <div id="bot-open" token="temptoken">
      <span>
          <img id="avatar" src="/img/avatar.png"/>
          <i id="message-count">2</i>
      </span>
    </div>
    <div id="bot-close"><img src="/img/close.png" height="20px"/>Close</div>
    <div id="webchat" role="main"></div>
</div>
<script src="https://cdn.botframework.com/botframework-webchat/master/webchat.js"></script>
<script src="/js/chat.js"></script>

JavaScript:

(async function () {
// In this demo, we are using Direct Line token from MockBot.
// To talk to your bot, you should use the token exchanged using your Direct Line secret.
// You should never put the Direct Line secret in the browser or client app.
// https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-authentication
var bearer_token = document.getElementById("bot-open").getAttribute("token");
const res = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', {
    method: 'POST',
    headers: {
        "Authorization": "Bearer " + bearer_token
    }
});
const {
    token
} = await res.json();

// We are using a customized store to add hooks to connect event
const store = window.WebChat.createStore({}, ({
    dispatch
}) => next => action => {
    if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
        // When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
        dispatch({
            type: 'WEB_CHAT/SEND_EVENT',
            payload: {
                name: 'webchat/join',
                value: {
                    language: window.navigator.language
                }
            }
        });
    }
    return next(action);
});

const styleOptions = {
    bubbleBackground: 'rgba(0, 0, 255, .1)',
    bubbleFromUserBackground: 'rgba(0, 255, 0, .1)',
    hideUploadButton: true,
    botAvatarInitials: 'DA',
};

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

sizeBotChat();

document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));

function sizeBotChat() {
    let bot_container = document.getElementById("bot-button");
    if (isMobileDevice()) {
        bot_container.style.width = "100%";
        bot_container.style.bottom = "0px";
        bot_container.style.right = "0px";
        let max_height = screen.height - 50;
        document.getElementById("webchat").style.maxHeight = max_height + "px";
        console.log(screen.height);
    } else {
        bot_container.style.width = "400px";
        bot_container.style.right = "50px";
        document.getElementById("webchat").style.maxHeight = "400px";
    }
}

CSS (通过 javascript 在头部元素中插入一个 link 加载) :

.triangle-isosceles {
  position: relative;
  padding: 15px;
  color: black;
  background: white;
  border-radius: 10px;
}

/* creates triangle */
.triangle-isosceles:after {
  content: "";
  display: block;
  /* reduce the damage in FF3.0 */
  position: absolute;
  bottom: -15px;
  right: 30px;
  width: 0;
  border-width: 15px 15px 0;
  border-style: solid;
  border-color: white transparent;
}

#avatar {
  height: 50px;
}

#need-help {
  display: none;
}

/* based on badge progress-bar-danger from bootstrap */
#message-count {
  display: inline-block;
  min-width: 10px;
  padding: 3px 7px 3px 7px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  color: white;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  border-radius: 10px;
  background-color: #d9534f;
  position: relative;
  top: -20px;
  right: 20px;
}

#bot-button {
  position: fixed;
  bottom: 50px;
  right: 0px;
  width: 100%;
}

#bot-open {
  height: 50px;
  width: 100%;
  text-align: right;
}

#bot-close {
  background-color: blue;
  background-image: url("https://localhost/img/avatar.png");
  background-repeat: no-repeat;
  color: white;
  height: 22px;
  display: none;
  height: 50px;
  padding: 15px 15px 0 0;
  text-align: right;
  vertical-align: middle;
}

/* hide chat on load */
#webchat {
  display: none;
  max-height: 400px;
  overflow: scroll;
}

#webchat div.row.message {
  margin: 0;
}

如果用户没有向上滚动,开发人员将 WebChat 设计为将对话滚动到底部。如果用户向上滚动,当机器人发送新消息时,聊天的右下角应该会出现一个 'New Message' 按钮。

您可以通过使用自定义中间件(看起来您已经是)来修改此行为,并在用户收到来自机器人的消息时将对话中的最后一个元素滚动到视图中。请参阅下面的代码片段。

const store = window.WebChat.createStore(
    {},
    ({ dispatch }) => next => action => {

        if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
            document.querySelector('ul[role="list"]').lastChild.scrollIntoView({behavior: 'smooth', block: 'start'});
        }
        ...
        return next(action);
    }
);

希望对您有所帮助!