UNPKG

5.34 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.SkillHttpClient = void 0;
13/**
14 * @module botbuilder
15 */
16/**
17 * Copyright (c) Microsoft Corporation. All rights reserved.
18 * Licensed under the MIT License.
19 */
20const botbuilder_core_1 = require("botbuilder-core");
21const botframework_connector_1 = require("botframework-connector");
22const botFrameworkHttpClient_1 = require("../botFrameworkHttpClient");
23/**
24 * A BotFrameworkHttpClient specialized for Skills that encapsulates Conversation ID generation.
25 */
26class SkillHttpClient extends botFrameworkHttpClient_1.BotFrameworkHttpClient {
27 /**
28 * Creates a new instance of the [SkillHttpClient](xref:botbuilder-core.SkillHttpClient) class.
29 * @param credentialProvider An instance of [ICredentialProvider](xref:botframework-connector.ICredentialProvider).
30 * @param conversationIdFactory An instance of a class derived from [SkillConversationIdFactoryBase](xref:botbuilder-core.SkillConversationIdFactoryBase).
31 * @param channelService Optional. The channel service.
32 */
33 constructor(credentialProvider, conversationIdFactory, channelService) {
34 super(credentialProvider, channelService);
35 if (!conversationIdFactory) {
36 throw new Error('conversationIdFactory missing');
37 }
38 this.conversationIdFactory = conversationIdFactory;
39 }
40 /**
41 * Uses the `SkillConversationIdFactory` to create or retrieve a Skill Conversation Id, and sends the [Activity](xref:botframework-schema.Activity).
42 * @param audienceOrFromBotId The OAuth audience scope, used during token retrieval or the AppId of the bot sending the [Activity](xref:botframework-schema.Activity).
43 * @param fromBotIdOrSkill The AppId of the bot sending the [Activity](xref:botframework-schema.Activity) or the skill to create the Conversation Id for.
44 * @param toSkillOrCallbackUrl The skill to create the Conversation Id for or the callback Url for the skill host.
45 * @param callbackUrlOrActivity The callback Url for the skill host or the [Activity](xref:botframework-schema.Activity) to send.
46 * @param activityToForward Optional. The [Activity](xref:botframework-schema.Activity) to forward.
47 * @returns A `Promise` representing the [InvokeResponse](xref:botbuilder-core.InvokeResponse) for the operation.
48 */
49 postToSkill(audienceOrFromBotId, fromBotIdOrSkill, toSkillOrCallbackUrl, callbackUrlOrActivity, activityToForward) {
50 return __awaiter(this, void 0, void 0, function* () {
51 let originatingAudience;
52 let fromBotId;
53 if (typeof fromBotIdOrSkill === 'string') {
54 fromBotId = fromBotIdOrSkill;
55 // If fromBotIdOrSkill is a string, then audienceOrFromBotId should be a string per the overload.
56 originatingAudience = audienceOrFromBotId;
57 }
58 else {
59 fromBotId = audienceOrFromBotId;
60 originatingAudience = botframework_connector_1.JwtTokenValidation.isGovernment(this.channelService)
61 ? botframework_connector_1.GovernmentConstants.ToChannelFromBotOAuthScope
62 : botframework_connector_1.AuthenticationConstants.ToChannelFromBotOAuthScope;
63 }
64 const toSkill = typeof toSkillOrCallbackUrl === 'object' ? toSkillOrCallbackUrl : fromBotIdOrSkill;
65 const callbackUrl = typeof callbackUrlOrActivity === 'string' ? callbackUrlOrActivity : toSkillOrCallbackUrl;
66 const activity = typeof activityToForward === 'object' ? activityToForward : callbackUrlOrActivity;
67 let skillConversationId;
68 try {
69 const createIdOptions = {
70 activity,
71 botFrameworkSkill: toSkill,
72 fromBotId: fromBotId,
73 fromBotOAuthScope: originatingAudience,
74 };
75 skillConversationId = yield this.conversationIdFactory.createSkillConversationIdWithOptions(createIdOptions);
76 }
77 catch (err) {
78 if (err.message === 'Not Implemented') {
79 skillConversationId = yield this.conversationIdFactory.createSkillConversationId(botbuilder_core_1.TurnContext.getConversationReference(activity));
80 }
81 else {
82 throw err;
83 }
84 }
85 return yield this.postActivity(fromBotId, toSkill.appId, toSkill.skillEndpoint, callbackUrl, skillConversationId, activity);
86 });
87 }
88}
89exports.SkillHttpClient = SkillHttpClient;
90//# sourceMappingURL=skillHttpClient.js.map
\No newline at end of file