Send data from bot to client in DirectLine WebChat - NodeJS Botframework

蓝咒 提交于 2019-12-25 02:26:49

问题


I added bot to an HTML page on our intranet using the following code:

 <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>

Now, I need to send data back to this client, to be executed on that HTML page, since the page exists within the context of our intranet (internal servers), so I want to have the intent return from LUIS and directed to specific dialog, then send the required entity value from this dialog to the client to be executed there, then send the result back to the server so I can display a formatted message to the user.

So basically, I would need to have 2-way communication between the client (added to my intranet) and the bot itself (the nodejs app hosted in azure)

Update:

I implemented the backchannel in my bot, so now the code looks like this:

  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");
        }
    })

My server side code looks like this:

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)
    }
});

However, if I send a message that's related to any dialog, it gets repeated as if the sender is me:


回答1:


According to your output, I assumpt that your bot application would contain a default root dialog / which will return any thing you input.

If so, you can try to change beginDialog to replaceDialog in your bot event register functions, to clear the previous dialog stack.

Also, you could provide more code about your bot application, so that we can have a deeper looking over.



来源:https://stackoverflow.com/questions/51904642/send-data-from-bot-to-client-in-directline-webchat-nodejs-botframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!