(sock, message, args, context)
| 44 | description: 'Get a URL for media (image, video, audio, sticker, document).', |
| 45 | usage: '.url (send or reply to media)', |
| 46 | async handler(sock, message, args, context) { |
| 47 | const chatId = context.chatId || message.key.remoteJid; |
| 48 | try { |
| 49 | let targetMsg = null; |
| 50 | if (message.message?.imageMessage || |
| 51 | message.message?.videoMessage || |
| 52 | message.message?.audioMessage || |
| 53 | message.message?.stickerMessage || |
| 54 | message.message?.documentMessage) { |
| 55 | targetMsg = message; |
| 56 | } |
| 57 | if (!targetMsg) { |
| 58 | const quoted = getQuotedMessage(message); |
| 59 | if (quoted) |
| 60 | targetMsg = quoted; |
| 61 | } |
| 62 | if (!targetMsg) { |
| 63 | return sock.sendMessage(chatId, { text: 'Send or reply to a media to get a URL.' }, { quoted: message }); |
| 64 | } |
| 65 | const ext = getExtFromMessage(targetMsg); |
| 66 | if (!ext) |
| 67 | throw new Error('Unsupported media type'); |
| 68 | const buffer = await getMediaBuffer(targetMsg, sock); |
| 69 | if (!buffer) |
| 70 | throw new Error('Failed to download media'); |
| 71 | const tempDir = path.join(process.cwd(), 'tmp'); |
| 72 | if (!fs.existsSync(tempDir)) |
| 73 | fs.mkdirSync(tempDir, { recursive: true }); |
| 74 | const tempPath = path.join(tempDir, `${Date.now()}${ext}`); |
| 75 | fs.writeFileSync(tempPath, buffer); |
| 76 | let url = ''; |
| 77 | try { |
| 78 | if (['.jpg', '.png', '.webp'].includes(ext)) { |
| 79 | try { |
| 80 | url = await TelegraPh(tempPath); |
| 81 | } |
| 82 | catch { |
| 83 | const res = await UploadFileUgu(tempPath); |
| 84 | url = typeof res === 'string' |
| 85 | ? res |
| 86 | : (res.url || res.url_full || ''); |
| 87 | } |
| 88 | } |
| 89 | else { |
| 90 | const res = await UploadFileUgu(tempPath); |
| 91 | url = typeof res === 'string' |
| 92 | ? res |
| 93 | : (res.url || res.url_full || ''); |
| 94 | } |
| 95 | } |
| 96 | finally { |
| 97 | setTimeout(() => { |
| 98 | try { |
| 99 | fs.unlinkSync(tempPath); |
| 100 | } |
| 101 | catch { } |
| 102 | }, 2000); |
| 103 | } |
nothing calls this directly
no test coverage detected