(sock, message, args, context)
| 7 | description: 'Download a file directly from a URL', |
| 8 | usage: '.fetch <url>', |
| 9 | async handler(sock, message, args, context) { |
| 10 | const chatId = context.chatId || message.key.remoteJid; |
| 11 | const url = args[0]; |
| 12 | if (!url || !url.startsWith('http')) { |
| 13 | return await sock.sendMessage(chatId, { text: 'Provide a valid URL starting with http/https.' }); |
| 14 | } |
| 15 | try { |
| 16 | await sock.sendMessage(chatId, { text: '📡 *Fetching data...*' }); |
| 17 | const res = await axios.get(url, { responseType: 'arraybuffer' }); |
| 18 | const buffer = Buffer.from(res.data, 'binary'); |
| 19 | const type = await fileTypeFromBuffer(buffer); |
| 20 | if (!type) { |
| 21 | return await sock.sendMessage(chatId, { text: buffer.toString().slice(0, 1000) }); |
| 22 | } |
| 23 | if (type.mime.startsWith('image/')) { |
| 24 | await sock.sendMessage(chatId, { image: buffer }); |
| 25 | } |
| 26 | else if (type.mime.startsWith('video/')) { |
| 27 | await sock.sendMessage(chatId, { video: buffer }); |
| 28 | } |
| 29 | else if (type.mime.startsWith('audio/')) { |
| 30 | await sock.sendMessage(chatId, { audio: buffer, mimetype: type.mime }); |
| 31 | } |
| 32 | else { |
| 33 | await sock.sendMessage(chatId, { document: buffer, mimetype: type.mime, fileName: `file.${type.ext}` }); |
| 34 | } |
| 35 | } |
| 36 | catch (err) { |
| 37 | await sock.sendMessage(chatId, { text: '❌ Failed to fetch. URL might be private or invalid.' }); |
| 38 | } |
| 39 | } |
| 40 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected