-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot_core.js
281 lines (259 loc) · 6.86 KB
/
bot_core.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
const token = process.env.TOKEN;
const Telegraf = require(`telegraf`);
const bot = new Telegraf(token);
const request = require(`request-promise`);
const atthemeEditorApi = require(`attheme-editor-api`);
const render = require(`./render-pool`);
const path = require(`path`);
const {
MINIMALISTIC_TEMPLATE,
REGULAR_TEMPLATE,
NEW_TEMPLATE,
DESKTOP_TEMPLATE,
} = require(`./preview-maker`);
const RESEND_ON_ERRORS = [`RequestError`, `FetchError`];
const handleRenderError = async (context, error) => {
console.error(error);
if (
context.updateType == `callback_query` ||
context.message.chat.type === `private`
) {
await context.reply(
`An error happened while making the preview. It may be a problem with your theme, or a bug in the bot. If you think it's the latter, please forward this message to @snejugal.
<pre>${error}</pre>`,
{ parse_mode: `HTML` }
);
}
};
bot.context.downloadFile = async function (fileId) {
if (!fileId) {
fileId = this.callbackQuery.message.reply_to_message.document.file_id;
}
const file = await bot.telegram.getFile(fileId);
const fileContent = await request({
encoding: null,
uri: `http://api.telegram.org/file/bot${token}/${file.file_path}`,
});
return fileContent;
};
const handleStart = async (context) => {
const id = context.message.text.slice(`/start `.length).trim();
if (id.length === 0) {
try {
await context.reply(
`Send me an .attheme or a .tdesktop-theme file to create its preview`
);
return;
} catch (err) {
console.error(err);
return;
}
}
const { name, theme } = await atthemeEditorApi.downloadTheme(id);
let preview;
try {
preview = await render({
theme,
name,
template: NEW_TEMPLATE,
});
} catch (error) {
handleRenderError(context, error);
return;
}
const sendPreview = async () => {
try {
await context.replyWithPhoto(
{ source: preview },
{
reply_to_message_id: context.message.message_id,
caption: `${name}\nCreated by @ThemePreviewBot`,
}
);
} catch (error) {
if (RESEND_ON_ERRORS.includes(error)) {
process.nextTick(sendPreview);
} else {
console.error(error);
}
}
};
sendPreview();
};
const choose = async (context) => {
const fileName = context.message.document.file_name;
if (!fileName) {
return;
}
const extenstion = path.extname(fileName);
const name = path.basename(fileName, extenstion);
if (![`.attheme`, `.tdesktop-theme`].includes(extenstion)) {
return;
}
const isAttheme = extenstion === `.attheme`;
if (
isAttheme &&
![`group`, `supergroup`].includes(context.message.chat.type)
) {
try {
await context.reply(`Select the style`, {
reply_markup: {
inline_keyboard: [
[
{ text: `Ordinary`, callback_data: `ordinary` },
{ text: `Minimalistic`, callback_data: `minimalistic` },
],
],
},
reply_to_message_id: context.message.message_id,
});
return;
} catch (err) {
console.error(err);
return;
}
}
bot.telegram.sendChatAction(context.message.chat.id, `upload_photo`);
let template = DESKTOP_TEMPLATE;
let reply_markup = {};
if (isAttheme) {
template = REGULAR_TEMPLATE;
reply_markup = {
inline_keyboard: [
[{ text: `Minimalistic`, callback_data: `minimalistic` }],
],
};
}
const theme = await context.downloadFile(context.message.document.file_id);
let preview;
try {
preview = await render({
theme,
name,
template,
});
} catch (error) {
handleRenderError(context, error);
return;
}
const sendPreview = async () => {
try {
await context.replyWithPhoto(
{ source: preview },
{
// eslint-disable-next-line camelcase
reply_to_message_id: context.message.message_id,
reply_markup,
caption: isAttheme
? `Created by @ThemePreviewBot`
: `Design by @voidrainbow`,
}
);
} catch (error) {
if (RESEND_ON_ERRORS.includes(error.name)) {
process.nextTick(sendPreview);
} else {
console.error(error);
}
}
};
sendPreview();
};
const handleDocument = async (context) => {
const callbackQuery = context.callbackQuery;
const callbackMessage = callbackQuery.message;
try {
if (callbackMessage.text == `Select the style`) {
await context.deleteMessage(callbackMessage.message_id);
} else {
await bot.telegram.editMessageCaption(
callbackMessage.chat.id,
callbackMessage.message_id,
callbackMessage.message_id,
`Updating the preview...`
);
}
} catch (err) {
console.error(err);
return;
}
if (!callbackMessage.reply_to_message) return;
try {
await bot.telegram.sendChatAction(callbackMessage.chat.id, `upload_photo`);
} catch (err) {
console.error(err);
return;
}
const fileName = callbackMessage.reply_to_message.document.file_name;
const theme = await context.downloadFile();
let preview;
try {
preview = await render({
theme,
name: fileName.replace(`.attheme`, ``),
template:
callbackQuery.data == `ordinary`
? REGULAR_TEMPLATE
: MINIMALISTIC_TEMPLATE,
});
} catch (error) {
handleRenderError(context, error);
return;
}
const sendPreview = async () => {
const reply_markup = {
inline_keyboard: [
[
{
text:
callbackQuery.data == `ordinary` ? `Minimalistic` : `Ordinary`,
callback_data:
callbackQuery.data == `ordinary` ? `minimalistic` : `ordinary`,
},
],
],
};
const caption =
callbackQuery.data == `minimalistic`
? `Design by @voidrainbow`
: `Created by @ThemePreviewBot`;
try {
if (callbackMessage.text == `Select the style`) {
await context.replyWithPhoto(
{ source: preview },
{
reply_to_message_id: callbackMessage.reply_to_message.message_id,
reply_markup,
caption,
}
);
} else {
await context.editMessageMedia(
{ type: `photo`, media: { source: preview }, caption },
{ reply_markup }
);
}
} catch (error) {
if (RESEND_ON_ERRORS.includes(error.name)) {
process.nextTick(sendPreview);
} else {
console.error(error);
}
}
};
sendPreview();
};
bot.start((context) => {
handleStart(context);
});
bot.help((context) => {
context.reply(`Send me an .attheme file to create its preview`);
});
bot.on(`document`, (context) => {
choose(context);
});
bot.action([`ordinary`, `minimalistic`], (context) => {
handleDocument(context);
});
bot.polling.offset = -100;
bot.startPolling();