-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (110 loc) · 3.7 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
import { Input, Telegraf } from "telegraf";
import { message } from "telegraf/filters";
import { generateCodeImage } from "./lib/carbon.js";
import { getImage } from "./lib/mods.js";
import express from "express";
import morgan from "morgan";
const logger = { info: console.log };
const defaultAvatar = "https://avatars.githubusercontent.com/u/124599?v=4";
const bot = new Telegraf(process.env.BOT_TOKEN);
const port = process.env.PORT || 3000;
const webhookDomain =
process.env.DETA_SPACE_APP_HOSTNAME ||
process.env.WEBHOOK_DOMAIN ||
"https://example.com";
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));
bot.start((ctx) => ctx.reply("Welcome to Code Beautifier Bot"));
bot.help((ctx) => ctx.reply("Send me a code to beautify"));
const loaderMessages = [
"🔅 Processing your code...",
"🔆 Please wait a moment...",
"☀️ This may take a while...",
"🌟 Generating image...",
"⭐️ Almost done...",
];
let loaderIndex = 0;
bot.on(message("text"), async (ctx) => {
const botUserName = "@" + bot.botInfo.username;
const isPrivate = ctx.message.chat.type === "private";
if (!isPrivate) {
if (
!ctx.message.text.includes(botUserName) &&
!ctx.message.text.includes("/format")
)
return;
}
let msg = "";
let _user = {};
// check is the message is a reply
if (ctx.message.reply_to_message) {
logger.info(`Reply message recieved from ${ctx.message.from.username}`);
// if (!ctx.message.reply_to_message.from.is_bot) return;
if (!ctx.message.reply_to_message.text) return;
msg = ctx.message.reply_to_message.text
.replace(botUserName, "")
.replace("/format", "");
_user = ctx.message.reply_to_message.from;
} else {
logger.info(`Message recieved from ${ctx.message.from.username}`);
if (!ctx.message.text) return;
msg = ctx.message.text.replace(botUserName, "").replace("/format", "");
_user = ctx.message.from;
}
if (!msg) return;
const _msg = msg.trim().toLowerCase();
if (_msg.length < 3) return;
const codeImage = generateCodeImage(msg); // Long task - Takes 10-15 seconds
let loader = null;
let loaderT = null;
if (isPrivate) {
loader = await ctx.reply("🔅 Nice");
loaderT = setInterval(async () => {
await ctx.telegram.editMessageText(
ctx.message.chat.id,
loader.message_id,
null,
loaderMessages[0]
);
loaderMessages.push(loaderMessages.shift());
}, 4000);
setTimeout(() => {
clearInterval(loaderT);
}, 1000 * 60);
}
const avatar = getAvatar(ctx, _user.id);
const image = await getImage(await codeImage, {
name: (_user.first_name || "") + " " + (_user.last_name || ""),
username: _user.username ? `@${_user.username}` : "Telegram User",
avatar: await avatar,
});
if (isPrivate) {
clearInterval(loaderT);
// delete loader message
loader &&
(await ctx.telegram.deleteMessage(
ctx.message.chat.id,
loader.message_id
));
}
ctx.replyWithPhoto(Input.fromBuffer(image, "code.png"), {
caption: "Here is your code 🎉",
});
});
const app = express();
app.use(morgan(":method :url :status :http-version :response-time "));
app.get("/", (req, res) => {
res.send("Its Working!");
});
app.use(await bot.createWebhook({ domain: webhookDomain }));
app.listen(port, () => console.log("Server listening on port", port));
async function getAvatar(ctx, id) {
try {
const userAvatar = await ctx.telegram.getUserProfilePhotos(id);
const avatarId = userAvatar.photos[0][0].file_id;
const avatar = await ctx.telegram.getFileLink(avatarId);
return avatar.toString() || defaultAvatar;
} catch (error) {
return defaultAvatar;
}
}