- Iniziato a scrivere la CHATBOT...
This commit is contained in:
@@ -11,6 +11,9 @@ const appTelegramDest = [tools.FREEPLANET, tools.FREEPLANET];
|
||||
|
||||
const appTeleg_BotOnGroup = [tools.IDAPP_BOTONGROUP];
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
//PIPPO
|
||||
|
||||
const printf = require('util').format;
|
||||
@@ -31,8 +34,6 @@ const server_constants = require('../tools/server_constants');
|
||||
// const {ListaIngresso} = require('../models/listaingresso');
|
||||
const { MsgTemplate } = require('../models/msg_template');
|
||||
|
||||
const globalTables = require('../tools/globalTables');
|
||||
|
||||
const i18n = require('i18n');
|
||||
|
||||
let url = process.env.URL || 'https://<PUBLIC-URL>';
|
||||
@@ -1386,6 +1387,8 @@ const MyTelegramBot = {
|
||||
|
||||
sendMsgFromSite: async function (idapp, user, params) {
|
||||
try {
|
||||
const globalTables = require('../tools/globalTables');
|
||||
|
||||
let ris = {
|
||||
numrec: 0,
|
||||
nummsgsent: 0,
|
||||
@@ -3810,34 +3813,69 @@ class Telegram {
|
||||
}
|
||||
}
|
||||
|
||||
// Soluzione 3: Implementazione completa con gestione errori e tentativi multipli
|
||||
async sendImageToTelegram(chatId, imageUrl, opt) {
|
||||
try {
|
||||
// Prima prova: invia direttamente l'URL
|
||||
// Primo tentativo: invia direttamente l'URL
|
||||
try {
|
||||
return await this.bot.sendPhoto(chatId, imageUrl, opt);
|
||||
} catch (error) {
|
||||
console.log('Primo tentativo fallito, provo a convertire...');
|
||||
}
|
||||
|
||||
// Seconda prova: scarica e converti in JPG
|
||||
const response = await axios.get(imageUrl, { responseType: 'arraybuffer' });
|
||||
const imageBuffer = Buffer.from(response.data);
|
||||
|
||||
const jpgBuffer = await sharp(imageBuffer).jpeg({ quality: 90 }).toBuffer();
|
||||
console.log('imageUrl', imageUrl);
|
||||
|
||||
// Secondo tentativo: scarica e converte con sharp
|
||||
try {
|
||||
return await this.bot.sendPhoto(chatId, jpgBuffer, opt);
|
||||
} catch (error) {
|
||||
console.log('Secondo tentativo fallito, provo come documento...');
|
||||
const response = await axios.get(imageUrl, { responseType: 'arraybuffer' });
|
||||
const contentType = response.headers['content-type'];
|
||||
const imageBuffer = Buffer.from(response.data);
|
||||
|
||||
if (!contentType.startsWith('image/')) {
|
||||
console.warn('Non è un formato immagine:', contentType);
|
||||
return await this.bot.sendDocument(chatId, imageBuffer, opt);
|
||||
}
|
||||
|
||||
try {
|
||||
const jpgBuffer = await sharp(imageBuffer).jpeg({ quality: 90 }).toBuffer();
|
||||
return await this.bot.sendPhoto(chatId, jpgBuffer, opt);
|
||||
} catch (err) {
|
||||
console.warn('Sharp conversion failed:', err.message);
|
||||
return await this.bot.sendDocument(chatId, imageBuffer, opt);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn("Errore nel download dell'immagine, provo con file locale...");
|
||||
}
|
||||
|
||||
// Ultima prova: invia come documento
|
||||
return await this.bot.sendDocument(chatId, imageBuffer, opt);
|
||||
} catch (error) {
|
||||
console.error('Tutti i tentativi di invio sono falliti:', error);
|
||||
// Terzo tentativo: accedi al file localmente
|
||||
try {
|
||||
if (!imageUrl.includes('/upload/')) {
|
||||
throw new Error('URL non contiene "/upload/", impossibile determinare il path locale.');
|
||||
}
|
||||
|
||||
let dirmain = '';
|
||||
|
||||
if (!tools.sulServer()) {
|
||||
dirmain = server_constants.DIR_PUBLIC_LOCALE;
|
||||
}
|
||||
|
||||
const relativePath = imageUrl.split('/upload/')[1]; // es. "profile/surya1977/mygoods/1_module.jpg"
|
||||
const mydir = tools.getdirByIdApp(opt.idapp) + dirmain + server_constants.DIR_UPLOAD;
|
||||
const localPath = path.join(mydir, relativePath);
|
||||
|
||||
console.log('Cerco il file localmente:', localPath);
|
||||
|
||||
const localBuffer = fs.readFileSync(localPath);
|
||||
return await this.bot.sendDocument(chatId, localBuffer, opt);
|
||||
} catch (err) {
|
||||
console.error('Lettura file locale fallita:', err.message);
|
||||
}
|
||||
|
||||
// Se tutto fallisce
|
||||
console.error('Tutti i tentativi di invio sono falliti');
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error('Errore generale:', error);
|
||||
return false;
|
||||
//throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3919,7 +3957,7 @@ class Telegram {
|
||||
}
|
||||
opt.img = tools.fixUrl(opt.img, opt.idapp);
|
||||
console.log('opt.img', opt.img);
|
||||
risSendPhoto = await this.sendImageToTelegram(id, opt.img, { caption: text, ...form }).catch((e) => {
|
||||
risSendPhoto = await this.sendImageToTelegram(id, opt.img, { caption: text, ...form, ...opt }).catch((e) => {
|
||||
let blocked = false;
|
||||
if (e.message.indexOf('Forbidden') > 0 || e.message.indexOf('chat not found') > 0) {
|
||||
blocked = true;
|
||||
@@ -4362,7 +4400,7 @@ if (true) {
|
||||
|
||||
// RISPOSTE ALLE CALLBACK (Bottoni)
|
||||
|
||||
let testo_notifica_di_risposta = ''
|
||||
let testo_notifica_di_risposta = '';
|
||||
|
||||
try {
|
||||
const myclTelegram = getclTelegBytoken(bot.token);
|
||||
@@ -4612,8 +4650,7 @@ if (true) {
|
||||
}
|
||||
|
||||
// CALLBACK DI RISPOSTA AL BOT
|
||||
bot.answerCallbackQuery(callbackQuery.id, { text: testo_notifica_di_risposta } ) ;
|
||||
|
||||
bot.answerCallbackQuery(callbackQuery.id, { text: testo_notifica_di_risposta });
|
||||
} catch (e) {
|
||||
console.error('Error BOT callback_query', e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user