|
|
@ -1,9 +1,10 @@
|
|
|
|
import TelegramBot, { InlineQuery } from 'node-telegram-bot-api';
|
|
|
|
import TelegramBot, { InlineQuery, InlineQueryResultArticle } from 'node-telegram-bot-api';
|
|
|
|
import { Options } from 'request';
|
|
|
|
import { Options } from 'request';
|
|
|
|
|
|
|
|
|
|
|
|
const translateDeepL = require('./deepl-translate-adapter').translate;
|
|
|
|
const translateDeepL = require('./deepl-translate-adapter').translate;
|
|
|
|
const translateGoogle = require('./google-translate-adapter').translate;
|
|
|
|
const translateGoogle = require('./google-translate-adapter').translate;
|
|
|
|
const detectGoogle = require('./google-translate-adapter').detect;
|
|
|
|
const detectGoogle = require('./google-translate-adapter').detect;
|
|
|
|
|
|
|
|
const makeResult = require('./results').makeResult;
|
|
|
|
const keys = require('../keys.json');
|
|
|
|
const keys = require('../keys.json');
|
|
|
|
|
|
|
|
|
|
|
|
const request_options: Options = {url: ''};
|
|
|
|
const request_options: Options = {url: ''};
|
|
|
@ -11,14 +12,32 @@ const options: TelegramBot.ConstructorOptions = {
|
|
|
|
polling: true,
|
|
|
|
polling: true,
|
|
|
|
request: request_options
|
|
|
|
request: request_options
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const bot = new TelegramBot(keys.TRANSLATE_REEBOT, options);
|
|
|
|
const bot = new TelegramBot(keys.TEST ? keys.TEST_TRANSLATE_REEBOT : keys.TRANSLATE_REEBOT, options);
|
|
|
|
const eol = ',,';
|
|
|
|
const eol = ',,';
|
|
|
|
|
|
|
|
const max = 62;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function makeLines(text: string): string[] {
|
|
|
|
|
|
|
|
let lines: string[] = [];
|
|
|
|
|
|
|
|
let split = text.split(' ').reverse();
|
|
|
|
|
|
|
|
let line = '';
|
|
|
|
|
|
|
|
while (split.length !== 0) {
|
|
|
|
|
|
|
|
let word = split.pop();
|
|
|
|
|
|
|
|
line = line + ' ' + word;
|
|
|
|
|
|
|
|
if (line.length + word.length + 1 >= max) {
|
|
|
|
|
|
|
|
lines.push(line);
|
|
|
|
|
|
|
|
line = '';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.trim() !== '') { lines.push(line); }
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bot.on('inline_query', async (query: InlineQuery) => {
|
|
|
|
bot.on('inline_query', async (query: InlineQuery) => {
|
|
|
|
const queryText = query.query;
|
|
|
|
const queryText = query.query;
|
|
|
|
const length = queryText.length;
|
|
|
|
const length = queryText.length;
|
|
|
|
|
|
|
|
if (length < 3) { return; }
|
|
|
|
const basicQuery = queryText.endsWith(eol);
|
|
|
|
const basicQuery = queryText.endsWith(eol);
|
|
|
|
const customQuery =
|
|
|
|
const customQuery = !basicQuery && (
|
|
|
|
queryText.endsWith(eol + 'de') ||
|
|
|
|
queryText.endsWith(eol + 'de') ||
|
|
|
|
queryText.endsWith(eol + 'en') ||
|
|
|
|
queryText.endsWith(eol + 'en') ||
|
|
|
|
queryText.endsWith(eol + 'ua') ||
|
|
|
|
queryText.endsWith(eol + 'ua') ||
|
|
|
@ -26,7 +45,8 @@ bot.on('inline_query', async (query: InlineQuery) => {
|
|
|
|
queryText.endsWith(eol + 'es') ||
|
|
|
|
queryText.endsWith(eol + 'es') ||
|
|
|
|
queryText.endsWith(eol + 'fr') ||
|
|
|
|
queryText.endsWith(eol + 'fr') ||
|
|
|
|
queryText.endsWith(eol + 'it') ||
|
|
|
|
queryText.endsWith(eol + 'it') ||
|
|
|
|
queryText.endsWith(eol + 'ru');
|
|
|
|
queryText.endsWith(eol + 'ru')
|
|
|
|
|
|
|
|
);
|
|
|
|
let lang: string;
|
|
|
|
let lang: string;
|
|
|
|
let text: string;
|
|
|
|
let text: string;
|
|
|
|
if (basicQuery) {
|
|
|
|
if (basicQuery) {
|
|
|
@ -46,16 +66,13 @@ bot.on('inline_query', async (query: InlineQuery) => {
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
translated = await translateDeepL(text, srcLang, lang);
|
|
|
|
translated = await translateDeepL(text, srcLang, lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const results: Array<InlineQueryResultArticle> = [];
|
|
|
|
const results: Array<TelegramBot.InlineQueryResult> = [
|
|
|
|
if (translated.length < max) {
|
|
|
|
{
|
|
|
|
results.push(makeResult(translated, translated));
|
|
|
|
type: 'article',
|
|
|
|
} else {
|
|
|
|
id: 'helloxx',
|
|
|
|
makeLines(translated).forEach((line: string) => {
|
|
|
|
title: translated,
|
|
|
|
results.push(makeResult(line, translated));
|
|
|
|
input_message_content: {
|
|
|
|
});
|
|
|
|
message_text: translated
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await bot.answerInlineQuery(query.id, results, {cache_time: 0, is_personal: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
await bot.answerInlineQuery(query.id, results);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|