2
votes

I need to send QnA users questions and answers to my Azure bot insights using telemetry. Already tried this tutorial :

https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-telemetry?view=azure-bot-service-4.0&tabs=javascript

And this SO posts:

Thing is, first it's done for LUIS and gives no additional info to Insights, also nothing for QnA...second ones are written for C#...

I need to send question and answer to customEvents logs on Azure insights using NodeJS but I can't find how, any help ?

Thanks in advance.

///// EDIT:

Here's what I got so far (only posted the code related to the telemetry and QnA that's already working fine):

Index.js

const { ApplicationInsightsTelemetryClient, TelemetryInitializerMiddleware } = require('botbuilder-applicationinsights');
const { TelemetryLoggerMiddleware } = require('botbuilder-core');

function getTelemetryClient(instrumentationKey) {
    if (instrumentationKey) {
        return new ApplicationInsightsTelemetryClient(instrumentationKey);
    }
    return new NullTelemetryClient();
}

const server = restify.createServer();
server.use(restify.plugins.bodyParser());


var telemetryClient = getTelemetryClient(process.env.InstrumentationKey);
var telemetryLoggerMiddleware = new TelemetryLoggerMiddleware(telemetryClient);
var initializerMiddleware = new TelemetryInitializerMiddleware(telemetryLoggerMiddleware);
adapter.use(initializerMiddleware);


const mybot = new MYBOT(conversationState,userState, telemetryClient);

mybot.js

class MYBOT extends ActivityHandler {
    constructor(conversationState,userState,telemetryClient) {
        super();

        this.conversationState = conversationState;
        this.userState = userState;
        this.telemetryClient = telemetryClient;
    }
}

//This is how I get my qna result:
console.log(this.telemetryClient);
var result = await this.qnaMaker.getAnswers(context);

As You can see, I pass the telemetryClient to the bot file, and if I console log that item I get the complete telemetry object, but how I pass it the user question and answer so its save on insights customevents ??

1

1 Answers

2
votes

Found a way to it, in case people that's looking for one of the possible solutions for Node may need it :

Basically, We use the same telemetry code process described in oficial documentation for instancing telemetry on index.js :

const { ApplicationInsightsTelemetryClient, TelemetryInitializerMiddleware } = require('botbuilder-applicationinsights');
const { TelemetryLoggerMiddleware } = require('botbuilder-core');

function getTelemetryClient(instrumentationKey) {
    if (instrumentationKey) {
        return new ApplicationInsightsTelemetryClient(instrumentationKey);
    }
    return new NullTelemetryClient();
}

const server = restify.createServer();
server.use(restify.plugins.bodyParser());


var telemetryClient = getTelemetryClient(process.env.InstrumentationKey);
var telemetryLoggerMiddleware = new TelemetryLoggerMiddleware(telemetryClient);
var initializerMiddleware = new TelemetryInitializerMiddleware(telemetryLoggerMiddleware);
adapter.use(initializerMiddleware);


const mybot = new MYBOT(conversationState,userState, telemetryClient);

Then, we pass it to the bot file (bot.js or the one you´re using):

class MYBOT extends ActivityHandler {
    constructor(conversationState,userState,telemetryClient) {
        super();

        this.conversationState = conversationState;
        this.userState = userState;
        this.telemetryClient = telemetryClient;
    }
}

And later in code, You can use telemetry.trackEvent method (Official docs are only in C#), but basically, it allows you to create a custom event you want to track in specifics events in your code, like when You're bot has an error or doesn´t found an answer to user. Code according to previous lines would be like this:

this.telemetryClient.trackEvent(
        {name: "myEvent", 
         properties: {my_user_question: 'Context activity text here or your captured question', 
         my_bot_answer: 'bot reply or whatever'}
        }
); // name and properties are part of the sintaxys, values inside properties object as you may need.

That way, on Azure insights customEvents model You will see records captured with the event name you used, also, with the properties objects as a dict in customdimensions field.