diff --git a/package.json b/package.json index 4d614d6..171b0f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "translate-reboot", - "version": "1.0.0", + "version": "0.4", "description": "", "scripts": { "build": "tsc", diff --git a/src/bot-hooks.ts b/src/bot-hooks.ts index 3ca4fe4..1bf34d1 100644 --- a/src/bot-hooks.ts +++ b/src/bot-hooks.ts @@ -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'; const translateDeepL = require('./deepl-translate-adapter').translate; const translateGoogle = require('./google-translate-adapter').translate; const detectGoogle = require('./google-translate-adapter').detect; +const makeResult = require('./results').makeResult; const keys = require('../keys.json'); const request_options: Options = {url: ''}; @@ -11,14 +12,32 @@ const options: TelegramBot.ConstructorOptions = { polling: true, 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 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) => { const queryText = query.query; const length = queryText.length; + if (length < 3) { return; } const basicQuery = queryText.endsWith(eol); - const customQuery = + const customQuery = !basicQuery && ( queryText.endsWith(eol + 'de') || queryText.endsWith(eol + 'en') || queryText.endsWith(eol + 'ua') || @@ -26,7 +45,8 @@ bot.on('inline_query', async (query: InlineQuery) => { queryText.endsWith(eol + 'es') || queryText.endsWith(eol + 'fr') || queryText.endsWith(eol + 'it') || - queryText.endsWith(eol + 'ru'); + queryText.endsWith(eol + 'ru') + ); let lang: string; let text: string; if (basicQuery) { @@ -46,16 +66,13 @@ bot.on('inline_query', async (query: InlineQuery) => { } else { translated = await translateDeepL(text, srcLang, lang); } - - const results: Array = [ - { - type: 'article', - id: 'helloxx', - title: translated, - input_message_content: { - message_text: translated - } - } - ]; - await bot.answerInlineQuery(query.id, results); + const results: Array = []; + if (translated.length < max) { + results.push(makeResult(translated, translated)); + } else { + makeLines(translated).forEach((line: string) => { + results.push(makeResult(line, translated)); + }); + } + await bot.answerInlineQuery(query.id, results, {cache_time: 0, is_personal: true}); }); diff --git a/src/results.ts b/src/results.ts new file mode 100644 index 0000000..0a46310 --- /dev/null +++ b/src/results.ts @@ -0,0 +1,12 @@ +import { InlineQueryResultArticle } from 'node-telegram-bot-api'; + +let id = 0; + +export function makeResult(title: string, message: string) : InlineQueryResultArticle { + return { + type: 'article', + id: '' + ++id, + title: title, + input_message_content: { message_text: message} + }; +}