2020-01-03 01:52:49 +01:00
const tools = require ( '../tools/general' ) ;
const appTelegram = [ '7' ] ;
const printf = require ( 'util' ) . format ;
const { User } = require ( '../models/user' ) ;
2020-01-21 01:37:15 +01:00
const { CalZoom } = require ( '../models/calzoom' ) ;
2020-01-03 01:52:49 +01:00
2020-01-03 22:02:18 +01:00
const emoji = require ( 'node-emoji' ) ;
2020-01-20 01:48:25 +01:00
const i18n = require ( "i18n" ) ;
2020-01-03 22:02:18 +01:00
const Benvenuto = emoji . get ( 'heartbeat' ) + emoji . get ( 'heartbeat' ) + emoji . get ( 'heartbeat' ) + ' Benvenuto!' ;
2020-01-03 01:52:49 +01:00
2020-01-20 01:48:25 +01:00
const emo = {
2020-01-21 01:37:15 +01:00
STARS : emoji . get ( 'stars' ) ,
FIRE : emoji . get ( 'fire' ) ,
2020-01-20 01:48:25 +01:00
DREAM : emoji . get ( 'beach_with_umbrella' ) ,
EYES : emoji . get ( 'eyes' ) ,
DIZZY : emoji . get ( 'dizzy' ) ,
ONE _HUNDRED : emoji . get ( '100' ) ,
SMILE _STAR : emoji . get ( 'star-struck' ) ,
CHECK _VERDE : emoji . get ( 'white_check_mark' ) ,
CHECK _GRIGIA : emoji . get ( 'ballot_box_with_check' ) ,
CROSS _ROSSA : emoji . get ( 'x' ) ,
ENVELOPE : emoji . get ( 'envelope' ) ,
EXCLAMATION _MARK : emoji . get ( 'exclamation' ) ,
QUESTION _MARK : emoji . get ( 'question' ) ,
ARROW _RIGHT : emoji . get ( 'arrow_right' ) ,
INVITATI : emoji . get ( 'man_and_woman_holding_hands' ) ,
HEART : emoji . get ( 'heart' ) ,
BLUE _HEART : emoji . get ( 'blue_heart' ) ,
GREEN _HEART : emoji . get ( 'green_heart' ) ,
YELLOW _HEART : emoji . get ( 'yellow_heart' ) ,
PURPLE _HEART : emoji . get ( 'purple_heart' ) ,
GIFT _HEART : emoji . get ( 'gift_heart' ) ,
} ;
function getemojibynumber ( number ) {
if ( number === 0 ) {
return emoji . get ( 'zero' )
} else if ( number === 1 ) {
return emoji . get ( 'one' )
} else if ( number === 2 ) {
return emoji . get ( 'two' )
} else if ( number === 3 ) {
return emoji . get ( 'three' )
} else if ( number === 4 ) {
return emoji . get ( 'four' )
} else if ( number === 5 ) {
return emoji . get ( 'five' )
} else if ( number === 6 ) {
return emoji . get ( 'six' )
} else if ( number === 7 ) {
return emoji . get ( 'seven' )
} else if ( number === 8 ) {
return emoji . get ( 'height' )
} else if ( number === 9 ) {
return emoji . get ( 'nine' )
}
}
2020-01-03 01:52:49 +01:00
const Menu = {
2020-01-03 22:02:18 +01:00
LAVAGNA : emoji . get ( 'om_symbol' ) + ' La tua Lavagna' ,
LINK _CONDIVIDERE : emoji . get ( 'link' ) + ' Link da condividere' ,
2020-01-21 01:37:15 +01:00
ZOOM : emoji . get ( 'information_source' ) + ' Zoom (Conferenze)' ,
2020-01-03 22:02:18 +01:00
INFO : emoji . get ( 'information_source' ) + ' Informazioni' ,
2020-01-30 01:19:25 +01:00
ASSISTENZA : emoji . get ( 'open_hands' ) + ' Chat e Assistenza' ,
2020-01-03 22:02:18 +01:00
2020-01-20 01:48:25 +01:00
ADMIN : emoji . get ( 'information_desk_person' ) + ' Admin' ,
ALTRO : emoji . get ( 'newspaper' ) + ' Altro' ,
MSGATUTTI : emoji . get ( 'incoming_envelope' ) + ' Invia a TUTTI' ,
INDIETRO : emoji . get ( 'back' ) + ' Indietro' ,
SI : emoji . get ( 'thumbsup' ) + ' SI' ,
NO : emoji . get ( 'thumbsdown' ) + ' NO' ,
2020-01-03 22:02:18 +01:00
EXIT _TELEGRAM : 'exittotelegram' ,
2020-01-03 01:52:49 +01:00
} ;
2020-01-20 01:48:25 +01:00
2020-01-21 01:37:15 +01:00
const MenuStandard = [ [ Menu . LAVAGNA , Menu . LINK _CONDIVIDERE ] , [ Menu . ZOOM , Menu . ASSISTENZA ] ] ;
const MenuPerAdmin = [ [ Menu . LAVAGNA , Menu . LINK _CONDIVIDERE ] , [ Menu . ZOOM , Menu . ASSISTENZA ] , [ Menu . ADMIN , Menu . ALTRO ] ] ;
2020-01-20 01:48:25 +01:00
const MenuYesNo = [ [ Menu . SI , Menu . NO ] ] ;
const MenuAdmin = [ [ Menu . MSGATUTTI , Menu . INDIETRO ] , [ '' , '' ] ] ;
const MenuYes = [ [ Menu . MSGATUTTI , Menu . INDIETRO ] , [ '' , '' ] ] ;
2020-01-03 01:52:49 +01:00
const Status = {
NONE : 0 ,
VERIFIED : 1 ,
WAITFOR _USERNAME _BO : 102 ,
WAITFOR _VERIFY _CODE : 103 ,
} ;
2020-01-20 01:48:25 +01:00
const StatusMSGALL = {
NONE : 0 ,
ASK : 1 ,
CONFIRM : 2 ,
} ;
2020-01-03 01:52:49 +01:00
const txt = {
2020-01-03 22:02:18 +01:00
MSG _SCEGLI _MENU : emoji . get ( 'dizzy' ) + ' Scegli una voce di menu:' + emoji . get ( 'dizzy' ) ,
2020-01-27 15:07:53 +01:00
MSG _ASK _USERNAME _BO : 'Inserire lo username con cui ti sei registrato sul sito %s:' ,
2020-01-03 01:52:49 +01:00
MSG _ERRORE _USERNAME : 'Attenzione! Devi inserire solo lo username (20 caratteri massimo)' ,
MSG _ERRORE _USERNAME _NOT _FOUND : 'Attenzione! Username non trovato in archivio. Verifica che sia corretto.' ,
2020-01-20 01:48:25 +01:00
MSG _ERRORE _USERNAME _ANNULLA : 'Inserimento Annullato. Riprovare' ,
MSG _OPERAZ _ANNULLATA : 'Operazione Annullata' ,
2020-01-03 01:52:49 +01:00
MSG _ERRORE _VERIFY _CODE _MAXLEN : 'Attenzione! Devi inserire solo il codice a 6 cifre' ,
2020-01-27 15:07:53 +01:00
MSG _VERIFY _CODE : 'Ritorna sul sito cliccando qui %s, poi torna qui e scrivi il Codice di Autenticazione che vedrai visualizzato' ,
2020-01-03 22:02:18 +01:00
MSG _ERR _VERIFY _CODE : 'Codice di Verifica Errato! Controllare sul Sito e reinserire il nuovo codice di 6 cifre.' ,
2020-01-27 15:07:53 +01:00
MSG _VERIFY _OK : emoji . get ( 'grinning' ) + ' Benvenuto %s. Sono il tuo BOT. Chiedi utilizzando il menu che trovi in basso.\nOra sei correttamente verificato!\nContinua la guida passo passo che trovi sul sito. Qui puoi ritornare per vedere La tua Lavagna in maniera rapida, senza passare dal sito.' ,
2020-01-03 22:02:18 +01:00
MSG _ERR _UNKNOWN _VERIFY _CODE : 'Errore durante il salvataggio sul Server. Riprovare piú tardi' ,
MSG _EXIT _TELEGRAM : 'L\'account è stato ora scollegato da questo Telegram BOT.' ,
2020-01-27 15:07:53 +01:00
MSG _APORTADOR _USER _REGISTERED : emo . FIRE + ' Si è appena Registrato "%s (n. %s)"\n(Invitato da %s)' ,
2020-01-20 01:48:25 +01:00
MSG _MSG _SENT : emoji . get ( 'envelope' ) + ' Messaggi Inviati !' ,
2020-01-03 01:52:49 +01:00
} ;
const TelegramBot = require ( 'node-telegram-bot-api' ) ;
2020-01-03 22:02:18 +01:00
module . exports = {
ADMIN _IDTELEGRAM _SERVER : '12429864' ,
ADMIN _USER _SERVER : 'paoloar77' ,
ADMIN _USER _NAME _SERVER : 'Paolo' ,
phase : {
REGISTRATION : 1
} ,
notifyToTelegram : async function ( phase , mylocalsconf ) {
let userdest = mylocalsconf . user . aportador _solidario ;
2020-01-20 01:48:25 +01:00
let NameFrom = ` ${ mylocalsconf . user . name } ${ mylocalsconf . user . surname } ` ;
2020-01-27 15:07:53 +01:00
const ind _order = mylocalsconf . user . ind _order ;
2020-01-20 01:48:25 +01:00
let aportador = '' ;
2020-01-20 02:18:11 +01:00
if ( userdest ) {
2020-01-20 01:48:25 +01:00
aportador = ` ( ${ userdest } ) ` ;
2020-01-20 02:18:11 +01:00
NameFrom += aportador ;
} else {
aportador = mylocalsconf . user . aportador _solidario _nome _completo ;
}
2020-01-20 01:48:25 +01:00
2020-01-03 22:02:18 +01:00
let text = '' ;
if ( phase === this . phase . REGISTRATION ) {
2020-01-20 01:48:25 +01:00
if ( userdest ) {
NameFrom = await User . getNameSurnameByUsername ( mylocalsconf . idapp , userdest ) + aportador ;
2020-01-20 02:18:11 +01:00
aportador = NameFrom ;
2020-01-20 01:48:25 +01:00
}
2020-01-27 15:07:53 +01:00
text = printf ( txt . MSG _APORTADOR _USER _REGISTERED , ` ${ mylocalsconf . user . name } ${ mylocalsconf . user . surname } ( ${ mylocalsconf . user . username } ) ` , ind _order , aportador ) ;
2020-01-03 22:02:18 +01:00
}
2020-01-20 02:18:11 +01:00
let addtext = "" ;
if ( ! ! mylocalsconf . user . aportador _solidario ) {
addtext = '[Inviato a ' + NameFrom + ']:' + '\n' ;
2020-01-20 01:48:25 +01:00
await this . sendMsgTelegram ( mylocalsconf . idapp , userdest , text ) ;
2020-01-20 02:18:11 +01:00
}
2020-01-03 22:02:18 +01:00
2020-01-20 02:18:11 +01:00
await this . sendMsgTelegramToTheManagers ( mylocalsconf . idapp , addtext + text ) ;
2020-01-03 22:02:18 +01:00
} ,
sendMsgTelegramToTheManagers : async function ( idapp , text ) {
const usersmanagers = await User . getusersManagers ( idapp ) ;
// console.log('usersmanagers', usersmanagers);
if ( usersmanagers ) {
2020-01-20 02:18:11 +01:00
for ( const rec of usersmanagers ) {
2020-01-03 22:02:18 +01:00
this . sendMsgTelegramByIdTelegram ( idapp , rec . profile . teleg _id , text )
2020-01-20 02:18:11 +01:00
}
2020-01-03 22:02:18 +01:00
}
} ,
2020-01-20 01:48:25 +01:00
sendMsgTelegramToALL : async function ( idapp , text ) {
const usersall = await User . getUsersTelegALL ( idapp ) ;
if ( usersall ) {
usersall . forEach ( ( rec ) => {
this . sendMsgTelegramByIdTelegram ( idapp , rec . profile . teleg _id , text )
} ) ;
}
} ,
2020-01-03 22:02:18 +01:00
sendMsgTelegram : async function ( idapp , username , text ) {
const teleg _id = await User . TelegIdByUsername ( idapp , username ) ;
const cl = getclTelegByidapp ( idapp ) ;
if ( cl && teleg _id ) {
2020-01-20 01:48:25 +01:00
await cl . sendMsg ( teleg _id , text )
2020-01-03 22:02:18 +01:00
}
} ,
2020-01-20 01:48:25 +01:00
sendMsgTelegramByIdTelegram : async function ( idapp , idtelegram , text ) {
if ( ! idtelegram )
return ;
2020-01-03 22:02:18 +01:00
const cl = getclTelegByidapp ( idapp ) ;
if ( cl && idtelegram ) {
2020-01-20 01:48:25 +01:00
await cl . sendMsg ( idtelegram , text )
2020-01-03 22:02:18 +01:00
}
}
} ;
2020-01-03 01:52:49 +01:00
function getstr ( lang , text ) {
return text ;
}
class Telegram {
constructor ( idapp , bot ) {
this . idapp = idapp ;
this . bot = bot ;
this . token = bot . token ;
this . arrUsers = [ ] ;
}
2020-01-20 01:48:25 +01:00
async createIfNotExist ( msg ) {
if ( ! await this . alreadyExist ( msg ) ) {
if ( this . insertIntoDB ( msg ) ) {
let rec = this . getRecInMem ( msg ) ;
if ( rec . user )
rec . status = Status . VERIFIED
}
return true
} else {
return false
}
}
2020-01-03 01:52:49 +01:00
2020-01-20 01:48:25 +01:00
async start ( msg ) {
2020-01-03 01:52:49 +01:00
// Check if Present to the DB
2020-01-20 01:48:25 +01:00
if ( ! await this . createIfNotExist ( msg ) ) {
2020-01-03 01:52:49 +01:00
let rec = this . getRecInMem ( msg ) ;
if ( ! rec )
rec = this . addUser ( msg ) ;
2020-01-20 01:48:25 +01:00
if ( rec . user )
rec . status = Status . VERIFIED
2020-01-03 01:52:49 +01:00
}
}
2020-01-20 01:48:25 +01:00
async isMenuNotVerified ( rec , msg ) {
if ( msg . text === Menu . ASSISTENZA ) {
await this . menuAssistenza ( msg )
} else if ( msg . text === Menu . INFO ) {
await this . menuInformazioni ( msg )
2020-01-21 01:37:15 +01:00
} else if ( msg . text === Menu . ZOOM ) {
await this . menuZoom ( msg )
2020-01-20 01:48:25 +01:00
} else {
await this . msgScegliMenu ( msg ) ;
}
}
async isMenu ( rec , msg ) {
2020-01-03 01:52:49 +01:00
if ( msg . text === Menu . LAVAGNA ) {
2020-01-20 01:48:25 +01:00
await this . menuLavagna ( msg )
2020-01-03 22:02:18 +01:00
} else if ( msg . text === Menu . LINK _CONDIVIDERE ) {
2020-01-20 01:48:25 +01:00
await this . menuLinkCondividere ( msg )
2020-01-03 22:02:18 +01:00
} else if ( msg . text === Menu . EXIT _TELEGRAM ) {
2020-01-20 01:48:25 +01:00
await this . menuExitToTelegram ( msg )
} else if ( msg . text === Menu . ADMIN ) {
await this . menuAdmin ( msg )
} else if ( msg . text === Menu . MSGATUTTI ) {
await this . menumsgAll ( msg )
} else if ( msg . text === Menu . INDIETRO ) {
await this . msgScegliMenu ( msg ) ;
2020-01-03 22:02:18 +01:00
} else {
2020-01-20 01:48:25 +01:00
await this . isMenuNotVerified ( rec , msg ) ;
}
}
2020-01-27 15:07:53 +01:00
getsymb ( symb , valid , isnum , num , smileif ) {
2020-01-20 01:48:25 +01:00
let str = symb + ': ' ;
if ( valid )
str += emo . CHECK _VERDE ;
else
str += emo . CROSS _ROSSA ;
if ( isnum ) {
2020-01-27 15:07:53 +01:00
str += ' ' + getemojibynumber ( num ) ;
2020-01-20 01:48:25 +01:00
if ( smileif )
str += emo . SMILE _STAR ;
2020-01-03 01:52:49 +01:00
}
2020-01-20 01:48:25 +01:00
return str + ' ' ;
}
isdreamset ( user ) {
if ( user ) {
if ( user . profile . my _dream )
if ( user . profile . my _dream . length > 10 )
return true
}
return false
2020-01-03 01:52:49 +01:00
}
async menuLavagna ( msg ) {
const rec = this . getRecInMem ( msg ) ;
// console.log('rec', rec);
let mystr = '' ;
if ( rec . user ) {
const dashboard = await User . getDashboard ( this . idapp , rec . user . aportador _solidario , rec . user . username ) ;
2020-01-20 01:48:25 +01:00
let numpersone = ( dashboard . downline ) ? dashboard . downline . length : 0 ;
mystr = "" ;
// if (dashboard.aportador) {
// mystr = emoji.get('seedling') + ` Chi ti ha invitato: ${dashboard.aportador.username} (${dashboard.aportador.name} ${dashboard.aportador.surname})\n`;
// } else {
// mystr = 'Chi ti ha invitato: Sei il Primo! ';
// }
// mystr += `____________________________________________\n`;
if ( rec . user ) {
2020-01-27 15:07:53 +01:00
mystr += tools . get _ _ ( 'BENVENUTO' , msg ) + ' ' + rec . user . name + ' (' + rec . user . username + ') ' + emo . DIZZY ;
2020-01-20 01:48:25 +01:00
mystr += '\n' + '\n' + this . getsymb ( emo . ENVELOPE , rec . user . verified _email ) ;
mystr += rec . user . verified _email ? tools . get _ _ ( 'EMAIL_VERIF' , msg ) : tools . get _ _ ( 'EMAIL_NON_VERIF' , msg ) ;
mystr += '\n' + '\n' + this . getsymb ( emo . EYES , rec . user . profile . saw _zoom _presentation ) + tools . get _ _ ( 'ZOOM_PARTECIPATO' , msg ) ;
mystr += '\n' + '\n' + this . getsymb ( emo . DREAM , this . isdreamset ( rec . user ) ) + tools . get _ _ ( 'SCRITTO_SOGNO' , msg ) ;
// } else{
// mystr += tools.get__('TELEGRAM_NOT_CONNECTED', msg) + ' ' + emoji.get('e-mail');
2020-01-03 01:52:49 +01:00
}
2020-01-20 01:48:25 +01:00
2020-01-27 15:07:53 +01:00
// numpersone = 2;
2020-01-20 01:48:25 +01:00
2020-01-27 15:07:53 +01:00
mystr += '\n' + '\n' + this . getsymb ( emo . INVITATI , numpersone >= 2 , numpersone , numpersone , numpersone >= 3 ) + tools . get _ _ ( 'INVITATI' , msg ) + '\n' ;
2020-01-20 01:48:25 +01:00
2020-01-03 01:52:49 +01:00
if ( numpersone > 0 ) {
let index = 1 ;
dashboard . downline . forEach ( ( user ) => {
2020-01-03 22:02:18 +01:00
mystr += emoji . get ( 'star-struck' ) + ` ${ index } °: ${ user . username } ( ${ user . name } ${ user . surname } ) \n ` ;
2020-01-03 01:52:49 +01:00
index ++ ;
} ) ;
}
}
if ( ! ! mystr )
2020-01-20 01:48:25 +01:00
await this . sendMsg ( msg . chat . id , mystr ) ;
2020-01-03 01:52:49 +01:00
}
async menuLinkCondividere ( msg ) {
const rec = this . getRecInMem ( msg ) ;
if ( rec . user ) {
const mystr = tools . getHostByIdApp ( this . idapp ) + '/signup/' + rec . user . username ;
2020-01-20 01:48:25 +01:00
await this . sendMsg ( msg . chat . id , mystr ) ;
2020-01-03 01:52:49 +01:00
}
}
2020-01-03 22:02:18 +01:00
async menuExitToTelegram ( msg ) {
const rec = this . getRecInMem ( msg ) ;
if ( rec . user ) {
await User . SetTelegramIdSuccess ( this . idapp , rec . user . username , 0 ) . then ( ( recuser ) => {
if ( recuser ) {
this . deleteRecInMem ( msg ) ;
this . sendMsg ( msg . chat . id , txt . MSG _EXIT _TELEGRAM ) ;
}
} )
}
}
2020-01-03 01:52:49 +01:00
async menuInformazioni ( msg ) {
const mystr = 'Informazioni Aggiuntive: ...' ;
2020-01-20 01:48:25 +01:00
await this . sendMsg ( msg . chat . id , mystr ) ;
}
2020-01-21 01:37:15 +01:00
async menuZoom ( msg ) {
let mystr = '' ;
const listazoom = await CalZoom . findAllIdApp ( this . idapp ) ;
const nextzoom = await CalZoom . getNextZoom ( this . idapp ) ;
mystr += emo . STARS + 'Zoom in programma:' + emo . STARS + '\n\n' ;
let index = 1 ;
listazoom . forEach ( ( evento ) => {
2020-01-27 15:07:53 +01:00
let iniziata = false ;
if ( nextzoom )
iniziata = ( nextzoom . _id . toString ( ) === evento . _id . toString ( ) ) ;
2020-01-21 01:37:15 +01:00
2020-01-27 15:07:53 +01:00
if ( iniziata ) {
mystr += emo . CHECK _VERDE + ' QUESTA CONFERENZA E\' INIZIATA! ' + emo . CHECK _VERDE + '\n' ;
}
mystr += ` ${ emo . EYES } ${ tools . getstrDateTimeShort ( evento . date _start ) } ${ emo . EYES } ` ;
mystr += ` \n ${ evento . title } \n ( ${ evento . note } ) \n \n ` ;
if ( nextzoom ) {
2020-01-21 01:37:15 +01:00
if ( iniziata ) {
2020-01-27 15:07:53 +01:00
mystr += emo . FIRE + 'CLICCA QUI PER ENTRARE ! ' + emo . FIRE + '\n' ;
mystr += tools . getlinkzoom ( evento . id _conf _zoom ) + '\n\n' ;
2020-01-21 01:37:15 +01:00
}
2020-01-27 15:07:53 +01:00
}
2020-01-21 01:37:15 +01:00
2020-01-27 15:07:53 +01:00
index ++ ;
} ) ;
if ( ! nextzoom && index > 1 ) {
mystr += "LINK ZOOM DA CONDIVIDERE :\n" + tools . getlinkzoom ( listazoom [ 0 ] . id _conf _zoom ) + '\n\n' ;
}
2020-01-21 01:37:15 +01:00
await
this
. sendMsg ( msg
. chat
. id
,
mystr
)
;
}
2020-01-20 01:48:25 +01:00
async menuAdmin ( msg ) {
const mystr = 'scegli una voce:' ;
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . chat . id , mystr , MenuAdmin ) ;
2020-01-20 01:48:25 +01:00
}
async menumsgAll ( msg ) {
const rec = this . getRecInMem ( msg ) ;
if ( rec . user ) {
const mystr = 'Scrivi qui un Messaggio da inviare a TUTTI:' ;
rec . msgall _status = StatusMSGALL . ASK ;
2020-01-27 15:07:53 +01:00
await this . sendMsg ( msg . chat . id , mystr , MenuAdmin ) ;
2020-01-20 01:48:25 +01:00
}
2020-01-03 01:52:49 +01:00
}
async menuAssistenza ( msg ) {
2020-01-20 01:48:25 +01:00
const mytext = tools . get _ _ ( 'TESTO_ASSISTENZA' , msg ) ;
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . chat . id , mytext ) ;
2020-01-03 01:52:49 +01:00
}
existInMemory ( msg ) {
const rec = this . getRecInMem ( msg ) ;
return ! ! rec
}
2020-01-20 01:48:25 +01:00
getstatus ( rec ) {
2020-01-03 01:52:49 +01:00
if ( ! ! rec )
return rec . status ;
else
return Status . NONE ;
}
2020-01-20 01:48:25 +01:00
getstatusInMemory ( msg ) {
const rec = this . getRecInMem ( msg ) ;
return this . getstatus ( rec ) ;
}
2020-01-03 01:52:49 +01:00
getRecInMem ( msg ) {
return this . arrUsers . find ( ( rec ) => rec . id === msg . from . id ) ;
}
2020-01-03 22:02:18 +01:00
deleteRecInMem ( msg ) {
this . arrUsers = this . arrUsers . filter ( ( rec ) => rec . id !== msg . from . id ) ;
}
2020-01-03 01:52:49 +01:00
async setUsernameBo ( msg ) {
try {
const rec = this . getRecInMem ( msg ) ;
2020-01-27 15:07:53 +01:00
if ( msg . text === undefined )
return false ;
2020-01-03 01:52:49 +01:00
console . log ( 'msg.text' , msg . text ) ;
const text = msg . text ;
if ( text . length < 20 ) {
if ( rec ) {
rec . username _bo = text ;
// Check if username exist
2020-01-21 01:37:15 +01:00
const user = await
User . findByUsername ( this . idapp , rec . username _bo ) ;
2020-01-03 01:52:49 +01:00
if ( ! user ) {
2020-01-27 15:07:53 +01:00
await this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME _NOT _FOUND ) ;
rec . username _bo = '' ;
2020-01-03 01:52:49 +01:00
} else {
2020-01-03 22:02:18 +01:00
rec . user = user ;
2020-01-21 01:37:15 +01:00
await
User . SetTelegramCheckCode ( this . idapp , rec . username _bo , rec . code ) ;
2020-01-03 01:52:49 +01:00
rec . status = Status . WAITFOR _VERIFY _CODE ;
2020-01-21 01:37:15 +01:00
await
2020-01-27 15:07:53 +01:00
this . sendMsg ( msg . from . id , printf ( txt . MSG _VERIFY _CODE , tools . getHostByIdApp ( this . idapp ) ) )
2020-01-03 01:52:49 +01:00
}
}
} else if ( text . length === 0 ) {
if ( rec )
rec . status = Status . NONE ;
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME _ANNULLA ) ;
2020-01-20 01:48:25 +01:00
this . deleteRecInMem ( msg )
2020-01-03 01:52:49 +01:00
} else {
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME )
2020-01-03 01:52:49 +01:00
}
} catch ( e ) {
console . error ( 'Error setUsernameBo:' , e )
}
}
async setVerifyCode ( msg ) {
try {
const rec = this . getRecInMem ( msg ) ;
2020-01-21 01:37:15 +01:00
const user = await
User . findByUsername ( this . idapp , rec . username _bo ) ;
2020-01-03 01:52:49 +01:00
const code = msg . text . toString ( ) . trim ( ) ;
2020-01-03 22:02:18 +01:00
let telegcode = 0 ;
if ( user ) {
telegcode = user . profile . teleg _checkcode . toString ( ) ;
} else {
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME _NOT _FOUND ) ;
2020-01-03 22:02:18 +01:00
return
}
2020-01-03 01:52:49 +01:00
if ( msg . text . length < 7 ) {
if ( rec ) {
2020-01-03 22:02:18 +01:00
if ( code === telegcode ) {
2020-01-03 01:52:49 +01:00
rec . status = Status . VERIFIED ;
2020-01-21 01:37:15 +01:00
await
User . SetTelegramIdSuccess ( this . idapp , rec . username _bo , msg . from . id ) . then ( ( recuser ) => {
if ( recuser ) {
let name = recuser . name ;
this . sendMsg ( msg . from . id , printf ( txt . MSG _VERIFY _OK , name ) )
} else {
this . sendMsg ( msg . from . id , txt . MSG _ERR _UNKNOWN _VERIFY _CODE ) ;
}
} ) ;
2020-01-03 01:52:49 +01:00
} else {
2020-01-20 01:48:25 +01:00
if ( rec . retry < 2 ) {
rec . retry ++ ;
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . from . id , txt . MSG _ERR _VERIFY _CODE )
2020-01-20 01:48:25 +01:00
} else {
rec . status = Status . NONE ;
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME _ANNULLA ) ;
2020-01-20 01:48:25 +01:00
this . deleteRecInMem ( msg ) ;
}
2020-01-03 01:52:49 +01:00
}
}
} else if ( msg . text . length === 0 ) {
if ( rec )
rec . status = Status . NONE ;
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME _ANNULLA )
2020-01-03 01:52:49 +01:00
} else {
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . from . id , txt . MSG _ERRORE _VERIFY _CODE _MAXLEN )
2020-01-03 01:52:49 +01:00
}
} catch ( e ) {
console . error ( 'Error setVerifyCode' , e ) ;
}
}
2020-01-20 01:48:25 +01:00
async sendMsgToAll ( rec , msg , texttosend ) {
if ( texttosend . length < 3 ) {
} else {
2020-01-21 01:37:15 +01:00
const usersall = await
User . getUsersTelegALL ( rec . user . idapp ) ;
2020-01-20 01:48:25 +01:00
let nummsgsent = 0 ;
if ( usersall ) {
for ( const rec of usersall ) {
2020-01-27 15:07:53 +01:00
await this . sendMsg ( rec . profile . teleg _id , texttosend ) ;
2020-01-20 01:48:25 +01:00
nummsgsent ++ ;
2020-01-27 15:07:53 +01:00
// ++Todo: SLEEP ??? sleep(0.03)
2020-01-20 01:48:25 +01:00
}
}
2020-01-21 01:37:15 +01:00
await
this . sendMsg ( msg . chat . id , nummsgsent + ' ' + getstr ( msg . from . language _code , txt . MSG _MSG _SENT ) ) ;
2020-01-20 01:48:25 +01:00
}
rec . start _write _msgall = false ;
}
selectMenuHelp ( msg ) {
return ( ( msg . text === Menu . INFO ) || ( msg . text === Menu . ASSISTENZA ) )
}
2020-01-03 01:52:49 +01:00
async receiveMsg ( msg ) {
let status = this . getstatusInMemory ( msg ) ;
if ( status === Status . NONE ) {
2020-01-21 01:37:15 +01:00
await
this . start ( msg ) ;
2020-01-03 22:02:18 +01:00
let status = this . getstatusInMemory ( msg ) ;
if ( status !== Status . VERIFIED )
return
2020-01-20 01:48:25 +01:00
} else {
2020-01-21 01:37:15 +01:00
await
this . setUser ( msg )
2020-01-03 01:52:49 +01:00
}
2020-01-20 01:48:25 +01:00
const rec = this . getRecInMem ( msg ) ;
status = this . getstatus ( rec ) ;
if ( status === Status . WAITFOR _USERNAME _BO && ! this . selectMenuHelp ( msg ) ) {
2020-01-21 01:37:15 +01:00
await
this . setUsernameBo ( msg )
2020-01-03 01:52:49 +01:00
} else if ( status === Status . WAITFOR _VERIFY _CODE ) {
2020-01-21 01:37:15 +01:00
await
this . setVerifyCode ( msg )
2020-01-03 01:52:49 +01:00
} else if ( status === Status . NONE ) {
2020-01-21 01:37:15 +01:00
await
this . start ( msg ) ;
2020-01-03 01:52:49 +01:00
} else if ( status === Status . VERIFIED ) {
2020-01-20 01:48:25 +01:00
let normale = true ;
if ( rec . msgall _status === StatusMSGALL . CONFIRM ) {
if ( msg . text === Menu . SI ) {
// Take msg to send to ALL
2020-01-21 01:37:15 +01:00
await
this . sendMsgToAll ( rec , msg , rec . msgtosent )
2020-01-20 01:48:25 +01:00
} else {
this . sendMsg ( msg . chat . id , txt . MSG _OPERAZ _ANNULLATA ) ;
}
rec . msgall _status = StatusMSGALL . NONE ;
normale = false ;
} else if ( rec . msgall _status === StatusMSGALL . ASK ) {
if ( msg . text !== Menu . INDIETRO ) {
rec . msgtosent = msg . text ;
rec . msgall _status = StatusMSGALL . CONFIRM ;
const domanda = 'Confermi d\'Inviare questo messaggio a TUTTI ?\n' + rec . msgtosent ;
this . ChiediSINO ( msg , domanda ) ;
} else {
rec . msgall _status = StatusMSGALL . NONE ;
2020-01-21 01:37:15 +01:00
await
this . msgScegliMenu ( msg ) ;
2020-01-20 01:48:25 +01:00
}
normale = false
}
if ( normale ) {
// Check Menu
2020-01-21 01:37:15 +01:00
await
this . isMenu ( rec , msg ) ;
2020-01-20 01:48:25 +01:00
}
} else {
2020-01-21 01:37:15 +01:00
await
this . isMenuNotVerified ( rec , msg ) ;
2020-01-03 01:52:49 +01:00
}
}
async askVerifiedCode ( msg ) {
const rec = this . arrUsers . find ( ( rec ) => rec . id === msg . from . id ) ;
if ( ! rec ) {
2020-01-21 01:37:15 +01:00
await
this . addUser ( msg ) ;
2020-01-03 01:52:49 +01:00
2020-01-21 01:37:15 +01:00
await
2020-01-27 15:07:53 +01:00
this . sendMsg ( msg . chat . id , getstr ( msg . from . language _code , printf ( txt . MSG _ASK _USERNAME _BO , tools . getHostByIdApp ( this . idapp ) ) ) ) ;
2020-01-20 01:48:25 +01:00
return false ;
2020-01-03 01:52:49 +01:00
}
2020-01-20 01:48:25 +01:00
return true ;
2020-01-03 01:52:49 +01:00
}
2020-01-20 01:48:25 +01:00
async setUser ( msg ) {
2020-01-03 01:52:49 +01:00
const id = msg . from . id ;
2020-01-21 01:37:15 +01:00
const user = await
User . UserByIdTelegram ( this . idapp , id ) ;
2020-01-03 01:52:49 +01:00
let rec = this . arrUsers . find ( ( rec ) => rec . id === msg . from . id ) ;
2020-01-20 01:48:25 +01:00
if ( user && rec ) {
2020-01-03 01:52:49 +01:00
rec . user = user ;
2020-01-20 01:48:25 +01:00
}
}
async alreadyExist ( msg ) {
const id = msg . from . id ;
let rec = null ;
try {
2020-01-21 01:37:15 +01:00
const user = await
User . UserByIdTelegram ( this . idapp , id ) ;
2020-01-20 01:48:25 +01:00
let rec = this . arrUsers . find ( ( rec ) => rec . id === msg . from . id ) ;
if ( user && ! rec ) {
rec = this . addUser ( msg ) ;
}
if ( rec ) {
rec . user = user ;
if ( user )
rec . username _bo = user . username ;
else
rec . username _bo = ''
}
} catch ( e ) {
console . error ( 'Error: ' , e ) ;
2020-01-03 22:02:18 +01:00
}
2020-01-03 01:52:49 +01:00
return ! ! rec
}
addUser ( msg ) {
const lang = msg . from . language _code ;
const code = 100000 + Math . round ( Math . random ( ) * 899999 ) ;
const rec = {
id : msg . from . id ,
code ,
lang ,
status : Status . WAITFOR _USERNAME _BO ,
retry : 0 ,
2020-01-20 01:48:25 +01:00
username _bo : '' ,
msgall _status : StatusMSGALL . NONE ,
msgtosent : '' ,
2020-01-03 01:52:49 +01:00
} ;
this . arrUsers . push ( rec ) ;
return rec ;
}
insertIntoDB ( msg ) {
2020-01-20 01:48:25 +01:00
return this . askVerifiedCode ( msg ) ;
}
async getKeyboard ( id , menu ) {
let keyb = MenuStandard ;
// Check if you are Admin
2020-01-21 01:37:15 +01:00
const ismanager = await
2020-01-27 15:07:53 +01:00
User . isAdminByIdTeleg ( this . idapp , id ) ;
2020-01-20 01:48:25 +01:00
if ( ismanager )
keyb = MenuPerAdmin ;
if ( menu ) {
keyb = menu
}
2020-01-03 01:52:49 +01:00
2020-01-20 01:48:25 +01:00
return keyb
2020-01-03 01:52:49 +01:00
}
2020-01-20 01:48:25 +01:00
async sendMsg ( id , text , menu ) {
if ( ! text )
return ;
2020-01-03 01:52:49 +01:00
this . bot . sendMessage ( id , text , {
"reply_markup" : {
2020-01-20 01:48:25 +01:00
"resize_keyboard" : true ,
"keyboard" : await this . getKeyboard ( id , menu )
2020-01-03 01:52:49 +01:00
}
2020-01-21 01:37:15 +01:00
} )
;
2020-01-03 01:52:49 +01:00
}
2020-01-20 01:48:25 +01:00
async msgBenvenuto ( id ) {
2020-01-03 01:52:49 +01:00
this . bot . sendMessage ( id , Benvenuto , {
"reply_markup" : {
2020-01-20 01:48:25 +01:00
"resize_keyboard" : true ,
"keyboard" : await this . getKeyboard ( id )
}
2020-01-21 01:37:15 +01:00
} )
;
2020-01-20 01:48:25 +01:00
}
async ChiediSINO ( msg , domanda ) {
this . bot . sendMessage ( msg . from . id , domanda , {
"reply_markup" : {
"resize_keyboard" : true ,
'one_time_keyboard' : true ,
"keyboard" : MenuYesNo
2020-01-03 01:52:49 +01:00
}
} ) ;
}
2020-01-03 22:02:18 +01:00
2020-01-20 01:48:25 +01:00
async msgScegliMenu ( msg ) {
// const rec = this.getRecInMem(msg);
2020-01-03 22:02:18 +01:00
this . bot . sendMessage ( msg . from . id , txt . MSG _SCEGLI _MENU , {
"reply_markup" : {
2020-01-20 01:48:25 +01:00
"resize_keyboard" : true ,
"keyboard" : await this . getKeyboard ( msg . from . id )
2020-01-03 22:02:18 +01:00
}
2020-01-21 01:37:15 +01:00
} )
;
2020-01-03 22:02:18 +01:00
}
2020-01-03 01:52:49 +01:00
}
const arrTelegram = [ ] ;
function getclTelegBytoken ( token ) {
2020-01-03 22:02:18 +01:00
const rec = arrTelegram . find ( ( rec ) => rec . cl . token === token ) ;
if ( ! ! rec )
return rec . cl ;
else
return null ;
}
function getclTelegByidapp ( idapp ) {
const rec = arrTelegram . find ( ( rec ) => rec . idapp === idapp ) ;
2020-01-03 01:52:49 +01:00
if ( ! ! rec )
return rec . cl ;
else
return null ;
}
2020-01-13 23:52:51 +01:00
if ( ! tools . testing ( ) ) {
console . log ( 'TELEGRAM STARTING.... ' ) ;
for ( const idapp of appTelegram ) {
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
const token = tools . getTelegramKeyByIdApp ( idapp ) ;
// console.log('idapp', idapp, 'token', token);
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
if ( ! ! token ) {
const bot = new TelegramBot ( token , { polling : true } ) ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
arrTelegram . push ( { idapp , cl : new Telegram ( idapp , bot ) } ) ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
bot . onText ( /\/start/ , ( msg ) => {
const myclTelegram = getclTelegBytoken ( bot . token ) ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
myclTelegram . start ( msg ) ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
} ) ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
// Matches "/echo [whatever]"
bot . onText ( /\/echo (.+)/ , ( msg , match ) => {
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
const chatId = msg . chat . id ;
const resp = match [ 1 ] ; // the captured "whatever"
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
// send back the matched "whatever" to the chat
bot . sendMessage ( chatId , resp ) ;
} ) ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
// Listen for any kind of message. There are different kinds of
// messages.
bot . on ( 'message' , ( msg ) => {
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
const myclTelegram = getclTelegBytoken ( bot . token ) ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
// console.log('msg', msg);
const chatId = msg . chat . id ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
myclTelegram . receiveMsg ( msg ) ;
// var bye = "bye";
// if (msg.text.toString().toLowerCase().includes(bye)) {
// bot.sendMessage(msg.chat.id, "Hope to see you around again , Bye");
// }
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
// send a message to the chat acknowledging receipt of their message
// bot.sendMessage(chatId, 'Received your message');
} ) ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
// Handle callback queries
bot . on ( 'callback_query' , function onCallbackQuery ( callbackQuery ) {
console . log ( 'callback_query' , callbackQuery ) ;
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
bot . answerCallbackQuery ( callbackQuery . id , { url } ) ;
} ) ;
}
2020-01-03 01:52:49 +01:00
2020-01-13 23:52:51 +01:00
}
2020-01-03 01:52:49 +01:00
}