Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

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

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: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...