6
6
* Licensed under the MIT License.
7
7
*/
8
8
import { Activity , ActivityTypes } from 'botbuilder-core' ;
9
- import { WaterfallDialog , Dialog , DialogTurnResult , DialogContext , WaterfallStepContext , DialogTurnStatus , DialogReason } from 'botbuilder-dialogs' ;
9
+ import { WaterfallDialog , Dialog , DialogTurnResult , DialogContext , WaterfallStepContext , DialogReason } from 'botbuilder-dialogs' ;
10
10
import { QnAMakerOptions } from './qnamaker-interfaces/qnamakerOptions' ;
11
11
import { RankerTypes } from './qnamaker-interfaces/rankerTypes' ;
12
12
import { QnAMaker , QnAMakerResult } from './' ;
13
13
import { FeedbackRecord , FeedbackRecords , QnAMakerMetadata } from './qnamaker-interfaces' ;
14
14
import { QnACardBuilder } from './qnaCardBuilder' ;
15
15
16
+ const V4_API_REGEX = / ^ h t t p s : \/ \/ .* \. a z u r e w e b s i t e s \. n e t \/ q n a m a k e r \/ ? / i;
17
+
16
18
/**
17
19
* QnAMakerDialog response options.
18
20
*/
@@ -91,12 +93,23 @@ export class QnAMakerDialog extends WaterfallDialog {
91
93
* @param strictFilters (Optional) QnAMakerMetadata collection used to filter / boost queries to the knowledgebase.
92
94
* @param dialogId (Optional) Id of the created dialog. Default is 'QnAMakerDialog'.
93
95
*/
94
- constructor ( knowledgeBaseId : string , endpointKey : string , hostName : string , noAnswer ?: Activity , threshold : number = 0.3 , activeLearningCardTitle : string = 'Did you mean:' , cardNoMatchText : string = 'None of the above.' , top : number = 3 , cardNoMatchResponse ?: Activity , strictFilters ?: QnAMakerMetadata [ ] , dialogId : string = 'QnAMakerDialog' ) {
96
+ public constructor ( knowledgeBaseId : string , endpointKey : string , hostName : string , noAnswer ?: Activity , threshold : number = 0.3 , activeLearningCardTitle : string = 'Did you mean:' , cardNoMatchText : string = 'None of the above.' , top : number = 3 , cardNoMatchResponse ?: Activity , strictFilters ?: QnAMakerMetadata [ ] , dialogId : string = 'QnAMakerDialog' ) {
95
97
super ( dialogId ) ;
98
+ if ( ! knowledgeBaseId ) {
99
+ throw new TypeError ( 'QnAMakerDialog: missing knowledgeBaseId parameter' ) ;
100
+ }
101
+
102
+ if ( ! endpointKey ) {
103
+ throw new TypeError ( 'QnAMakerDialog: missing endpointKey parameter' ) ;
104
+ }
105
+
106
+ if ( ! hostName ) {
107
+ throw new TypeError ( 'QnAMakerDialog: missing hostName parameter' ) ;
108
+ }
96
109
97
110
this . knowledgeBaseId = knowledgeBaseId ;
98
- this . hostName = hostName ;
99
111
this . endpointKey = endpointKey ;
112
+ this . hostName = hostName ;
100
113
this . threshold = threshold ;
101
114
this . top = top ;
102
115
this . activeLearningCardTitle = activeLearningCardTitle ;
@@ -352,8 +365,48 @@ export class QnAMakerDialog extends WaterfallDialog {
352
365
const endpoint = {
353
366
knowledgeBaseId : this . knowledgeBaseId ,
354
367
endpointKey : this . endpointKey ,
355
- host : this . hostName
368
+ host : this . getHost ( )
356
369
} ;
357
370
return new QnAMaker ( endpoint ) ;
358
371
}
359
- }
372
+
373
+ /**
374
+ * Gets unmodified v5 API hostName or constructs v4 API hostName
375
+ * @remarks
376
+ * Example of a complete v5 API endpoint: "https://qnamaker-acom.azure.com/qnamaker/v5.0"
377
+ * Template literal to construct v4 API endpoint: `https://${ this.hostName }.azurewebsites.net/qnamaker`
378
+ */
379
+ private getHost ( ) : string {
380
+ let host : string = this . hostName ;
381
+ // If hostName includes 'qnamaker/v5', return the v5 API hostName.
382
+ if ( host . includes ( 'qnamaker/v5' ) ) {
383
+ return host ;
384
+ }
385
+
386
+ // V4 API logic
387
+ // If the hostname contains all the necessary information, return it
388
+ if ( / ^ h t t p s : \/ \/ .* \. a z u r e w e b s i t e s \. n e t \/ q n a m a k e r \/ ? / i. test ( host ) ) {
389
+ return host ;
390
+ }
391
+
392
+ // Otherwise add required components
393
+ if ( ! ( / h t t p s ? : \/ \/ / i. test ( host ) ) ) {
394
+ host = 'https://' + host ;
395
+ }
396
+
397
+ // Web App Bots provisioned through the QnAMaker portal have "xxx.azurewebsites.net" in their
398
+ // environment variables
399
+ if ( host . endsWith ( '.azurewebsites.net' ) ) {
400
+ // Add the remaining required path
401
+ return host + '/qnamaker' ;
402
+ }
403
+
404
+ // If this.hostName is just the azurewebsite subdomain, finish the remaining V4 API behavior shipped in 4.8.0
405
+ // e.g. `https://${ this.hostName }.azurewebsites.net/qnamaker`
406
+ if ( ! host . endsWith ( '.azurewebsites.net/qnamaker' ) ) {
407
+ host = host + '.azurewebsites.net/qnamaker' ;
408
+ }
409
+
410
+ return host ;
411
+ }
412
+ }
0 commit comments