-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.ts
248 lines (204 loc) · 6.27 KB
/
utils.ts
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
import { Context as Context_, GrammyError } from "grammy/mod.ts";
import {
InlineKeyboardButton,
InlineKeyboardMarkup,
MessageEntity,
} from "grammy/types.ts";
import { FileFlavor } from "grammy_files/mod.ts";
import { Language, languages } from "./data.ts";
export type Context = FileFlavor<Context_>;
export function answer(ctx: Context, text: string) {
return ctx.answerCallbackQuery({ text, show_alert: true });
}
export function answerError(ctx: Context, err: unknown) {
if (err instanceof GrammyError) {
let text;
if (err.description.length > 200) {
text = err.description.slice(0, 197) + "...";
} else {
text = err.description;
}
return ctx.answerCallbackQuery({ text, show_alert: true });
}
}
export async function findLanguage(
ctx: Context & {
chat: NonNullable<Context["chat"]>;
from: NonNullable<Context["from"]>;
},
): Promise<Language & { id: string }> {
const filteredLanguage = Object.entries(languages).filter(
([_, { edit }]) => edit == ctx.chat.id,
)[0];
if (typeof filteredLanguage === "undefined") {
await answer(ctx, "Language not found.");
throw new Error("LanguageNotFound");
}
const [id, language] = filteredLanguage;
if (!language.translators.includes(ctx.from.id)) {
await answer(ctx, "Action not allowed.");
throw new Error("ActionNotAllowed");
}
return { ...language, id };
}
export function removeButton(
replyMarkup: InlineKeyboardMarkup,
callbackData: string,
) {
for (const x in replyMarkup.inline_keyboard) {
for (const y in replyMarkup.inline_keyboard[x]) {
if (
(replyMarkup
.inline_keyboard[x][y] as InlineKeyboardButton.CallbackButton)
.callback_data == callbackData
) {
delete replyMarkup.inline_keyboard[x][y];
}
}
replyMarkup.inline_keyboard[x] = replyMarkup.inline_keyboard[x].filter(
(y) => y,
);
}
replyMarkup.inline_keyboard = replyMarkup.inline_keyboard.filter(
(x) => x.length != 0,
);
}
export function replaceButton(
replyMarkup: InlineKeyboardMarkup,
currentCallbackData: string,
newText: string | ((current: string) => string),
newCallbackData: string,
) {
for (const i in replyMarkup.inline_keyboard) {
const x = replyMarkup.inline_keyboard[i];
for (const i in x) {
const y = x[i] as InlineKeyboardButton.CallbackButton;
if (y.callback_data == currentCallbackData) {
y.text = typeof newText === "string" ? newText : newText(y.text);
y.callback_data = newCallbackData;
}
}
}
}
export function hasButton(
{ inline_keyboard }: InlineKeyboardMarkup,
callbackData: string,
) {
for (const i in inline_keyboard) {
const x = inline_keyboard[i];
for (const i in x) {
const y = x[i] as InlineKeyboardButton.CallbackButton;
if (y.callback_data.includes(callbackData)) {
return true;
}
}
}
return false;
}
export function getUserLink(id: number) {
return `<a href="tg://user?id=${id}">${id}</>`;
}
export function escape(s: string) {
s = s.replace(/&/g, "&");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/"/g, """);
s = s.replace(/\'/g, "'");
return s;
}
function fixLengths(text: string, entities: MessageEntity[]) {
for (const entity of entities) {
while (text[entity.offset + entity.length - 1] == "\n") {
entity.length--;
}
}
}
// Taken from Telethon with some modifications.
export function unparse(
text: string,
entities: MessageEntity[],
offset = 0,
length?: number,
): string {
if (!text) return text;
else if (entities.length == 0) return escape(text);
fixLengths(text, entities);
length = length ?? text.length;
const html = [];
let lastOffset = 0;
for (let i = 0; i < entities.length; i++) {
const entity = entities[i];
if (entity.offset >= offset + length) break;
const relativeOffset = entity.offset - offset;
if (relativeOffset > lastOffset) {
html.push(escape(text.slice(lastOffset, relativeOffset)));
} else if (relativeOffset < lastOffset) continue;
let skipEntity = false;
const length_ = entity.length;
const text_ = unparse(
text.slice(relativeOffset, relativeOffset + length_),
entities.slice(i + 1, entities.length),
entity.offset,
length_,
);
switch (entity.type) {
case "bold":
html.push(`<b>${text_}</b>`);
break;
case "italic":
html.push(`<i>${text_}</i>`);
break;
case "underline":
html.push(`<u>${text_}</u>`);
break;
case "strikethrough":
html.push(`<s>${text_}</s>`);
break;
case "text_link":
html.push(`<a href="${entity.url}">${text_}</a>`);
break;
case "text_mention":
html.push(`<a href="tg://user?id=${entity.user.id}">${text_}</a>`);
break;
case "spoiler":
html.push(`<span class="tg-spoiler">${text_}</span>`);
break;
case "code":
html.push(`<code>${text_}</code>`);
break;
case "pre":
html.push(
`<pre${
entity.language && ` class="${entity.language}"`
}>${text_}</pre>`,
);
break;
case "blockquote":
html.push(`<blockquote>${text_}</blockquote>`);
break;
default:
skipEntity = true;
}
lastOffset = relativeOffset + (skipEntity ? 0 : length_);
}
html.push(escape(text.slice(lastOffset, text.length)));
return html.join("");
}
export function fixTrans(trans: string, isZh?: boolean) {
for (const tag of ["b", "i", "u", "s", "a", "span", "code", "pre"]) {
trans = trans.replace(new RegExp(`< ?${tag} ?> ?`, "g"), `<${tag}>`);
trans = trans.replace(new RegExp(` ?< ?/ ?${tag} ?>`, "g"), `</${tag}>`);
}
trans = trans.replace(/< ?a href ?= ?"(.+)" ?> ?/g, '<a href="$1">');
trans = trans.replace(/< ?span class ?= ?"(.+)" ?> ?/g, '<span class="$1">');
trans = trans.replace(/< ?pre class ?= ?"(.+)" ?> ?/g, '<pre class="$1">');
if (isZh) trans = trans.replace(/微信/g, "Telegram");
return trans;
}
export function fixText(text: string, isZh?: boolean) {
if (isZh) text = text.replace(/Telegram/gi, "WeChat");
return text;
}
export function isZh(targetLang?: string) {
return !!targetLang?.toLowerCase().startsWith("zh");
}