Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
protected createEmulatorContext(session: ConversationReference): TurnContext {
// Get emulators adapter
const emulator = this.findAdapter('emulator');
if (!emulator) {
throw new Error(`BotDebugger: Cannot log debug activity to emulator. Adapter not found.`)
}
// Create request with session address
const request = TurnContext.applyConversationReference({
type: ActivityTypes.Event,
name: 'debuggerSession'
}, session);
// Return context for session
return new TurnContext(emulator, request);
}
const adapter: BotFrameworkAdapter = (context.adapter as BotFrameworkAdapter);
// Cache the ClaimsIdentity and ConnectorClient on the context so that it's available inside of the bot's logic.
context.turnState.set(adapter.BotIdentityKey, claimsIdentity);
context.turnState.set(this.SkillConversationReferenceKey, skillConversationReference);
activity = TurnContext.applyConversationReference(activity, conversationReference) as Activity;
const client = adapter.createConnectorClient(activity.serviceUrl);
context.turnState.set(adapter.ConnectorClientKey, client);
context.activity.id = replyToActivityId;
switch (activity.type) {
case ActivityTypes.EndOfConversation:
await this.conversationIdFactory.deleteConversationReference(conversationId);
SkillHandler.applyEoCToTurnContextActivity(context, activity);
await this.bot.run(context);
break;
case ActivityTypes.Event:
SkillHandler.applyEventToTurnContextActivity(context, activity);
await this.bot.run(context);
break;
default:
await context.sendActivity(activity);
break;
}
};
protected async onTurn(context: TurnContext, next: () => Promise): Promise {
const {channelId, type, name, value} = context.activity;
// Check for debugging mode
const log: Partial[] = [];
const debugging = channelId === 'emulator' || await this.inDebugSession(context);
if (debugging) {
// Check for debug commands from emulator
if (type === ActivityTypes.Event && name === 'debuggerCommand') {
const command: BotDebuggerCommand = value;
const relatesTo = command.relatesTo;
switch (command.name) {
case 'loadBotState':
const state = await this.loadBotState(relatesTo);
await context.sendActivity(BotDebugger.trace(state, 'https://www.botframework.com/schemas/botState', 'BotState', 'Bot State'));
break;
case 'saveBotState':
await this.saveBotState(relatesTo, command.value as PersistedState);
break;
}
return;
}
// Log all activities and changes
// - We don't need to do this for the emulator as its already seeing everything
public get displayLabel(): string {
if (typeof this._value === 'object') {
switch (this._value.type) {
case ActivityTypes.Message:
return this._value.text;
case ActivityTypes.Event:
return `event:${this._value.name}`;
default:
return this._value.type;
}
} else {
return this._value || '';
}
}
private isTokenResponseEvent(context: TurnContext): boolean {
const activity: Activity = context.activity;
return activity.type === ActivityTypes.Event && activity.name === 'tokens/response';
}
protected shouldHandleSentActivity(activity: Partial): boolean {
switch (activity.type) {
case ActivityTypes.EndOfConversation:
case ActivityTypes.Event:
return true;
default:
return false;
}
}