From 578139f4f4a76c853f0ba1043bddbb4b2e43edbd Mon Sep 17 00:00:00 2001 From: danzok Date: Thu, 17 Nov 2022 17:02:57 -0300 Subject: [PATCH] base done. --- .gitignore | 3 - package.json | 1 + src/handle/chat.js | 23 ++++--- src/helper/serialize.js | 134 ++++++++++++++++++++++++++++++++++++++++ src/index.js | 6 +- 5 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 src/helper/serialize.js diff --git a/.gitignore b/.gitignore index 2c9c0cb..fc3b943 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,3 @@ node_modules/ package-lock.json yarn.lock - - -sessions/ \ No newline at end of file diff --git a/package.json b/package.json index f59f69c..71b4fcd 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "homepage": "https://github.com/danzok/bot-whatsApp#readme", "dependencies": { "@adiwajshing/baileys": "^4.4.0", + "dotenv": "^16.0.3", "mongodb": "^4.12.0", "qrcode-terminal": "^0.12.0", "yarn": "^1.22.19" diff --git a/src/handle/chat.js b/src/handle/chat.js index d8ba527..f509bd6 100644 --- a/src/handle/chat.js +++ b/src/handle/chat.js @@ -1,7 +1,12 @@ -import {config} from '../config.js' -import serialize from "../helpers/serialize.js"; +import { ping } from '../commands/ping.js'; +import {config} from '../config.js' +import serialize from "../helper/serialize.js"; export default async function chatHandle(m,conn){ + const prefix = config.prefix + const owner = config.owner + const multiPref = new RegExp("^[" + "!#".replace(/[|\\{}()[\]^$+*?.\-\^]/g, "\\$&") + "]"); + try { if (m.type !== "notify") return; let msg = serialize(JSON.parse(JSON.stringify(m.messages[0])), conn); @@ -15,13 +20,17 @@ export default async function chatHandle(m,conn){ ) return; - let { body } = msg; - const { isGroup, sender, from } = msg; - - console.log(body) + let { body } = msg; + const { isGroup, sender, from } = msg; + const gcMeta = isGroup ? await sock.groupMetadata(from) : ""; + const gcName = isGroup ? gcMeta.subject : ""; + const isOwner = owner.includes(sender) || msg.isSelf; + if(body == 'ping'){ + msg.reply('texto') + } } catch (error) { } -} \ No newline at end of file +} diff --git a/src/helper/serialize.js b/src/helper/serialize.js new file mode 100644 index 0000000..d3dec14 --- /dev/null +++ b/src/helper/serialize.js @@ -0,0 +1,134 @@ +import pkg from '@adiwajshing/baileys'; +const { proto, getContentType, jidDecode, downloadContentFromMessage } = pkg; + + +const downloadMedia = (message, pathFile) => + new Promise(async (resolve, reject) => { + const type = Object.keys(message)[0]; + let mimeMap = { + imageMessage: "image", + videoMessage: "video", + stickerMessage: "sticker", + documentMessage: "document", + audioMessage: "audio", + }; + try { + if (pathFile) { + const stream = await downloadContentFromMessage(message[type], mimeMap[type]); + let buffer = Buffer.from([]); + for await (const chunk of stream) { + buffer = Buffer.concat([buffer, chunk]); + } + await fs.promises.writeFile(pathFile, buffer); + resolve(pathFile); + } else { + const stream = await downloadContentFromMessage(message[type], mimeMap[type]); + let buffer = Buffer.from([]); + for await (const chunk of stream) { + buffer = Buffer.concat([buffer, chunk]); + } + resolve(buffer); + } + } catch (e) { + reject(e); + } +}); + +const decodeJid = (jid) => { + if (/:\d+@/gi.test(jid)) { + const decode = jidDecode(jid) || {}; + return ((decode.user && decode.server && decode.user + "@" + decode.server) || jid).trim(); + } else return jid.trim(); +}; + +export default function serialize(msg, conn) { + if (msg.key) { + msg.id = msg.key.id; + msg.isSelf = msg.key.fromMe; + msg.from = decodeJid(msg.key.remoteJid); + msg.isGroup = msg.from.endsWith("@g.us"); + msg.sender = msg.isGroup ? decodeJid(msg.key.participant) : msg.isSelf ? decodeJid(conn.user.id) : msg.from; + } + if (msg.message) { + msg.type = getContentType(msg.message); + if (msg.type === "ephemeralMessage") { + msg.message = msg.message[msg.type].message; + const tipe = Object.keys(msg.message)[0]; + msg.type = tipe; + if (tipe === "viewOnceMessage") { + msg.message = msg.message[msg.type].message; + msg.type = getContentType(msg.message); + } + } + if (msg.type === "viewOnceMessage") { + msg.message = msg.message[msg.type].message; + msg.type = getContentType(msg.message); + } + + msg.mentions = msg.message[msg.type]?.contextInfo ? msg.message[msg.type]?.contextInfo.mentionedJid : null; + try { + const quoted = msg.message[msg.type]?.contextInfo; + if (quoted.quotedMessage["ephemeralMessage"]) { + const tipe = Object.keys(quoted.quotedMessage.ephemeralMessage.message)[0]; + if (tipe === "viewOnceMessage") { + msg.quoted = { + type: "view_once", + stanzaId: quoted.stanzaId, + participant: decodeJid(quoted.participant), + message: quoted.quotedMessage.ephemeralMessage.message.viewOnceMessage.message, + }; + } else { + msg.quoted = { + type: "ephemeral", + stanzaId: quoted.stanzaId, + participant: decodeJid(quoted.participant), + message: quoted.quotedMessage.ephemeralMessage.message, + }; + } + } else if (quoted.quotedMessage["viewOnceMessage"]) { + msg.quoted = { + type: "view_once", + stanzaId: quoted.stanzaId, + participant: decodeJid(quoted.participant), + message: quoted.quotedMessage.viewOnceMessage.message, + }; + } else { + msg.quoted = { + type: "normal", + stanzaId: quoted.stanzaId, + participant: decodeJid(quoted.participant), + message: quoted.quotedMessage, + }; + } + msg.quoted.isSelf = msg.quoted.participant === decodeJid(conn.user.id); + msg.quoted.mtype = Object.keys(msg.quoted.message).filter( + (v) => v.includes("Message") || v.includes("conversation") + )[0]; + msg.quoted.text = + msg.quoted.message[msg.quoted.mtype]?.text || + msg.quoted.message[msg.quoted.mtype]?.description || + msg.quoted.message[msg.quoted.mtype]?.caption || + msg.quoted.message[msg.quoted.mtype]?.hydratedTemplate?.hydratedContentText || + msg.quoted.message[msg.quoted.mtype] || + ""; + msg.quoted.key = { id: msg.quoted.stanzaId, fromMe: msg.quoted.isSelf, remoteJid: msg.from }; + msg.quoted.delete = () => conn.sendMessage(msg.from, { delete: msg.quoted.key }); + msg.quoted.download = (pathFile) => downloadMedia(msg.quoted.message, pathFile); + } catch { + msg.quoted = null; + } + msg.body = + msg.message?.conversation || + msg.message?.[msg.type]?.text || + msg.message?.[msg.type]?.caption || + (msg.type === "listResponseMessage" && msg.message?.[msg.type]?.singleSelectReply?.selectedRowId) || + (msg.type === "buttonsResponseMessage" && + msg.message?.[msg.type]?.selectedButtonId?.includes("SMH") && + msg.message?.[msg.type]?.selectedButtonId) || + (msg.type === "templateButtonReplyMessage" && msg.message?.[msg.type]?.selectedId) || + ""; + msg.reply = (text) => conn.sendMessage(msg.from, { text }, { quoted: msg }); + msg.download = (pathFile) => downloadMedia(msg.message, pathFile); + } + return msg; +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 82bda32..6f67254 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import makeWASocket, {Browsers, useMultiFileAuthState } from '@adiwajshing/baileys'; +import makeWASocket, {Browsers } from '@adiwajshing/baileys'; import P from 'pino'; import chatHandle from './handle/chat.js'; import connectionHandle from './handle/connection.js'; @@ -26,11 +26,11 @@ export default async function main(){ conn.ev.on('creds.update', saveCreds) store.bind(conn.ev); - conn.ev.on('connection.update', (update)=> { + conn.ev.on('connection.update', update => { connectionHandle(update, conn, main); }) - conn.ev.on('messages.upsert', (msg) => { + conn.ev.on('messages.upsert', msg => { chatHandle(msg,conn); })