Versione 1.0.17

- Fix saldo pendente
- aggiunto bottone markup Telegram
- aggionta opzione per registrarsi direttamente su Telegram, senza doversi registrare ad un sito
This commit is contained in:
Surya Paolo
2023-10-21 15:27:53 +02:00
parent 536fbd1752
commit c1a9a9a555
17 changed files with 559 additions and 76 deletions

View File

@@ -3,14 +3,15 @@ const tools = require('../tools/general');
const appTelegram = [tools.FREEPLANET, tools.RISO];
const appTelegram_TEST = [tools.FREEPLANET, tools.RISO];
const appTelegram_DEVELOP = [tools.RISO];
// const appTelegram_DEVELOP = [tools.RISO];
const appTelegram_DEVELOP = [tools.FIOREDELLAVITA];
const appTelegramFinti = ['2', tools.CNM];
const appTelegramDest = [tools.FREEPLANET, tools.FREEPLANET];
const printf = require('util').format;
const { User } = require('../models/user');
const { User, FuncUsers } = require('../models/user');
const { MyGroup } = require('../models/mygroup');
const { Circuit } = require('../models/circuit');
const { CalZoom } = require('../models/calzoom');
@@ -1474,7 +1475,7 @@ class Telegram {
if (!riscreate) {
let rec = this.getRecInMem(msg);
if (!rec)
rec = this.addUser(msg);
rec = await this.addUser(msg);
if (rec.user)
rec.status = Status.VERIFIED;
@@ -1601,9 +1602,14 @@ class Telegram {
}
}
let parse_mode = 'HTML';
let arr_bott_inline = []
if (risp === '') {
if (menusite) {
risp = await this.getValueMenu(this.idapp, rec, msg, testo, lang);
parse_mode = await this.getParseModeByMenu(this.idapp, rec, msg, testo, lang);
arr_bott_inline = await this.getArrBottInlineByMenu(this.idapp, rec, msg, testo, lang);
noanswer = true;
} else if (MsgBot.CIAO.includes(testo.replace('!', ''))) {
risp = 'Ciao ';
@@ -1762,7 +1768,7 @@ class Telegram {
if (contastiera) {
keyboard = {
'parse_mode': 'HTML',
'parse_mode': parse_mode,
'reply_markup': {
'resize_keyboard': true,
'keyboard': await this.getKeyboard(id, undefined, this.getlang(msg)),
@@ -1771,6 +1777,12 @@ class Telegram {
}
let keyboard_inline = undefined;
if (arr_bott_inline.length > 0) {
keyboard_inline = cl.getInlineKeyboard(myuser.lang, arr_bott_inline);
}
if (rec.numdomande > 3 && rec.msgcodeprec === '') {
// if (rec.user) {
// const dashboard = await User.getDashboard(this.idapp, rec.user.aportador_solidario, rec.user.username, rec.user.aportador_solidario_name_surname);
@@ -1796,7 +1808,7 @@ class Telegram {
await tools.snooze(300);
risp = '[BOT' + emo.ROBOT_FACE + ' scrive]:\n' + risp;
}
this._inviaMsg(id, risp, keyboard);
this._inviaMsg(id, risp, keyboard, undefined, undefined, undefined, undefined, { parse_mode, keyboard_inline });
let strlog = 'USER [' + myname + ']: ' + testo + '\n';
@@ -2780,6 +2792,52 @@ class Telegram {
}
}
async addUserInDB_enteringInTelegramBot(msg) {
try {
// cerca se esiste già l'utente in Memoria
const rec = this.getRecInMem(msg);
if (!rec.user) {
// cerca se esiste già l'utente con il suo ID Telegram
const user = await User.UserByIdTelegram(this.idapp, msg.from.id);
if (!user) {
// Se non esiste già, creo la registrazione dell'Utente, senza password:
const myuserdata = {
idapp: this.idapp,
username: msg.from.username || msg.from.id,
name: msg.from.first_name || '',
lang: msg.from.language_code || 'it',
surname: msg.from.last_name || '',
profile: {
teleg_id: msg.from.id,
}
}
const recuser = await FuncUsers.createRegistration_withTelegram(myuserdata);
if (recuser) {
rec.user = recuser;
console.log(recuser.username, ' SI E\' VERIFICATO CON TELEGRAM !');
// let username = recuser.name;
/*if (!!msg.from.username) {
await MyTelegramBot.askConfirmationUser(this.idapp, shared_consts.CallFunz.REGISTRATION, recuser);
} else {
console.log(' ... MA GLI MANCA L\'USERNAME TELEGRAM !! ');
} */
}
}
}
return { ris: !!rec.user, recuser: rec.user };
} catch (e) {
console.error('Error addUserInDB_enteringInTelegramBot', e);
}
return { ris: false, recuser: null };
}
async sistemaRecDest(rec, msg) {
let recdest = this.getRecByUsername(rec.msgall_username_specifico);
if (!recdest) {
@@ -3267,7 +3325,7 @@ class Telegram {
let user = await User.UserByIdTelegram(this.idapp, id);
let rec = this.getRecInMem(msg);
if (user && !rec) {
rec = this.addUser(msg);
rec = await this.addUser(msg);
}
if (rec) {
rec.user = user;
@@ -3289,7 +3347,7 @@ class Telegram {
}
}
addUser(msg) {
async addUser(msg) {
const lang = this.getlang(msg);
const code = 100000 + Math.round(Math.random() * 899999);
@@ -3321,20 +3379,37 @@ class Telegram {
pageChange: false,
menuSaved: {},
pagenow: 1,
parse_mode: 'HTML',
menuDb: null,
aportador_solidario: '',
};
rec.status = Status.NONE;
if (!msg.from.username) {
rec.status = Status.WAITFOR_USERNAME_TELEGRAM;
if (tools.getConfSiteOptionEnabledByIdApp(this.idapp, shared_consts.ConfSite.AskUsernameSulBot)) {
rec.status = Status.WAITFOR_USERNAME_TELEGRAM;
}
} else {
rec.status = Status.WAITFOR_USERNAME_INVITANTE;
if (tools.getConfSiteOptionEnabledByIdApp(this.idapp, shared_consts.ConfSite.AskInvitantesulBot)) {
rec.status = Status.WAITFOR_USERNAME_INVITANTE;
}
}
this.arrUsers.push(rec);
// Aportador
this.setInvitante(msg, this.getInvitanteByMsg(msg), false);
if (tools.getConfSiteOptionEnabledByIdApp(this.idapp, shared_consts.ConfSite.AskInvitantesulBot)) {
// Aportador
this.setInvitante(msg, this.getInvitanteByMsg(msg), false);
}
if (tools.getConfSiteOptionEnabledByIdApp(this.idapp, shared_consts.ConfSite.registerUserWithBot)) {
const ris = await this.addUserInDB_enteringInTelegramBot(msg);
if (ris && ris.recuser) {
rec.recuser = ris.recuser;
rec.status = Status.VERIFIED;
}
}
return rec;
@@ -3388,13 +3463,13 @@ class Telegram {
for (const rec of recuser.menuDb) {
rec.active_mem = false;
const visibilita = (tools.isBitAttivoESelez(rec.visibility, shared_consts.VISIB_ONLY_ADMIN, isAdmin)
&& tools.isBitAttivoESelez(rec.visibility, shared_consts.VISIB_ONLY_MANAGER, isManager)
&& tools.isBitAttivoESelez(rec.visibility, shared_consts.VISIB_ONLYIF_VERIFIED, isVerified))
|| rec.visibility === 0;
if (rec.active && rec.page === recuser.pagenow
&& (tools.isBitAttivoESelez(rec.visibility,
shared_consts.VISIB_ONLY_ADMIN, isAdmin) &&
tools.isBitAttivoESelez(rec.visibility,
shared_consts.VISIB_ONLY_MANAGER, isManager) &&
tools.isBitAttivoESelez(rec.visibility,
shared_consts.VISIB_ONLYIF_VERIFIED, isVerified))
&& visibilita
) {
rec.active_mem = true;
if (true) {
@@ -3480,6 +3555,8 @@ class Telegram {
this.isMenu(recuser, msg, recdb.value, true);
return shared_consts.RIS_OK;
}
} else if (recdb.type === shared_consts.BOTTYPE_MARKDOWN) {
return recdb.value;
}
}
@@ -3493,6 +3570,59 @@ class Telegram {
}
async getParseModeByMenu(idapp, recuser, msg, testo, lang) {
try {
let parse_mode = 'HTML';
if (recuser) {
for (const recdb of recuser.menuDb) {
if (recdb.active_mem) {
if (recdb.idapp === idapp && recdb.lang === lang &&
recdb.label.toLowerCase() === testo) {
if (recdb.type === shared_consts.BOTTYPE_MARKDOWN) {
return 'Markdown'
} else {
return parse_mode;
}
}
}
}
}
return '';
} catch (e) {
return '';
}
}
async getArrBottInlineByMenu(idapp, recuser, msg, testo, lang) {
try {
let arr_bott_inline = [];
if (recuser) {
for (const recdb of recuser.menuDb) {
if (recdb.active_mem) {
if (recdb.idapp === idapp && recdb.lang === lang &&
recdb.label.toLowerCase() === testo) {
if (recdb.type === shared_consts.BOTTYPE_BOTTONI_INLINE) {
const jsonString = '[{"text": "primo", "callback_data": "1"}, {"text": "sec", "callback_data": "2"}]';
const jsonObject = JSON.parse(jsonString);
let myobj = JSON.parse(recdb.value);
arr_bott_inline.push(myobj);
}
}
}
}
}
return arr_bott_inline;
} catch (e) {
console.error(e);
return [];
}
}
async getKeyboard(id, menu, lang) {
let keyb = null;
@@ -3588,11 +3718,12 @@ class Telegram {
if (!form) {
form = {
'parse_mode': 'HTML',
'parse_mode': opt && opt.parse_mode ? opt.parse_mode : 'HTML',
'message_id': msg_id,
'reply_markup': {
'resize_keyboard': true,
'keyboard': await this.getKeyboard(id, menu, mylang),
'keyboard_inline': (opt && opt.keyboard_inline) ? opt.keyboard_inline : undefined,
},
};
}