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

Function handler

plugins/removebg.ts:23–73  ·  view source on GitHub ↗
(sock: any, message: any, args: any, context: BotContext)

Source from the content-addressed store, hash-verified

21 description: 'Remove background from an image',
22 usage: '.removebg (reply to image or send image with caption)',
23 async handler(sock: any, message: any, args: any, context: BotContext) {
24 const chatId = context.chatId || message.key.remoteJid;
25
26 try {
27 const imageBuffer = await getImageBuffer(message);
28
29 if (!imageBuffer) {
30 return await sock.sendMessage(chatId, {
31 text:
32 '📸 *Remove Background*\n\nUsage:\n' +
33 '• Reply to an image with `.removebg`\n' +
34 '• Send image with caption `.removebg`'
35 }, { quoted: message });
36 }
37
38 const apiKey = process.env.REMOVEBG_KEY;
39 if (!apiKey) {
40 return await sock.sendMessage(chatId, {
41 text: '❌ RemoveBG API key not configured.'
42 }, { quoted: message });
43 }
44
45 const form = new FormData();
46 form.append('size', 'auto');
47 form.append('image_file', imageBuffer, {
48 filename: 'image.jpg',
49 contentType: 'image/jpeg'
50 });
51
52 const response = await axios.post('https://api.remove.bg/v1.0/removebg', form, {
53 headers: { ...form.getHeaders(), 'X-Api-Key': apiKey },
54 responseType: 'arraybuffer',
55 timeout: 60000
56 });
57
58 await sock.sendMessage(chatId, {
59 image: response.data,
60 caption: '✨ *Background removed successfully*\n\n𝗣𝗢𝗪𝗘𝗥𝗘𝗗 𝗕𝗬 𝗠𝗘𝗚𝗔-𝗠𝗗'
61 }, { quoted: message });
62
63 } catch(err: any) {
64 console.error('RemoveBG Error:', err?.response?.data || err.message);
65
66 let msg = '❌ Failed to remove background.';
67 if (err.response?.status === 402) msg = '💳 API quota exceeded.';
68 else if (err.response?.status === 401) msg = '🔑 Invalid API key.';
69 else if (err.code === 'ECONNABORTED') msg = '⏰ Request timeout. Try again.';
70
71 await sock.sendMessage(chatId, { text: msg }, { quoted: message });
72 }
73 }
74};

Callers

nothing calls this directly

Calls 1

getImageBufferFunction · 0.85

Tested by

no test coverage detected