【问题标题】:Proactively Triggering a Health Bot Scenario Through Code Throws an Error通过代码主动触发 Health Bot 场景会引发错误
【发布时间】:2020-10-29 04:07:30
【问题描述】:

我认为这与导致错误的 json Web 令牌有关。我想我可能错误地创建了 json Web 令牌,但不是 100% 确定。如果我确实创建不正确,不确定我在哪里搞砸了。我被困在这部分。非常感谢任何帮助。

附带说明,这是一个使用 .NET Core 3.1 的 Microsoft MVC Web 应用程序项目。这适用于连接到 Microsoft Health Bot 的网络聊天。客户端javascript代码基于Health Bot Container Sample代码。

目标: 通过 javascript 代码主动触发 Microsoft Health Bot 场景。

客户端收到错误响应: 加载资源失败:服务器响应状态为 502 ()。

{
  "error": {
    "code": "BadArgument",
    "message": "Missing token or secret"
  }
}

服务器端 C# 代码:

using ........
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
private string GenerateJsonWebToken()
{
    // Using the direct line secret key, create a new
    // symmetric security key instance.
    var tmpSecretKey = Encoding.UTF8.GetBytes("secretKey");
    SymmetricSecurityKey tmpSecurityKey = new SymmetricSecurityKey(tmpSecretKey);

    // Using the symmetric security key, create a new 
    // signing credential instance.
    SigningCredentials tmpSigningCreds = new SigningCredentials(tmpSecurityKey, SecurityAlgorithms.HmacSha512Signature);

    // New claims collection that contains some user info.
    List<Claim> tmpClaims = new List<Claim>();
    tmpClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, "userId"));
    tmpClaims.Add(new Claim(JwtRegisteredClaimNames.Sub, "userName"));

    // JWT security token instance.
    JwtSecurityToken tmpSecurityToken = new JwtSecurityToken(claims: tmpClaims, signingCredentials: tmpSigningCreds);

    // JWT security token handler to create json web tokens.
    JwtSecurityTokenHandler tmpSecurityTokenHandler = new JwtSecurityTokenHandler();

    string tmpResult = tmpSecurityTokenHandler.WriteToken(tmpSecurityToken);

    return tmpResult;
}
public IActionResult Index()
{
    // Security token.
    ViewBag.JsonWebToken = GenerateJsonWebToken();

    // Scenario to launch when the web chat is launched.
    ViewBag.AutomaticWelcomeScenario = this._automaticWelcomeScenario;

    return View();
}

客户端 Javascript 代码:

const tmpJWT = "@ViewBag.JsonWebToken";

// Create our own store where we could specify a scenario to use as an automatic welcome scenario
// to greet the user when the web chat is first started up.
// Sample code:
// https://github.com/microsoft/HealthBotContainerSample/blob/master/public/index.js
var tmpStore = window.WebChat.createStore({}, function (store) {

    return function (next) {
        return function (action) {

            if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {

                store.dispatch({
                    type: 'DIRECT_LINE/POST_ACTIVITY',
                    meta: { method: 'keyboard' },
                    payload: {
                        activity: {
                            type: "invoke",
                            name: "InitConversation",
                            locale: "en-US",
                            value: {

                                // Must use for authenticated conversation.
                                jsonWebToken: tmpJWT,

                                // Use the following activity to proactively invoke a bot scenario.
                                triggeredScenario: {
                                    trigger: "@ViewBag.AutomaticWelcomeScenario",
                                    args: {
                                        myVariable1: "Test Value 1",
                                        myVariable2: "Test Value 2"
                                    }
                                }
                            }
                        }
                    }
                });
            }

            return next(action);
        }
    }

});

【问题讨论】:

  • 所以您要做的就是在网络聊天加载时启动您的机器人? Health Bot 不会自动执行此操作吗?我对您尝试实现的自定义功能感到困惑
  • 嗨@KyleDelaney,你是对的。在“配置->对话”页面的“交互”选项卡上的健康机器人管理站点,可以通过从下拉列表中选择某个场景来设置“自动欢迎场景”。所以通过 UI,我们可以做到这一点。我试图弄清楚如何做到这一点,但通过 javascript 代码。在一些 Microsoft SME 的帮助下,我终于能够让它在这方面工作。我发现我不需要使用 json Web 令牌,因为这不是经过身份验证的对话。我错过了那部分。现在一切正常
  • 您想将其作为答案发布以便您接受吗?

标签: javascript c# direct-line-botframework web-chat microsoft-health-bot


【解决方案1】:

在 Microsoft 的帮助下,我得以完成这项工作。因为聊天对话不是经过身份验证的对话,所以我不需要 json Web 令牌。在我取出 json web token 的用法后,它起作用了。我能够在网络聊天启动时通过代码加载特定场景。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-06
    • 2018-06-16
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 2020-12-01
    • 2023-03-20
    • 2015-01-14
    相关资源
    最近更新 更多