Abilitazione del BOT

This commit is contained in:
Surya Paolo
2023-12-17 16:20:44 +01:00
parent 4290895a97
commit c71f4af370
8 changed files with 221 additions and 13 deletions

View File

@@ -50,11 +50,11 @@ const BotSchema = new Schema({
},
});
BotSchema.statics.getFieldsForSearch = function() {
BotSchema.statics.getFieldsForSearch = function () {
return [{ field: 'label', type: tools.FieldType.string }]
};
BotSchema.statics.executeQueryTable = function(idapp, params) {
BotSchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
@@ -66,12 +66,141 @@ BotSchema.statics.DuplicateAllRecords = async function (idapporig, idappdest) {
};
BotSchema.statics.findAllIdApp = async function(idapp) {
BotSchema.statics.generateBotMenuRecords = async function (idapp) {
try {
let arrrec = [
{
"page": 1,
"index": 1,
"riga": 1,
"active": true,
"label": "Vai al Sito",
"type": 3,
"value": "{host}",
"visibility": 0,
idapp,
"lang": "it",
"main": true
},
{
"page": 1,
"index": 1,
"riga": 2,
"active": true,
"label": "Il mio Profilo",
"type": 3,
"value": "{host}/my/{username}",
"visibility": 1,
idapp,
"lang": "it",
"main": true
},
{
"page": 1,
"index": 1,
"riga": 3,
"active": true,
"label": "Link da Condividere",
"type": 2,
"value": "{host}/registrati/{username}",
"visibility": 1,
idapp,
"lang": "it",
"main": true,
},
{
"page": 1,
"index": 2,
"riga": 1,
"active": true,
"label": "🔮 Help",
"type": 4,
"value": "",
"visibility": 0,
idapp,
"lang": "it",
"main": true,
},
{
"page": 1,
"index": 1,
"riga": 3,
"active": true,
"label": "💁‍♀️ Admin",
"type": 4,
"value": "",
"visibility": 5,
idapp,
"lang": "it",
"main": true
},
{
"page": 1,
"index": 2,
"riga": 2,
"active": true,
"label": "Imposta Foto Profilo",
"type": 4,
"value": "🖼 SetPicProfile",
"visibility": 1,
idapp,
"lang": "it",
"main": true
},
{
"page": 1,
"index": 2,
"riga": 3,
"active": true,
"label": "🛠 Strumenti",
"type": 1,
"value": "2",
"visibility": 1,
idapp,
"lang": "it",
"main": true,
},
{
"page": 2,
"index": 1,
"riga": 1,
"active": true,
"label": "🔑 Cambio Password",
"type": 4,
"value": "🔑 SetResetPwd",
"visibility": 1,
idapp,
"lang": "it",
},
{
"page": 2,
"index": 1,
"riga": 2,
"active": true,
"label": "👉🏻 Indietro",
"type": 1,
"value": "1",
"visibility": 1,
idapp,
"lang": "it",
}];
const ris = await MyBot.insertMany(arrrec);
return ris;
} catch (e) {
console.error('Err:', e);
}
}
BotSchema.statics.findAllIdApp = async function (idapp) {
const Bot = this;
const myfind = {idapp};
const myfind = { idapp };
return await Bot.find(myfind).sort({page: 1, lang: 1, riga: 1, index: 1}).lean();
return await Bot.find(myfind).sort({ page: 1, lang: 1, riga: 1, index: 1 }).lean();
};
const MyBot = mongoose.model('Bot', BotSchema);
@@ -80,4 +209,4 @@ MyBot.createIndexes((err) => {
if (err) throw err;
});
module.exports = {MyBot};
module.exports = { MyBot };

View File

@@ -262,6 +262,7 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
return order;
});
console.log('*** Num myorderscart ', myorderscart.length);
return myorderscart
} catch (e) {
console.error('Err:', e);

View File

@@ -64,6 +64,12 @@ const SiteSchema = new Schema({
telegram_key_test: {
type: String,
},
load_process_telegram: {
type: Boolean,
},
load_process_telegram_test: {
type: String,
},
teleg_cfg: {
type: String,
},

View File

@@ -30,6 +30,7 @@ const i18n = require('i18n');
const shared_consts = require('../tools/shared_nodejs');
mongoose.Promise = global.Promise;
mongoose.level = 'F';
@@ -5387,6 +5388,8 @@ UserSchema.statics.addNewSite = async function (idappPass, body) {
};
if (arrSite && arrSite.length === 1 && numutenti < 2) {
const MyTelegramBot = require('../telegram/telegrambot');
// Nessun Sito Installato e Nessun Utente installato !
let myuser = new User();
myuser._id = new ObjectID();
@@ -5403,11 +5406,19 @@ UserSchema.statics.addNewSite = async function (idappPass, body) {
myuser.perm = '3';
myuser.profile.special_req = true;
myuser.profile.nationality = 'IT';
myuser.profile.manage_telegram = true;
myuser.profile.teleg_id = MyTelegramBot.ADMIN_IDTELEGRAM_SERVER;
myuser.profile.username_telegram = MyTelegramBot.ADMIN_USERNAME_TELEGRAM;
myuser.lasttimeonline = new Date();
myuser.date_reg = new Date();
await myuser.save();
const { MyBot } = require('../models/bot');
// Genera il Menu del BOT:
await MyBot.generateBotMenuRecords(idapp);
return { code: server_constants.RIS_CODE_OK, idapp };
}
}

View File

@@ -17,6 +17,8 @@ const { Settings } = require('../models/settings');
const { SendNotif } = require('../models/sendnotif');
const { MyBot } = require('../models/bot');
const tools = require('../tools/general');
const shared_consts = require('../tools/shared_nodejs');
@@ -1409,6 +1411,11 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) {
} else if (mydata.dbop === 'updateSaldoAndTransato_AllAccounts') {
await Account.updateSaldoAndTransato_AllAccounts(idapp);
} else if (mydata.dbop === 'generateBotMenuRecords') {
await MyBot.generateBotMenuRecords(idapp);
} else if (mydata.dbop === 'GenerateVapiKey') {
await tools.generateVapiKey();

View File

@@ -695,6 +695,7 @@ const ADMIN_IDTELEGRAM_TEST = 5356627050; // 5022837609; //Surya Arena
const MyTelegramBot = {
ADMIN_IDTELEGRAM_SERVER: '12429864', //Paolo
ADMIN_USERNAME_TELEGRAM: 'surya1977', //Paolo
ADMIN_USER_SERVER: 'paoloar77',
ADMIN_USER_NAME_SERVER: 'Paolo',
@@ -710,7 +711,7 @@ const MyTelegramBot = {
} else {
return appTelegram;
}
},
getAppTelegramTest: function () {
@@ -719,7 +720,7 @@ const MyTelegramBot = {
} else {
return appTelegram_TEST;
}
},
local_sendMsgTelegramByIdTelegram: async function (idapp, idtelegram, text,
@@ -978,7 +979,7 @@ const MyTelegramBot = {
if (send_notif && msg_notifpush) {
// SEND PUSH NOTIFICATION
await tools.sendNotificationToUser(userId, title, msg_notifpush, openUrl, '', tag, actions);
await tools.sendNotificationToUser(userId, title, msg_notifpush, openUrl, '', tag, actions);
}
@@ -3435,15 +3436,26 @@ class Telegram {
rec.status = Status.NONE;
if (!msg.from.username) {
// Non ha l'Username:
// Chiedi l'username sul Bot:
if (tools.getConfSiteOptionEnabledByIdApp(this.idapp, shared_consts.ConfSite.AskUsernameSulBot)) {
rec.status = Status.WAITFOR_USERNAME_TELEGRAM;
}
} else {
// Ha l'Username
// Chiedi l'invitante ?
if (tools.getConfSiteOptionEnabledByIdApp(this.idapp, shared_consts.ConfSite.AskInvitantesulBot)) {
rec.status = Status.WAITFOR_USERNAME_INVITANTE;
}
}
if (rec.status === Status.NONE) {
// Se il Sito non chiede di autenticarsi o l'invitante, allora lo Verifico direttamente !
rec.status = Status.VERIFIED;
}
this.arrUsers.push(rec);
if (tools.getConfSiteOptionEnabledByIdApp(this.idapp, shared_consts.ConfSite.AskInvitantesulBot)) {
@@ -4065,11 +4077,16 @@ if (true) {
else if (process.env.NODE_ENV === 'test')
arrTeleg = MyTelegramBot.getAppTelegramTest();
const arrTelegFromSite = tools.getArrTelegramFromSite();
if (arrTelegFromSite.length > 0) {
arrTeleg = arrTelegFromSite;
}
var internetAvailable = require('internet-available');
internetAvailable().then(() => {
// ..
console.log('TELEGRAM STARTING.... NODE_ENV:' + process.env.NODE_ENV);
console.log('TELEGRAM STARTING.... ' + process.env.NODE_ENV);
for (const idapp of arrTeleg) {
@@ -4080,7 +4097,9 @@ if (true) {
// console.log('idapp', idapp, 'token', token);
if (!!token) {
console.log('*** START BOT ' + nomebot);
console.log('-------------------------------------');
console.log('*** STARTING BOT ' + nomebot);
console.log('-------------------------------------');
const bot = new TelegramBot(token, { polling: true });
if (url === '0') {
@@ -4095,6 +4114,7 @@ if (true) {
arrTelegram.push({ idapp, cl: new Telegram(idapp, bot) });
bot.onText(/\/start/, (msg) => {
console.log('*** BOT PARTITO CORRETTAMENTE !!! (Start Cmd)');
const myclTelegram = getclTelegBytoken(bot.token);
myclTelegram.start(msg);

View File

@@ -1804,7 +1804,7 @@ module.exports = {
getTelegramBotNameByIdApp: function (idapp) {
const myapp = this.MYAPPS.find((item) => item.idapp === idapp);
if (process.env.NODE_ENV === 'test')
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development')
return (myapp) ? myapp.telegram_bot_name_test : '';
else
return (myapp) ? myapp.telegram_bot_name : '';
@@ -1823,12 +1823,30 @@ module.exports = {
getTelegramKeyByIdApp: function (idapp) {
const myapp = this.MYAPPS.find((item) => item.idapp === idapp);
if (process.env.NODE_ENV === 'test')
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development')
return (myapp) ? myapp.telegram_key_test : '';
else
return (myapp) ? myapp.telegram_key : '';
},
getArrTelegramFromSite: function () {
const myapp = this.MYAPPS;
let arrteleg = []
for (const site of myapp) {
if (site.active) {
if (process.env.NODE_ENV === 'test') {
if (myapp.load_process_telegram_test)
arrteleg.push(myapp.idapp)
} else {
if (myapp.load_process_telegram)
arrteleg.push(myapp.idapp)
}
}
}
return arrteleg;
},
getLookupPipeLine(params, proj) {
let myquery = [
@@ -4025,7 +4043,11 @@ module.exports = {
if (!msg)
return msg;
if (!!user) {
if (msg.includes('{host}')) {
msg = msg.replace('{host}', this.getHostByIdApp(user.idapp));
}
if (msg.includes('{appname}'))
msg = msg.replace('{appname}', this.getNomeAppByIdApp(user.idapp));
msg = msg.replace('{username}', user.username);