-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbot_core.js
227 lines (210 loc) · 6.03 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
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`];
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) {
await context.reply(
`Send me an .attheme or a .tdesktop-theme file to create its preview`
);
return;
}
const { name, theme } = await atthemeEditorApi.downloadTheme(id);
const preview = await render({
theme,
name,
template: NEW_TEMPLATE,
});
const sendPreview = async () => {
try {
await context.replyWithPhoto(
{ source: preview },
{
reply_to_message_id: context.message.message_id,
caption: `${name}\nCreated by @AW_ThemeBot`,
}
);
} 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)
) {
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;
}
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);
const preview = await render({
theme,
name,
template,
});
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 @AW_ThemeBot`
: `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;
if (callbackMessage.text == `Select the style`) {
context.deleteMessage(callbackMessage.message_id);
} else {
bot.telegram.editMessageCaption(
callbackMessage.chat.id,
callbackMessage.message_id,
callbackMessage.message_id,
`Updating the preview...`
);
}
bot.telegram.sendChatAction(callbackMessage.chat.id, `upload_photo`);
const fileName = callbackMessage.reply_to_message.document.file_name;
const theme = await context.downloadFile();
const preview = await render({
theme,
name: fileName.replace(`.attheme`, ``),
template:
callbackQuery.data == `ordinary`
? REGULAR_TEMPLATE
: MINIMALISTIC_TEMPLATE,
});
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 @AW_ThemeBot`;
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();
};
adminsList = [1250637598, 1099898513, 1204418975, 1265752496, 1183697491, 873921077, 1201425400];
const isAdmin = (id) => adminsList.includes(id);
bot.start((context) => {
if (isAdmin(context.update.message.from.id)) handleStart(context);
});
bot.help((context) => {
if (isAdmin(context.update.message.from.id))
context.reply(`Send me an .attheme file to create its preview`);
});
bot.on(`document`, (context) => {
if (isAdmin(context.update.message.from.id)) choose(context);
});
bot.action([`ordinary`, `minimalistic`], (context) => {
if (isAdmin(context.update.callback_query.from.id)) handleDocument(context);
});
bot.polling.offset = -100;
bot.startPolling();