0

我正在尝试将 DirectLineJS 设置为在网站中工作,仅用于测试目的。我已经构建了 DirectLineJS 存储库并将 /built/directLine.js 添加到我的网站文件夹中,遵循此处的文档https://github.com/Microsoft/BotFramework-DirectLineJS

在 HTML 页面中,我只是使用一个按钮尝试通过直线发送消息

<html>

<head>
    <meta charset="UTF-8" />
    <title>Bot Chat</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="http://unpkg.com/botframework-directlinejs/directLine.js" type="javascript"></script>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <style>
        .wc-chatview-panel {
            width: 320px;
            height: 500px;
            position: relative;
        }

        .h2 {
            font-family: Segoe UI;
        }
    </style>
</head>

<body>

    <h2 style="font-family:Segoe UI;">Welcome to my custom Bot!</h2>
    <section class="container"> 

        <input id="clickMe" type="button" value="clickme" onclick="connectDirectLine();" />

    </section>

    <textarea id="myTextarea">
342 Alvin Road
Ducksburg</textarea>

    <p>Click the button to change the contents of the text area.</p>

    <button type="button" onclick="myFunction()">Try it</button>

    <script>
        function myFunction() {
            var text = document.getElementById("myTextarea").value;
            console.log(text)
        }
    </script></body>

</html>

<script>        
        function connectDirectLine() {

            import { DirectLine } from 'botframework-directlinejs';
            var directLine = new DirectLine({
                secret: "mySecret",
            });

            directLine.postActivity({
                from: { id: 'myUserId', name: 'myUserName' }, // required (from.name is optional)
                type: 'message',
                text: 'a message for you, Rudy'
            }).subscribe(
                id => console.log("Posted activity, assigned ID ", id),
                error => console.log("Error posting activity", error)
                );

            directLine.activity$
                .filter(activity => activity.type === 'message' && activity.from.id === 'yourBotHandle')
                .subscribe(
                message => console.log("received message ", message)
                );
        }
</script>
<html

当我运行网站时,我得到的错误是意外的令牌导入 import { DirectLine } from "botframework-directlinejs";

如何正确导入 botframework-directlinejs 文件以便使用 DirectLine 对象?

4

1 回答 1

2

您正在TypeScriptJavaScript.

要使用Direct Line将以下内容添加到HTML您的页面:

<script src="http://unpkg.com/botframework-directlinejs/directLine.js"/>

然后您应该删除您的Import语句,最后,将创建 DirectLine 对象的代码更新为:

 var directLine = new DirectLine.DirectLine({
     secret: "mySecret",
 });

注意 DirectLine.DirectLine

于 2017-04-27T18:19:03.267 回答