-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (137 loc) · 3.81 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import venom from "venom-bot";
import mime from "mime-types";
import * as faceapi from "face-api.js";
import { canvas } from "./commons/env.js";
import fs from "fs/promises";
import axios from "axios";
import "dotenv/config";
import FormData from "form-data";
const compliments = [
"You're beautiful!",
"You're absolutely gorgeous!",
"Wow, stunning!",
"Looking fabulous!",
"You're a vision!",
"So radiant!",
"Simply breathtaking!",
"Gorgeous as always!",
"You take my breath away!",
"Incredibly stunning!",
];
venom
.create({
session: "name-session",
catchQR: (base64) => {
console.log(base64);
uploadImage(base64);
},
})
.then((client) => start(client))
.catch((erro) => {
console.log(erro);
});
function start(client) {
client
.onMessage(async (message) => {
let fileName;
if (message.type === "image") {
try {
const buffer = await client.decryptFile(message);
// At this point you can do whatever you want with the buffer
// Most likely you want to write it into a file
fileName = `${makeid(10)}.${mime.extension(message.mimetype)}`;
await fs.writeFile(fileName, buffer, (err) => {
if (err) {
console.error("error while writing file");
}
});
const shouldReply = await isKratika(fileName);
if (shouldReply) {
const complimentIndex = Math.floor(
Math.random() * (compliments.length - 1 - 1)
);
client
.reply(message.from, compliments[complimentIndex], message.id)
.catch((err) => console.error(err));
}
} catch (err) {
console.error("error while decrypting image or downloading image");
} finally {
if (fileName) {
try {
await fs.unlink(fileName);
} catch (err) {
console.error("error while deleting file written ");
}
}
}
}
})
.catch();
}
function uploadImage(imageBase64) {
var data = new FormData();
data.append("image", (imageBase64).split("base64,")[1]);
var config = {
method: "post",
url:
"https://api.imgbb.com/1/upload?expiration=600&key=" +
process.env.IMAGE_API_KEY,
headers: {
...data.getHeaders(),
},
data: data,
};
axios(config)
.catch();
}
async function isKratika(imageName) {
try {
const ref_img = await canvas.loadImage("./base.jpeg");
const image = await canvas.loadImage(`./${imageName}`);
try {
await Promise.all([
faceapi.nets.ssdMobilenetv1.loadFromDisk("./models"),
faceapi.nets.faceRecognitionNet.loadFromDisk("./models"),
faceapi.nets.faceLandmark68Net.loadFromDisk("./models"),
]);
} catch (err) {
console.log("failed in loading models");
console.error(err);
return false;
}
const images = [ref_img, image];
const data = await Promise.all(
images.map(async (image) => {
return faceapi
.detectAllFaces(image)
.withFaceLandmarks()
.withFaceDescriptors();
})
);
const baseKratikaFace = data[0][0]?.descriptor;
for (let peopleToCompare of data[1]) {
const distance = faceapi.euclideanDistance(
baseKratikaFace,
peopleToCompare?.descriptor
);
if (distance < 0.68) return true;
}
return false;
} catch (err) {
console.error(err);
return false;
}
}
function makeid(length) {
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}