@@ -186,71 +186,70 @@ export function makeWASocket(connectionOptions, options = {}) {
186
186
enumerable : true
187
187
} ,
188
188
sendFile : {
189
- /**
190
- * Send Media/File with Automatic Type Specifier
191
- * @param {String } jid
192
- * @param {String|Buffer } path
193
- * @param {String } filename
194
- * @param {String } caption
195
- * @param {import('@adiwajshing/baileys').proto.WebMessageInfo } quoted
196
- * @param {Boolean } ptt
197
- * @param {Object } options
198
- */
199
- async value ( jid , path , filename = '' , caption = '' , quoted , ptt = false , options = { } ) {
200
- let type = await conn . getFile ( path , true )
201
- let { res, data : file , filename : pathFile } = type
202
- if ( res && res . status !== 200 || file . length <= 65536 ) {
203
- try { throw { json : JSON . parse ( file . toString ( ) ) } }
204
- catch ( e ) { if ( e . json ) throw e . json }
205
- }
206
- let opt = { filename }
207
- if ( quoted ) opt . quoted = quoted
208
- if ( ! type ) options . asDocument = true
209
- let mtype = '' , mimetype = options . mimetype || type . mime , convert
210
- if ( / w e b p / . test ( type . mime ) || ( / i m a g e / . test ( type . mime ) && options . asSticker ) ) mtype = 'sticker'
211
- else if ( / i m a g e / . test ( type . mime ) || ( / w e b p / . test ( type . mime ) && options . asImage ) ) mtype = 'image'
212
- else if ( / v i d e o / . test ( type . mime ) ) mtype = 'video'
213
- else if ( / a u d i o / . test ( type . mime ) ) (
214
- convert = await toAudio ( file , type . ext ) ,
215
- file = convert . data ,
216
- pathFile = convert . filename ,
217
- mtype = 'audio' ,
218
- mimetype = options . mimetype || 'audio/ogg; codecs=opus'
219
- )
220
- else mtype = 'document'
221
- if ( options . asDocument ) mtype = 'document'
189
+ /**
190
+ * Send Media/File with Automatic Type Specifier
191
+ * @param {String } jid - WhatsApp JID to send the message to
192
+ * @param {String|Buffer } path - The path to the file or a buffer
193
+ * @param {String } filename - The name of the file (optional)
194
+ * @param {String } caption - The caption for the media (optional)
195
+ * @param {import('@adiwajshing/baileys').proto.WebMessageInfo } quoted - Message to reply to (optional)
196
+ * @param {Boolean } ptt - If the file is a voice note
197
+ * @param {Object } options - Additional options for sending media
198
+ * @returns {Promise<import('@adiwajshing/baileys').proto.WebMessageInfo> }
199
+ */
200
+ async value ( jid , path , filename = '' , caption = '' , quoted , ptt = false , options = { } ) {
201
+ try {
202
+ // Get the file details
203
+ let type = await conn . getFile ( path , true ) ;
204
+ let { res, data : file , filename : pathFile } = type ;
222
205
223
- delete options . asSticker
224
- delete options . asLocation
225
- delete options . asVideo
226
- delete options . asDocument
227
- delete options . asImage
206
+ if ( res && res . status !== 200 || file . length <= 65536 ) {
207
+ try { throw { json : JSON . parse ( file . toString ( ) ) } }
208
+ catch ( e ) { if ( e . json ) throw e . json }
209
+ }
228
210
229
- let message = {
230
- ...options ,
231
- caption,
232
- ptt,
233
- [ mtype ] : { url : pathFile } ,
234
- mimetype,
235
- fileName : filename || pathFile . split ( '/' ) . pop ( )
236
- }
237
- /**
238
- * @type {import('@adiwajshing/baileys').proto.WebMessageInfo }
239
- */
240
- let m
241
- try {
242
- m = await conn . sendMessage ( jid , message , { ...opt , ...options } )
243
- } catch ( e ) {
244
- console . error ( e )
245
- m = null
246
- } finally {
247
- if ( ! m ) m = await conn . sendMessage ( jid , { ...message , [ mtype ] : file } , { ...opt , ...options } )
248
- file = null // releasing the memory
249
- return m
250
- }
251
- } ,
252
- enumerable : true
253
- } ,
211
+ // Determine the media type
212
+ const getMimeType = function ( mime , options ) {
213
+ if ( / w e b p / . test ( mime ) || ( options . asSticker && / i m a g e / . test ( mime ) ) ) return 'sticker' ;
214
+ if ( / i m a g e / . test ( mime ) || ( options . asImage && / w e b p / . test ( mime ) ) ) return 'image' ;
215
+ if ( / v i d e o / . test ( mime ) ) return 'video' ;
216
+ if ( / a u d i o / . test ( mime ) ) return 'audio' ;
217
+ return options . asDocument ? 'document' : 'file' ; // Default to document or file
218
+ } ;
219
+ let mtype = getMimeType ( type . mime , options ) ;
220
+ let mimetype = options . mimetype || type . mime ;
221
+
222
+ // Convert audio if needed
223
+ if ( / a u d i o / . test ( type . mime ) ) {
224
+ let convert = await toAudio ( file , type . ext ) ;
225
+ file = convert . data ;
226
+ pathFile = convert . filename ;
227
+ mtype = 'audio' ;
228
+ mimetype = options . mimetype || 'audio/ogg; codecs=opus' ;
229
+ }
230
+
231
+ // Prepare message options
232
+ let message = {
233
+ caption,
234
+ ptt,
235
+ [ mtype ] : { url : pathFile } ,
236
+ mimetype,
237
+ fileName : filename || pathFile . split ( '/' ) . pop ( ) ,
238
+ ...options
239
+ } ;
240
+
241
+ // Send the message
242
+ let opt = { filename, quoted, ptt, ...options } ;
243
+ let m = await conn . sendMessage ( jid , message , opt ) ;
244
+
245
+ return m ;
246
+ } catch ( err ) {
247
+ console . error ( 'Failed to send file:' , err ) ;
248
+ throw new Error ( 'Failed to send media file: ' + err . message ) ;
249
+ }
250
+ } ,
251
+ enumerable : true
252
+ } ,
254
253
sendContact : {
255
254
/**
256
255
* Send Contact
0 commit comments