-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
138 lines (112 loc) · 4.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { DynamoDbPersistenceAdapter } from 'ask-sdk-dynamodb-persistence-adapter';
import { DefaultApiClient, getIntentName, getRequestType, HandlerInput, SkillBuilders } from 'ask-sdk-core';
import { Response, SessionEndedRequest, IntentRequest, ResponseEnvelope } from 'ask-sdk-model';
import Assistant from './services/assistant';
import { AudioState } from './models/AudioState';
const LaunchRequestHandler = {
canHandle: (handlerInput: HandlerInput): boolean => {
return getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
handle: async (handlerInput: HandlerInput): Promise<Response> => {
console.log('Launch Request');
if (!handlerInput.requestEnvelope.context.System.user.accessToken) {
return handlerInput.responseBuilder
.withLinkAccountCard()
.speak('You must link your Google account to use this skill. Please use the link in the Alexa app to authorize your Google Account.')
.withShouldEndSession(true)
.getResponse();
}
return handlerInput.responseBuilder.speak('Welcome to Alexa Assistant').getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle: (handlerInput: HandlerInput): boolean => {
return getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
},
handle: async (handlerInput: HandlerInput): Promise<Response> => {
console.log('Session ended Request');
console.log(`Session has ended with reason ${(handlerInput.requestEnvelope.request as SessionEndedRequest).reason}`);
if ((handlerInput.requestEnvelope.request as SessionEndedRequest).error) {
console.log(`Session error`, (handlerInput.requestEnvelope.request as SessionEndedRequest).error);
}
const attributes = handlerInput.attributesManager.getRequestAttributes();
if (attributes['microphone_open']) {
return SearchIntentHandler.handle(handlerInput, 'goodbye');
}
return handlerInput.responseBuilder.withShouldEndSession(true).getResponse();
},
};
const SearchIntentHandler = {
canHandle: (handlerInput: HandlerInput): boolean => {
return getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && getIntentName(handlerInput.requestEnvelope) === 'SearchIntent';
},
handle: async (handlerInput: HandlerInput, overrideText?: string): Promise<Response> => {
console.log('Search Intent');
const audioState: AudioState = {
microphoneOpen: true,
alexaUtteranceText: '',
googleResponseText: '',
};
if (overrideText) {
audioState.alexaUtteranceText = overrideText;
console.log('Utterance received from another intent: ' + overrideText);
} else {
const slots = (handlerInput.requestEnvelope.request as IntentRequest).intent.slots;
audioState.alexaUtteranceText = slots?.search?.value ?? '';
}
console.log('Input text to be processed is "' + audioState.alexaUtteranceText + '"');
console.log('Starting Search Intent');
const assistant = new Assistant(handlerInput.requestEnvelope, handlerInput.attributesManager, handlerInput.responseBuilder);
try {
await assistant.executeAssist(audioState);
} catch (err) {
console.log('Execute assist returned error:', err);
handlerInput.responseBuilder.withShouldEndSession(true);
}
return handlerInput.responseBuilder.getResponse();
},
};
const StopIntentHandler = {
canHandle: (handlerInput: HandlerInput): boolean => {
return getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent';
},
handle: async (handlerInput: HandlerInput): Promise<Response> => {
console.log('Stop Intent');
const attributes = handlerInput.attributesManager.getRequestAttributes();
if (attributes['microphone_open']) {
return SearchIntentHandler.handle(handlerInput, 'stop');
}
return handlerInput.responseBuilder.speak('Stopped').withShouldEndSession(true).getResponse();
},
};
// Repeat for other intent handlers (HelpIntentHandler, CancelIntentHandler, etc.)
const UnhandledHandler = {
canHandle: (handlerInput: HandlerInput): boolean => {
return true;
},
handle: (handlerInput: HandlerInput): Response => {
console.log('Unhandled event');
return handlerInput.responseBuilder.reprompt("I'm not sure what you said. Can you repeat please?").getResponse();
},
};
exports.handler = async function (event: any, context: any): Promise<ResponseEnvelope> {
const skillBuilder = SkillBuilders.custom();
skillBuilder.addRequestHandlers(
LaunchRequestHandler,
SessionEndedRequestHandler,
SearchIntentHandler,
StopIntentHandler,
// Add remaining handlers here
UnhandledHandler
);
skillBuilder.withApiClient(new DefaultApiClient());
skillBuilder.withPersistenceAdapter(
new DynamoDbPersistenceAdapter({
tableName: 'AlexaAssistantSkillSettings',
partitionKeyName: 'userId',
})
);
const response = await skillBuilder.create().invoke(event, context);
console.log('Response is:', response);
return response;
};