import { BotAdapter, BotTelemetryClient, InvokeResponse, Severity, TurnContext } from 'botbuilder'; import { ActivityExtensions, IRemoteUserTokenProvider } from 'botbuilder-solutions'; import { Activity, ActivityTypes, ConversationReference, ResourceResponse } from 'botframework-schema'; import { v4 as uuid } from 'uuid'; import { BotCallbackHandler, IActivityHandler } from '../activityHandler'; export class SkillHttpBotAdapter extends BotAdapter implements IActivityHandler, IRemoteUserTokenProvider { private readonly telemetryClient: BotTelemetryClient; private readonly queuedActivities: Partial[]; public constructor(telemetryClient: BotTelemetryClient) { super(); this.telemetryClient = telemetryClient; this.queuedActivities = []; } public async sendActivities(context: TurnContext, activities: Partial[]): Promise { const responses: ResourceResponse[] = []; activities.forEach(async (activity: Partial): Promise => { if (!activity.id) { activity.id = uuid(); } if (!activity.timestamp) { activity.timestamp = new Date(); } if (activity.type === 'delay') { // The BotFrameworkAdapter and Console adapter implement this // hack directly in the POST method. Replicating that here // to keep the behavior as close as possible to facilitate // more realistic tests. // eslint-disable-next-line @typescript-eslint/tslint/config const delayMs: number = activity.value; await sleep(delayMs); } else if (activity.type === ActivityTypes.Trace && activity.channelId !== 'emulator') { // if it is a Trace activity we only send to the channel if it's the emulator. } else if (activity.type === ActivityTypes.Typing && activity.channelId !== 'test') { // If it's a typing activity we omit this in test scenarios to avoid test failures } else { // Queue up this activity for aggregation back to the calling Bot in one overall message. this.queuedActivities.push(activity); } responses.push({ id: activity.id }); }); return responses; } public async deleteActivity(context: TurnContext, reference: Partial): Promise { throw new Error('Http Request/Response model doesn\'t support DeleteActivityAsync call!'); } public async updateActivity(context: TurnContext, activity: Partial): Promise { throw new Error('Http Request/Response model doesn\'t support UpdateActivityAsync call!'); } public async continueConversation(reference: Partial, logic: BotCallbackHandler): Promise { throw new Error('Http Request/Response model doesn\'t support ContinueConversationAsync call!'); } public async processActivity(activity: Activity, callback: BotCallbackHandler): Promise { const messageIn: string = `SkillHttpBotAdapter: Received an incoming activity. Activity id: ${activity.id}`; this.telemetryClient.trackTrace({ message: messageIn, severityLevel: Severity.Information }); // Process the Activity through the Middleware and the Bot, this will generate Activities which we need to send back. const context: TurnContext = new TurnContext(this, activity); await this.runMiddleware(context, callback); const messageOut: string = `SkillHttpBotAdapter: Batching activities in the response. ReplyToId: ${activity.id}`; this.telemetryClient.trackTrace({ message: messageOut, severityLevel: Severity.Information }); // Any Activity responses are now available (via SendActivitiesAsync) so we need to pass back for the response return { status: 200, body: this.queuedActivities.splice(0, this.queuedActivities.length) }; } public async sendRemoteTokenRequestEvent(turnContext: TurnContext): Promise { // We trigger a Token Request from the Parent Bot by sending a "TokenRequest" event back and then waiting for a "TokenResponse" const response: Activity = ActivityExtensions.createReply(turnContext.activity); response.type = ActivityTypes.Event; response.name = 'tokens/request'; // Send the tokens/request Event await this.sendActivities(turnContext, [response]); } } async function sleep(delay: number): Promise { return new Promise((resolve: (value: void) => void): void => { setTimeout(resolve, delay); }); }