MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MDX / handler

Function handler

plugins/fetch.ts:12–44  ·  view source on GitHub ↗
(sock: any, message: any, args: any, context: BotContext)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected