【问题标题】:How to change username/userid in custom webchat implementation bot framework如何在自定义网络聊天实现机器人框架中更改用户名/用户 ID
【发布时间】:2018-12-16 17:50:24
【问题描述】:

我需要在自定义网络聊天控件中动态填充用户名。我知道网络聊天会呈现脚本中存在的用户名,但我该如何更改呢?我什至尝试使用 .NET SDK 从服务器端发送具有不同用户名的活动,但仍然无法实现。

Custom Web Chat

<script>

            var user = {
                id: 'user-id',
                name: 'user name'
            };

            var botConnection = new BotChat.DirectLine({
                token: '[directlinesecret]',
                user: user
            });

            BotChat.App({
                user: user,
                botConnection: botConnection,
                bot: { id: 'bot-id', name: 'bot name' },
                resize: 'detect'
            }, document.getElementById("bot"));

            botConnection
                .postActivity({
                    from: user,
                    name: 'requestWelcomeDialog',
                    type: 'event',
                    value: ''
                })
                .subscribe(function (id) {
                    console.log('"trigger requestWelcomeDialog" sent');
                });

        </script>

【问题讨论】:

    标签: c# azure botframework bots direct-line-botframework


    【解决方案1】:

    我需要在自定义网络聊天控件中动态填充用户名。

    我假设您希望将网络聊天嵌入您的网站并根据当前用户(登录用户)动态指定user: { id: user_id, name: user_name },您可以尝试在用户登录您的网站时将用户信息保留在 cookie 中,然后您就可以从cookie中检索当前用户的信息并发起网络聊天。

    例子:

    <!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://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
        <style>
            .wc-chatview-panel {
                width: 350px;
                height: 500px;
                position: relative;
            }
        </style>
    </head>
    <body onload="initiatewebchat()">
       <div id="chat"></div>
    </body>
    </html>
    <script>
        function initiatewebchat() {
    
            var botuser = getCookie("botuser");
    
            if (botuser!=null) {
                var userinfo = JSON.parse(botuser);
    
                var botConnection = new BotChat.DirectLine({
                    secret: '{directline_secret_here}',
                });
    
                var user = {
                    id: userinfo.user_id,
                    name: userinfo.user_name
                };
    
                var bot = {
                    id: 'fehanbasicbot',
                    name: 'botname'
                };
    
                BotChat.App({
                    botConnection: botConnection,
                    user: user,
                    bot: bot,
                }, document.getElementById('chat'));
    
            } else {
                alert("do not find user identity for web chat! please login to our website.");
            }
        }
    
        function getCookie(cname) {
            var name = cname + "=";
            var decodedCookie = decodeURIComponent(document.cookie);
            var ca = decodedCookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') {
                    c = c.substring(1);
                }
                if (c.indexOf(name) == 0) {
                    return c.substring(name.length, c.length);
                }
            }
            return "";
        }
    </script>
    

    测试结果:

    注意:

    在上面的测试样本中,我在 cookie 中保留了"{"user_id":"0001","user_name":"user1"}"

    【讨论】:

    • 您好,韩飞,感谢您的快速输入。在我的情况下,用户没有登录,但是是的,我们将状态存储在 Azure 表存储中,其中存在用户名(机器人在订购商品之前询问用户名称)。我可以使用 C# SDK 中的状态管理从天蓝色表存储中获取名称,但无法弄清楚如何在 javascript 中发送它。抱歉,因为我是新手,所以我不知道我是否在这里遗漏了什么。
    • 如果可以的话,可以在页面加载时提示用户输入用户名prompt() method,然后可以传入当前名称的用户名发起网络聊天。
    • 当然感谢韩飞,但正在寻找从 azure 表存储中检索用户名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多