mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: Send Status (status@broadcast) {text, media, audio(with waveform)} (#249)
Co-authored-by: Davidson Gomes <contato@agenciadgcode.com>
This commit is contained in:
@@ -207,11 +207,20 @@ export async function getAudioDuration(buffer: Buffer | string | Readable) {
|
||||
/**
|
||||
referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
|
||||
*/
|
||||
export async function getAudioWaveform(bodyPath: string, logger?: Logger) {
|
||||
export async function getAudioWaveform(buffer: Buffer | string | Readable, logger?: Logger) {
|
||||
try {
|
||||
const { default: audioDecode } = await import('audio-decode')
|
||||
const fileBuffer = await fs.readFile(bodyPath)
|
||||
const audioBuffer = await audioDecode.default(fileBuffer)
|
||||
const audioDecode = (...args) => import('audio-decode').then(({ default: audioDecode }) => audioDecode(...args))
|
||||
let audioData: Buffer
|
||||
if(Buffer.isBuffer(buffer)) {
|
||||
audioData = buffer
|
||||
} else if(typeof buffer === 'string') {
|
||||
const rStream = createReadStream(buffer)
|
||||
audioData = await toBuffer(rStream)
|
||||
} else {
|
||||
audioData = await toBuffer(buffer)
|
||||
}
|
||||
|
||||
const audioBuffer = await audioDecode(audioData)
|
||||
|
||||
const rawData = audioBuffer.getChannelData(0) // We only need to work with one channel of data
|
||||
const samples = 64 // Number of samples we want to have in our final data set
|
||||
@@ -773,3 +782,8 @@ const MEDIA_RETRY_STATUS_MAP = {
|
||||
[proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
|
||||
[proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418,
|
||||
} as const
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
function __importStar(arg0: any): any {
|
||||
throw new Error('Function not implemented.')
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import {
|
||||
WAProto,
|
||||
WATextMessage,
|
||||
} from '../Types'
|
||||
import { isJidGroup, jidNormalizedUser } from '../WABinary'
|
||||
import { isJidGroup, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary'
|
||||
import { sha256 } from './crypto'
|
||||
import { generateMessageID, getKeyAuthor, unixTimestampSeconds } from './generics'
|
||||
import { downloadContentFromMessage, encryptedStream, generateThumbnail, getAudioDuration, getAudioWaveform, MediaDownloadOptions } from './messages-media'
|
||||
@@ -31,7 +31,7 @@ import { downloadContentFromMessage, encryptedStream, generateThumbnail, getAudi
|
||||
type MediaUploadData = {
|
||||
media: WAMediaUpload
|
||||
caption?: string
|
||||
ptt?: boolean
|
||||
ptt?: boolean | string
|
||||
seconds?: number
|
||||
gifPlayback?: boolean
|
||||
fileName?: string
|
||||
@@ -40,6 +40,7 @@ type MediaUploadData = {
|
||||
width?: number
|
||||
height?: number
|
||||
waveform?: Uint8Array
|
||||
backgroundArgb?: number
|
||||
}
|
||||
|
||||
const MIMETYPE_MAP: { [T in MediaType]?: string } = {
|
||||
@@ -82,6 +83,21 @@ export const generateLinkPreviewIfRequired = async(text: string, getUrlInfo: Mes
|
||||
}
|
||||
}
|
||||
|
||||
const assertColor = async(color) => {
|
||||
let assertedColor
|
||||
if(typeof color === 'number') {
|
||||
assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1
|
||||
} else {
|
||||
let hex = color.trim().replace('#', '')
|
||||
if(hex.length <= 6) {
|
||||
hex = 'FF' + hex.padStart(6, '0')
|
||||
}
|
||||
|
||||
assertedColor = parseInt(hex, 16)
|
||||
return assertedColor
|
||||
}
|
||||
}
|
||||
|
||||
export const prepareWAMessageMedia = async(
|
||||
message: AnyMediaMessageContent,
|
||||
options: MediaGenerationOptions
|
||||
@@ -139,7 +155,8 @@ export const prepareWAMessageMedia = async(
|
||||
const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined'
|
||||
const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') &&
|
||||
(typeof uploadData['jpegThumbnail'] === 'undefined')
|
||||
const requiresWaveformProcessing = mediaType === 'audio' && uploadData?.ptt === true
|
||||
const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === 'true' || true
|
||||
const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === 'true' || true
|
||||
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation
|
||||
const {
|
||||
mediaKey,
|
||||
@@ -195,6 +212,16 @@ export const prepareWAMessageMedia = async(
|
||||
uploadData.waveform = await getAudioWaveform(bodyPath!, logger)
|
||||
logger?.debug('processed waveform')
|
||||
}
|
||||
|
||||
if(requiresWaveformProcessing) {
|
||||
uploadData.waveform = await getAudioWaveform(bodyPath!, logger)
|
||||
logger?.debug('processed waveform')
|
||||
}
|
||||
|
||||
if(requiresAudioBackground) {
|
||||
uploadData.backgroundArgb = await assertColor(options.backgroundColor)
|
||||
logger?.debug('computed backgroundColor audio status')
|
||||
}
|
||||
} catch(error) {
|
||||
logger?.warn({ trace: error.stack }, 'failed to obtain extra info')
|
||||
}
|
||||
@@ -321,6 +348,14 @@ export const generateWAMessageContent = async(
|
||||
}
|
||||
}
|
||||
|
||||
if(options.backgroundColor) {
|
||||
extContent.backgroundArgb = await assertColor(options.backgroundColor)
|
||||
}
|
||||
|
||||
if(options.font) {
|
||||
extContent.font = options.font
|
||||
}
|
||||
|
||||
m.extendedTextMessage = extContent
|
||||
} else if('contacts' in message) {
|
||||
const contactLen = message.contacts.contacts.length
|
||||
@@ -583,7 +618,7 @@ export const generateWAMessageFromContent = (
|
||||
message: message,
|
||||
messageTimestamp: timestamp,
|
||||
messageStubParameters: [],
|
||||
participant: isJidGroup(jid) ? userJid : undefined,
|
||||
participant: isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined,
|
||||
status: WAMessageStatus.PENDING
|
||||
}
|
||||
return WAProto.WebMessageInfo.fromObject(messageJSON)
|
||||
|
||||
Reference in New Issue
Block a user