使用 DirectLine 时如何修改 Microsoft Chatbot 的 webchat 容器

我有聊天机器人,我使用 DirectLine 将机器人嵌入我的网站。DirectLine UI 看起来像一个带有白色背景的页面,我想将其修改为如下图所示或接近于它:

http://img.mukewang.com/619f749f0001652905100423.jpg

我正在关注这里的示例。


我能够更改容器的大小,但无法使用提供的代码更改聊天气泡的字体或背景颜色。这是我的代码:


<body>

<div id="webchat" role="main">


</div>

<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>

<!--<script src="webchat.js"></script>-->

<script>

    const styleSetOptions= window.WebChat.createStyleSet({

        bubbleBackground: 'rgba(0, 0, 255, .1)',

        bubbleFromUserBackground: 'rgba(0, 255, 0, .1)'

    });


    var d1 = window.WebChat.createDirectLine({ token: '<secret token>' })

    var siteDomain = document.URL

    window.WebChat.renderWebChat(

        {

            directLine: Object.assign(

                {},

                d1,

                {

                    postActivity: activity => {

                        var newActivity = Object.assign({}, activity, { channelData: { "siteDomain": siteDomain } });

                        return d1.postActivity(newActivity);

                    }

                }),

        styleSetOptions 

        },

        document.getElementById('webchat')

    );

</script>

这不起作用。我已经下载了 CDN webchat.js。我也通过创建自己的 JS 文件并将其链接到我的HTML页面中将它集成到本地,但它不起作用。我错过了什么吗?


沧海一幻觉
浏览 153回答 2
2回答

一只甜甜圈

您必须将您创建的styleSet传递给renderWebChat。您的 styleSet 已创建,但未在任何地方使用。检查以下示例:&nbsp;const styleOptions = {&nbsp; &nbsp; &nbsp; &nbsp; bubbleBackground: 'rgba(0, 0, 255, .1)',&nbsp; &nbsp; &nbsp; &nbsp; bubbleFromUserBackground: 'rgba(0, 255, 0, .1)'&nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp;window.WebChat.renderWebChat(&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;directLine: window.WebChat.createDirectLine({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secret: 'YOUR_BOT_SECRET'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Passing 'styleOptions' when rendering Web Chat&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;styleOptions&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('webchat')&nbsp; &nbsp; &nbsp;);

烙印99

您需要将styleSetOptions对象分配给方法的styleOptions属性,rederWebchat即const styleSetOptions= window.WebChat.createStyleSet({&nbsp; &nbsp; bubbleBackground: 'rgba(0, 0, 255, .1)',&nbsp; &nbsp; bubbleFromUserBackground: 'rgba(0, 255, 0, .1)'});&nbsp;window.WebChat.renderWebChat(&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; directLine: Object.assign(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postActivity: activity => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newActivity = Object.assign({}, activity, { channelData: {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "siteDomain": siteDomain } });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return d1.postActivity(newActivity);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;styleOptions: styleSetOptions&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('webchat')&nbsp; &nbsp; );
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript