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-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 = {
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' ,
INFO : emoji . get ( 'information_source' ) + ' Informazioni' ,
ASSISTENZA : emoji . get ( 'open_hands' ) + ' Assistenza' ,
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-03 22:02:18 +01:00
const MenuStandard = [ [ Menu . LAVAGNA , Menu . LINK _CONDIVIDERE ] , [ Menu . INFO , Menu . ASSISTENZA ] ] ;
2020-01-20 01:48:25 +01:00
const MenuPerAdmin = [ [ Menu . LAVAGNA , Menu . LINK _CONDIVIDERE ] , [ Menu . INFO , Menu . ASSISTENZA ] , [ Menu . ADMIN , Menu . ALTRO ] ] ;
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-03 01:52:49 +01:00
MSG _ASK _USERNAME _BO : 'Inserire lo username con cui ti sei registrato sul sito:' ,
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-20 01:48:25 +01:00
MSG _VERIFY _CODE : 'Vai sul sito e scrivi qui 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.' ,
MSG _VERIFY _OK : emoji . get ( 'grinning' ) + ' Benvenuto %s. Ora sei correttamente verificato!' ,
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.' ,
MSG _APORTADOR _USER _REGISTERED : emoji . get ( 'heart_eyes' ) + ' Si è appena Registrato con il tuo link:\n%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 } ` ;
let aportador = '' ;
if ( userdest )
aportador = ` ( ${ userdest } ) ` ;
NameFrom += aportador ;
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 ;
}
text = printf ( txt . MSG _APORTADOR _USER _REGISTERED , ` ${ mylocalsconf . user . name } ${ mylocalsconf . user . surname } ( ${ mylocalsconf . user . username } ) ` ) ;
2020-01-03 22:02:18 +01:00
}
2020-01-20 01:48:25 +01:00
if ( ! ! mylocalsconf . user . aportador _solidario )
await this . sendMsgTelegram ( mylocalsconf . idapp , userdest , text ) ;
2020-01-03 22:02:18 +01:00
await this . sendMsgTelegramToTheManagers ( mylocalsconf . idapp , '[Inviato a ' + NameFrom + ']:' + '\n' + text ) ;
} ,
sendMsgTelegramToTheManagers : async function ( idapp , text ) {
const usersmanagers = await User . getusersManagers ( idapp ) ;
// console.log('usersmanagers', usersmanagers);
if ( usersmanagers ) {
usersmanagers . forEach ( ( rec ) => {
this . sendMsgTelegramByIdTelegram ( idapp , rec . profile . teleg _id , text )
} ) ;
}
} ,
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 )
} 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 ) ;
}
}
getsymb ( symb , valid , isnum , verdeif , smileif ) {
let str = symb + ': ' ;
if ( valid )
str += emo . CHECK _VERDE ;
else
str += emo . CROSS _ROSSA ;
if ( isnum ) {
str += ' ' + getemojibynumber ( valid ) ;
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 ) {
mystr += tools . get _ _ ( 'BENVENUTO' , msg ) + ' ' + rec . user . name + ' ' + emo . DIZZY ;
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
// numpersone = 3;
mystr += '\n' + '\n' + this . getsymb ( emo . INVITATI , numpersone , true , numpersone >= 2 , numpersone >= 3 ) + tools . get _ _ ( 'INVITATI' , msg ) + '\n' ;
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 ) ;
}
async menuAdmin ( msg ) {
const mystr = 'scegli una voce:' ;
await this . sendMsg ( msg . chat . id , mystr , MenuAdmin ) ;
}
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 ;
await this . sendMsg ( msg . chat . id , mystr , MenuAdmin ) ;
}
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 ) ;
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 ) ;
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-03 22:02:18 +01:00
const user = await User . findByUsername ( this . idapp , rec . username _bo ) ;
2020-01-03 01:52:49 +01:00
if ( ! user ) {
2020-01-20 01:48:25 +01:00
await this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME _NOT _FOUND )
2020-01-03 01:52:49 +01:00
} else {
2020-01-03 22:02:18 +01:00
rec . user = user ;
2020-01-03 01:52:49 +01:00
await User . SetTelegramCheckCode ( this . idapp , rec . username _bo , rec . code ) ;
rec . status = Status . WAITFOR _VERIFY _CODE ;
2020-01-20 01:48:25 +01:00
await this . sendMsg ( msg . from . id , txt . MSG _VERIFY _CODE )
2020-01-03 01:52:49 +01:00
}
}
} else if ( text . length === 0 ) {
if ( rec )
rec . status = Status . NONE ;
2020-01-20 01:48:25 +01:00
await this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME _ANNULLA ) ;
this . deleteRecInMem ( msg )
2020-01-03 01:52:49 +01:00
} else {
2020-01-20 01:48:25 +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-03 22:02:18 +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-20 01:48:25 +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 ;
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 ) ;
}
} ) ;
} else {
2020-01-20 01:48:25 +01:00
if ( rec . retry < 2 ) {
rec . retry ++ ;
await this . sendMsg ( msg . from . id , txt . MSG _ERR _VERIFY _CODE )
} else {
rec . status = Status . NONE ;
await this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME _ANNULLA ) ;
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-20 01:48:25 +01:00
await this . sendMsg ( msg . from . id , txt . MSG _ERRORE _USERNAME _ANNULLA )
2020-01-03 01:52:49 +01:00
} else {
2020-01-20 01:48:25 +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 {
const usersall = await User . getUsersTelegALL ( rec . user . idapp ) ;
let nummsgsent = 0 ;
if ( usersall ) {
for ( const rec of usersall ) {
await this . sendMsg ( rec . profile . teleg _id , texttosend ) ;
nummsgsent ++ ;
}
}
await this . sendMsg ( msg . chat . id , nummsgsent + ' ' + getstr ( msg . from . language _code , txt . MSG _MSG _SENT ) ) ;
}
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 ) {
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 {
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-03 22:02:18 +01:00
await this . setUsernameBo ( msg )
2020-01-03 01:52:49 +01:00
} else if ( status === Status . WAITFOR _VERIFY _CODE ) {
2020-01-03 22:02:18 +01:00
await this . setVerifyCode ( msg )
2020-01-03 01:52:49 +01:00
} else if ( status === Status . NONE ) {
2020-01-03 22:02:18 +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
await this . sendMsgToAll ( rec , msg , rec . msgtosent )
} 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 ;
await this . msgScegliMenu ( msg ) ;
}
normale = false
}
if ( normale ) {
// Check Menu
await this . isMenu ( rec , msg ) ;
}
} else {
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 ) {
await this . addUser ( msg ) ;
2020-01-20 01:48:25 +01:00
await this . sendMsg ( msg . chat . id , getstr ( msg . from . language _code , txt . MSG _ASK _USERNAME _BO ) ) ;
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 ;
const user = await User . UserByIdTelegram ( this . idapp , id ) ;
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 {
const user = await User . UserByIdTelegram ( this . idapp , id ) ;
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
const ismanager = await User . isManagerByIdTeleg ( this . idapp , id ) ;
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-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 )
}
} ) ;
}
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-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
}