MCPcopy Create free account
hub / github.com/Xchristech2/Zenitsu-Bot / urlCommand

Function urlCommand

commands/url.js:50–97  ·  view source on GitHub ↗
(sock, chatId, message)

Source from the content-addressed store, hash-verified

48}
49
50async function urlCommand(sock, chatId, message) {
51 try {
52 // Prefer current message media, else quoted media
53 let media = await getMediaBufferAndExt(message);
54 if (!media) media = await getQuotedMediaBufferAndExt(message);
55
56 if (!media) {
57 await sock.sendMessage(chatId, { text: 'Send or reply to a media (image, video, audio, sticker, document) to get a URL.' }, { quoted: message });
58 return;
59 }
60
61 const tempDir = path.join(__dirname, '../temp');
62 if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir, { recursive: true });
63 const tempPath = path.join(tempDir, `${Date.now()}${media.ext}`);
64 fs.writeFileSync(tempPath, media.buffer);
65
66 let url = '';
67 try {
68 if (media.ext === '.jpg' || media.ext === '.png' || media.ext === '.webp') {
69 // Try TelegraPh for images/webp first (fast, simple)
70 try {
71 url = await TelegraPh(tempPath);
72 } catch {
73 // Fallback to Uguu for any file type
74 const res = await UploadFileUgu(tempPath);
75 url = typeof res === 'string' ? res : (res.url || res.url_full || JSON.stringify(res));
76 }
77 } else {
78 const res = await UploadFileUgu(tempPath);
79 url = typeof res === 'string' ? res : (res.url || res.url_full || JSON.stringify(res));
80 }
81 } finally {
82 setTimeout(() => {
83 try { if (fs.existsSync(tempPath)) fs.unlinkSync(tempPath); } catch {}
84 }, 2000);
85 }
86
87 if (!url) {
88 await sock.sendMessage(chatId, { text: 'Failed to upload media.' }, { quoted: message });
89 return;
90 }
91
92 await sock.sendMessage(chatId, { text: `URL: ${url}` }, { quoted: message });
93 } catch (error) {
94 console.error('[URL] error:', error?.message || error);
95 await sock.sendMessage(chatId, { text: 'Failed to convert media to URL.' }, { quoted: message });
96 }
97}
98
99module.exports = urlCommand;
100

Callers

nothing calls this directly

Calls 4

getMediaBufferAndExtFunction · 0.85
TelegraPhFunction · 0.85
UploadFileUguFunction · 0.85

Tested by

no test coverage detected