如何解决BotChat.App未捕获ReferenceError?

时间:2019-05-15 15:34:27

标签: c# html botframework azure-bot-service direct-line-botframework

我正在尝试使用在Embedding a bot on a website中找到的一些代码。 我知道已经进行了一些更新,我的问题是BotChat.App是否可以使用。 因为我使用BotChat.App并给了我这个错误Uncaught ReferenceError:未定义BotChat。

我尝试将其更改为window.WebChat.renderWebChat,它可以工作,但在此行给了我错误document.getElementsByClassName(“ wc-header”)[0] .setAttribute(“ id”,“ chatbotheader”);


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
   <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
  <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
    <style>
        #mychat {
            margin: 10px;
            position: fixed;
            bottom: 30px;
            right: 10px;
            z-index: 1000000;
        }
    </style>
</head>
<body>
    <div id="container">
        <h1>Hello World</h1>
        <!--other page contents-->
        <img id="mychat" src="https://i.stack.imgur.com/RD7i4.png" style="float:right" />
    </div>
</body>
</html>
<script>
    (function () {
        var div = document.createElement("div");
        document.getElementsByTagName('body')[0].appendChild(div);
        div.outerHTML = "<div id='botDiv' style='width: 400px; height: 0px; margin:10px; position: fixed; bottom: 0; right:0; z-index: 1000;><div  id='botTitleBar' style='height: 40px; width: 400px; position:fixed; cursor: pointer;'></div></div>";
        BotChat.App({
            directLine: { secret: 'myAppSecret' },
        }, document.getElementById("botDiv"));

        document.getElementsByClassName("wc-header")[0].setAttribute("id", "chatbotheader");
        document.querySelector('body').addEventListener('click', function (e) {
            e.target.matches = e.target.matches || e.target.msMatchesSelector;
            if (e.target.matches('#chatbotheader')) {
                var botDiv = document.querySelector('#botDiv');

                botDiv.style.height = "0px";

                document.getElementById("mychat").style.display = "block";
            };
        });

        document.getElementById("mychat").addEventListener("click", function (e) {

            document.getElementById("botDiv").style.height = '500px';

            e.target.style.display = "none";
        })
    }());
</script>

未捕获的ReferenceError:未定义BotChat html:31

1 个答案:

答案 0 :(得分:1)

该错误表明这是由于以下事实造成的:尽管您使用BotChat.App,但没有引用BotChat

尝试在标题部分导入<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>