Skip to content

Commit

Permalink
feat: add a new interceptor (Opening talk)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidetaka Okamoto committed Jan 28, 2021
1 parent b36d4cf commit bc9a03c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/framework/__tests__/SkillFactory.class.spec.ts
Expand Up @@ -229,6 +229,33 @@ describe('SkillFactoryr', () => {
});
});
});
describe.skip('openingTalk', () => {
beforeEach(() => {
requestEnvelope = new RequestEnvelopeFactory(
new LaunchRequestFactory()
).getRequest();
skill = new SkillFactory({
apiClient: {
useDefault: true,
},
opening: {
text: 'opening talk is here',
},
});
});
it('should execute the skill', async () => {
await expect(
skill.createLambdaHandler()(requestEnvelope)
).rejects.toMatchObject({
name: 'ServiceError',
statusCode: 401,
response: {
type: 'INVALID_AUTHENTICATION_TOKEN',
message: 'The authentication token is not valid.',
},
});
});
});
describe('addRequestHandlers', () => {
beforeEach(() => {
requestEnvelope = new RequestEnvelopeFactory(
Expand Down
9 changes: 9 additions & 0 deletions src/framework/skillFactory.class.ts
Expand Up @@ -30,6 +30,7 @@ import { SavePersistentAttributesInterceptor } from '../PersistentAttributesMana
import { SkillInvocationRecorder } from '../CRM';
import { SkillConfig } from '../Config/Config';
import { RequestSituationInterceptor } from '../Situation/Situation.interceptor';
import { createOpningTalkInterceptor } from '../handlers/OpeningInterceptor/Opening.interceptor';

// let cachedSkill: CustomSkillBuilder

Expand Down Expand Up @@ -85,9 +86,17 @@ export class SkillFactory {
if (skillId) this.skillBuilders.withSkillId(skillId);
this._configureAPIClients(config);
this._configureDBClients(config);
this._addOpeningInterceptor(config);
this._errorHandler = errorHandler;
}

private _addOpeningInterceptor(config?: TalkyJSSkillConfig) {
if (!config) return;
const interceptor = createOpningTalkInterceptor(config);
if (!interceptor) return;
this.addRequestInterceptors(interceptor);
}

/**
* Get factory instance
* @param config
Expand Down
4 changes: 4 additions & 0 deletions src/framework/skillFactory.interface.ts
Expand Up @@ -60,6 +60,10 @@ export interface TalkyJSSkillConfig {
skillId?: string;
apiClient?: TalkyJSAPIClientConfig;
errorHandler?: TalkyJSErrorHandlerConfig;
opening?: {
ssml?: string;
text?: string;
};
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/handlers/OpeningInterceptor/Opening.interceptor.ts
@@ -0,0 +1,39 @@
import { RequestInterceptor, isNewSession } from 'ask-sdk-core';
import { isSkillEvent } from '@ask-utils/core';
import { TalkyJSSkillConfig } from '../../framework';

export const createOpningTalkInterceptor = ({
opening,
}: TalkyJSSkillConfig): RequestInterceptor | undefined => {
if (!opening) return;
const speechText = (() => {
if (opening.ssml) return opening.ssml;
if (opening.text) {
return `<speak>${opening.text}</speak>`;
}
return undefined;
})();
if (!speechText) return;
return {
async process(input) {
console.log({ speechText });
const { requestEnvelope, serviceClientFactory } = input;
if (!serviceClientFactory) return;
if (!isNewSession(requestEnvelope)) return;
if (isSkillEvent(requestEnvelope)) return;

const directiveServiceClient = serviceClientFactory.getDirectiveServiceClient();

// build the progressive response directive
await directiveServiceClient.enqueue({
header: {
requestId: requestEnvelope.request.requestId,
},
directive: {
type: 'VoicePlayer.Speak',
speech: speechText,
},
});
},
};
};

0 comments on commit bc9a03c

Please sign in to comment.