-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprepareImage.js
44 lines (37 loc) · 1.25 KB
/
prepareImage.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
const axios = require("axios");
const { Buffer } = require("node:buffer");
const { MessageMedia } = require("whatsapp-web.js");
function prepareImage(ctx) {
return new Promise((resolve, reject) => {
ctx.telegram
.getFileLink(
// get the file id of the last image in the array, why the last image? because the last image is the highest resolution
ctx.update.channel_post.photo[ctx.update.channel_post.photo.length - 1]
.file_id
)
.then((url) => {
// request the image from telegram via axios
axios({ url, responseType: "arraybuffer" })
.then((response) => {
// convert the image to base64
myImage = Buffer.from(response.data, "base64").toString("base64");
// prepare the image to be sent to whatsapp
attachmentData = new MessageMedia(
"image/jpeg",
myImage,
null,
null
);
let caption = ctx.update.channel_post.caption || "";
resolve({ attachmentData, caption });
})
.catch((error) => {
reject(error);
});
})
.catch((error) => {
reject(error);
});
});
}
module.exports = { prepareImage };