2021-03-09 21:42:25 +01:00
/ *
"name" : "alina_bot" ,
"version" : "2.0.0" ,
"description" : "Text translation bot" ,
"main" : "index.js" ,
"scripts" : {
"test" : "echo \"Error: no test specified\" && exit 1"
} ,
"keywords" : [ ] ,
"author" : "Fernando Méndez (https://fermdez.ddns.net | @HumperCobra)" ,
2021-03-10 11:51:15 +01:00
"license" : "Educational Community License v2.0 (ECL-2.0)" ,
2021-03-09 21:42:25 +01:00
}
* /
/* Bot token and API */
const TelegramBot = require ( 'node-telegram-bot-api' ) ;
2021-03-10 11:50:12 +01:00
const _private = require ( './private.js' ) ;
const token = _private . token ( ) ;
2021-03-09 21:42:25 +01:00
const bot = new TelegramBot ( token , { polling : true } ) ;
/* Translator */
//const translate = require('translate-api');
2021-03-11 18:12:45 +01:00
const translate = require ( '@vitalets/google-translate-api' ) ; //https://www.npmjs.com/package/@vitalets/google-translate-api
2021-03-09 21:42:25 +01:00
//const translate = require('google-translate-api');
2021-03-10 11:50:12 +01:00
// To overwrite the users.json file:
const fs = require ( 'fs' ) ;
// File with user information:
2021-03-11 18:12:45 +01:00
try {
if ( ! ( fs . existsSync ( "./users.json" ) ) ) {
fs . writeFile ( './users.json' , JSON . stringify ( { users : [ ] } ) , 'utf8' , function ( err ) {
if ( err ) return console . log ( err ) ;
console . log ( "Users file not found. A new one has been created." ) ;
} ) ;
} else {
console . log ( "Users file load successfully." ) ;
}
} catch ( err ) {
console . log ( "Error creating user file: \n\n" + err ) ;
}
2021-03-10 11:50:12 +01:00
const usersFile = require ( "./users.json" ) ;
// Stores user IDs read from "usuers.json":
const _users = JSON . parse ( JSON . stringify ( usersFile . users ) ) ;
2021-03-09 21:42:25 +01:00
bot . on ( 'polling_error' , function ( error ) {
console . log ( error ) ;
} ) ;
bot . onText ( /^\/start/ , function ( msg ) {
var chatId = msg . chat . id ;
var nameUser = msg . from . first _name ;
var userId = msg . from . id ;
var userIndex = indexOfArray ( msg . from ) ;
if ( userIndex === - 1 ) {
addUser ( msg . from ) ;
2021-03-10 18:51:50 +01:00
//updateUsers();
2021-03-09 21:42:25 +01:00
}
bot . sendMessage ( chatId , "Welcome " + nameUser + "!\n\nI am a text translation bot created by *Fernando* (https://fermdez.ddns.net/en-UK/)."
+ "\n\nUse the command */translator* to change languages." , { parse _mode : 'Markdown' } ) ;
console . log ( "User /start: " + nameUser + ", Id: " + userId ) ;
} ) ;
// Author:
bot . onText ( /^\/author/ , function ( msg ) {
var chatId = msg . chat . id ;
var nameUser = msg . from . first _name ;
var userId = msg . from . id ;
var date = msg . date ;
bot . sendMessage ( chatId , "Author👤: *Fernando Méndez 'Humpercobra'* \n" +
"Website🌐: https://fermdez.ddns.net/en-UK/" , { parse _mode : "Markdown" } ) ;
console . log ( "[" + Date ( date ) + "] " + nameUser + " (" + userId + "): Ha solicitado el autor." ) ;
} ) ;
// Traductor command:
bot . onText ( /^\/translator/ , function ( msg ) {
translator ( msg ) ;
} ) ;
/* Translator */
bot . on ( 'message' , ( msg ) => {
var chatId = msg . chat . id ;
var nameUser = msg . from . first _name ;
2021-03-10 18:51:50 +01:00
//updateUsers();
2021-03-09 21:42:25 +01:00
var userIndex = indexOfArray ( msg . from ) ;
2021-03-10 18:51:50 +01:00
2021-03-09 21:42:25 +01:00
if ( userIndex === - 1 ) {
addUser ( msg . from ) ;
2021-03-10 11:50:12 +01:00
bot . sendMessage ( chatId , "Welcome *" + nameUser + "*!" + "\nWe have just added you to the list of users. \n\nChoose the language you want to translate with the command: */translator*." , { parse _mode : 'Markdown' } ) ;
2021-03-09 21:42:25 +01:00
}
2021-03-10 18:51:50 +01:00
else {
if ( msg . text . includes ( 'Spanish[🇪🇸]' ) || msg . text . includes ( 'English[🇬🇧]' ) || msg . text . includes ( 'Russian[🇷🇺]' ) ) {
switch ( msg . text ) {
case 'Spanish[🇪🇸] --> English[🇬🇧]' : //_from='es'; _to='en';
_users [ userIndex ] . from = 'es' ; _users [ userIndex ] . to = 'en' ;
updateUsers ( ) ;
bot . sendMessage ( chatId , "*" + nameUser + "*" + ", now I will translate the texts from *Spanish*🇪🇸 to *English*🇬🇧." , { parse _mode : 'Markdown' } ) ;
break ;
case 'English[🇬🇧] --> Spanish[🇪🇸]' : //_from='en'; _to='es';
_users [ userIndex ] . from = 'en' ; _users [ userIndex ] . to = 'es' ;
updateUsers ( ) ;
bot . sendMessage ( chatId , "*" + nameUser + "*" + ", now I will translate the texts from *English*🇬🇧 to *Spanish*🇪🇸." , { parse _mode : 'Markdown' } ) ;
break ;
case 'Spanish[🇪🇸] --> Russian[🇷🇺]' : //_from='es'; _to='ru';
_users [ userIndex ] . from = 'es' ; _users [ userIndex ] . to = 'ru' ;
updateUsers ( ) ;
bot . sendMessage ( chatId , "*" + nameUser + "*" + ", now I will translate the texts from *Spanish*🇪🇸 to *Russian*🇷🇺." , { parse _mode : 'Markdown' } ) ;
break ;
case 'Russian[🇷🇺] --> Spanish[🇪🇸]' : //_from='ru'; _to='es';
_users [ userIndex ] . from = 'ru' ; _users [ userIndex ] . to = 'es' ;
updateUsers ( ) ;
bot . sendMessage ( chatId , "*" + nameUser + "*" + ", now I will translate the texts from *Russian*🇷🇺 to *Spanish*🇪🇸." , { parse _mode : 'Markdown' } ) ;
break ;
case 'English[🇬🇧] --> Russian[🇷🇺]' : //_from='en'; _to='ru';
_users [ userIndex ] . from = 'en' ; _users [ userIndex ] . to = 'ru' ;
updateUsers ( ) ;
bot . sendMessage ( chatId , "*" + nameUser + "*" + ", now I will translate the texts from *English*🇬🇧 to *Russian*🇷🇺." , { parse _mode : 'Markdown' } ) ;
break ;
case 'Russian[🇷🇺] --> English[🇬🇧]' : //_from='ru'; _to='en';
_users [ userIndex ] . from = 'ru' ; _users [ userIndex ] . to = 'en' ;
updateUsers ( ) ;
bot . sendMessage ( chatId , "*" + nameUser + "*" + ", now I will translate the texts from *Russian*🇷🇺 to *English*🇬🇧." , { parse _mode : 'Markdown' } ) ;
break ;
}
} else if ( ! msg . text . includes ( '/' ) && userIndex != - 1 ) {
traduce ( msg ) ;
2021-03-09 21:42:25 +01:00
}
}
} ) ;
function translator ( msg ) {
var chatId = msg . chat . id ;
bot . sendMessage ( chatId , "<b>Choose an option: </b> 🇪🇸 🇬🇧 🇷🇺" ,
{
reply _markup : {
keyboard : [
[
{
text : "Spanish[🇪🇸] --> English[🇬🇧]" , callback _data : 'es_en' ,
} ,
{
text : "English[🇬🇧] --> Spanish[🇪🇸]" , callback _data : 'en_es' ,
}
] ,
[
{
text : "Spanish[🇪🇸] --> Russian[🇷🇺]" , callback _data : 'es_ru' ,
} ,
{
text : "Russian[🇷🇺] --> Spanish[🇪🇸]" , callback _data : 'ru_es' ,
}
] ,
[
{
text : "English[🇬🇧] --> Russian[🇷🇺]" , callback _data : 'es_ru' ,
} ,
{
text : "Russian[🇷🇺] --> English[🇬🇧]" , callback _data : 'ru_es' ,
}
]
]
} ,
parse _mode : "HTML" ,
} ) ;
}
function traduce ( msg ) {
var chatId = msg . chat . id ;
var message = msg . text ;
2021-03-10 18:51:50 +01:00
//var tipoChat = msg.chat.type;
2021-03-10 11:50:12 +01:00
2021-03-09 21:42:25 +01:00
var userIndex = indexOfArray ( msg . from ) ;
2021-03-10 18:51:50 +01:00
/ * i f ( u s e r I n d e x = = = - 1 ) {
addUser ( msg . from ) ;
userIndex = indexOfArray ( msg . from ) ;
} * /
2021-03-09 21:42:25 +01:00
var _from = _users [ userIndex ] . from ;
var _to = _users [ userIndex ] . to ;
/* Without Google-API */
translate ( message , { from : _from , to : _to } ) . then ( res => {
if ( res . from . text . didYouMean ) {
translate ( res . from . text . value , { from : _from , to : _to } ) . then ( resFixed => {
bot . sendMessage ( chatId , "I have corrected a possible typing error: \n\n" + res . from . text . value + "\n\n-----\n\nTraduction: \n\n" + resFixed . text ) ;
console . log ( "Bot auto-corrected --> " + message + " : " + resFixed . text ) ;
} ) ;
}
else {
bot . sendMessage ( chatId , res . text ) ;
console . log ( "Bot traduce --> " + message + " : " + res . text ) ;
}
} ) . catch ( err => {
console . error ( err ) ;
} ) ;
}
function addUser ( newUser ) {
let userToSave ;
var index = indexOfArray ( newUser ) ;
var _from = newUser . language _code ;
var _to = 'en' ;
newUser . from = _from ;
newUser . to = _to ;
if ( index === - 1 ) {
_users . push ( newUser ) ;
userToSave = { users : _users } ;
2021-03-10 18:51:50 +01:00
fs . writeFile ( './users.json' , JSON . stringify ( userToSave ) , 'utf8' , ( err ) => {
2021-03-09 21:42:25 +01:00
if ( err ) /*throw err*/ console . log ( 'ERROR: no se ha actualizado la lista de Usuarios.' ) ;
} ) ;
console . log ( newUser + ' Usuario nuevo añadido la base de datos.' ) ;
} else if ( index > - 1 ) {
console . log ( newUser + ' Ya existe el usuario en la base de datos.' ) ;
}
}
function updateUsers ( ) {
let userToSave = { users : _users } ;
2021-03-10 18:51:50 +01:00
fs . writeFile ( './users.json' , JSON . stringify ( userToSave ) , 'utf8' , ( err ) => {
2021-03-09 21:42:25 +01:00
if ( err ) /*throw err*/ console . log ( 'ERROR: no se ha actualizado la lista de Usuarios.' ) ;
} ) ;
}
function indexOfArray ( _user ) {
var encontrado = false ;
2021-03-10 21:00:45 +01:00
var index = - 1 ;
2021-03-09 21:42:25 +01:00
var i = 0 ;
2021-03-10 18:51:50 +01:00
if ( _users . length === 0 ) {
index = - 1 ;
2021-03-09 21:42:25 +01:00
} else {
2021-03-10 21:00:45 +01:00
do {
if ( _users [ i ] . id === _user . id ) {
index = i ;
}
2021-03-09 21:42:25 +01:00
if ( index != - 1 ) {
encontrado = true ;
} else {
i ++ ;
}
2021-03-10 21:00:45 +01:00
} while ( ! encontrado && i < _users . length ) ;
2021-03-09 21:42:25 +01:00
}
return index ;
2021-03-09 21:46:49 +01:00
}