将数据从漫游器发送到DirectLine WebChat中的客户端-NodeJS Botframework

时间:2018-08-18 02:00:55

标签: node.js botframework

我使用以下代码将漫游器添加到了Intranet的HTML页面上:

 <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
    <style>    
          #bot{
               height: 600px;
              }
</style>
    <div>
        <div id="bot" />
    </div>
    <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
    <script>
        var user = {
            id: 'user-id',
            name: 'user name'
        };
        var botConnection = new BotChat.DirectLine({
            token: '[token]',
            user: user
        });
        BotChat.App({
            user: user,
            botConnection: botConnection,
            bot: { id: 'test', name: 'test' }

        }, document.getElementById("bot"));
        botConnection
            .postActivity({
                from: user,
                name: 'WelcomeDialog',
                type: 'event',
                value: ''
            })
            .subscribe(function (id) {
                console.log('"trigger requestWelcomeDialog" sent');
            });
    </script>

现在,我需要将数据发送回此客户端,以便在该HTML页面上执行,因为该页面存在于我们的Intranet(内部服务器)的上下文中,所以我希望从LUIS返回并进行定向到特定对话框,然后将所需的实体值从该对话框发送到要在此处执行的客户端,然后将结果发送回服务器,以便我可以向用户显示格式化的消息。

所以基本上,我需要在客户端(添加到我的内联网)和bot本身(以azure托管的nodejs应用)之间进行双向通信

更新

我在我的机器人中实现了反向通道,因此现在的代码如下:

  jQuery(function () {
            //get user name from the system
            var userid = _spPageContextInfo.userId;
            var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
            var requestHeaders = { "accept": "application/json;odata=verbose" };
            $.ajax({
                url: requestUri,
                contentType: "application/json;odata=verbose",
                headers: requestHeaders,
                success: onSuccess,
                error: onError
        });

        function onSuccess(data, request) {
            var loginName = data.d.Title;
            var user = {
                id: userid,
                name: loginName
            };
            var botConnection = new BotChat.DirectLine({
                token: '[token]',
                user: user
            });



            let FindPerson = function (personName) {
                let msg = `You asked for ${personName}`
                botConnection
                    .postActivity({ type: "event", value: msg, from: { id: "me" }, name: "FindPersonResultFound" })
                    .subscribe(id => console.log("success"));
            }

            BotChat.App({
                user: user,
                botConnection: botConnection,
                bot: { id: 'TestBot', name: 'test bot' }

            }, document.getElementById("bot"));
            botConnection
                .postActivity({
                    from: user,
                    name: 'WelcomeDialog',
                    type: 'event',
                    value: ''
                })
                .subscribe(function (id) {
                    console.log('"trigger requestWelcomeDialog" sent');
                });
            botConnection.activity$
                .filter(activity => activity.type === "event" && activity.name === "FindPerson")
                .subscribe(activity => FindPerson(activity.value))
        }

        function onError(error) {
            alert("error");
        }
    })

我的服务器端代码如下:

bot.on('event', function (message) {
    if (message.name == 'WelcomeDialog') {
        bot.beginDialog(message.address, message.name);
    }
    if (message.name === "FindPersonResultFound") {
        bot.beginDialog(message.address, message.name, message.value)
    }
});

但是,如果我发送与任何对话框相关的消息,则会重复发送,就像发件人是我一样:enter image description here

1 个答案:

答案 0 :(得分:0)

根据您的输出,我假设您的bot应用程序将包含一个默认的根对话框/,它将返回您输入的任何内容。

如果是这样,您可以尝试在机器人事件寄存器功能中将beginDialog更改为replaceDialog,以清除先前的对话框堆栈。

此外,您可以提供有关您的机器人应用程序的更多代码,以便我们进行更深入的了解。