-
Notifications
You must be signed in to change notification settings - Fork 15
/
albumMeBot.js
126 lines (107 loc) · 3.48 KB
/
albumMeBot.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
const { Telegraf } = require("telegraf");
const TelegrafI18n = require("telegraf-i18n");
const TelegrafLocalSession = require("telegraf-session-local");
const path = require("path");
// Prepare i18n
const i18n = new TelegrafI18n({
defaultLanguage: "en",
allowMissing: true,
directory: path.resolve(__dirname, "locales"),
});
// Prepare sessions
const LocalSession = new TelegrafLocalSession({
storage: TelegrafLocalSession.storageMemory,
});
// Create bot and load middlewares
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.use(i18n.middleware());
bot.use(LocalSession.middleware());
//
// Bot logic
//
bot.start((ctx) => {
const message = ctx.i18n.t("greeting", {
username: ctx.from.username ?? "human",
});
return ctx
.reply(message, {
reply_markup: {
keyboard: [
[{ text: ctx.i18n.t("keyboard_done") }],
[{ text: ctx.i18n.t("keyboard_clear") }],
],
one_time_keyboard: false,
},
})
.catch((e) => console.error(e));
});
bot.help((ctx) => {
ctx.reply(ctx.i18n.t("help")).catch((e) => console.error(e));
});
bot.settings((ctx) => {
ctx.reply(ctx.i18n.t("settings")).catch((e) => console.error(e));
});
bot.command("source", (ctx) => {
ctx.reply(ctx.i18n.t("source")).catch((e) => console.error(e));
});
// Add album entries
bot.on("photo", ({ message, session, reply, i18n }) => {
// Ensure a mediaQueue exists in the user's session
if (session.mediaQueue === undefined) session.mediaQueue = [];
//Add photo to album in session
const imgFileId = message.photo.pop().file_id;
session.mediaQueue.push({ type: "photo", media: imgFileId });
});
// Add video entries
bot.on("video", ({ message, session, reply, i18n }) => {
// Ensure a mediaQueue exists in the user's session
if (session.mediaQueue === undefined) session.mediaQueue = [];
//Add video to album in session
const vidFileId = message.video.file_id;
session.mediaQueue.push({ type: "video", media: vidFileId });
});
// Finish album creation
bot.hears(
TelegrafI18n.match("keyboard_done"),
async ({ i18n, reply, replyWithMediaGroup, session }) => {
// Return if only one media item is present
if (!session.mediaQueue || session.mediaQueue.length < 1) {
reply(i18n.t("not_enough_media_items")).catch((e) => console.error(e));
return;
}
// Remove media queue from session
const queue = session.mediaQueue;
session.mediaQueue = [];
// split media into media groups
let n = queue.length;
let pages = Math.ceil(n / 10);
let ItemsPerPage = Math.floor(n / pages);
let k =
n % pages; /* How many times we need to sneek an extra mediaItem in */
try {
for (let i = 0; i < pages; i++) {
// Move media items from queue to to-be-sent queue
const mediaToSend = queue.splice(0, ItemsPerPage + (i < k ? 1 : 0));
// Send media group
await replyWithMediaGroup(mediaToSend);
}
} catch (error) {
reply(
"Something went wrong while sending the album. Please try again in a minute or contact us. (Contact in this bot's profile)"
).catch((err) => {
console.error("Could not send album AND error message to user.");
});
}
}
);
bot.hears(TelegrafI18n.match("keyboard_clear"), (ctx) => {
ctx.session.mediaQueue = [];
return ctx.reply(ctx.i18n.t("queue_cleared")).catch((e) => console.error(e));
});
// Start the bot
console.log(
`Starting longPolling (chat_id of bot: ${
process.env.BOT_TOKEN.split(":")[0]
})...`
);
bot.launch();