Search code examples
javascriptc#direct-line-botframeworkweb-chatmicrosoft-health-bot

Proactively Triggering a Health Bot Scenario Through Code Throws an Error


I think it has something to do with the json web token that's causing the error. I think I may have created the json web token incorrectly but not 100% sure. And if I did create it incorrectly, not sure where I screwed up on. I'm stuck on this part. Any help is much appreciated.

On a side note, this is a Microsoft MVC web application project that uses .NET Core 3.1. This is for a web chat that is connected to the Microsoft Health Bot. The client-side javascript code is based on the Health Bot Container Sample code.

Objective: To proactively trigger a Microsoft Health Bot scenario through javascript code.

Error response received on client-side: Failed to load resource: the server responded with a status of 502 ().

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

Server-side C# Code:

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();
}

Client-side Javascript code:

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);
        }
    }

});


Solution

  • I was able to get this to work with Microsoft's help. Because the chat conversation is not an authenticated conversation, I won't need the json web token. After I took out the usage of the json web token, it worked. I was able to load a specific scenario through code upon the launch of the web chat.