new(basic.gblib): CHART PROMPT and chart mode.
This commit is contained in:
parent
c51ceb649c
commit
14b833a580
2 changed files with 35 additions and 48 deletions
|
@ -651,11 +651,13 @@ export class GBConversationalService {
|
||||||
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;
|
||||||
|
|
||||||
|
@ -666,8 +668,6 @@ export class GBConversationalService {
|
||||||
|
|
||||||
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: [
|
||||||
|
|
|
@ -106,7 +106,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
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,7 +115,6 @@ 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;
|
||||||
|
@ -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`;
|
||||||
|
|
Loading…
Add table
Reference in a new issue