首页 > 解决方案 > 通过代码主动触发 Health Bot 场景会引发错误

问题描述

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

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

目标: 通过 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);
        }
    }

});

标签: javascriptc#direct-line-botframeworkweb-chatmicrosoft-health-bot

解决方案


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


推荐阅读