-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy path_chatbot.js
81 lines (69 loc) · 1.76 KB
/
_chatbot.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
import fetch from 'node-fetch'
import axios from 'axios'
export async function before(m, { conn }) {
try {
if (m.isBaileys && m.fromMe) {
return true
}
if (!m.isGroup) {
return false
}
const users = global.db.data.users
const chats = global.db.data.chats
const user = global.db.data.users[m.sender]
const chat = global.db.data.chats[m.chat]
let name = conn.getName(m.sender)
if (
m.mtype === 'protocolMessage' ||
m.mtype === 'pollUpdateMessage' ||
m.mtype === 'reactionMessage' ||
m.mtype === 'stickerMessage'
) {
return
}
if (
!m.msg ||
!m.message ||
m.key.remoteJid !== m.chat ||
users[m.sender].banned ||
chats[m.chat].isBanned
) {
return
}
if (!m.quoted || !m.quoted.isBaileys) return
if (!chat.chatbot) {
return true
}
const msg = encodeURIComponent(m.text)
console.log(msg)
const response = await axios.post(
'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=AIzaSyDJC5a882ruaC4XL6ejY1yhgRkN-JNQKg8',
{
contents: [
{
parts: [
{
text: msg,
},
],
},
],
}
)
const data = response.data
if (data.candidates && data.candidates.length > 0) {
const candidate = data.candidates[0]
const content = candidate.content
let reply = content.parts[0].text
if (reply) {
reply = reply.replace(/Google/gi, 'Guru')
reply = reply.replace(/a large language model/gi, botname)
m.reply(reply)
}
} else {
m.reply('No suitable response from the API.')
}
} catch (error) {
console.log(error)
}
}