diff --git a/src/server/locales/it.json b/src/server/locales/it.json index 5401f34..ed61a5c 100644 --- a/src/server/locales/it.json +++ b/src/server/locales/it.json @@ -1,11 +1,12 @@ { "Hello": "Ciao", "Hello %s": "Ciao %s", + "Good": "Bene", + "Service": "Servizio", + "Hosp": "Ospitalità", "Good: %s": "Bene: %s", "Service: %s": "Servizio: %s", - "NEW_GOOD": "❇️ %s ha aggiunto un nuovo Bene: \n%s", - "NEW_SERVICE": "❇️ %s ha aggiunto un nuovo Servizio: \n%s", - "NEW_HOSP": "❇️ %s ha aggiunto una nuova Ospitalità: \n%s", + "Hosp: %s": "Ospitalità: %s", "NEW_EVENT": "❇️ %s ha aggiunto un nuovo Evento: \n%s\n%s\n%s", "NEW_EVENT_TELEGRAM": "%s\n\n❇️ %s\n\n%s\n\n%s", "NEW_ANNUNCIO_TELEGRAM": "❇️ %s\n\n%s\n\n%s", @@ -115,5 +116,6 @@ "🚫 Hai rifiutato l'accesso alla App di RISO da parte di %s!": "🚫 Hai rifiutato l'accesso alla App di RISO da parte di %s!", "🚫 Ti è stato rifiutato l'accesso. Probabilmente l'username con cui ti sei registrato non ti conosce. (%s) !
Contatta l'Assistenza Tecnica.": "🚫 Ti è stato rifiutato l'accesso. Probabilmente l'username con cui ti sei registrato non ti conosce. (%s) !
Contatta l'Assistenza Tecnica.", "🚫 %s ha rifiutato l'accesso alla App a %s !": "🚫 %s ha rifiutato l'accesso alla App a %s !", - "✅ Hai Abilitato l'accesso alla App a %s !": "✅ Hai Abilitato l'accesso alla App a %s !" + "✅ Hai Abilitato l'accesso alla App a %s !": "✅ Hai Abilitato l'accesso alla App a %s !", + "Good %s": "Good %s" } \ No newline at end of file diff --git a/src/server/models/contribtype.js b/src/server/models/contribtype.js index 96b1fab..9446519 100755 --- a/src/server/models/contribtype.js +++ b/src/server/models/contribtype.js @@ -47,7 +47,7 @@ ContribtypeSchema.statics.findAllIdApp = async function (idapp) { return await Contribtype.find(myfind, (err, arrrec) => { return arrrec - }); + }).lean(); }; const Contribtype = mongoose.model('Contribtype', ContribtypeSchema); diff --git a/src/server/models/sendnotif.js b/src/server/models/sendnotif.js index 244b609..a6ee829 100755 --- a/src/server/models/sendnotif.js +++ b/src/server/models/sendnotif.js @@ -204,20 +204,24 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = async function (recnotif, us } if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_BACHECA) { + let tablerec = ''; if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_GOOD) { newdescr = i18n.__('NEW_GOOD', userorig, mydescr); recnotif.openUrl = shared_consts.getDirectoryByTable(shared_consts.TABLES_MYGOODS, true) + myidrec; tag = 'newgood'; + tablerec = 'mygoods'; } else if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_SERVICE) { newdescr = i18n.__('NEW_SERVICE', userorig, mydescr); recnotif.openUrl = shared_consts.getDirectoryByTable(shared_consts.TABLES_MYSKILLS, true) + myidrec; tag = 'newservice'; + tablerec = 'myskills'; } else if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_HOSP) { newdescr = i18n.__('NEW_HOSP', userorig, mydescr); recnotif.openUrl = shared_consts.getDirectoryByTable(shared_consts.TABLES_MYHOSPS, true) + myidrec; tag = 'newhosp'; + tablerec = 'myhosps'; } - let eventobj = await tools.getAnnuncioForTelegram(recnotif.myrectableorig, mydescr, userorig); + let eventobj = await tools.getAnnuncioForTelegram(recnotif.myrectableorig, tablerec, mydescr, userorig, true); newdescr = eventobj.newdescr; recnotif.textcontent_Telegram = eventobj.newdescrtelegram; recnotif.linkaddTelegram = i18n.__('SHOW_POST'); diff --git a/src/server/telegram/telegrambot.js b/src/server/telegram/telegrambot.js index f7cd24e..8258475 100755 --- a/src/server/telegram/telegrambot.js +++ b/src/server/telegram/telegrambot.js @@ -9,6 +9,9 @@ const appTelegram_DEVELOP = [tools.RISO]; const appTelegramFinti = ['2', tools.CNM]; const appTelegramDest = [tools.FREEPLANET, tools.FREEPLANET]; + +const appTeleg_BotOnGroup = [tools.IDAPP_BOTONGROUP]; + //PIPPO const printf = require('util').format; @@ -722,7 +725,7 @@ const MyTelegramBot = { } } - + return arrTeleg; }, @@ -1350,6 +1353,25 @@ const MyTelegramBot = { invia = true; } else if (params.cmd === shared_consts.MsgTeleg.SHARE_TEXT) { invia = !!params.content; + } else if (params.cmd === shared_consts.MsgTeleg.SHARE_ANNUNCIO) { + const mytable = globalTables.getTableByTableName(params.tablerec); + + if (params.idrec && params.idrec !== '0') { + // const myrectableorig = await mytable.findOne({ _id: myrecnotifpass.idrec }).lean(); + const myrectableorig = await mytable.getCompleteRecord(params.idapp, params.idrec); + + if (myrectableorig) { + const eventobj = await tools.getAnnuncioForTelegram(myrectableorig, params.tablerec, '', user.username, false); + if (eventobj) { + params.textcontent_Telegram = eventobj.newdescrtelegram; + params.typesend = shared_consts.TypeSend.TELEGRAM; + //const rismsg = + params.content = eventobj.newdescr; + params.title = ''; + invia = true; + } + } + } } if (tipomsgtempl > 0) { @@ -4089,6 +4111,17 @@ function getclTelegBytoken(token) { return null; } +// Funzione per ottenere il nome del gruppo +async function getGroupName(bot, groupId) { + try { + const chat = await bot.getChat(groupId); + return chat.title; + } catch (error) { + console.error('Errore durante l\'ottenimento del nome del gruppo:', error); + return null; + } +} + function getclTelegByidapp(idapp) { // **************************** if (appTelegramFinti.includes(idapp)) { @@ -4112,6 +4145,81 @@ if (true) { internetAvailable().then(() => { // .. + + if (appTeleg_BotOnGroup.length > 0) { + console.log('TELEGRAM BOT GROUP .... '); + + for (const idapp of appTeleg_BotOnGroup) { + + const token = tools.getTelegramKeyByIdApp(idapp); + const nomebot = tools.getTelegramBotNameByIdApp(idapp); + + if (token && nomebot) { + const bot = new TelegramBot(token, { polling: true }); + + // Gestisce il comando /start + bot.onText(/\/start/, (msg) => { + console.log('START BOT GROUP ... '); + /* + const chatId = msg.chat.id; + const message = 'Benvenuto! Usa il comando /request per inviare una richiesta di accesso al gruppo.'; + bot.sendMessage(chatId, message); + */ + }); + + bot.onText(/\/request/, (msg) => { + const chatId = msg.chat.id; + const fromId = msg.from.id; + const fromUsername = msg.from.username; + + const groupId = chatId; + + if (chatId == groupId) { + const message = `Richiesta di accesso al gruppo da parte di ${msg.from.first_name} ${msg.from.last_name} (@${fromUsername})`; + bot.sendMessage(groupId, message); + } else { + console.log('Comando /request ricevuto in una chat privata.'); + } + }); + + bot.on('text', (msg) => { + console.log('TEXT...'); + const chatId = msg.chat.id; + const userName = msg.from.username || msg.from.first_name; + const userId = msg.from.id; + }); + + bot.on('new_chat_members', async (msg) => { + const chatId = msg.chat.id; + const newMembers = msg.new_chat_members; + + const groupId = chatId; + + const nomegruppo = await getGroupName(bot, groupId); + + // Ottiene la lista degli amministratori del gruppo + const admins = await bot.getChatAdministrators(chatId); + + // Trova l'ID dell'amministratore del gruppo + const adminId = admins.find(admin => admin.status === 'administrator').user.id; + + // Verifica se il nuovo membro è stato aggiunto al gruppo + if (chatId == groupId) { + newMembers.forEach(async (member) => { + + //const message = `🔑 Nuova Richiesta di accesso al gruppo ${nomegruppo} da parte di ${member.username} (` + member.first_name + ' ' + member.last_name + ')'; + const message = `Benvenuto/a ${member.first_name} (${member.username}) sul gruppo "${nomegruppo}" !`; + bot.sendMessage(adminId, message); + + // bot.sendMessage(groupId, message); + }); + } + }); + + } + } + } + console.log('TELEGRAM STARTING.... ', process.env.NODE_ENV); for (const idapp of arrTeleg) { diff --git a/src/server/tools/general.js b/src/server/tools/general.js index 1e74afa..faccea8 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -21,6 +21,11 @@ const shared_consts = require('./shared_nodejs'); const mongoose = require('mongoose').set('debug', false); const Subscription = require('../models/subscribers'); +const { Contribtype } = require('../models/contribtype'); +const { Skill } = require('../models/skill'); +const { Good } = require('../models/good'); +const { SectorGood } = require('../models/sectorgood'); + const server_constants = require('./server_constants'); const download = require('image-downloader'); @@ -29,6 +34,7 @@ const download = require('image-downloader'); const webpush = require('web-push'); const i18n = require('i18n'); +const { StatusSkill } = require('../models/statusSkill'); const FILELOG = 'filelog.txt'; @@ -418,6 +424,8 @@ module.exports = { FIOREDELLAVITA: '15', PIUCHEBUONO: '17', + IDAPP_BOTONGROUP: '1000', + HELP_CHAT: '', TYPECONF_ZOOM: 'zoom', TYPECONF_JITSI: 'jitsi', @@ -2904,7 +2912,7 @@ module.exports = { $match: { $or: [ { $and: params.filtersearch3and }, - { idStatusSkill: { $in: [2]} } + { idStatusSkill: { $in: [2] } } ] } }); @@ -4238,13 +4246,22 @@ module.exports = { return myval.charAt(0).toUpperCase() + myval.slice(1); }, - firstchars(value, numchars = 200) { + firstchars(value, numchars = 200, continua, link) { if (!value) { return ''; } try { let mycar = value.substring(0, numchars); - if (value.length > numchars) mycar += '...'; + if (value.length > numchars) { + if (continua) { + if (link) + mycar += ` (... continua sulla App)`; + else + mycar += ` (... continua sulla App)`; + } else { + mycar += ' ...'; + } + } return mycar; } catch (e) { return value; @@ -4307,21 +4324,6 @@ module.exports = { return `${username}`; }, - - firstchars(value, numchars) { - if (!value) { - return '' - } - try { - let mycar = value.substring(0, numchars) - if (value.length > numchars) - mycar += '...' - return mycar - } catch (e) { - return value - } - }, - replaceStringAtEnd(inputString, searchString, replacement) { // Create a regular expression that matches the searchString at the end of the inputString const regex = new RegExp(searchString + '$'); @@ -4390,26 +4392,244 @@ module.exports = { }, - async getAnnuncioForTelegram(myrec, mydescr, userorig) { + addRowTelegram(icon, title, content, acapo, islink) { + let mystr = ''; + + let descrcontent = content; + if (islink) { + title = `${title}`; + descrcontent = ''; + } + + mystr += `${icon} ${title} ${icon}\n`; + if (descrcontent) { + mystr += `${descrcontent}\n`; + + if (acapo) { + mystr += `\n`; + } + } + + return mystr; + }, + + async getInCambioDiByRec(myrec) { + const contribtype = await Contribtype.findAllIdApp(idapp); + + try { + let mystr = ''; + for (const contribId of myrec.idContribType) { + const reccontr = contribtype.find((rec) => rec._id.toString() === contribId); + if (reccontr) { + if (mystr) { + mystr += ', '; + } + mystr += reccontr.label; + } + } + return mystr; + } catch (e) { + + } + }, + + async getStatusSkillByRec(myrec, onlyifNoDiPersona) { + const statusskill = await StatusSkill.findAllIdApp(idapp); + + try { + let mystr = ''; + if (onlyifNoDiPersona) { + if ((myrec.idStatusSkill.length === 1) && (myrec.idStatusSkill[0] === shared_consts.STATUSSKILL_DI_PERSONA)) + return '' + } + for (const status of myrec.idStatusSkill) { + const reccontr = statusskill.find((rec) => rec._id === status); + if (reccontr) { + if (mystr) { + mystr += ' e '; + } + mystr += reccontr.descr; + } + } + return mystr; + } catch (e) { + + } + }, + + getIconByAdType(adType) { + if (adType === shared_consts.AdType.OFFRO) + return '🟢'; + else if (adType === shared_consts.AdType.CERCO) + return '🔴'; + + return ''; + }, + + async getCategoriaSkillByRec(myrec) { + const skillrec = await Skill.findAllIdApp(idapp); + + try { + let mystr = ''; + const rec = skillrec.find((rec) => rec._id === myrec.idSkill); + if (rec) { + mystr += rec.descr; + } + return mystr; + } catch (e) { + + } + }, + async getCategoriaGoodByRec(myrec) { + // const goodrec = await Good.findAllIdApp(idapp); + const secgoodrec = await SectorGood.findAllIdApp(idapp); + + try { + let mystr = ''; + const rec = secgoodrec.find((rec) => rec._id === myrec.idSectorGood); + // const rec = goodrec.find((rec) => rec._id === myrec.idSectorGood); + if (rec) { + mystr += rec.descr; + } + return mystr; + } catch (e) { + + } + }, + + getComuniEProvinceByRec(myrec) { + try { + let mystr = ''; + if (myrec.hasOwnProperty('mycities')) { + let ind = 0; + for (const city of myrec.mycities) { + if (ind > 0) + mystr += ', ' + mystr += city.comune + ' (' + city.prov + ')'; + ind++; + } + } + return mystr; + } catch (e) { + + } + }, + + async getAnnuncioForTelegram(myrec, tablerec, mydescr, userorig, nuovo) { try { - let dovestr = this.getDoveStrByEvent(myrec); - let descrestesa = await this.getDescrEstesaStrByEvent(myrec); - let contributo = myrec.contribstr; let newdescr = ''; - if (myrec.adType === shared_consts.TypeNotifs.ID_BACHECA_NEW_GOOD) { - newdescr = i18n.__('NEW_GOOD', userorig, mydescr); - } else if (myrec.adType === shared_consts.TypeNotifs.ID_BACHECA_NEW_SERVICE) { - newdescr = i18n.__('NEW_SERVICE', userorig, mydescr); - } else if (myrec.adType === shared_consts.TypeNotifs.ID_BACHECA_NEW_HOSP) { - newdescr = i18n.__('NEW_HOSP', userorig, mydescr); + let out = ''; + + let tiposcambio = '' + let iconascambio = '🟢'; + + let datastr = ''; + let dovestr = ''; + let organizedby = ''; + let contributo = ''; + + if ((tablerec === shared_consts.TABLES_MYGOODS) || (tablerec === shared_consts.TABLES_MYSKILLS)) { + iconascambio = this.getIconByAdType(myrec.adType); + if (myrec.adType === shared_consts.AdType.OFFRO) + tiposcambio = 'Offro'; + else if (myrec.adType === shared_consts.AdType.CERCO) + tiposcambio = 'Cerco'; + } else if (tablerec === shared_consts.TABLES_MYHOSPS) { + tiposcambio = 'Offro '; + if (myrec.typeHosp === shared_consts.TYPEHOSP_OSPITALITA) + tiposcambio += 'Ospitalità'; + else if (myrec.typeHosp === shared_consts.TYPEHOSP_SCAMBIOCASA) + tiposcambio += 'Scambio Casa'; + } else if (tablerec === shared_consts.TABLES_MYBACHECAS) { + datastr = this.getstrDateTimeEvent(myrec); + organizedby = myrec.organisedBy; + contributo = myrec.contribstr; } - let newdescrtelegram = i18n.__('NEW_ANNUNCIO_TELEGRAM', mydescr, dovestr, descrestesa, userorig); - newdescrtelegram += i18n.__('ADDED_FROM', userorig); + if (nuovo) { + if (tablerec === shared_consts.TABLES_MYGOODS) { + newdescr = i18n.__('NEW_GOOD', userorig, mydescr); + } else if (tablerec === shared_consts.TABLES_MYSKILLS) { + newdescr = i18n.__('NEW_SERVICE', userorig, mydescr); + } else if (tablerec === shared_consts.TABLES_MYHOSPS) { + newdescr = i18n.__('NEW_HOSP', userorig, mydescr); + } + } else { + if (tablerec === shared_consts.TABLES_MYGOODS) { + newdescr = i18n.__('Good', userorig, mydescr); + } else if (tablerec === shared_consts.TABLES_MYSKILLS) { + newdescr = i18n.__('Service', userorig, mydescr); + } else if (tablerec === shared_consts.TABLES_MYHOSPS) { + newdescr = i18n.__('Hosp', userorig, mydescr); + } + } + let cat = ''; + let status = ''; + let online = false; + let solo_online = false; + if (tablerec === shared_consts.TABLES_MYGOODS) { + cat = await this.getCategoriaGoodByRec(myrec) + } else if (tablerec === shared_consts.TABLES_MYSKILLS) { + cat = await this.getCategoriaSkillByRec(myrec) + online = myrec.idStatusSkill.findIndex((id) => id === shared_consts.STATUSSKILL_ONLINE) >= 0 + solo_online = online && myrec.idStatusSkill.length === 1 + status = await this.getStatusSkillByRec(myrec, true); + if (status) { + status = ' ' + status; + } + } else if (tablerec === shared_consts.TABLES_MYHOSPS) { + cat = ''; + } + // let out = i18n.__('NEW_ANNUNCIO_TELEGRAM', mydescr, dovestr, descrestesa, userorig); - return { newdescr, newdescrtelegram } + if (tiposcambio) + out += this.addRowTelegram(iconascambio, tiposcambio + status, '', false); + + const url = this.getHostByIdApp(myrec.idapp) + shared_consts.getDirectoryByTable(tablerec, true) + myrec._id; + + out += `${myrec.descr}\n\n`; + + if (datastr) + out += this.addRowTelegram('🗓', 'Data Evento', datastr, true); + + if (cat) + out += this.addRowTelegram('⭐️', 'Categoria', cat, true); + + let descrcontent = this.firstchars(this.removeLastSpaceAndACapo(myrec.note), 175, true, url); + + if (descrcontent) + out += this.addRowTelegram('📝', 'Descrizione ' + newdescr, descrcontent, true); + + const localita = this.getComuniEProvinceByRec(myrec); + + if (!solo_online) + out += this.addRowTelegram('🏠', 'Località', localita, true); + + const incambiodi = await this.getInCambioDiByRec(myrec); + if (incambiodi) + out += this.addRowTelegram('⚖️', 'In cambio di', incambiodi, true); + + if (contributo) + out += this.addRowTelegram('💰', 'Contributo', contributo, true); + + let contatto = userorig; + try { + let username_telegram = myrec.profile.username_telegram; + + if (username_telegram) + contatto = `@${username_telegram}`; + } catch (e) { + } + + out += this.addRowTelegram('👤', 'Contatto', contatto, true); + + out += this.addRowTelegram('', `👉🏻 Vedi Annuncio completo su RISO`, url, true, true); + + // out += i18n.__('ADDED_FROM', ); + + return { newdescr, newdescrtelegram: out } } catch (e) { console.error('Error', e); } @@ -4534,5 +4754,22 @@ module.exports = { return inputString.replace(/[^\d.,]/g, '').replace(',', '.'); }, + removeLastSpaceAndACapo(miastr) { + miastr = miastr.trimRight().replace(/[\n\r]+$/, ''); + // Rimuove eventuali '
' in fondo alla stringa + // Rimuove eventuali '

' in fondo alla stringa + miastr = miastr.replace(/<\/div>$/, ''); + miastr = miastr.replace(/<\/div>$/, ''); + miastr = miastr.replace(/
$/, ''); + miastr = miastr.replace(/

<\/div>$/, ''); + miastr = miastr.replace(/
$/, ''); + miastr = miastr.trimRight().replace(/[\n\r]+$/, ''); + miastr = miastr.replace(/

<\/div>$/, ''); + miastr = miastr.replace(/
$/, ''); + miastr = miastr.replace(/
$/, ''); + miastr = miastr.replace(/
$/, ''); + + return miastr; + } }; diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index 9986e4e..3aafd40 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -347,6 +347,11 @@ module.exports = { OUT: 2, }, + AdType: { + OFFRO: 1, + CERCO: 2, + }, + TypeCashStr: [ { label: '[Nessuno]', @@ -508,6 +513,7 @@ module.exports = { MsgTeleg: { SHARE_MSGREG: 1, SHARE_TEXT: 2, + SHARE_ANNUNCIO: 3, }, TypeMsgTemplate: { @@ -874,6 +880,7 @@ module.exports = { getProjectForAll(proj_add, table) { let proj = { + idapp: 1, idContribType: 1, idCity: 1, pub_to_share: 1, @@ -1015,4 +1022,10 @@ module.exports = { MAX_QTA_PREORD: 5000, + STATUSSKILL_DI_PERSONA: 1, + STATUSSKILL_ONLINE: 2, + + TYPEHOSP_SCAMBIOCASA: 1, + TYPEHOSP_OSPITALITA: 2, + };