Skip to content

Instantly share code, notes, and snippets.

@itacirgabral
Created March 28, 2021 16:23
Show Gist options
  • Save itacirgabral/2b623e9dee182ec4909598ebb286d485 to your computer and use it in GitHub Desktop.
Save itacirgabral/2b623e9dee182ec4909598ebb286d485 to your computer and use it in GitHub Desktop.
punkdrummer
const wbi = JSON.parse(message)
const wid = wbi.key.id
const fromMe = wbi.key.fromMe
const remoteJid = wbi.key.remoteJid.split('@s.whatsapp.net')[0]
const participant = wbi.participant
? wbi.participant.split('@s.whatsapp.net')[0]
: undefined
const isCallMissed = wbi.messageStubType === 'CALL_MISSED_VOICE'
if (isCallMissed) {
wbi.message = {
appNotification: 'callMissed'
}
}
const isMessageDeleted = wbi.messageStubType === 'REVOKE'
if (isMessageDeleted) {
wbi.message = {
appNotification: 'messageDeleted'
}
}
const isGroup = remoteJid.indexOf('-') !== -1
const groupId = isGroup
? remoteJid.split('@g.us')[0]
: undefined
const to = isGroup
? seed.shard
: fromMe ? remoteJid : seed.shard
const from = fromMe
? seed.shard
: isGroup
? groupId
: remoteJid
const author = isGroup
? participant
: undefined
const timestamp = wbi.messageTimestamp
if (!(from === 'status@broadcast' || fromMe)) {
const conversation = wbi.message.conversation
const quoteMsg = wbi.message.extendedTextMessage
const location = wbi.message.locationMessage
const contact = wbi.message.contactMessage
const image = wbi.message.imageMessage
const document = wbi.message.documentMessage
const audio = wbi.message.audioMessage
const video = wbi.message.videoMessage
const appNotification = wbi.message.appNotification
const { type, isForwarded, isQuoted, msg, notification } = sortingMessages({
conversation,
quoteMsg,
location,
contact,
image,
document,
audio,
video,
appNotification
})
const { file, params, jsontosend } = await switcher({
type,
timestamp,
to,
from,
wid,
author,
isQuoted,
isForwarded,
seed,
wbi,
msg,
quoteMsg,
location,
contact,
document,
image,
audio,
video,
notification
})
const hook = {
type: 'sendhook',
hardid: seed.hardid,
shard: seed.shard,
file, // only media messagens have it
params,
json: JSON.stringify(jsontosend)
}
const sortingMessages = ({
conversation,
quoteMsg,
location,
contact,
image,
document,
audio,
video,
appNotification
}) => {
let type
let isQuoted = false
let isForwarded = false
let msg
if (conversation) {
type = 'textMessage'
msg = conversation
} else if (quoteMsg) {
type = 'textMessage'
msg = quoteMsg.text
if (quoteMsg.contextInfo?.isForwarded) {
isForwarded = true
}
if (quoteMsg.contextInfo?.stanzaId) {
isQuoted = true
}
} else if (contact) {
type = 'contactMessage'
if (contact?.contextInfo?.isForwarded) {
isForwarded = true
}
if (contact?.contextInfo?.stanzaId) {
isQuoted = true
}
} else if (location) {
type = 'locationMessage'
if (location?.contextInfo?.isForwarded) {
isForwarded = true
}
if (location?.contextInfo?.stanzaId) {
isQuoted = true
}
} else if (image) {
type = 'imageMessage'
if (image?.contextInfo?.isForwarded) {
isForwarded = true
}
if (image?.contextInfo?.stanzaId) {
isQuoted = true
}
} else if (document) {
type = 'documentMessage'
if (document?.contextInfo?.isForwarded) {
isForwarded = true
}
if (document?.contextInfo?.stanzaId) {
isQuoted = true
}
} else if (audio) {
type = 'audioMessage'
if (audio?.contextInfo?.isForwarded) {
isForwarded = true
}
if (audio?.contextInfo?.stanzaId) {
isQuoted = true
}
} else if (video) {
type = 'videoMessage'
if (video?.contextInfo?.isForwarded) {
isForwarded = true
}
if (video?.contextInfo?.stanzaId) {
isQuoted = true
}
} else if (appNotification) {
type = appNotification
}
return { type, isForwarded, isQuoted, msg }
}
module.exports = sortingMessages
const path = require('path')
const switcher = async ({
type,
timestamp,
to,
from,
wid,
author,
isQuoted,
isForwarded,
seed,
wbi,
msg,
quoteMsg,
location,
contact,
document,
image,
audio,
video
}) => {
let file, params, jsontosend
switch (type) {
case 'textMessage':
jsontosend = {
type,
author,
timestamp,
to,
from,
msg,
forwarded: isForwarded ? true : undefined,
quoted: isQuoted ? quoteMsg.contextInfo.stanzaId : undefined,
wid
}
break
case 'locationMessage':
jsontosend = {
type,
author,
timestamp,
to,
from,
forwarded: isForwarded ? true : undefined,
quoted: isQuoted ? location.contextInfo.stanzaId : undefined,
wid,
description: location.address,
latitude: location.degreesLatitude,
longitude: location.degreesLongitude
}
break
case 'contactMessage':
jsontosend = {
type,
author,
timestamp,
to,
from,
forwarded: isForwarded ? true : undefined,
quoted: isQuoted ? contact.contextInfo.stanzaId : undefined,
vcard: contact.vcard,
wid
}
break
case 'callMissed':
jsontosend = {
type,
timestamp,
from,
to
}
break
case 'messageDeleted':
jsontosend = {
type,
author,
timestamp,
to,
from,
wid
}
break
case 'imageMessage':
file = await seed.conn.downloadAndSaveMediaMessage(wbi, path.join(process.cwd(), process.env.UPLOADFOLDER, String(Date.now())))
params = {
type,
timestamp,
to,
author,
from,
wid,
caption: image.caption,
forwarded: isForwarded ? true : undefined,
quoted: isQuoted ? image.contextInfo.stanzaId : undefined,
mimetype: image.mimetype,
size: image.fileLength
}
break
case 'documentMessage':
file = await seed.conn.downloadAndSaveMediaMessage(wbi, path.join(process.cwd(), process.env.UPLOADFOLDER, String(Date.now())))
params = {
type,
timestamp,
to,
author,
from,
wid,
forwarded: isForwarded ? true : undefined,
quoted: isQuoted ? document.contextInfo.stanzaId : undefined,
filename: document.fileName,
mimetype: document.mimetype,
size: document.fileLength
}
break
case 'audioMessage':
file = await seed.conn.downloadAndSaveMediaMessage(wbi, path.join(process.cwd(), process.env.UPLOADFOLDER, String(Date.now())))
params = {
type,
timestamp,
to,
author,
from,
wid,
forwarded: isForwarded ? true : undefined,
quoted: isQuoted ? audio.contextInfo.stanzaId : undefined,
seconds: audio.seconds,
mimetype: audio.mimetype,
size: audio.fileLength
}
break
case 'videoMessage':
file = await seed.conn.downloadAndSaveMediaMessage(wbi, path.join(process.cwd(), process.env.UPLOADFOLDER, String(Date.now())))
params = {
type,
timestamp,
to,
author,
from,
wid,
caption: video.caption,
forwarded: isForwarded ? true : undefined,
quoted: isQuoted ? video.contextInfo.stanzaId : undefined,
seconds: video.seconds,
loop: !!video.gifPlayback,
mimetype: video.mimetype,
size: video.fileLength
}
break
}
return { file, params, jsontosend }
}
module.exports = switcher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment