Skip to content

Commit

Permalink
Merge pull request #732 from AbhiPwr/delegate_from_ac_to_im_handler
Browse files Browse the repository at this point in the history
Feature: Delegate from AC Dialog to Intent Requesthandler
  • Loading branch information
AbhiPwr committed Apr 3, 2023
2 parents e9b26ae + 7ad1651 commit 13a2bf9
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import { Response, dialog, interfaces, Intent } from 'ask-sdk-model';
import { generateSlotsFromApiRequest, getRequestType } from '../../../util/RequestEnvelopeUtils';
import { CustomSkillRequestHandler } from './CustomSkillRequestHandler';
import { HandlerInput } from './HandlerInput';

import APIInvocationRequest = interfaces.conversations.APIInvocationRequest;
import APIRequest = interfaces.conversations.APIRequest;

const ACDL_AUTOGEN_NAMESPACE:string = "com.amazon.autogenerated";
const SPLIT_CHAR:string = "_";

export class DelegateToIntentHandler implements CustomSkillRequestHandler {

canHandle(input : HandlerInput) : boolean {
if (getRequestType(input.requestEnvelope) !== 'Dialog.API.Invoked') {
return false;
}
if (!(input.requestEnvelope.request as APIInvocationRequest).apiRequest
|| !(input.requestEnvelope.request as APIInvocationRequest).apiRequest.name) {
return false;
}
const apiName: string = (input.requestEnvelope.request as APIInvocationRequest).apiRequest.name;
if (!apiName.startsWith(ACDL_AUTOGEN_NAMESPACE)) {
return false;
}
return true;
}

handle(input: HandlerInput): Response | Promise<Response> {
const apiRequest:APIRequest = (input.requestEnvelope.request as APIInvocationRequest).apiRequest;
const apiName: string = apiRequest.name;
const intentName: string = (apiName.substring(apiName.indexOf(SPLIT_CHAR) + 1));

const directiveType: ('Dialog.DelegateRequest') = "Dialog.DelegateRequest";
const delegationTarget: string = "skill";
const updatedRequestType: ("Dialog.InputRequest" | "IntentRequest") = "IntentRequest";
const delegationPeriod:dialog.DelegationPeriod = {
until: 'EXPLICIT_RETURN'
};

const intent:Intent = {
name: intentName,
confirmationStatus: 'NONE',
slots: generateSlotsFromApiRequest(apiRequest)
};
const updatedRequest:dialog.UpdatedRequest = {
type: updatedRequestType,
intent
};
const delegateRequestDirective:dialog.DelegateRequestDirective = {
type: directiveType,
target: delegationTarget,
period: delegationPeriod,
updatedRequest
};

return input.responseBuilder.addDirective(delegateRequestDirective).getResponse();
}
}
1 change: 1 addition & 0 deletions ask-sdk-core/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export { AttributesManagerFactory } from './attributes/AttributesManagerFactory'
export { PersistenceAdapter } from './attributes/persistence/PersistenceAdapter';
export { CustomSkillErrorHandler as ErrorHandler } from './dispatcher/error/handler/CustomSkillErrorHandler';
export { CustomSkillRequestHandler as RequestHandler } from './dispatcher/request/handler/CustomSkillRequestHandler';
export { DelegateToIntentHandler } from './dispatcher/request/handler/DelegateToIntentHandler';
export { HandlerInput } from './dispatcher/request/handler/HandlerInput';
export { CustomSkillRequestInterceptor as RequestInterceptor } from './dispatcher/request/interceptor/CustomSkillRequestInterceptor';
export { CustomSkillResponseInterceptor as ResponseInterceptor } from './dispatcher/request/interceptor/CustomSkillResponseInterceptor';
Expand Down
32 changes: 29 additions & 3 deletions ask-sdk-core/lib/util/RequestEnvelopeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
import {
DialogState,
IntentRequest,
ListSlotValue,
Request,
RequestEnvelope,
Session,
SimpleSlotValue,
Slot,
SlotValue,
SupportedInterfaces
SupportedInterfaces,
interfaces
} from 'ask-sdk-model';
import { createAskSdkError } from 'ask-sdk-runtime';

