-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.js
230 lines (202 loc) · 8.22 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
require('dotenv').config()
const { default: makeWASocket, DisconnectReason, useMultiFileAuthState, fetchLatestBaileysVersion } = require('@whiskeysockets/baileys')
const { downloadMediaMessage } = require("@whiskeysockets/baileys")
const { initializeAgentExecutorWithOptions } = require("langchain/agents")
const { ChatOpenAI } = require("langchain/chat_models/openai")
const { DynamicTool } = require("langchain/tools")
const { BufferWindowMemory, ChatMessageHistory } = require("langchain/memory");
const { SystemMessage } = require('langchain/schema')
const fs = require("fs")
const axios = require("axios")
const tesseract = require("node-tesseract-ocr")
const WSF = require("wa-sticker-formatter")
const webpConverter = require("./lib/webpconverter.js")
const redis = require('./redis.js')
const yargs = require('yargs/yargs')
global.yargs = yargs(process.argv).argv
const MEMORY = {}
const chat = new ChatOpenAI({ modelName: process.env.MODEL_NAME || 'gpt-4o-mini', temperature: 0.3 });
async function connectToWhatsApp() {
try {
await redis.connect()
} catch (err) {
}
const { state, saveCreds } = await useMultiFileAuthState('login')
const { version } = await fetchLatestBaileysVersion()
const sock = makeWASocket({
version,
printQRInTerminal: true,
auth: state,
})
sock.ev.on('connection.update', async (update) => {
const { connection, lastDisconnect } = update
if (connection === 'close') {
var _a, _b
var shouldReconnect = ((_b = (_a = lastDisconnect.error) === null || _a === void 0 ? void 0 : _a.output) === null || _b === void 0 ? void 0 : _b.statusCode) !== DisconnectReason.loggedOut
console.log('connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
if (shouldReconnect) {
connectToWhatsApp()
}
} else if (connection === 'open') {
console.log('opened connection')
}
})
sock.ev.on('creds.update', saveCreds)
sock.ev.on('messages.upsert', (m) => {
const messageKeys = m.messages.map(message => {
if (!message.message || message.key.fromMe || message.key && message.key.remoteJid == 'status@broadcast') {
return null;
}
return message.key
}).filter(k => k != null)
if (messageKeys.length > 0) {
sock.readMessages(messageKeys).catch(() => {
console.log("Terjadi error saat membaca pesan")
})
}
m.messages.forEach(async (message) => {
if (!message.message || message.key.fromMe || message.key && message.key.remoteJid == 'status@broadcast') return
if (message.message.ephemeralMessage) {
message.message = message.message.ephemeralMessage.message
}
const myNumber = sock.user.id.split(':')[0]
const senderNumber = message.key.remoteJid
const isGroup = senderNumber.endsWith('@g.us')
const imageMessage = message.message.imageMessage
const videoMessage = message.message.videoMessage
const stickerMessage = message.message.stickerMessage
const extendedTextMessage = message.message.extendedTextMessage
const quotedMessageContext = extendedTextMessage && extendedTextMessage.contextInfo && extendedTextMessage.contextInfo
const quotedMessage = quotedMessageContext && quotedMessageContext.quotedMessage
const textMessage = message.message.conversation || message.message.extendedTextMessage && message.message.extendedTextMessage.text || imageMessage && imageMessage.caption || videoMessage && videoMessage.caption || 'lakukuan sesuatu'
const isMentioned = textMessage.includes('@' + myNumber)
if (isGroup && !isMentioned) {
return
}
const tools = [
new DynamicTool({
name: 'image_to_sticker',
description: 'untuk membuat sticker dari gambar (id) yang dikirim',
func: async (image_id) => {
try {
const image = Buffer.from(await redis.get(image_id), 'base64')
const sticker = new WSF.Sticker(image, { crop: false, pack: "i hope you fine :)", author: 'Dato Sang Peretas' })
await sticker.build()
const bufferImage = await sticker.get()
await sock.sendMessage(senderNumber, { sticker: bufferImage }, { quoted: message })
return 'beri tau user bahwa berhasil tanpa embel-embel'
} catch (err) {
return 'gagal'
}
}
}),
new DynamicTool({
name: 'text_to_sticker',
description: 'untuk mengubah text ke sticker',
func: async (text) => {
try {
const response = await axios.post("https://salism3api.pythonanywhere.com/text2img", { "text": text.slice(0, 60) })
const sticker = new WSF.Sticker(response.data.image, { crop: false, pack: "i hope you fine :)", author: 'Sugito Tempest' })
await sticker.build()
const bufferImage = await sticker.get()
await sock.sendMessage(senderNumber, { sticker: bufferImage }, { quoted: message })
return 'beri tau user bahwa konversinya berhasil'
} catch (err) {
return 'gagal'
}
}
}),
new DynamicTool({
name: 'text_to_sticker_gif',
description: 'untuk mengubah text ke sticker gerak',
func: async (text) => {
try {
const response = await axios.post("https://salism3api.pythonanywhere.com/text2gif/", { "text": text.slice(0, 60) })
let image = await axios.get(response.data.image, { "responseType": "arraybuffer" })
image = Buffer.from(image.data, "binary")
image = await webpConverter.gifToWebp(image)
await sock.sendMessage(senderNumber, { sticker: image }, { quoted: message })
return 'beri tau user bahwa konversinya berhasil'
} catch (err) {
return 'gagal'
}
}
}),
new DynamicTool({
name: 'ocr',
description: 'untuk mengubah gambar ke text',
func: async (id) => {
try {
const image = Buffer.from(await redis.get(id), 'base64')
const imagePath = Math.floor(Math.random() * 1000000) + '.jpg'
fs.writeFileSync(imagePath, image)
const textImage = (await tesseract.recognize(imagePath)).trim()
fs.unlinkSync(imagePath)
return 'hasilnya: ' + textImage
} catch (err) {
return 'gagal'
}
}
})
]
let memory = MEMORY[senderNumber]
if (!memory) {
memory = new BufferWindowMemory({
chatHistory: new ChatMessageHistory([
new SystemMessage('Kamu adalah Vera. asisten virtual berbasis whatsapp bot. kamu dibuat oleh Salis Mazaya, programmer jago yang sudah berpengalaman selama 5 tahun'),
new SystemMessage(`
Berikut adalah hal-hal yang mungkin bisa kamu lakukan!
1. Menjawab Pertanyaan
2. Ubah Text ke Sticker
3. Ubah Text ke Sticker Gerak
4. Mengubah Gambar ke Text (OCR)
`.trim())
]),
memoryKey: 'chat_history',
returnMessages: true,
k: 10,
})
MEMORY[senderNumber] = memory
}
const executor = await initializeAgentExecutorWithOptions(tools, chat, {
agentType: "openai-functions",
verbose: true,
memory,
})
if (imageMessage) {
const image = await downloadMediaMessage(message, 'buffer')
const id = 'img_' + Math.random().toString(36).slice(2, 7)
await redis.set(id, image.toString('base64'), { EX: 30000 })
await memory.chatHistory.addUserMessage(`ini id gambarnya ${id} untuk kamu proses`)
}
if (videoMessage && videoMessage.mimetype == "video/mp4") {
if (videoMessage.seconds > 8) {
// await sock.sendMessage(senderNumber, { text: 'Maksimal 8 detik kak!' }, { quoted: message })
return 'beri tau user. bahwa maksimal durasinya adalah 8 detik'
}
const image = await downloadMediaMessage(message, 'buffer')
const id = 'img_' + Math.random().toString(36).slice(2, 7)
await redis.set(id, image.toString('base64'), { EX: 30000 })
await memory.chatHistory.addUserMessage(`ini id videonya ${id} untuk kamu proses`)
}
if (stickerMessage) {
return
}
try {
await sock.sendPresenceUpdate('composing', message.key.remoteJid)
const { output } = await executor.invoke({ 'input': textMessage })
await sock.sendMessage(message.key.remoteJid, { text: output }, { quoted: message });
} catch (e) {
if (!global.yargs.dev) {
console.log("[ERROR] " + e.message);
await sock.sendMessage(message.key.remoteJid, { "text": "Terjadi error! coba lagi nanti" }, { quoted: message });
} else {
console.log(e);
}
} finally {
await sock.sendPresenceUpdate('available', message.key.remoteJid)
}
})
})
}
connectToWhatsApp()