+ add en/ru/de

+ add eol separator
+ update doc
master
io42630 3 years ago
parent fa86d1ae6f
commit 94847c745f

@ -1,10 +1,43 @@
# Translate ReeBot
### About
* Uses DeepL for Telegram inline translations.
* Uses DeepL for Telegram inline translations to `de`, `en` and `ru` (there is no `ua` in DeepL).
### How To
### How To Use
* **setup**
* go to `@translate_reebot`
* type `/start`
* **usage**
* go to any chat
* type `@translate_reebot hello,,`
* replace `hello` with text of your choice.
* (`,,` is the "send query" command)
* this will translate to `de`
* can also type `@translate_reebot hello,,ru` or `@translate_reebot hello,,en`
* this will translate to `ru` or `en`
* for next use should be able to type `@tr` and `TAB`
### Как использовать
* **установка**
* зайдите на `@translate_reebot`
* введите `/start`
* **использование**
* зайдите в любой чат
* введите `@translate_reebot hello,,`
* замените `hello` на текст по вашему выбору.
* (`,,` - это команда "отправить запрос")
* это будет переведено как `de`
* можно также ввести `@translate_reebot hello,,ru` или `@translate_reebot hello,,en`
* это переведет на `ru` или `en`.
* для следующего использования должна быть возможность набирать `@tr` и `TAB`
### How To Run
* enter API-keys into `keys-template.json`, then rename to `keys.json`.
* `npm run build / start`
* in Telegram : add bot > enable inline > /start > use via inline.
### Special Thanks
* https://github.com/baitun/node-telegram-translate-bot
* https://github.com/yagop/node-telegram-bot-api

@ -10,20 +10,38 @@ const options: TelegramBot.ConstructorOptions = {
request: request_options
};
const bot = new TelegramBot(keys.TRANSLATE_REEBOT, options);
const eol = ',,';
bot.on('inline_query', async (msg: InlineQuery) => {
if (msg.query.length > 1) {
const translated = (await translate(msg.query, 'de')).text;
const results: Array<TelegramBot.InlineQueryResult> = [
{
type: 'article',
id: 'hello_id',
title: translated,
input_message_content: {
message_text: translated
}
}
];
await bot.answerInlineQuery(msg.id, results);
bot.on('inline_query', async (query: InlineQuery) => {
const queryText = query.query;
const length = queryText.length;
const basicQuery = queryText.endsWith(eol);
const customQuery =
queryText.endsWith(eol + 'de') ||
queryText.endsWith(eol + 'en') ||
queryText.endsWith(eol + 'ru');
let lang: string;
let transText: string;
if (basicQuery) {
lang = 'de';
transText = queryText.slice(0, length - 2);
} else if (customQuery) {
lang = queryText.slice(length - 2, length);
lang = lang === 'en' ? 'en-US' : lang;
transText = queryText.slice(0, length - 4);
} else {
return;
}
const translated = (await translate(transText, lang)).text;
const results: Array<TelegramBot.InlineQueryResult> = [
{
type: 'article',
id: 'hello',
title: translated,
input_message_content: {
message_text: translated
}
}
];
await bot.answerInlineQuery(query.id, results);
});

Loading…
Cancel
Save