import APIRequest = interfaces.conversations.APIRequest;
/**
* Retrieves the locale from the request.
*
Expand Down Expand Up @@ -293,3 +293,29 @@ export function isNewSession(requestEnvelope : RequestEnvelope) : boolean {
'RequestEnvelopeUtils',
`The provided request doesn't contain a session.`);
}

/**
* Extracts slots from Dialog Api Request
*
*
* @param {APIRequest} apiRequest
*/
export function generateSlotsFromApiRequest(apiRequest:APIRequest) : {
[key: string]: Slot;
} {
if (!apiRequest.slots) {
return {};
}
const intentSlots: {[key : string] : Slot} = {};
Object.keys(apiRequest.slots).forEach((slotKey:string) => {
const slotValue: SlotValue = apiRequest.slots[slotKey];
const intentSlot: Slot = {
name: slotKey,
confirmationStatus: 'NONE',
...((slotValue as SimpleSlotValue).value ? { value : (slotValue as SimpleSlotValue).value } : {}),
...((slotValue as SimpleSlotValue).resolutions ? { resolutions : (slotValue as SimpleSlotValue).resolutions } : {})
};
intentSlots[slotKey] = intentSlot;
});
return intentSlots;
}
2 changes: 1 addition & 1 deletion ask-sdk-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"nyc": "^14.1.1",
"sinon": "^7.0.13",
"ts-node": "^6.0.1",
"typescript": "^3.5.3"
"typescript": "^4.9.5"
},
"repository": "github:alexa/alexa-skills-kit-sdk-for-nodejs",
"bugs": "https://github.com/alexa/alexa-skill-sdk-for-nodejs/issues",
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk-dynamodb-persistence-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"mocha": "^5.0.5",
"nyc": "^14.1.1",
"ts-node": "^6.0.1",
"typescript": "^3.5.3"
"typescript": "^4.9.5"
},
"repository": "github:alexa/alexa-skills-kit-sdk-for-nodejs",
"bugs": "https://github.com/alexa/alexa-skill-sdk-for-nodejs/issues",
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk-express-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"sinon": "^7.0.13",
"supertest": "^3.1.0",
"ts-node": "^6.0.1",
"typescript": "^3.5.3"
"typescript": "^4.9.5"
},
"repository": "github:alexa/alexa-skills-kit-sdk-for-nodejs",
"bugs": "https://github.com/alexa/alexa-skill-sdk-for-nodejs/issues",
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"nyc": "^14.1.1",
"sinon": "^7.0.13",
"ts-node": "^6.0.1",
"typescript": "^3.5.3"
"typescript": "^4.9.5"
},
"repository": "github:alexa/alexa-skills-kit-sdk-for-nodejs",
"bugs": "https://github.com/alexa/alexa-skill-sdk-for-nodejs/issues",
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk-s3-persistence-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"mocha": "^5.0.5",
"nyc": "^14.1.1",
"ts-node": "^6.2.0",
"typescript": "^3.5.3"
"typescript": "^4.9.5"
},
"repository": "github:alexa/alexa-skills-kit-sdk-for-nodejs",
"bugs": "https://github.com/alexa/alexa-skill-sdk-for-nodejs/issues",
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk-v1adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"nyc": "^14.1.1",
"sinon": "^7.0.13",
"ts-node": "^6.0.1",
"typescript": "^3.5.3"
"typescript": "^4.9.5"
},
"repository": "github:alexa/alexa-skills-kit-sdk-for-nodejs",
"bugs": "https://github.com/alexa/alexa-skill-sdk-for-nodejs/issues",
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"mocha": "^5.0.5",
"nyc": "^14.1.1",
"ts-node": "^6.0.1",
"typescript": "^3.5.3"
"typescript": "^4.9.5"
},
"repository": "github:alexa/alexa-skills-kit-sdk-for-nodejs",
"bugs": "https://github.com/alexa/alexa-skill-sdk-for-nodejs/issues",
Expand Down

0 comments on commit 13a2bf9

Please sign in to comment.