forked from victorharry/zap-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (89 loc) · 3.28 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const { Client, MessageMedia, LocalAuth } = require('whatsapp-web.js')
const qrcode = require('qrcode-terminal')
const axios = require('axios')
require('dotenv').config()
const client = new Client({
authStrategy: new LocalAuth()
})
client.on('qr', qr => {
qrcode.generate(qr, {small: true})
});
client.on('authenticated', (session) => console.log(`Autenticado`))
client.on('ready', () => console.log('O zap-gpt está pronto 😋 Não esquece da estrelinha no repo ⭐ by: Victor Harry 🧙'))
client.on('message_create', message => commands(message))
client.initialize();
const headers = {
'Authorization': `Bearer ${process.env.OPENAI_KEY}`,
'Content-Type': 'application/json'
}
const axiosInstance = axios.create({
baseURL: 'https://api.openai.com/',
timeout: 120000,
headers: headers
});
const getDavinciResponse = async (clientText) => {
const body = {
"model": "text-davinci-003",
"prompt": clientText,
"max_tokens": 2048,
"temperature": 0.1
}
try {
const { data } = await axiosInstance.post('v1/completions', body)
const botAnswer = data.choices[0].text
return `Melhoramigo 🤖 ${botAnswer}`
} catch (e) {
return `❌ OpenAI Response Error`
}
}
const getDalleResponse = async (clientText) => {
const body = {
prompt: clientText, // Descrição da imagem
n: 1, // Número de imagens a serem geradas
size: "256x256", // Tamanho da imagem
}
try {
const { data } = await axiosInstance.post('v1/images/generations', body)
return data.data[0].url
} catch (e) {
return `❌ OpenAI Response Error`
}
}
const commands = async (message) => {
const iaCommands = {
davinci3: "/cnv"
* dalle: "/img",
}
let firstWord = message.body.substring(0, message.body.indexOf(" "))
/*
* Faremos uma validação no message.from
* para caso a gente envie um comando
* a response não seja enviada para
* nosso próprio número e sim para
* a pessoa ou grupo para o qual eu enviei
*/
const sender = message.from.includes(process.env.PHONE_NUMBER) ? message.to : message.from
switch (firstWord) {
case iaCommands.davinci3:
const question = message.body.substring(message.body.indexOf(" "));
getDavinciResponse(question).then(async (response) => {
const contact = await message.getContact()
client.sendMessage(sender, `${response}\n\n_Generated by @${contact.id.user}_`, { mentions: [contact] })
})
break
case iaCommands.dalle:
const imgDescription = message.body.substring(message.body.indexOf(" "));
const contact = await message.getContact();
getDalleResponse(imgDescription, message).then(async (imgUrl) => {
const media = await MessageMedia.fromUrl(imgUrl)
// Caso queira mandar como Sticker, acrescente em options -> sendMediaAsSticker: true
const options = {
mentions: [contact],
caption: `_Generated by @${contact.id.user}_`,
media: media,
}
await client.sendMessage(sender, media, options)
})
break
}
}