new(basic.gblib): SEND FILE pdf as temporary images.
This commit is contained in:
		
							parent
							
								
									975d4961a4
								
							
						
					
					
						commit
						25245a010e
					
				
					 5 changed files with 111 additions and 60 deletions
				
			
		
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										52
									
								
								.wwebjs_cache/2.3000.1017054665.html
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								.wwebjs_cache/2.3000.1017054665.html
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| 
						 | 
				
			
			@ -1110,16 +1110,15 @@ export class GBMinService {
 | 
			
		|||
            }
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        GBMinService.pidsConversation[conversationId] = pid; 
 | 
			
		||||
        GBMinService.pidsConversation[conversationId] = pid;
 | 
			
		||||
        step.context.activity['pid'] = pid;
 | 
			
		||||
 | 
			
		||||
        const notes = min.core.getParam(min.instance, 'Notes', null);
 | 
			
		||||
        if (await this.handleUploads(min, step, user, params, notes != null))
 | 
			
		||||
        {
 | 
			
		||||
        if (await this.handleUploads(min, step, user, params, notes != null)) {
 | 
			
		||||
          return;
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
        // Required for MSTEAMS handling of persisted conversations.
 | 
			
		||||
 | 
			
		||||
        if (step.context.activity.channelId === 'msteams') {
 | 
			
		||||
| 
						 | 
				
			
			@ -1334,16 +1333,16 @@ export class GBMinService {
 | 
			
		|||
      const base64Data = url.split(';base64,')[1];
 | 
			
		||||
      buffer = Buffer.from(base64Data, 'base64');
 | 
			
		||||
    } else {
 | 
			
		||||
        const options = {
 | 
			
		||||
          method: 'GET',
 | 
			
		||||
          encoding: 'binary'
 | 
			
		||||
        };
 | 
			
		||||
        const res = await fetch(url, options);
 | 
			
		||||
      const options = {
 | 
			
		||||
        method: 'GET',
 | 
			
		||||
        encoding: 'binary'
 | 
			
		||||
      };
 | 
			
		||||
      const res = await fetch(url, options);
 | 
			
		||||
      buffer = arrayBufferToBuffer(await res.arrayBuffer());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    await fs.writeFile(localFileName, buffer);
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    return {
 | 
			
		||||
      name: attachment.name,
 | 
			
		||||
      filename: localFileName,
 | 
			
		||||
| 
						 | 
				
			
			@ -1363,19 +1362,18 @@ export class GBMinService {
 | 
			
		|||
 | 
			
		||||
  private async handleUploads(min, step, user, params, autoSave) {
 | 
			
		||||
    // Prepare Promises to download each attachment and then execute each Promise.
 | 
			
		||||
    let ret = false;
 | 
			
		||||
    if (
 | 
			
		||||
      step.context.activity.attachments &&
 | 
			
		||||
      step.context.activity.attachments[0] &&
 | 
			
		||||
      step.context.activity.attachments[0].contentType != 'text/html'
 | 
			
		||||
    ) {
 | 
			
		||||
      const promises = step.context.activity.attachments.map(
 | 
			
		||||
         GBMinService.downloadAttachmentAndWrite.bind({ min, user, params })
 | 
			
		||||
        GBMinService.downloadAttachmentAndWrite.bind({ min, user, params })
 | 
			
		||||
      );
 | 
			
		||||
      const successfulSaves = await Promise.all(promises);
 | 
			
		||||
      async function replyForReceivedAttachments(attachmentData) {
 | 
			
		||||
        if (attachmentData) {
 | 
			
		||||
          
 | 
			
		||||
 | 
			
		||||
          // In case of not having HEAR activated before, it is
 | 
			
		||||
          // a upload with no Dialog, so run Auto Save to .gbdrive.
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1412,7 +1410,6 @@ export class GBMinService {
 | 
			
		|||
      const replyPromises = successfulSaves.map(replyForReceivedAttachments.bind(step.context));
 | 
			
		||||
      await Promise.all(replyPromises);
 | 
			
		||||
      if (successfulSaves.length > 0) {
 | 
			
		||||
        ret = true;
 | 
			
		||||
        class GBFile {
 | 
			
		||||
          data: Buffer;
 | 
			
		||||
          filename: string;
 | 
			
		||||
| 
						 | 
				
			
			@ -1436,13 +1433,11 @@ export class GBMinService {
 | 
			
		|||
            throw new Error('It is only possible to upload one file per message, right now.');
 | 
			
		||||
          }
 | 
			
		||||
          min.cbMap[user.userId].promise = results[0];
 | 
			
		||||
          return;
 | 
			
		||||
        } else {
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      return successfulSaves.length > 0;
 | 
			
		||||
    }
 | 
			
		||||
    return ret;
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -693,7 +693,7 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
            break;
 | 
			
		||||
 | 
			
		||||
          case 'image/png':
 | 
			
		||||
            await this.sendFileToDevice(to, attachment.contentUrl, attachment.name, attachment.name, 0, 0);
 | 
			
		||||
            await this.sendFileToDevice(to, attachment.contentUrl, attachment.name, attachment.name, 0, false);
 | 
			
		||||
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -710,7 +710,7 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
    return `${attachment.content.title} - ${attachment.content.text}`;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async sendFileToDevice(to, url, filename, caption, chatId, viewOnce) {
 | 
			
		||||
  public async sendFileToDevice(to, url, filename, caption, chatId, isViewOnce = false) {
 | 
			
		||||
    let options;
 | 
			
		||||
    switch (this.provider) {
 | 
			
		||||
      case 'meta':
 | 
			
		||||
| 
						 | 
				
			
			@ -724,7 +724,7 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
          whatsappServiceNumber = GBServer.globals.minBoot.instance.whatsappServiceNumber;
 | 
			
		||||
          whatsappServiceKey = GBServer.globals.minBoot.instance.whatsappServiceKey;
 | 
			
		||||
        }
 | 
			
		||||
        if (viewOnce) {
 | 
			
		||||
        if (isViewOnce) {
 | 
			
		||||
          await this.sendImageViewOnce(to, url, caption);
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
| 
						 | 
				
			
			@ -744,7 +744,7 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
          }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        await this.customClient.sendMessage(to, attachment, { caption: caption , viewOnce});
 | 
			
		||||
        await this.customClient.sendMessage(to, attachment, { caption: caption, isViewOnce });
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -923,7 +923,7 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async sendToDevice(to: any, msg: string, conversationId, viewOnce= false) {
 | 
			
		||||
  public async sendToDevice(to: any, msg: string, conversationId, isViewOnce = false) {
 | 
			
		||||
    try {
 | 
			
		||||
      const cmd = '/audio ';
 | 
			
		||||
      let url;
 | 
			
		||||
| 
						 | 
				
			
			@ -947,7 +947,7 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
              await CollectionUtil.asyncForEach(messages, async msg => {
 | 
			
		||||
 | 
			
		||||
                await this.sendTextMessage(to, msg);
 | 
			
		||||
                
 | 
			
		||||
 | 
			
		||||
                if (messages.length > 1) {
 | 
			
		||||
                  await GBUtil.sleep(3000);
 | 
			
		||||
                }
 | 
			
		||||
| 
						 | 
				
			
			@ -984,7 +984,7 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
              }
 | 
			
		||||
            }
 | 
			
		||||
            if ((await this.customClient.getState()) === WAState.CONNECTED) {
 | 
			
		||||
              await this.customClient.sendMessage(to, msg, { isViewOnce: isViewOnce });
 | 
			
		||||
              await this.customClient.sendMessage(to, msg, { isViewOnce });
 | 
			
		||||
            } else {
 | 
			
		||||
              GBLogEx.info(this.min, `WhatsApp OFFLINE ${to}: ${msg}`);
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			@ -1397,10 +1397,10 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
 | 
			
		||||
    // Define the API base URL and endpoints
 | 
			
		||||
    const baseUrl = 'https://graph.facebook.com/v20.0'; // API version 20.0
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
    const accessToken = this.whatsappServiceKey;
 | 
			
		||||
    const sendMessageEndpoint = `${baseUrl}/${this.whatsappServiceNumber}/messages`;
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
    const messageData = {
 | 
			
		||||
      messaging_product: 'whatsapp',
 | 
			
		||||
      to: mobile,
 | 
			
		||||
| 
						 | 
				
			
			@ -1409,7 +1409,7 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
        body: message,
 | 
			
		||||
      }
 | 
			
		||||
    };
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
    const response = await fetch(sendMessageEndpoint, {
 | 
			
		||||
      method: 'POST',
 | 
			
		||||
      headers: {
 | 
			
		||||
| 
						 | 
				
			
			@ -1418,15 +1418,15 @@ export class WhatsappDirectLine extends GBService {
 | 
			
		|||
      },
 | 
			
		||||
      body: JSON.stringify(messageData)
 | 
			
		||||
    });
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
    if (!response.ok) {
 | 
			
		||||
      const errorData = await response.json();
 | 
			
		||||
      throw new Error(`Failed to send message: ${JSON.stringify(errorData)}`);
 | 
			
		||||
    }
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
    const result = await response.json();
 | 
			
		||||
    GBLogEx.info(0, 'Message sent successfully:' + result);
 | 
			
		||||
    return result;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,11 @@
 | 
			
		|||
TALK "Please, take a photo of the QR Code and send to me."
 | 
			
		||||
HEAR doc as QRCODE
 | 
			
		||||
TALK "Reading document " + doc + "..."
 | 
			
		||||
text = GET doc
 | 
			
		||||
text = null
 | 
			
		||||
 | 
			
		||||
IF doc THEN 
 | 
			
		||||
    TALK "Reading document " + doc + "..."
 | 
			
		||||
    text = GET doc
 | 
			
		||||
END IF
 | 
			
		||||
 | 
			
		||||
IF text THEN
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -9,7 +13,7 @@ IF text THEN
 | 
			
		|||
    SET CONTEXT text 
 | 
			
		||||
    SET ANSWER MODE "document"
 | 
			
		||||
    TALK "Document ${doc} loaded. You can ask me anything about it."
 | 
			
		||||
    TALK "I will also send it to you..."
 | 
			
		||||
    TALK "Please, wait while I convert pages to images..."
 | 
			
		||||
    SEND FILE doc  
 | 
			
		||||
 | 
			
		||||
ELSE
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue