通过代码主动触发Health Bot场景抛错
Proactively Triggering a Health Bot Scenario Through Code Throws an Error
我认为它与导致错误的 json 网络令牌有关。我想我可能错误地创建了 json 网络令牌,但不是 100% 确定。如果我确实创建错误,我不确定我在哪里搞砸了。我被困在这部分了。非常感谢任何帮助。
附带说明一下,这是一个使用 .NET Core 3.1 的 Microsoft MVC Web 应用程序项目。这是用于连接到 Microsoft Health Bot 的网络聊天。客户端 javascript 代码基于 Health Bot Container Sample 代码。
Objective:
通过 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);
}
}
});
在 Microsoft 的帮助下,我能够让它正常工作。因为聊天对话不是经过身份验证的对话,所以我不需要 json 网络令牌。在我取出json web token的用法后,它起作用了。我能够在启动网络聊天时通过代码加载特定场景。
我认为它与导致错误的 json 网络令牌有关。我想我可能错误地创建了 json 网络令牌,但不是 100% 确定。如果我确实创建错误,我不确定我在哪里搞砸了。我被困在这部分了。非常感谢任何帮助。
附带说明一下,这是一个使用 .NET Core 3.1 的 Microsoft MVC Web 应用程序项目。这是用于连接到 Microsoft Health Bot 的网络聊天。客户端 javascript 代码基于 Health Bot Container Sample 代码。
Objective: 通过 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);
}
}
});
在 Microsoft 的帮助下,我能够让它正常工作。因为聊天对话不是经过身份验证的对话,所以我不需要 json 网络令牌。在我取出json web token的用法后,它起作用了。我能够在启动网络聊天时通过代码加载特定场景。