forked from Joao208/alexa-chatgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (133 loc) · 4.29 KB
/
index.js
File metadata and controls
153 lines (133 loc) · 4.29 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const Alexa = require("ask-sdk-core");
const { ask, getProvider } = require("./aiClient");
function formatString(text) {
return text.replace(/\n+/g, " ");
}
const LaunchRequestHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === "LaunchRequest"
);
},
async handle(handlerInput) {
const response =
'Bem-vindo ao ChatGPT. Você pode perguntar algo como "chat qual a capital da França?"';
return handlerInput.responseBuilder.speak(response).getResponse();
},
};
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
Alexa.getIntentName(handlerInput.requestEnvelope) === "AskToGPTIntent"
);
},
async handle(handlerInput) {
const question = Alexa.getSlotValue(
handlerInput.requestEnvelope,
"question"
);
try {
const responseFromGPT = await ask(question, {
sessionId: handlerInput.requestEnvelope.session?.sessionId,
locale: handlerInput.requestEnvelope.request?.locale,
userId: handlerInput.requestEnvelope.context?.System?.user?.userId,
});
const formattedResponse = formatString(responseFromGPT);
return handlerInput.responseBuilder
.speak(formattedResponse || "Desculpe, estou sem resposta no momento.")
.getResponse();
} catch (error) {
console.error("AI provider request failed", error?.response?.data || error?.message || error);
return handlerInput.responseBuilder
.speak("Desculpe, não consegui responder agora. Tente novamente em instantes.")
.getResponse();
}
},
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
Alexa.getIntentName(handlerInput.requestEnvelope) === "AMAZON.HelpIntent"
);
},
handle(handlerInput) {
const speakOutput =
"Você pode perguntar algo como 'chat ideia de nome para bebes'";
return handlerInput.responseBuilder.speak(speakOutput).getResponse();
},
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
(Alexa.getIntentName(handlerInput.requestEnvelope) ===
"AMAZON.CancelIntent" ||
Alexa.getIntentName(handlerInput.requestEnvelope) ===
"AMAZON.StopIntent")
);
},
handle(handlerInput) {
const speakOutput = "Até mais!";
return handlerInput.responseBuilder.speak(speakOutput).getResponse();
},
};
const FallbackIntentHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
Alexa.getIntentName(handlerInput.requestEnvelope) ===
"AMAZON.FallbackIntent"
);
},
handle(handlerInput) {
const speakOutput =
"Desculpe, não tenho conhecimento sobre isso. Por favor, tente novamente.";
return handlerInput.responseBuilder.speak(speakOutput).getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) ===
"SessionEndedRequest"
);
},
handle(handlerInput) {
return handlerInput.responseBuilder.getResponse();
},
};
const IntentReflectorHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest"
);
},
handle(handlerInput) {
const speakOutput = `Que tal me perguntar algo como 'quanto está o dólar hoje?'`;
return handlerInput.responseBuilder.speak(speakOutput).getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput) {
const speakOutput =
"Desculpe, parece que tivemos um problema aqui! Acesse github.com/joao208/alexa-chatgpt para mais informações.";
return handlerInput.responseBuilder.speak(speakOutput).getResponse();
},
};
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
HelloWorldIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
FallbackIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler
)
.addErrorHandlers(ErrorHandler)
.lambda();