html - Microsoft BotFramework-WebChat 滚动问题

标签 html css scroll botframework web-chat

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

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

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

有没有办法让聊天窗口自动滚动?

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://learn.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 (通过在 head 元素中插入链接的 javascript 加载):

.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 设计为将对话滚动到底部。如果用户向上滚动,当机器人发送新消息时,聊天的右下角应该会出现一个“新消息”按钮。

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

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

希望这对您有所帮助!

关于html - Microsoft BotFramework-WebChat 滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55121857/

相关文章:

emacs - 如何在 Emacs 中定义比三轮向下更快的滚动?

php - 文件上传返回上传html页面

html - 在 Sharepoint 日历中将一天变灰

css - 在 css 中对齐图像下方的文本

html - 如何在溢出父级的同时水平居中 div?

Jquery,聚焦一个元素一次,然后滚动到下一个&下一个&下一个

html - 下拉菜单框大小不一样

html - CSS:使用 div 和背景图像,图像未调整大小以适合 div 大小

javascript - 带有 .wait 和 .animate 的 jQuery 顺序 CSS 动画

scroll - 在 Twitter Bootstrap 的 ScrollSpy 中,我究竟可以在哪里放置 data-spy ="scroll"?