Add files via upload

This commit is contained in:
Fernando Méndez 2021-03-10 18:51:50 +01:00 committed by GitHub
parent 06b29173aa
commit b6f4dc6963
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 46 deletions

View File

@ -22,7 +22,7 @@ const bot = new TelegramBot(token, {polling:true});
/* Translator */ /* Translator */
//const translate = require('translate-api'); //const translate = require('translate-api');
const translate = require('@vitalets/google-translate-api'); const translate = require('@vitalets/google-translate-api'); //https://www.npmjs.com/package/@vitalets/google-translate-api
//const translate = require('google-translate-api'); //const translate = require('google-translate-api');
// To overwrite the users.json file: // To overwrite the users.json file:
@ -44,7 +44,7 @@ bot.onText(/^\/start/, function(msg){
var userIndex = indexOfArray(msg.from); var userIndex = indexOfArray(msg.from);
if(userIndex === -1){ if(userIndex === -1){
addUser(msg.from); addUser(msg.from);
updateUsers(); //updateUsers();
} }
bot.sendMessage(chatId, "Welcome " + nameUser + "!\n\nI am a text translation bot created by *Fernando* (https://fermdez.ddns.net/en-UK/)." bot.sendMessage(chatId, "Welcome " + nameUser + "!\n\nI am a text translation bot created by *Fernando* (https://fermdez.ddns.net/en-UK/)."
@ -75,13 +75,15 @@ bot.onText(/^\/translator/, function(msg){
bot.on('message', (msg) => { bot.on('message', (msg) => {
var chatId = msg.chat.id; var chatId = msg.chat.id;
var nameUser = msg.from.first_name; var nameUser = msg.from.first_name;
//updateUsers();
var userIndex = indexOfArray(msg.from); var userIndex = indexOfArray(msg.from);
if(userIndex === -1){ if(userIndex === -1){
addUser(msg.from); addUser(msg.from);
updateUsers();
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'}); 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'});
} }
else{
if(msg.text.includes('Spanish[🇪🇸]') || msg.text.includes('English[🇬🇧]') || msg.text.includes('Russian[🇷🇺]')){ if(msg.text.includes('Spanish[🇪🇸]') || msg.text.includes('English[🇬🇧]') || msg.text.includes('Russian[🇷🇺]')){
switch(msg.text){ switch(msg.text){
case 'Spanish[🇪🇸] --> English[🇬🇧]': //_from='es'; _to='en'; case 'Spanish[🇪🇸] --> English[🇬🇧]': //_from='es'; _to='en';
@ -118,6 +120,7 @@ bot.on('message', (msg) => {
} else if(!msg.text.includes('/') && userIndex != -1){ } else if(!msg.text.includes('/') && userIndex != -1){
traduce(msg); traduce(msg);
} }
}
}); });
function translator(msg){ function translator(msg){
@ -160,9 +163,13 @@ function translator(msg){
function traduce(msg){ function traduce(msg){
var chatId = msg.chat.id; var chatId = msg.chat.id;
var message = msg.text; var message = msg.text;
var tipoChat = msg.chat.type; //var tipoChat = msg.chat.type;
var userIndex = indexOfArray(msg.from); var userIndex = indexOfArray(msg.from);
/* if(userIndex === -1){
addUser(msg.from);
userIndex = indexOfArray(msg.from);
} */
var _from = _users[userIndex].from; var _from = _users[userIndex].from;
var _to = _users[userIndex].to; var _to = _users[userIndex].to;
@ -197,7 +204,7 @@ function addUser(newUser){
if (index === -1){ if (index === -1){
_users.push(newUser); _users.push(newUser);
userToSave = {users:_users}; userToSave = {users:_users};
fs.writeFile('users.json', JSON.stringify(userToSave), 'utf8', (err) => { fs.writeFile('./users.json', JSON.stringify(userToSave), 'utf8', (err) => {
if (err) /*throw err*/ console.log('ERROR: no se ha actualizado la lista de Usuarios.'); 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.'); console.log(newUser + ' Usuario nuevo añadido la base de datos.');
@ -208,7 +215,7 @@ function addUser(newUser){
function updateUsers(){ function updateUsers(){
let userToSave = {users:_users}; let userToSave = {users:_users};
fs.writeFile('users.json', JSON.stringify(userToSave), 'utf8', (err) => { fs.writeFile('./users.json', JSON.stringify(userToSave), 'utf8', (err) => {
if (err) /*throw err*/ console.log('ERROR: no se ha actualizado la lista de Usuarios.'); if (err) /*throw err*/ console.log('ERROR: no se ha actualizado la lista de Usuarios.');
}); });
@ -216,13 +223,14 @@ function updateUsers(){
function indexOfArray(_user){ function indexOfArray(_user){
var encontrado = false; var encontrado = false;
var index = 0;
var i = 0; var i = 0;
if(_users.length == 0){ if(_users.length === 0){
var index = -1; index = -1;
} else { } else {
while(!encontrado && i < _users.length){ while(!encontrado && i < _users.length){
var index = _users.findIndex(function (_user) { index = _users.findIndex(function (_user) {
if(i > 0) return _users[i].id === _user.id; if(i > 0) return _users[i].id === _user.id;
}); });
if(index != -1){ if(index != -1){

View File

@ -1 +1 @@
{"users":[{"id":-1,"is_bot":false,"first_name":"null","username":"NULL","language_code":"es","from":"es","to":"en"}]} {"users":[{"id":-1,"is_bot":false,"first_name":"null","username":"NULL","language_code":"en","from":"en","to":"en"}]}