botbuilder-core
Version:
Core components for Microsoft Bot Builder. Components in this library can run either in a browser or on the server.
359 lines • 20.8 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TelemetryLoggerMiddleware = void 0;
const botTelemetryClient_1 = require("./botTelemetryClient");
const telemetryConstants_1 = require("./telemetryConstants");
const turnContext_1 = require("./turnContext");
const botframework_schema_1 = require("botframework-schema");
// Internal helper duplicated from
// https://github.com/microsoft/botbuilder-js/commit/9277f901701ef270cf5af089e37e7aa7ab2579e1
function isTeamsChannelData(channelData) {
return typeof channelData === 'object';
}
/**
* Middleware for logging incoming, outgoing, updated or deleted Activity messages.
* Uses the botTelemetryClient interface.
*/
class TelemetryLoggerMiddleware {
/**
* Initializes a new instance of the TelemetryLoggerMiddleware class.
*
* @param telemetryClient The BotTelemetryClient used for logging.
* @param logPersonalInformation (Optional) Enable/Disable logging original message name within Application Insights.
*/
constructor(telemetryClient, logPersonalInformation = false) {
this._telemetryClient = telemetryClient || new botTelemetryClient_1.NullTelemetryClient();
this._logPersonalInformation = logPersonalInformation;
}
/**
* Gets a value indicating whether to log personal information that came from the user.
*
* @returns A value indicating whether to log personal information or not.
*/
get logPersonalInformation() {
return this._logPersonalInformation;
}
/**
* Gets the currently configured botTelemetryClient that logs the events.
*
* @returns The currently configured [BotTelemetryClient](xref:botbuilder-core.BotTelemetryClient) that logs the events.
*/
get telemetryClient() {
return this._telemetryClient;
}
/**
* Logs events based on incoming and outgoing activities using the botTelemetryClient class.
*
* @param context The context object for this turn.
* @param next The delegate to call to continue the bot middleware pipeline
*/
onTurn(context, next) {
return __awaiter(this, void 0, void 0, function* () {
if (context === null) {
throw new Error('context is null');
}
context.turnState.set('telemetryClient', this.telemetryClient);
// log incoming activity at beginning of turn
if (context.activity !== null) {
const activity = context.activity;
// Log Bot Message Received
yield this.onReceiveActivity(activity);
}
// hook up onSend pipeline
context.onSendActivities((ctx, activities, nextSend) => __awaiter(this, void 0, void 0, function* () {
// run full pipeline
const responses = yield nextSend();
activities.forEach((act) => __awaiter(this, void 0, void 0, function* () {
yield this.onSendActivity(act);
}));
return responses;
}));
// hook up update activity pipeline
context.onUpdateActivity((ctx, activity, nextUpdate) => __awaiter(this, void 0, void 0, function* () {
// run full pipeline
const response = yield nextUpdate();
yield this.onUpdateActivity(activity);
return response;
}));
// hook up delete activity pipeline
context.onDeleteActivity((ctx, reference, nextDelete) => __awaiter(this, void 0, void 0, function* () {
// run full pipeline
yield nextDelete();
const deletedActivity = turnContext_1.TurnContext.applyConversationReference({
type: botframework_schema_1.ActivityTypes.MessageDelete,
id: reference.activityId,
}, reference, false);
yield this.onDeleteActivity(deletedActivity);
}));
if (next !== null) {
yield next();
}
});
}
/**
* Invoked when a message is received from the user.
* Performs logging of telemetry data using the IBotTelemetryClient.TrackEvent() method.
* The event name logged is "BotMessageReceived".
*
* @param activity Current activity sent from user.
*/
onReceiveActivity(activity) {
return __awaiter(this, void 0, void 0, function* () {
this.telemetryClient.trackEvent({
name: TelemetryLoggerMiddleware.botMsgReceiveEvent,
properties: yield this.fillReceiveEventProperties(activity),
});
});
}
/**
* Invoked when the bot sends a message to the user.
* Performs logging of telemetry data using the botTelemetryClient.trackEvent() method.
* The event name logged is "BotMessageSend".
*
* @param activity Last activity sent from user.
*/
onSendActivity(activity) {
return __awaiter(this, void 0, void 0, function* () {
this.telemetryClient.trackEvent({
name: TelemetryLoggerMiddleware.botMsgSendEvent,
properties: yield this.fillSendEventProperties(activity),
});
});
}
/**
* Invoked when the bot updates a message.
* Performs logging of telemetry data using the botTelemetryClient.trackEvent() method.
* The event name used is "BotMessageUpdate".
*
* @param activity Current activity sent from user.
*/
onUpdateActivity(activity) {
return __awaiter(this, void 0, void 0, function* () {
this.telemetryClient.trackEvent({
name: TelemetryLoggerMiddleware.botMsgUpdateEvent,
properties: yield this.fillUpdateEventProperties(activity),
});
});
}
/**
* Invoked when the bot deletes a message.
* Performs logging of telemetry data using the botTelemetryClient.trackEvent() method.
* The event name used is "BotMessageDelete".
*
* @param activity Current activity sent from user.
*/
onDeleteActivity(activity) {
return __awaiter(this, void 0, void 0, function* () {
this.telemetryClient.trackEvent({
name: TelemetryLoggerMiddleware.botMsgDeleteEvent,
properties: yield this.fillDeleteEventProperties(activity),
});
});
}
/**
* Fills the Application Insights Custom Event properties for BotMessageReceived.
* These properties are logged in the custom event when a new message is received from the user.
*
* @param activity Last activity sent from user.
* @param telemetryProperties Additional properties to add to the event.
* @returns A dictionary that is sent as "Properties" to botTelemetryClient.trackEvent method.
*/
fillReceiveEventProperties(activity, telemetryProperties) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
return __awaiter(this, void 0, void 0, function* () {
const properties = {};
if (activity) {
properties[telemetryConstants_1.TelemetryConstants.fromIdProperty] = (_b = (_a = activity.from) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : '';
properties[telemetryConstants_1.TelemetryConstants.conversationIdProperty] = (_d = (_c = activity.conversation) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : '';
properties[telemetryConstants_1.TelemetryConstants.conversationNameProperty] = (_f = (_e = activity.conversation) === null || _e === void 0 ? void 0 : _e.name) !== null && _f !== void 0 ? _f : '';
properties[telemetryConstants_1.TelemetryConstants.localeProperty] = (_g = activity.locale) !== null && _g !== void 0 ? _g : '';
properties[telemetryConstants_1.TelemetryConstants.recipientIdProperty] = (_j = (_h = activity.recipient) === null || _h === void 0 ? void 0 : _h.id) !== null && _j !== void 0 ? _j : '';
properties[telemetryConstants_1.TelemetryConstants.recipientNameProperty] = (_l = (_k = activity.recipient) === null || _k === void 0 ? void 0 : _k.name) !== null && _l !== void 0 ? _l : '';
properties[telemetryConstants_1.TelemetryConstants.activityTypeProperty] = (_m = activity.type) !== null && _m !== void 0 ? _m : '';
properties[telemetryConstants_1.TelemetryConstants.activityIdProperty] = (_o = activity.id) !== null && _o !== void 0 ? _o : '';
// Use the LogPersonalInformation flag to toggle logging PII data, text and user name are common examples
if (this.logPersonalInformation) {
const fromName = (_q = (_p = activity.from) === null || _p === void 0 ? void 0 : _p.name) === null || _q === void 0 ? void 0 : _q.trim();
if (fromName) {
properties[telemetryConstants_1.TelemetryConstants.fromNameProperty] = fromName;
}
const activityText = (_r = activity.text) === null || _r === void 0 ? void 0 : _r.trim();
if (activityText) {
properties[telemetryConstants_1.TelemetryConstants.textProperty] = activityText;
}
const activitySpeak = (_s = activity.speak) === null || _s === void 0 ? void 0 : _s.trim();
if (activitySpeak) {
properties[telemetryConstants_1.TelemetryConstants.speakProperty] = activitySpeak;
}
}
// Additional Properties can override "stock" properties.
if (telemetryProperties) {
return Object.assign({}, properties, telemetryProperties);
}
}
this.populateAdditionalChannelProperties(activity, properties);
// Additional Properties can override "stock" properties.
if (telemetryProperties) {
return Object.assign({}, properties, telemetryProperties);
}
return properties;
});
}
/**
* Fills the Application Insights Custom Event properties for BotMessageSend.
* These properties are logged in the custom event when a response message is sent by the Bot to the user.
*
* @param activity - Last activity sent from user.
* @param telemetryProperties Additional properties to add to the event.
* @returns A dictionary that is sent as "Properties" to botTelemetryClient.trackEvent method.
*/
fillSendEventProperties(activity, telemetryProperties) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
return __awaiter(this, void 0, void 0, function* () {
const properties = {};
if (activity) {
properties[telemetryConstants_1.TelemetryConstants.replyActivityIdProperty] = (_a = activity.replyToId) !== null && _a !== void 0 ? _a : '';
properties[telemetryConstants_1.TelemetryConstants.recipientIdProperty] = (_c = (_b = activity.recipient) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : '';
properties[telemetryConstants_1.TelemetryConstants.conversationIdProperty] = (_e = (_d = activity.conversation) === null || _d === void 0 ? void 0 : _d.id) !== null && _e !== void 0 ? _e : '';
properties[telemetryConstants_1.TelemetryConstants.conversationNameProperty] = (_g = (_f = activity.conversation) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : '';
properties[telemetryConstants_1.TelemetryConstants.localeProperty] = (_h = activity.locale) !== null && _h !== void 0 ? _h : '';
properties[telemetryConstants_1.TelemetryConstants.activityTypeProperty] = (_j = activity.type) !== null && _j !== void 0 ? _j : '';
properties[telemetryConstants_1.TelemetryConstants.activityIdProperty] = (_k = activity.id) !== null && _k !== void 0 ? _k : '';
// Use the LogPersonalInformation flag to toggle logging PII data, text and user name are common examples
if (this.logPersonalInformation) {
const recipientName = (_m = (_l = activity.recipient) === null || _l === void 0 ? void 0 : _l.name) === null || _m === void 0 ? void 0 : _m.trim();
if (recipientName) {
properties[telemetryConstants_1.TelemetryConstants.recipientNameProperty] = recipientName;
}
const activityText = (_o = activity.text) === null || _o === void 0 ? void 0 : _o.trim();
if (activityText) {
properties[telemetryConstants_1.TelemetryConstants.textProperty] = activityText;
}
const activitySpeak = (_p = activity.speak) === null || _p === void 0 ? void 0 : _p.trim();
if (activitySpeak) {
properties[telemetryConstants_1.TelemetryConstants.speakProperty] = activitySpeak;
}
if ((_q = activity.attachments) === null || _q === void 0 ? void 0 : _q.length) {
properties[telemetryConstants_1.TelemetryConstants.attachmentsProperty] = JSON.stringify(activity.attachments);
}
}
// Additional Properties can override "stock" properties.
if (telemetryProperties) {
return Object.assign({}, properties, telemetryProperties);
}
}
return properties;
});
}
/**
* Fills the event properties for BotMessageUpdate.
* These properties are logged when an activity message is updated by the Bot.
* For example, if a card is interacted with by the use, and the card needs to be updated to reflect
* some interaction.
*
* @param activity - Last activity sent from user.
* @param telemetryProperties Additional properties to add to the event.
* @returns A dictionary that is sent as "Properties" to botTelemetryClient.trackEvent method.
*/
fillUpdateEventProperties(activity, telemetryProperties) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
return __awaiter(this, void 0, void 0, function* () {
const properties = {};
if (activity) {
properties[telemetryConstants_1.TelemetryConstants.recipientIdProperty] = (_b = (_a = activity.recipient) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : '';
properties[telemetryConstants_1.TelemetryConstants.conversationIdProperty] = (_d = (_c = activity.conversation) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : '';
properties[telemetryConstants_1.TelemetryConstants.conversationNameProperty] = (_f = (_e = activity.conversation) === null || _e === void 0 ? void 0 : _e.name) !== null && _f !== void 0 ? _f : '';
properties[telemetryConstants_1.TelemetryConstants.localeProperty] = (_g = activity.locale) !== null && _g !== void 0 ? _g : '';
properties[telemetryConstants_1.TelemetryConstants.activityTypeProperty] = (_h = activity.type) !== null && _h !== void 0 ? _h : '';
properties[telemetryConstants_1.TelemetryConstants.activityIdProperty] = (_j = activity.id) !== null && _j !== void 0 ? _j : '';
// Use the LogPersonalInformation flag to toggle logging PII data, text is a common example
if (this.logPersonalInformation) {
const activityText = (_k = activity.text) === null || _k === void 0 ? void 0 : _k.trim();
if (activityText) {
properties[telemetryConstants_1.TelemetryConstants.textProperty] = activityText;
}
}
// Additional Properties can override "stock" properties.
if (telemetryProperties) {
return Object.assign({}, properties, telemetryProperties);
}
}
return properties;
});
}
/**
* Fills the Application Insights Custom Event properties for BotMessageDelete.
* These properties are logged in the custom event when an activity message is deleted by the Bot. This is a relatively rare case.
*
* @param activity - Last activity sent from user.
* @param telemetryProperties Additional properties to add to the event.
* @returns A dictionary that is sent as "Properties" to botTelemetryClient.trackEvent method.
*/
fillDeleteEventProperties(activity, telemetryProperties) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
return __awaiter(this, void 0, void 0, function* () {
const properties = {};
if (activity) {
properties[telemetryConstants_1.TelemetryConstants.channelIdProperty] = (_a = activity.channelId) !== null && _a !== void 0 ? _a : '';
properties[telemetryConstants_1.TelemetryConstants.recipientIdProperty] = (_c = (_b = activity.recipient) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : '';
properties[telemetryConstants_1.TelemetryConstants.conversationIdProperty] = (_e = (_d = activity.conversation) === null || _d === void 0 ? void 0 : _d.id) !== null && _e !== void 0 ? _e : '';
properties[telemetryConstants_1.TelemetryConstants.conversationNameProperty] = (_g = (_f = activity.conversation) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : '';
properties[telemetryConstants_1.TelemetryConstants.activityTypeProperty] = (_h = activity.type) !== null && _h !== void 0 ? _h : '';
properties[telemetryConstants_1.TelemetryConstants.activityIdProperty] = (_j = activity.id) !== null && _j !== void 0 ? _j : '';
// Additional Properties can override "stock" properties.
if (telemetryProperties) {
return Object.assign({}, properties, telemetryProperties);
}
}
return properties;
});
}
populateAdditionalChannelProperties(activity, properties) {
var _a, _b, _c, _d;
if (activity) {
const channelData = activity.channelData;
switch (activity.channelId) {
case 'msteams':
properties.TeamsUserAadObjectId = (_b = (_a = activity.from) === null || _a === void 0 ? void 0 : _a.aadObjectId) !== null && _b !== void 0 ? _b : '';
if (isTeamsChannelData(channelData)) {
properties.TeamsTenantId = (_d = (_c = channelData.tenant) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : '';
if (channelData.team) {
properties.TeamsTeamInfo = JSON.stringify(channelData.team);
}
}
break;
default:
break;
}
}
}
}
exports.TelemetryLoggerMiddleware = TelemetryLoggerMiddleware;
/**
* The name of the event when when new message is received from the user.
*/
TelemetryLoggerMiddleware.botMsgReceiveEvent = 'BotMessageReceived';
/**
* The name of the event when a message is updated by the bot.
*/
TelemetryLoggerMiddleware.botMsgSendEvent = 'BotMessageSend';
/**
* The name of the event when a message is updated by the bot.
*/
TelemetryLoggerMiddleware.botMsgUpdateEvent = 'BotMessageUpdate';
/**
* The name of the event when a message is deleted by the bot.
*/
TelemetryLoggerMiddleware.botMsgDeleteEvent = 'BotMessageDelete';
//# sourceMappingURL=telemetryLoggerMiddleware.js.map