new(basic.gblib): CHART PROMPT and chart mode.

This commit is contained in:
Rodrigo Rodriguez 2024-09-05 14:53:21 -03:00
parent c51ceb649c
commit 14b833a580
2 changed files with 35 additions and 48 deletions

View file

@ -644,30 +644,30 @@ export class GBConversationalService {
public async fillAndBroadcastTemplate(min: GBMinInstance, template, mobile: string, text) { public async fillAndBroadcastTemplate(min: GBMinInstance, template, mobile: string, text) {
template = template.replace(/\-/gi, '_'); template = template.replace(/\-/gi, '_');
template = template.replace(/\./gi, '_'); template = template.replace(/\./gi, '_');
let isMedia = let isMedia =
text.toLowerCase().endsWith('.jpg') || text.toLowerCase().endsWith('.jpg') ||
text.toLowerCase().endsWith('.jpeg') || text.toLowerCase().endsWith('.jpeg') ||
text.toLowerCase().endsWith('.png') || text.toLowerCase().endsWith('.png') ||
text.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mp4') ||
text.toLowerCase().endsWith('.mov'); text.toLowerCase().endsWith('.mov');
let mediaType = text.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mov') ? 'video' : 'image';
let mediaFile = !isMedia ? /(.*)\n/gim.exec(text)[0].trim() : text; let mediaFile = !isMedia ? /(.*)\n/gim.exec(text)[0].trim() : text;
// Set folder based on media type
const folder = mediaType === 'videos' ? 'videos' : 'images';
const gbaiName = DialogKeywords.getGBAIPath(min.botId); const gbaiName = DialogKeywords.getGBAIPath(min.botId);
const fileUrl = urlJoin(process.env.BOT_URL, 'kb', gbaiName, `${min.botId}.gbkb`, 'media', mediaFile); const fileUrl = urlJoin(process.env.BOT_URL, 'kb', gbaiName, `${min.botId}.gbkb`, folder, mediaFile);
let urlMedia = mediaFile.startsWith('http') ? mediaFile : fileUrl; let urlMedia = mediaFile.startsWith('http') ? mediaFile : fileUrl;
if (!isMedia) { if (!isMedia) {
text = text.substring(mediaFile.length + 1).trim(); text = text.substring(mediaFile.length + 1).trim();
text = text.replace(/\n/g, '\\n'); text = text.replace(/\n/g, '\\n');
} }
template = isMedia ? mediaFile.replace(/\.[^/.]+$/, '') : template; template = isMedia ? mediaFile.replace(/\.[^/.]+$/, '') : template;
let mediaType = text.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mov') ? 'video' : 'image';
let data: any = { let data: any = {
name: template, name: template,
components: [ components: [
@ -684,11 +684,11 @@ export class GBConversationalService {
} }
] ]
}; };
await this.sendToMobile(min, mobile, data, null); await this.sendToMobile(min, mobile, data, null);
GBLogEx.info(min, `Sending answer file to mobile: ${mobile}. Header: ${urlMedia}`); GBLogEx.info(min, `Sending answer file to mobile: ${mobile}. Header: ${urlMedia}`);
} }
// tslint:enable:no-unsafe-any // tslint:enable:no-unsafe-any
public async sendMarkdownToMobile(min: GBMinInstance, step: GBDialogStep, mobile: string, text: string) { public async sendMarkdownToMobile(min: GBMinInstance, step: GBDialogStep, mobile: string, text: string) {

View file

@ -101,12 +101,11 @@ export class WhatsappDirectLine extends GBService {
super(); super();
this.min = min; this.min = min;
this.botId = botId; this.botId = botId;
this.whatsappServiceKey = whatsappServiceKey; this.whatsappServiceKey = whatsappServiceKey;
this.whatsappServiceNumber = whatsappServiceNumber; this.whatsappServiceNumber = whatsappServiceNumber;
this.whatsappServiceUrl = whatsappServiceUrl; this.whatsappServiceUrl = whatsappServiceUrl;
this.provider = whatsappServiceKey === 'internal' ? 'GeneralBots' : 'meta'; this.provider = whatsappServiceKey === 'internal' ? 'GeneralBots' : 'meta';
} }
public static async asyncForEach(array, callback) { public static async asyncForEach(array, callback) {
@ -116,9 +115,8 @@ export class WhatsappDirectLine extends GBService {
} }
public async setup(setUrl: boolean) { public async setup(setUrl: boolean) {
this.directLineClient = GBUtil.getDirectLineClient(this.min); this.directLineClient = GBUtil.getDirectLineClient(this.min);
let url: string; let url: string;
let options: any; let options: any;
@ -750,27 +748,37 @@ export class WhatsappDirectLine extends GBService {
// Function to create or update a template using WhatsApp Business API // Function to create or update a template using WhatsApp Business API
public async createOrUpdateTemplate(min: GBMinInstance, template, text) { public async createOrUpdateTemplate(min: GBMinInstance, template, text) {
template = template.replace(/\-/gi, '_');
template = template.replace(/\./gi, '_');
template = template.replace(/\-/gi, '_') // Determine if media is image or video
template = template.replace(/\./gi, '_') let isMedia =
text.toLowerCase().endsWith('.jpg') ||
let image = /(.*)\n/gim.exec(text)[0].trim(); text.toLowerCase().endsWith('.jpeg') ||
text.toLowerCase().endsWith('.png') ||
text.toLowerCase().endsWith('.mp4') ||
text.toLowerCase().endsWith('.mov');
let mediaType = text.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mov') ? 'video' : 'image';
let mediaFile = /(.*)\n/gim.exec(text)[0].trim();
// Set folder based on media type
let folder = mediaType === 'video' ? 'videos' : 'images';
let path = DialogKeywords.getGBAIPath(min.botId, `gbkb`); let path = DialogKeywords.getGBAIPath(min.botId, `gbkb`);
path = Path.join(process.env.PWD, 'work', path, 'images', image); path = Path.join(process.env.PWD, 'work', path, folder, mediaFile);
text = text.substring(image.length + 1).trim(); text = text.substring(mediaFile.length + 1).trim();
text = text.replace(/\n/g, '\\n'); text = text.replace(/\n/g, '\\n');
const handleImage = await min.whatsAppDirectLine.uploadLargeFile(min, path); // Upload the media file based on media type
const handleMedia = await min.whatsAppDirectLine.uploadLargeFile(min, path);
let data: any = { let data: any = {
name: template, name: template,
components: [ components: [
{ {
type: 'HEADER', type: 'HEADER',
format: 'IMAGE', format: mediaType.toUpperCase(), // Use IMAGE or VIDEO format
example: { header_handle: [handleImage] } example: { header_handle: [handleMedia] }
}, },
{ {
type: 'BODY', type: 'BODY',
@ -782,17 +790,14 @@ export class WhatsappDirectLine extends GBService {
const name = data.name; const name = data.name;
// Define the API base URL and endpoints // Define the API base URL and endpoints
const baseUrl = 'https://graph.facebook.com/v20.0'; // API version 20.0 const baseUrl = 'https://graph.facebook.com/v20.0'; // API version 20.0
const businessAccountId = this.whatsappBusinessManagerId; const businessAccountId = this.whatsappBusinessManagerId;
const accessToken = this.whatsappServiceKey; const accessToken = this.whatsappServiceKey;
// Endpoint for listing templates // Endpoint for listing templates
const listTemplatesEndpoint = `${baseUrl}/${businessAccountId}/message_templates?access_token=${accessToken}`; const listTemplatesEndpoint = `${baseUrl}/${businessAccountId}/message_templates?access_token=${accessToken}`;
// Step 1: Check if the template exists // Step 1: Check if the template exists
const listResponse = await fetch(listTemplatesEndpoint, { const listResponse = await fetch(listTemplatesEndpoint, {
method: 'GET', method: 'GET',
headers: { headers: {
@ -808,25 +813,7 @@ export class WhatsappDirectLine extends GBService {
const templateExists = templates.data.find(template => template.name === name); const templateExists = templates.data.find(template => template.name === name);
if (templateExists) { if (templateExists) {
// // Step 2: Update the template GBLogEx.info(min, `Template update skipped: ${name}`);
// const updateTemplateEndpoint = `${baseUrl}/${templateExists.id}`;
// const updateResponse = await fetch(updateTemplateEndpoint, {
// method: 'POST',
// headers: {
// Authorization: `Bearer ${accessToken}`,
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify({
// components: data.components
// })
// });
// if (!updateResponse.ok) {
// throw new Error(`Failed to update template: ${name} ${await updateResponse.text()}`);
// }
GBLogEx.info(min, `Template update skiped: ${name}`);
} else { } else {
// Step 3: Create the template // Step 3: Create the template
const createTemplateEndpoint = `${baseUrl}/${businessAccountId}/message_templates`; const createTemplateEndpoint = `${baseUrl}/${businessAccountId}/message_templates`;
@ -1186,7 +1173,7 @@ export class WhatsappDirectLine extends GBService {
} else { } else {
let t; let t;
activeMin = GBServer.globals.minInstances.filter(p => p.instance.instanceId === user.instanceId)[0]; activeMin = GBServer.globals.minInstances.filter(p => p.instance.instanceId === user.instanceId)[0];
if (activeMin === undefined) { if (activeMin === undefined) {
activeMin = GBServer.globals.minBoot; activeMin = GBServer.globals.minBoot;
t = (activeMin as any).whatsAppDirectLine; t = (activeMin as any).whatsAppDirectLine;
await t.sendToDevice( await t.sendToDevice(