-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (93 loc) · 2.78 KB
/
index.js
File metadata and controls
103 lines (93 loc) · 2.78 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
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
const app = express();
const port = 3000;
import * as dotenv from "dotenv";
dotenv.config();
app.use(bodyParser.json());
const initialMessage = "Bem-vindo ao Chat GPT, como posso te ajudar?";
const secretKey = process.env.OPEN_AI_API_KEY;
const zapiUrl = `https://api.z-api.io/instances/${process.env.Z_API_INSTANCE_ID}/token/${process.env.Z_API_INSTANCE_TOKEN}/send-text`;
const chats = {};
const replyMessage = async (phone, message) => {
await axios.post(zapiUrl, {
phone,
message,
delayTyping: 3,
});
};
const appendChat = (phone, message) => {
chats[phone].messages.push(message.replace(/(\r\n|\n|\r)/gm, ""));
if (chats[phone].messages.length > 7) {
chats[phone].messages.shift();
}
};
const onNewMessage = async (message) => {
if (chats[message.phone] && chats[message.phone].blocked) {
return await replyMessage(message.phone, "Um momento por favor");
}
chats[message.phone].blocked = true;
const text = ` ${message.phone}: ${message.text.message}`;
await appendChat(message.phone, text);
await appendChat(message.phone, ` OPENAI:`);
try {
const prompt = chats[message.phone].messages.join("\n");
const response = await axios.post(
`https://api.openai.com/v1/completions`,
{
model: "text-davinci-003",
prompt,
temperature: 0.9,
max_tokens: 500,
top_p: 1,
frequency_penalty: 0.0,
presence_penalty: 0.6,
stop: [` ${message.phone}:`, " OPENAI:"],
},
{
headers: {
Authorization: `Bearer ${secretKey}`,
},
}
);
if (response.data.choices.length > 0) {
await appendChat(message.phone, `${response.data.choices[0].text}`);
await replyMessage(message.phone, response.data.choices[0].text.trim());
} else {
throw "NOT_FOUND";
}
} catch (e) {
console.log(e);
return await replyMessage(
message.phone,
"Desculpe, mas tive problemas no processamento, você pode reiniciar nossa conversando mandando novamente o comando !gpt"
);
} finally {
chats[message.phone].blocked = false;
}
};
app.post("/on-new-message", async (req, res) => {
if (
!req.body.fromMe &&
chats[req.body.phone] &&
req.body.text &&
req.body.text.message
) {
await onNewMessage(req.body);
}
if (!req.body.fromMe && req.body.text && req.body.text.message === "!gpt") {
chats[req.body.phone] = {
blocked: false,
messages: [],
};
await replyMessage(req.body.phone, initialMessage);
await appendChat(req.body.phone, ` OPENAI: ${initialMessage}`);
}
res.status(200).send({
message: "success",
});
});
app.listen(port, () => {
console.log(`Rodando na porta ${port}`);
});