Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
protected async getEmulatorSession(context: TurnContext): Promise {
const memory = await this.getMemory(context.activity.conversation.id);
const {id} = memory;
const conversationReference = TurnContext.getConversationReference(context.activity);
// If this activity is not from an Emulator session,
// we should have a conversation Id from the ConnectorAPI
// used to start a debug session.
if (context.activity.channelId !== 'emulator') {
conversationReference.conversation.id = id;
}
conversationReference.channelId = 'emulator';
conversationReference.serviceUrl = process.env.EMULATOR_URL;
return conversationReference as ConversationReference;
}
// Execute turn
try {
await next();
} catch (err) {
// Log error
if (debugging) {
log.push(BotDebugger.trace({message: err.toString()}, 'https://www.botframework.com/schemas/error', 'TurnError', 'Turn Error'));
}
throw err;
} finally {
// Dump debug info to emulator
if (debugging) {
// Append snapshot of turns final bot state to log
let state = context.turnState.get(DialogManager.PersistedStateSnapshotKey);
if (!state) {
state = await this.loadBotState(TurnContext.getConversationReference(context.activity));
}
log.push(BotDebugger.trace(state, 'https://www.botframework.com/schemas/botState', 'BotState', 'Bot State'));
// Send log to emulator
await this.sendToEmulator(context, log);
}
}
}
private async processActivity(claimsIdentity: ClaimsIdentity, conversationId: string, replyToActivityId: string, activity: Activity): Promise {
const conversationReference = await this.conversationIdFactory.getConversationReference(conversationId);
if (!conversationReference) {
throw new Error('conversationReference not found.');
}
const skillConversationReference = TurnContext.getConversationReference(activity);
/**
* Callback passed to the BotFrameworkAdapter.createConversation() call.
* This function does the following:
* - Caches the ClaimsIdentity on the TurnContext.turnState
* - Applies the correct ConversationReference to the Activity for sending to the user-router conversation.
* - For EndOfConversation Activities received from the Skill, removes the ConversationReference from the
* ConversationIdFactory
*/
const callback = async (context: TurnContext): Promise => {
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);
if (activity.value.hasOwnProperty('conversationId')) {
const ref = decodeConversationId(activity.value['conversationId']);
activity.value['conversationId'] = ref.conversation.id;
if (ref.serviceUrl) { activity.value['serviceUrl'] = ref.serviceUrl }
}
// Re-address passed in activities
// - Similar to above, we need to fixup the conversationId and serviceUrl embedded in any
// activities sent to us by a skill. We'll preserve the conversationId for the skill
// instance by moving it to a relatesTo field off the activity. That way the bot can still
// tell which skill it was invoked by.
// - NOTE: we're currently assuming that the "from" information sent by a skill will contain
// the ID of the bot and not the skill. Therefore there's no need for us to correct it here.
if (activity.value.hasOwnProperty('activity')) {
const a: Partial = activity.value['activity'];
a.relatesTo = TurnContext.getConversationReference(a) as ConversationReference;
const ref = decodeConversationId(a.conversation.id);
a.conversation.id = ref.conversation.id;
if (ref.serviceUrl) { a.serviceUrl = ref.serviceUrl }
}
return activity;
}
public createSkillConversation(context: TurnContext): Partial {
const skillConversation = TurnContext.getConversationReference(context.activity);
const id = encodeURIComponent(createId());
const cid = encodeURIComponent(skillConversation.conversation.id);
const url = encodeURIComponent(skillConversation.serviceUrl);
skillConversation.conversation.id = `cid=${cid}&url=${url}&id=${id}`;
skillConversation.serviceUrl = this._settings.serviceUrl;
return skillConversation;
}
public async postToSkill(fromBotId: string, toSkill: BotFrameworkSkill, serviceUrl: string, activity: Activity): Promise {
const skillConversationId = await this.conversationIdFactory.createSkillConversationId(TurnContext.getConversationReference(activity) as ConversationReference);
return await this.postActivity(fromBotId, toSkill.appId, toSkill.skillEndpoint, serviceUrl, skillConversationId, activity);
}
}
public async getSignInLink(context: TurnContext, connectionName: string): Promise {
this.checkEmulatingOAuthCards(context);
const conversation: Partial = TurnContext.getConversationReference(context.activity);
const url: string = this.oauthApiUrl(context);
const client: TokenApiClient = this.createTokenApiClient(url);
const state: any = {
ConnectionName: connectionName,
Conversation: conversation,
MsAppId: (client.credentials as AppCredentials).appId
};
const finalState: string = Buffer.from(JSON.stringify(state)).toString('base64');
return (await client.botSignIn.getSignInUrl(finalState, { channelId: context.activity.channelId }))._response.bodyAsText;
}