如何使用直线API使聊天机器人显示为实时聊天?

时间:2018-06-19 11:37:29

标签: botframework direct-line-botframework

<!DOCTYPE html>
<html>
  <head>
    <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
  </head>
  <body>
    <div id="bot"/>
    <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
    <script>
      BotChat.App({
        directLine: { secret: direct_line_secret },
        user: { id: 'userid' },
        bot: { id: 'botid' },
        resize: 'detect'
      }, document.getElementById("bot"));
    </script>
  </body>
</html>

我有这段代码可以使用直接Line API而不是通常的iframe将机器人作为实时聊天嵌入到机器人中,但是当我放入我的Directline秘密密钥时,Bot会占据整个网页。我需要它显示在网页的右下角,并在用户单击时作为生活聊天弹出。请有人指导我实现这一目标。谢谢

2 个答案:

答案 0 :(得分:6)

您的问题与显示div(包括漫游器)的方式有关:<div id="bot"/>

您可以设置此div的样式以使其显示为您想要的样式; bot Webchat Github项目上提供了一些示例:

我强烈建议您看一下第一个样本,该样本演示了窄,正常和宽div

答案 1 :(得分:2)

如Nicolas R建议的那样,您可以设置容器div <div id="bot"/>的样式,以将其放置在网页的右下角。我在一个项目中达到了相同的要求,以下示例代码对我有用,您可以参考它。

<!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://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
    <style>
        .wc-chatview-panel {
            width: 350px;
            height: 450px;
            position: relative;
        }

        #bot {
            position: fixed;
            bottom: 0;
            right: 0;
        }

        .closechat {
            top: 405px !important;
        }
    </style>
</head>
<body>
    <div id="bot"></div>
</body>
</html>
<script>
    BotChat.App({
        directLine: { secret: "{your_directline_secret}" },
        user: { id: 'You' },
        bot: { id: '{Your_BotId}' },
        resize: 'detect'
    }, document.getElementById("bot"));

    $(function () {
        $("div.wc-header").prop("isopen", "true");

        $("div.wc-header").click(function () {
            var isopen = $(this).prop("isopen");
            //alert(isopen);
            if (isopen == "true") {
                $("div.wc-chatview-panel").addClass("closechat");
                $("div.wc-header").prop("isopen", "false");
            } else {
                $("div.wc-chatview-panel.closechat").removeClass("closechat");
                $("div.wc-header").prop("isopen", "true");
            }
        })
    })
</script>

测试结果:

enter image description here