new(basic.gblib): SEND FILE pdf as temporary images.
This commit is contained in:
		
							parent
							
								
									7013d168df
								
							
						
					
					
						commit
						6c0d3ac311
					
				
					 2 changed files with 52 additions and 9 deletions
				
			
		| 
						 | 
					@ -1463,7 +1463,6 @@ export class DialogKeywords {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await CollectionUtil.asyncForEach(pngs, async png => {
 | 
					      await CollectionUtil.asyncForEach(pngs, async png => {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Prepare a cache to be referenced by Bot Framework.
 | 
					        // Prepare a cache to be referenced by Bot Framework.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(png.localName));
 | 
					        url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(png.localName));
 | 
				
			||||||
| 
						 | 
					@ -1478,7 +1477,7 @@ export class DialogKeywords {
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (channel === 'omnichannel' || !user) {
 | 
					        if (channel === 'omnichannel' || !user) {
 | 
				
			||||||
          await min.conversationalService.sendFile(min, null, mobile, url, caption);
 | 
					          await min.whatsAppDirectLine.sendFileToDevice(mobile, url, filename, caption);
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
          await min.conversationalService['sendOnConversation'](min, user, reply);
 | 
					          await min.conversationalService['sendOnConversation'](min, user, reply);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -536,7 +536,7 @@ export class WhatsappDirectLine extends GBService {
 | 
				
			||||||
        let response;
 | 
					        let response;
 | 
				
			||||||
        if (GBConfigService.get('STORAGE_NAME')) {
 | 
					        if (GBConfigService.get('STORAGE_NAME')) {
 | 
				
			||||||
          response = await client.apis.Conversations.Conversations_StartConversation(
 | 
					          response = await client.apis.Conversations.Conversations_StartConversation(
 | 
				
			||||||
            
 | 
					
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
          response = await client.apis.Conversations.Conversations_StartConversation(
 | 
					          response = await client.apis.Conversations.Conversations_StartConversation(
 | 
				
			||||||
| 
						 | 
					@ -556,7 +556,7 @@ export class WhatsappDirectLine extends GBService {
 | 
				
			||||||
        WhatsappDirectLine.mobiles[generatedConversationId] = from;
 | 
					        WhatsappDirectLine.mobiles[generatedConversationId] = from;
 | 
				
			||||||
        WhatsappDirectLine.usernames[from] = fromName;
 | 
					        WhatsappDirectLine.usernames[from] = fromName;
 | 
				
			||||||
        WhatsappDirectLine.chatIds[generatedConversationId] = message?.chatId;
 | 
					        WhatsappDirectLine.chatIds[generatedConversationId] = message?.chatId;
 | 
				
			||||||
        
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        this.pollMessages(client, generatedConversationId, from, fromName);
 | 
					        this.pollMessages(client, generatedConversationId, from, fromName);
 | 
				
			||||||
        this.inputMessage(client, generatedConversationId, text, from, fromName, group, attachments, pid);
 | 
					        this.inputMessage(client, generatedConversationId, text, from, fromName, group, attachments, pid);
 | 
				
			||||||
| 
						 | 
					@ -693,7 +693,7 @@ export class WhatsappDirectLine extends GBService {
 | 
				
			||||||
    return `${attachment.content.title} - ${attachment.content.text}`;
 | 
					    return `${attachment.content.title} - ${attachment.content.text}`;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async sendFileToDevice(to, url, filename, caption, chatId) {
 | 
					  public async sendFileToDevice(to, url, filename, caption, chatId, viewOnce) {
 | 
				
			||||||
    let options;
 | 
					    let options;
 | 
				
			||||||
    switch (this.provider) {
 | 
					    switch (this.provider) {
 | 
				
			||||||
      case 'meta':
 | 
					      case 'meta':
 | 
				
			||||||
| 
						 | 
					@ -707,9 +707,13 @@ export class WhatsappDirectLine extends GBService {
 | 
				
			||||||
          whatsappServiceNumber = GBServer.globals.minBoot.instance.whatsappServiceNumber;
 | 
					          whatsappServiceNumber = GBServer.globals.minBoot.instance.whatsappServiceNumber;
 | 
				
			||||||
          whatsappServiceKey = GBServer.globals.minBoot.instance.whatsappServiceKey;
 | 
					          whatsappServiceKey = GBServer.globals.minBoot.instance.whatsappServiceKey;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        const driver = createBot(whatsappServiceNumber, whatsappServiceKey);
 | 
					        if (viewOnce){
 | 
				
			||||||
        await driver.sendImage(to, url, { caption: caption });
 | 
					          this.sendImageViewOnce(to, url, caption);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else{
 | 
				
			||||||
 | 
					          const driver = createBot(whatsappServiceNumber, whatsappServiceKey);
 | 
				
			||||||
 | 
					          await driver.sendImage(to, url, { caption: caption });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      case 'GeneralBots':
 | 
					      case 'GeneralBots':
 | 
				
			||||||
| 
						 | 
					@ -761,7 +765,47 @@ export class WhatsappDirectLine extends GBService {
 | 
				
			||||||
  public async sendTextAsAudioToDevice(to, msg: string, chatId) {
 | 
					  public async sendTextAsAudioToDevice(to, msg: string, chatId) {
 | 
				
			||||||
    const url = await GBConversationalService.getAudioBufferFromText(msg);
 | 
					    const url = await GBConversationalService.getAudioBufferFromText(msg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await this.sendFileToDevice(to, url, 'Audio', msg, chatId);
 | 
					    await this.sendFileToDevice(to, url, 'Audio', msg, chatId, false);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public async sendImageViewOnce(mobile, imageUrl, caption = '') {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 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.whatsappBusinessManagerId}/messages`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const messageData = {
 | 
				
			||||||
 | 
					      messaging_product: 'whatsapp',
 | 
				
			||||||
 | 
					      recipient_type: 'individual',
 | 
				
			||||||
 | 
					      to: mobile,
 | 
				
			||||||
 | 
					      type: 'image',
 | 
				
			||||||
 | 
					      image: {
 | 
				
			||||||
 | 
					        link: imageUrl,
 | 
				
			||||||
 | 
					        caption: caption
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      view_once: true
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const response = await fetch(sendMessageEndpoint, {
 | 
				
			||||||
 | 
					      method: 'POST',
 | 
				
			||||||
 | 
					      headers: {
 | 
				
			||||||
 | 
					        'Authorization': `Bearer ${accessToken}`,
 | 
				
			||||||
 | 
					        'Content-Type': 'application/json'
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      body: JSON.stringify(messageData)
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!response.ok) {
 | 
				
			||||||
 | 
					      const errorData = await response.json();
 | 
				
			||||||
 | 
					      throw new Error(`Failed to send image: ${JSON.stringify(errorData)}`);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const result = await response.json();
 | 
				
			||||||
 | 
					    GBLogEx.info(0, 'Image sent successfully:' + result);
 | 
				
			||||||
 | 
					    return result;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Function to create or update a template using WhatsApp Business API
 | 
					  // Function to create or update a template using WhatsApp Business API
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue