-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
139 lines (120 loc) · 3.55 KB
/
bot.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
require("dotenv").config();
const { Bot, GrammyError, HttpError } = require("grammy");
const grabLink = require("youtube-thumbnail-grabber");
const youtubeRegex = new RegExp(
/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+|^(www\.)?youtu\.be\/.+/
);
// Bot
const bot = new Bot(process.env.BOT_TOKEN);
// Response
async function responseTime(ctx, next) {
const before = Date.now();
await next();
const after = Date.now();
console.log(`Response time: ${after - before} ms`);
}
bot.use(responseTime);
// Commands
bot.command("start", async (ctx) => {
if (!ctx.chat.type == "private") {
await bot.api.sendMessage(
ctx.chat.id,
"*Channels and groups are not supported presently.*",
{ parse_mode: "Markdown" }
);
return;
}
await ctx
.reply("*Welcome!* ✨\n_Send a YouTube link to get the thumbnail._", {
parse_mode: "Markdown",
})
.then(console.log("New user added:\n", ctx.from));
});
bot.command("help", async (ctx) => {
await ctx
.reply(
"*@anzubo Project.*\n\n_This bot gets thumbnails for YouTube videos.\nSend a link to try it out!_",
{ parse_mode: "Markdown" }
)
.then(console.log("Help command sent to", ctx.chat.id));
});
// Messages
bot.on("message", async (ctx) => {
// Logging
const from = ctx.from;
const name =
from.last_name === undefined
? from.first_name
: `${from.first_name} ${from.last_name}`;
console.log(
`From: ${name} (@${from.username}) ID: ${from.id}\nMessage: ${ctx.message.text}`
);
// Logic
if (!youtubeRegex.test(ctx.message.text)) {
await ctx.reply("*Send a valid YouTube link.*", {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id,
});
return;
}
try {
async function getLink(ytUrl) {
const thumbnailLink = grabLink(ytUrl, "max");
await ctx.replyWithPhoto(thumbnailLink, {
reply_to_message_id: ctx.message.message_id,
});
}
await getLink(ctx.message.text);
} catch (error) {
if (error instanceof GrammyError) {
if (error.message.includes("Forbidden: bot was blocked by the user")) {
console.log("Bot was blocked by the user");
} else if (error.message.includes("Call to 'sendPhoto' failed!")) {
console.log("Error sending message: ", error);
await ctx.reply(`*Error contacting Telegram.*`, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id,
});
} else {
await ctx.reply(`*An error occurred: ${error.message}*`, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id,
});
}
console.log(`Error sending message: ${error.message}`);
return;
} else {
console.log(`An error occured:`, error);
await ctx.reply(`*An error occurred.*\n_Error: ${error.message}_`, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id,
});
return;
}
}
});
// Error
bot.catch((err) => {
const ctx = err.ctx;
console.error(
"Error while handling update",
ctx.update.update_id,
"\nQuery:",
ctx.msg.text
);
const e = err.error;
if (e instanceof GrammyError) {
console.error("Error in request:", e);
if (e.description === "Forbidden: bot was blocked by the user") {
console.log("Bot was blocked by the user");
} else {
ctx.reply("An error occurred");
}
} else if (e instanceof HttpError) {
console.error("Could not contact Telegram:", e);
} else {
console.error("Unknown error:", e);
}
});
// Run
bot.start();