Microsoft BotFramework-WebChat 滚动问题

2023-11-30

我正在使用微软/BotFramework-WebChat,但我在让它正确滚动时遇到问题。

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

我找不到任何有关挂钩的文档,可以让我调用 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,如果用户没有向上滚动,则将对话滚动到底部。如果用户向上滚动,当机器人发送新消息时,聊天的右下角应该会出现一个“新消息”按钮。

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

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

希望这可以帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Microsoft BotFramework-WebChat 滚动问题 的相关文章

  • 在所有浏览器中启用我的网站的平滑滚动

    我正在开发一个视差滚动网站Stellar http markdalgleish com projects stellar js and Skrollr https github com Prinzhorn skrollr图书馆 该网站在 F
  • 外部 css 文件在 Flask 框架中不起作用

    我正在尝试在我的 html 文件中使用外部 css 文件 起初我使用 bootstrap 框架 效果很好 然而 当我尝试通过添加自定义的css文件来自定义网页时 它根本不起作用 这是我的代码
  • 使用 jQuery 更改 CSS 类属性

    有没有办法使用 jQuery 更改 CSS 类的属性 而不是元素属性 这是一个实际的例子 我有一个 div 类red red background red 我想转班级red背景属性 而不是具有类的元素red分配的背景 如果我用 jQuery
  • Highcharts - 使用选定的饼图切片获得 3D 效果

    在 highcharts 中 我试图使当用户选择或将鼠标悬停在饼图的切片上时 该切片会产生沿 z 轴 朝向用户 上升的效果 我试图通过 css 设置阴影过滤器并使切片的边框更宽 填充颜色相同 来实现此目的 然而 我面临的问题是切片仍然可以位
  • HTML5 视频自动播放功能在 fullpage.js 中不起作用

    我的 HTML5 视频自动播放不起作用
  • Android浏览器上的Web应用程序宽度问题

    所以到目前为止我只在 Android 浏览器上遇到过这个问题 基本上我的网站几乎一直运行良好 而且我还没有在 Dolphin Opera 或 Skyfire 上看到这个问题 但偶尔当我从手机主屏幕之一上的书签重新打开 Android 浏览器
  • 自定义 SVG 未加载到我的图像标签中

    目前我正在尝试将自定义 svg 组件加载到图像标签内 但由于某种原因 我无法看到地图组件内的 svg 图像 我的自定义 SVG 文件如下所示 我在 SVG 中加载另一个图像
  • Facebook 分享自定义消息

    项目网站上有一个测验 您可以回答一些问题 然后根据答案得出结果 结果有时会有所不同 但客户要求结果 自定义消息 应该能够在 Facebook 上共享 我想做的就是通过自定义消息分享测验的网址 即 我在有关历史的测验中回答了 10 个问题中的
  • CSS 边框底部的曲线

    我需要 CSS 中的这个图像 并且在这个边框内需要一个背景图像 我努力了 border radius 0 0 50 50 webkit border radius 0 0 50 50 但没有得到所需的形状 任何帮助 将不胜感激 边界半径 您
  • HTML 和 标签有什么区别?

    HEAD 标签和 BODY 标签有什么区别 大多数 HTML 书籍仅 简短 提及 and 标签 但它们消失得很快 它们会影响浏览器呈现网页的方式吗 另外 它们会影响 javascript 的运行顺序吗 我的意思是 如果我里面有一个javas
  • a:悬停颜色不起作用

    一件很奇怪的事情 我想在悬停时更改链接的文本颜色和背景颜色 这是代码 css link menu a color white display block height 100 width 100 text decoration none t
  • 仅在 Chrome 上我收到此错误:Uncaught TypeError: Illegal constructor [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 当我在 Chrome 上加载 jQuery 时 我会收到此错误 Uncaught TypeError Illegal constr
  • 在 Chrome 中隐藏 HTML 时间输入字段上的图标

    在 Chrome 中 当您type time 属性到输入框 您会在输入旁边看到一个小图标时钟图标 有没有办法去掉这个小时钟图标 基于这个问题的答案 将日期输入三角形更改为日历图标 https stackoverflow com questi
  • 在 Android Chrome 中隐藏 HTML5 音频/视频通知

    我的网络应用程序上有一个 HTML5 音频元素 在某些时候 我使用以下代码以编程方式停止播放 audioElement pause audioElement currentTime 0 播放音频时 我的 Android 设备 使用 Goog
  • 选择单选按钮时隐藏/显示 3 个文本框

    我有 2 个单选按钮 选择一个文本框时 我想显示 3 个文本框 并在选择其他文本框时隐藏它 这是代码 这是我的 2 个单选按钮
  • 如何对多行文本中的每一行应用填充?

    我已将背景颜色应用于 span 标签 也有左和右padding设置在它上面 问题是 padding仅适用于左侧 开始 和右侧 结束 span 而不是当文本换行时每行的左侧 开始 和右侧 结束 我该如何应用左和右padding to the
  • 将两个数字相加将它们连接起来而不是计算总和

    我将两个数字相加 但没有得到正确的值 例如 做1 2返回 12 而不是 3 我在这段代码中做错了什么 function myFunction var y document getElementById txt1 value var z do
  • PHP 文件上传帮助

    div align center div 这是我的代码
  • 使用溢出支持定位粘性填充材料[重复]

    这个问题在这里已经有答案了 我在用position sticky在我的应用程序中 在使用overflow属性来显示滚动条 我已经寻找了一个确实支持这种情况的polyfill 但到目前为止没有任何运气 有谁知道这样的polyfill shim
  • 水平和垂直居中 div 位于页面中间,页眉和页脚粘在页面顶部和底部

    我正在尝试制作一个具有固定高度页眉和页脚的页面 页眉位于屏幕顶部 100 宽度 页脚位于底部 100 宽度 我想将一个具有可变高度内容的 div 居中放置在页眉和页脚之间的空间中 在下面的 jsfiddle 中 如果内容比空格短 它会起作用

随机推荐