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('.mp4') ||
 | 
			
		||||
      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;
 | 
			
		||||
  
 | 
			
		||||
    // Set folder based on media type
 | 
			
		||||
    const folder = mediaType === 'videos' ? 'videos' : 'images';
 | 
			
		||||
    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;
 | 
			
		||||
  
 | 
			
		||||
| 
						 | 
				
			
			@ -666,8 +668,6 @@ export class GBConversationalService {
 | 
			
		|||
  
 | 
			
		||||
    template = isMedia ? mediaFile.replace(/\.[^/.]+$/, '') : template;
 | 
			
		||||
  
 | 
			
		||||
    let mediaType = text.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mov') ? 'video' : 'image';
 | 
			
		||||
 | 
			
		||||
    let data: any = {
 | 
			
		||||
      name: template,
 | 
			
		||||
      components: [
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -106,7 +106,6 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
    this.whatsappServiceNumber = whatsappServiceNumber;
 | 
			
		||||
    this.whatsappServiceUrl = whatsappServiceUrl;
 | 
			
		||||
    this.provider = whatsappServiceKey === 'internal' ? 'GeneralBots' : 'meta';
 | 
			
		||||
    
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public static async asyncForEach(array, callback) {
 | 
			
		||||
| 
						 | 
				
			
			@ -116,7 +115,6 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
  }
 | 
			
		||||
 | 
			
		||||
  public async setup(setUrl: boolean) {
 | 
			
		||||
    
 | 
			
		||||
    this.directLineClient = GBUtil.getDirectLineClient(this.min);
 | 
			
		||||
 | 
			
		||||
    let url: string;
 | 
			
		||||
| 
						 | 
				
			
			@ -750,27 +748,37 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
  // Function to create or update a template using WhatsApp Business API
 | 
			
		||||
 | 
			
		||||
  public async createOrUpdateTemplate(min: GBMinInstance, template, text) {
 | 
			
		||||
    template = template.replace(/\-/gi, '_');
 | 
			
		||||
    template = template.replace(/\./gi, '_');
 | 
			
		||||
 | 
			
		||||
    template = template.replace(/\-/gi, '_')
 | 
			
		||||
    template = template.replace(/\./gi, '_')
 | 
			
		||||
 | 
			
		||||
    let image = /(.*)\n/gim.exec(text)[0].trim();
 | 
			
		||||
    // Determine if media is image or video
 | 
			
		||||
    let isMedia =
 | 
			
		||||
      text.toLowerCase().endsWith('.jpg') ||
 | 
			
		||||
      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`);
 | 
			
		||||
    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');
 | 
			
		||||
 | 
			
		||||
    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 = {
 | 
			
		||||
      name: template,
 | 
			
		||||
      components: [
 | 
			
		||||
        {
 | 
			
		||||
          type: 'HEADER',
 | 
			
		||||
          format: 'IMAGE',
 | 
			
		||||
          example: { header_handle: [handleImage] }
 | 
			
		||||
          format: mediaType.toUpperCase(), // Use IMAGE or VIDEO format
 | 
			
		||||
          example: { header_handle: [handleMedia] }
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          type: 'BODY',
 | 
			
		||||
| 
						 | 
				
			
			@ -782,17 +790,14 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
    const name = data.name;
 | 
			
		||||
 | 
			
		||||
    // Define the API base URL and endpoints
 | 
			
		||||
 | 
			
		||||
    const baseUrl = 'https://graph.facebook.com/v20.0'; // API version 20.0
 | 
			
		||||
    const businessAccountId = this.whatsappBusinessManagerId;
 | 
			
		||||
    const accessToken = this.whatsappServiceKey;
 | 
			
		||||
 | 
			
		||||
    // Endpoint for listing templates
 | 
			
		||||
 | 
			
		||||
    const listTemplatesEndpoint = `${baseUrl}/${businessAccountId}/message_templates?access_token=${accessToken}`;
 | 
			
		||||
 | 
			
		||||
    // Step 1: Check if the template exists
 | 
			
		||||
 | 
			
		||||
    const listResponse = await fetch(listTemplatesEndpoint, {
 | 
			
		||||
      method: 'GET',
 | 
			
		||||
      headers: {
 | 
			
		||||
| 
						 | 
				
			
			@ -808,25 +813,7 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
    const templateExists = templates.data.find(template => template.name === name);
 | 
			
		||||
 | 
			
		||||
    if (templateExists) {
 | 
			
		||||
      // // Step 2: Update the template
 | 
			
		||||
      // 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}`);
 | 
			
		||||
      GBLogEx.info(min, `Template update skipped: ${name}`);
 | 
			
		||||
    } else {
 | 
			
		||||
      // Step 3: Create the template
 | 
			
		||||
      const createTemplateEndpoint = `${baseUrl}/${businessAccountId}/message_templates`;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue