-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
79 lines (67 loc) · 1.97 KB
/
Copy pathapp.js
File metadata and controls
79 lines (67 loc) · 1.97 KB
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
require("dotenv").config("/.env");
const { Telegraf } = require("telegraf");
const Jimp = require("jimp");
const axios = require("axios");
const fs = require("fs");
const Path = require("path");
var Token = process.env.BOT_TOKEN;
const bot = new Telegraf(Token);
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Bot token:', token => {
Token = token;
readline.close();
});
const send_image = (id, img_path) => {
console.log(id);
bot.telegram
.sendPhoto(id, { source: img_path })
.catch((err) => console.log(err));
};
bot.start((ctx) => ctx.reply("Welcome!"));
bot.command("img", (ctx) => {
ctx.replyWithPhoto({ source: "./savedimages/123.png" });
});
bot.on("photo", async (ctx) => {
console.log("received msg");
const imageData = await bot.telegram
.getFile(ctx.message.photo[ctx.message.photo.length - 1].file_id)
.catch((err) => console.log(err));
const savepath = Path.resolve(
__dirname,
"savedimages",
`${Date.now()}-original.png`
);
const writer = fs.createWriteStream(savepath);
response = await axios({
method: "get",
url: `https://api.telegram.org/file/bot${Token}/${imageData.file_path}`,
responseType: "stream",
}).then(async (response) => {
await response.data.pipe(writer);
writer.on("finish", () => {
writer.end();
ctx.reply("Trying to resize to 512x512");
resizedImgName = resiz(savepath, ctx.chat.id);
// console.log("path is: --- ", savepath);
// send_image(ctx.chat.id, savepath);
});
});
});
async function resiz(path, chat_id) {
const img_name = `./resizedimages/${Date.now()}512x512.png`;
console.log("name set");
Jimp.read(path)
.then((img) => {
img.resize(512, 512).write(img_name).then(send_image(chat_id, img_name)); // save
})
.catch((err) => {
console.error(err);
});
console.log("returning image");
console.log(img_name);
return img_name;
}
bot.launch();