Skip to content

Commit

Permalink
new(basic.gblib): SEND FILE pdf as temporary images.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Oct 3, 2024
1 parent 2cffae8 commit 3791373
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,7 @@ export class WhatsappDirectLine extends GBService {
image: {
link: imageUrl,
caption: caption,
},
view_once: true
}
};

const response = await fetch(sendMessageEndpoint, {
Expand Down Expand Up @@ -946,8 +945,9 @@ export class WhatsappDirectLine extends GBService {
messages = msg.match(/(.|[\r\n]){1,4096}/g);

await CollectionUtil.asyncForEach(messages, async msg => {
await this.customClient.sendText(to, msg);

await this.sendTextMessage(to, msg);

if (messages.length > 1) {
await GBUtil.sleep(3000);
}
Expand Down Expand Up @@ -1393,5 +1393,41 @@ export class WhatsappDirectLine extends GBService {
console.error('Error during image download:', error.message);
}
}
public async sendTextMessage(mobile, message) {

// 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',
recipient_type: 'individual',
to: mobile,
type: 'text',
text: {
body: message,
}
};

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 message: ${JSON.stringify(errorData)}`);
}

const result = await response.json();
GBLogEx.info(0, 'Message sent successfully:' + result);
return result;
}

}

0 comments on commit 3791373

Please sign in to comment.