forked from leifermendez/bot-ws-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
83 lines (69 loc) · 2.37 KB
/
app.js
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
require("dotenv").config();
const {
createBot,
createProvider,
createFlow,
addKeyword,
EVENTS,
} = require("@bot-whatsapp/bot");
const QRPortalWeb = require("@bot-whatsapp/portal");
const { init } = require("bot-ws-plugin-openai");
const BaileysProvider = require("@bot-whatsapp/provider/baileys");
const MockAdapter = require("@bot-whatsapp/database/mock");
const { handlerAI } = require("./utils");
const { textToVoice } = require("./services/eventlab");
const employeesAddonConfig = {
model: "gpt-4-0613",
temperature: 0,
apiKey: process.env.OPENAI_API_KEY,
};
const employeesAddon = init(employeesAddonConfig);
const flowStaff = addKeyword(EVENTS.ACTION).addAnswer(
["Claro que te interesa?", "mejor te envio audio.."],
null,
async (_, { flowDynamic, state }) => {
console.log("🙉 texto a voz....");
const currentState = state.getMyState();
const path = await textToVoice(currentState.answer);
console.log(`🙉 Fin texto a voz....[PATH]:${path}`);
await flowDynamic([{ body: "escucha", media: path }]);
}
);
const flowVoiceNote = addKeyword(EVENTS.VOICE_NOTE).addAction(
async (ctx, ctxFn) => {
await ctxFn.flowDynamic("dame un momento para escucharte...🙉");
console.log("🤖 voz a texto....");
const text = await handlerAI(ctx);
console.log(`🤖 Fin voz a texto....[TEXT]: ${text}`);
const currentState = ctxFn.state.getMyState();
const fullSentence = `${currentState?.answer ?? ""}. ${text}`;
const { employee, answer } = await employeesAddon.determine(fullSentence);
ctxFn.state.update({ answer });
employeesAddon.gotoFlow(employee, ctxFn);
}
);
const main = async () => {
const adapterDB = new MockAdapter();
const adapterFlow = createFlow([flowVoiceNote, flowStaff]);
const adapterProvider = createProvider(BaileysProvider);
/**
* 🤔 Empledos digitales
* Imaginar cada empleado descrito con sus deberes de manera explicita
*/
const employees = [
{
name: "EMPLEADO_STAFF_TOUR",
description:
"Soy Jorge el staff amable encargado de atentender las solicitudes de los viajeros si tienen dudas, preguntas sobre el tour o la ciudad de madrid, mis respuestas son breves.",
flow: flowStaff,
}
];
employeesAddon.employees(employees);
createBot({
flow: adapterFlow,
provider: adapterProvider,
database: adapterDB,
});
QRPortalWeb()
};
main();