commit 401cadf040a3598c10112156074304f20c20ce57 Author: io42630 Date: Wed Jun 22 22:04:12 2022 +0200 + init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9b39cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/.idea/ +*keys.json +/ree-bot-translate.iml +package-lock.json +/node_modules +/dist diff --git a/package.json b/package.json new file mode 100644 index 0000000..1ed743e --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "translate-reboot", + "version": "1.0.0", + "description": "", + "scripts": { + "build": "tsc", + "start": "npm run serve", + "serve": "node dist/bot-adapter.js" + }, + "dependencies": { + "axios": "^0.27.2", + "crypto": "^1.0.1", + "deepl-node": "^1.3.1", + "node-telegram-bot-api": "^0.57.0", + "tsc": "^2.0.4", + "tslib": "^2.4.0", + "typescript": "^4.7.4", + "@types/node": "^18.0.0", + "@types/node-telegram-bot-api" : "^0.57.0" + } +} diff --git a/src/bot-adapter.ts b/src/bot-adapter.ts new file mode 100644 index 0000000..c68713c --- /dev/null +++ b/src/bot-adapter.ts @@ -0,0 +1,33 @@ +import TelegramBot from "node-telegram-bot-api"; +import { Options } from "request"; + +const translate = require('./translate-adapter').translate +const keys = require('../keys.json'); + +let request_options: Options = { url: "" }; + +let options: TelegramBot.ConstructorOptions = { + polling: true, + request: request_options +}; +const bot = new TelegramBot(keys.TRANSLATE_REEBOT, options); + +bot.on("inline_query", async msg => { + console.log(JSON.stringify(msg)); + let results: Array = []; + if (msg.query.length <= 1) return; + if (msg.query.length > 1) { + let translated = await translate(msg.query, 'de'); + results = [ + { + type: 'article', + id: 'hello_id', + title: translated.text, + input_message_content: { + message_text: translated.text + } + } + ]; + } + bot.answerInlineQuery(msg.id, results); +}); diff --git a/src/translate-adapter.ts b/src/translate-adapter.ts new file mode 100644 index 0000000..54a062d --- /dev/null +++ b/src/translate-adapter.ts @@ -0,0 +1,16 @@ +import { TextResult, Translator } from 'deepl-node'; + +const keys = require('../keys.json'); +const translator = new Translator(keys.DEEPL); + +export const translate: (a: String, b: String) => Promise = + (async (textToTranslate: any, lang: any) => { + return await translator.translateText(textToTranslate, null, lang); + }); + + + + + + + diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..25f792f --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "target": "es6", + "noImplicitAny": true, + "moduleResolution": "node", + "sourceMap": true, + "outDir": "dist", + "baseUrl": ".", + "paths": { + "*": [ + "node_modules/*", + "src/types/*" + ] + } + }, + "include": [ + "src/**/*" + ] +}