forked from yusufusta/WhatsAsena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremovebg.js
54 lines (43 loc) · 1.85 KB
/
removebg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Copyright (C) 2020 Yusuf Usta.
Licensed under the GPL-3.0 License;
you may not use this file except in compliance with the License.
WhatsAsena - Yusuf Usta
*/
const Asena = require('../events');
const {MessageType, Mimetype} = require('@adiwajshing/baileys');
const Config = require('../config');
const fs = require('fs');
const got = require('got');
const FormData = require('form-data');
const stream = require('stream');
const {promisify} = require('util');
const pipeline = promisify(stream.pipeline);
const Language = require('../language');
const Lang = Language.getString('removebg');
Asena.addCommand({pattern: 'removebg ?(.*)', fromMe: true, desc: Lang.REMOVEBG_DESC}, (async (message, match) => {
if (message.reply_message === false || message.reply_message.image === false) return await message.client.sendMessage(message.jid,Lang.NEED_PHOTO,MessageType.text);
if (Config.RBG_API_KEY === false) return await message.client.sendMessage(message.jid,Lang.NO_API_KEY,MessageType.text);
var load = await message.reply(Lang.RBGING);
var location = await message.client.downloadAndSaveMediaMessage({
key: {
remoteJid: message.reply_message.jid,
id: message.reply_message.id
},
message: message.reply_message.data.quotedMessage
});
var form = new FormData();
form.append('image_file', fs.createReadStream(location));
form.append('size', 'auto');
var rbg = await got.stream.post('https://api.remove.bg/v1.0/removebg', {
body: form,
headers: {
'X-Api-Key': Config.RBG_API_KEY
}
});
await pipeline(
rbg,
fs.createWriteStream('rbg.png')
);
await message.client.sendMessage(message.jid,fs.readFileSync('rbg.png'), MessageType.document, {filename: 'WhatsAsena.png', mimetype: Mimetype.png});
await load.delete();
}));