Skip to content

Commit

Permalink
base done.
Browse files Browse the repository at this point in the history
  • Loading branch information
ItKarma committed Nov 17, 2022
1 parent 8e1cac3 commit 578139f
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 13 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@ node_modules/
package-lock.json

yarn.lock


sessions/
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 16 additions & 7 deletions src/handle/chat.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) {

}
}
}
134 changes: 134 additions & 0 deletions src/helper/serialize.js
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
})

Expand Down

0 comments on commit 578139f

Please sign in to comment.