Spaces:
Runtime error
Runtime error
const { proto, downloadContentFromMessage, getContentType } = require('@whiskeysockets/baileys') | |
const fs = require('fs') | |
const downloadMediaMessage = async(m, filename) => { | |
if (m.type === 'viewOnceMessage') { | |
m.type = m.msg.type | |
} | |
if (m.type === 'imageMessage') { | |
var nameJpg = filename ? filename + '.jpg' : 'undefined.jpg' | |
const stream = await downloadContentFromMessage(m.msg, 'image') | |
let buffer = Buffer.from([]) | |
for await (const chunk of stream) { | |
buffer = Buffer.concat([buffer, chunk]) | |
} | |
fs.writeFileSync(nameJpg, buffer) | |
return fs.readFileSync(nameJpg) | |
} else if (m.type === 'videoMessage') { | |
var nameMp4 = filename ? filename + '.mp4' : 'undefined.mp4' | |
const stream = await downloadContentFromMessage(m.msg, 'video') | |
let buffer = Buffer.from([]) | |
for await (const chunk of stream) { | |
buffer = Buffer.concat([buffer, chunk]) | |
} | |
fs.writeFileSync(nameMp4, buffer) | |
return fs.readFileSync(nameMp4) | |
} else if (m.type === 'audioMessage') { | |
var nameMp3 = filename ? filename + '.mp3' : 'undefined.mp3' | |
const stream = await downloadContentFromMessage(m.msg, 'audio') | |
let buffer = Buffer.from([]) | |
for await (const chunk of stream) { | |
buffer = Buffer.concat([buffer, chunk]) | |
} | |
fs.writeFileSync(nameMp3, buffer) | |
return fs.readFileSync(nameMp3) | |
} else if (m.type === 'stickerMessage') { | |
var nameWebp = filename ? filename + '.webp' : 'undefined.webp' | |
const stream = await downloadContentFromMessage(m.msg, 'sticker') | |
let buffer = Buffer.from([]) | |
for await (const chunk of stream) { | |
buffer = Buffer.concat([buffer, chunk]) | |
} | |
fs.writeFileSync(nameWebp, buffer) | |
return fs.readFileSync(nameWebp) | |
} else if (m.type === 'documentMessage') { | |
var ext = m.msg.fileName.split('.')[1].toLowerCase().replace('jpeg', 'jpg').replace('png', 'jpg').replace('m4a', 'mp3') | |
var nameDoc = filename ? filename + '.' + ext : 'undefined.' + ext | |
const stream = await downloadContentFromMessage(m.msg, 'document') | |
let buffer = Buffer.from([]) | |
for await (const chunk of stream) { | |
buffer = Buffer.concat([buffer, chunk]) | |
} | |
fs.writeFileSync(nameDoc, buffer) | |
return fs.readFileSync(nameDoc) | |
} | |
} | |
const sms = (conn, m, store) => { | |
if (!m) return m | |
let M = proto.WebMessageInfo | |
if (m.key) { | |
m.id = m.key.id | |
m.isBot = m.id.startsWith('BAES') && m.id.length === 16 | |
m.isBaileys = m.id.startsWith('BAE5') && m.id.length === 16 | |
m.chat = m.key.remoteJid | |
m.fromMe = m.key.fromMe | |
m.isGroup = m.chat.endsWith('@g.us') | |
m.sender = m.fromMe ? conn.user.id.split(':')[0]+'@s.whatsapp.net' : m.isGroup ? m.key.participant : m.key.remoteJid | |
//m.sender = conn.decodeJid(m.fromMe && conn.user.id || m.participant || m.key.participant || m.chat || '') | |
//if (m.isGroup) m.participant = conn.decodeJid(m.key.participant) || '' | |
} | |
if (m.message) { | |
m.mtype = getContentType(m.message) | |
m.msg = (m.mtype == 'viewOnceMessage' ? m.message[m.mtype].message[getContentType(m.message[m.mtype].message)] : m.message[m.mtype]) | |
try { | |
m.body = (m.mtype === 'conversation') ? m.message.conversation : | |
(m.mtype == 'imageMessage' && m.message.imageMessage.caption != undefined) ? m.message.imageMessage.caption : | |
(m.mtype == 'videoMessage' && m.message.videoMessage.caption != undefined) ? m.message.videoMessage.caption : | |
(m.mtype == 'extendedTextMessage' && m.message.extendedTextMessage.text != undefined) ? m.message.extendedTextMessage.text : | |
(m.mtype == 'buttonsResponseMessage') ? m.message.buttonsResponseMessage.selectedButtonId : | |
(m.mtype == 'listResponseMessage') ? m.message.listResponseMessage.singleSelectReply.selectedRowId : | |
(m.mtype == 'templateButtonReplyMessage') ? m.message.templateButtonReplyMessage.selectedId : | |
(m.mtype === 'messageContextInfo') ? (m.message.buttonsResponseMessage?.selectedButtonId || m.message.listResponseMessage?.singleSelectReply.selectedRowId || m.text) : ''; | |
} catch { | |
m.body = false | |
} | |
let quoted = (m.quoted = m.msg.contextInfo ? m.msg.contextInfo.quotedMessage : null); | |
m.mentionedJid = m.msg.contextInfo ? m.msg.contextInfo.mentionedJid : [] | |
if (m.quoted) { | |
let type = getContentType(quoted) | |
m.quoted = m.quoted[type] | |
if (['productMessage'].includes(type)) { | |
type = getContentType(m.quoted) | |
m.quoted = m.quoted[type] | |
} | |
if (typeof m.quoted === 'string') m.quoted = { text: m.quoted } | |
if(quoted.viewOnceMessageV2) | |
{ | |
console.log("entered ==================================== ") | |
//console.log ("m Is : ",m,"\nm Quoted is :",m.quoted ,"\n Quoted is : ",quoted,"\nviewOnce : ", quoted.viewOnceMessageV2.message) | |
} else | |
{ | |
m.quoted.mtype = type | |
m.quoted.id = m.msg.contextInfo.stanzaId | |
m.quoted.chat = m.msg.contextInfo.remoteJid || m.chat | |
m.quoted.isBot = m.quoted.id ? m.quoted.id.startsWith('BAES') && m.quoted.id.length === 16 : false | |
m.quoted.isBaileys = m.quoted.id ? m.quoted.id.startsWith('BAE5') && m.quoted.id.length === 16 : false | |
m.quoted.sender = conn.decodeJid(m.msg.contextInfo.participant) | |
m.quoted.fromMe = m.quoted.sender === (conn.user && conn.user.id) | |
m.quoted.text = m.quoted.text || m.quoted.caption || m.quoted.conversation || m.quoted.contentText || m.quoted.selectedDisplayText || m.quoted.title || '' | |
m.quoted.mentionedJid = m.msg.contextInfo ? m.msg.contextInfo.mentionedJid : [] | |
m.getQuotedObj = m.getQuotedMessage = async () => { | |
if (!m.quoted.id) return false | |
let q = await store.loadMessage(m.chat, m.quoted.id, conn) | |
return exports.sms(conn, q, store) | |
} | |
let vM = m.quoted.fakeObj = M.fromObject({ | |
key: { | |
remoteJid: m.quoted.chat, | |
fromMe: m.quoted.fromMe, | |
id: m.quoted.id | |
}, | |
message: quoted, | |
...(m.isGroup ? { participant: m.quoted.sender } : {}) | |
}) | |
/** | |
* | |
* @returns | |
*/ | |
let { chat, fromMe, id } = m.quoted; | |
const key = { | |
remoteJid: m.chat, | |
fromMe: false, | |
id: m.quoted.id, | |
participant: m.quoted.sender | |
} | |
m.quoted.delete = async() => await conn.sendMessage(m.chat, { delete: key }) | |
/** | |
* | |
* @param {*} jid | |
* @param {*} forceForward | |
* @param {*} options | |
* @returns | |
*/ | |
m.forwardMessage = (jid, forceForward = true, options = {}) => conn.copyNForward(jid, vM, forceForward,{contextInfo: {isForwarded: false}}, options) | |
/** | |
* | |
* @returns | |
*/ | |
m.quoted.download = () => conn.downloadMediaMessage(m.quoted) | |
} | |
} | |
} | |
if (m.msg.url) m.download = () => conn.downloadMediaMessage(m.msg) | |
m.text = m.msg.text || m.msg.caption || m.message.conversation || m.msg.contentText || m.msg.selectedDisplayText || m.msg.title || '' | |
/** | |
* Reply to this message | |
* @param {String|Object} text | |
* @param {String|false} chatId | |
* @param {Object} options | |
*/ | |
/** | |
* Copy this message | |
*/ | |
m.copy = () => exports.sms(conn, M.fromObject(M.toObject(m))) | |
/** | |
* | |
* @param {*} jid | |
* @param {*} forceForward | |
* @param {*} options | |
* @returns | |
*/ | |
m.copyNForward = (jid = m.chat, forceForward = false, options = {}) => conn.copyNForward(jid, m, forceForward, options) | |
m.sticker = (stik, id = m.chat, option = { mentions: [m.sender] }) => conn.sendMessage(id, { sticker: stik, contextInfo: { mentionedJid: option.mentions } }, { quoted: m }) | |
m.replyimg = (img, teks, id = m.chat, option = { mentions: [m.sender] }) => conn.sendMessage(id, { image: img, caption: teks, contextInfo: { mentionedJid: option.mentions } }, { quoted: m }) | |
m.imgurl = (img, teks, id = m.chat, option = { mentions: [m.sender] }) => conn.sendMessage(id, { image: {url: img }, caption: teks, contextInfo: { mentionedJid: option.mentions } }, { quoted: m }) | |
m.reply = async (content,opt = { packname: "Secktor", author: "SamPandey001" }, type = "text") => { | |
switch (type.toLowerCase()) { | |
case "text":{ | |
return await conn.sendMessage( m.chat, { text: content }, { quoted:m }); | |
} | |
break; | |
case "image": { | |
if (Buffer.isBuffer(content)) { | |
return await conn.sendMessage(m.chat, { image: content, ...opt }, { ...opt } ); | |
} else if (isUrl(content)) { | |
return conn.sendMessage( m.chat, { image: { url: content }, ...opt },{ ...opt } ); | |
} | |
} | |
break; | |
case "video": { | |
if (Buffer.isBuffer(content)) { | |
return await conn.sendMessage(m.chat, { video: content, ...opt }, { ...opt } ); | |
} else if (isUrl(content)) { | |
return await conn.sendMessage( m.chat, { video: { url: content }, ...opt }, { ...opt } ); | |
} | |
} | |
case "audio": { | |
if (Buffer.isBuffer(content)) { | |
return await conn.sendMessage( m.chat, { audio: content, ...opt }, { ...opt } ); | |
} else if (isUrl(content)) { | |
return await conn.sendMessage( m.chat, { audio: { url: content }, ...opt }, { ...opt }); | |
} | |
} | |
break; | |
case "template": | |
let optional = await generateWAMessage(m.chat, content, opt); | |
let message = { viewOnceMessage: { message: { ...optional.message,}, },}; | |
await conn.relayMessage(m.chat, message, { messageId: optional.key.id,}); | |
break; | |
case "sticker":{ | |
let { data, mime } = await conn.getFile(content); | |
if (mime == "image/webp") { | |
let buff = await writeExifWebp(data, opt); | |
await conn.sendMessage(m.chat, { sticker: { url: buff }, ...opt }, opt ); | |
} else { | |
mime = await mime.split("/")[0]; | |
if (mime === "video") { | |
await conn.sendImageAsSticker(m.chat, content, opt); | |
} else if (mime === "image") { | |
await conn.sendImageAsSticker(m.chat, content, opt); | |
} | |
} | |
} | |
break; | |
} | |
} | |
m.senddoc = (doc,type, id = m.chat, option = { mentions: [m.sender], filename: Config.ownername, mimetype: type, | |
externalAdRepl: { | |
title: Config.ownername, | |
body: ' ', | |
thumbnailUrl: ``, | |
thumbnail: log0, | |
mediaType: 1, | |
mediaUrl: '', | |
sourceUrl: gurl, | |
} }) => conn.sendMessage(id, { document: doc, mimetype: option.mimetype, fileName: option.filename, contextInfo: { | |
externalAdReply: option.externalAdRepl, | |
mentionedJid: option.mentions } }, { quoted: m }) | |
m.sendcontact = (name, info, number) => { | |
var vcard = 'BEGIN:VCARD\n' + 'VERSION:3.0\n' + 'FN:' + name + '\n' + 'ORG:' + info + ';\n' + 'TEL;type=CELL;type=VOICE;waid=' + number + ':+' + number + '\n' + 'END:VCARD' | |
conn.sendMessage(m.chat, { contacts: { displayName: name, contacts: [{ vcard }] } }, { quoted: m }) | |
} | |
m.react = (emoji) => conn.sendMessage(m.chat, { react: { text: emoji, key: m.key } }) | |
return m | |
} | |
module.exports = { sms, downloadMediaMessage } | |