fix(WhatsappDirectLine): streamline media handling and improve template creation
Some checks are pending
GBCI / build (push) Waiting to run
Some checks are pending
GBCI / build (push) Waiting to run
This commit is contained in:
parent
2da1e99c0d
commit
2e42c36ecb
2 changed files with 209 additions and 222 deletions
|
@ -646,14 +646,10 @@ export class GBConversationalService {
|
|||
text.toLowerCase().endsWith('.png') ||
|
||||
text.toLowerCase().endsWith('.mp4') ||
|
||||
text.toLowerCase().endsWith('.mov');
|
||||
|
||||
let text1= /(.*)\n/gim.exec(text);
|
||||
|
||||
let mediaFile = !isMedia ? (text1? text1[0].trim() : text):text;
|
||||
let mediaFile = !isMedia ? /(.*)\n/gim.exec(text)[0].trim() : text;
|
||||
let mediaType = mediaFile.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mov') ? 'video' : 'image';
|
||||
|
||||
// Set folder based on media type.
|
||||
|
||||
// Set folder based on media type
|
||||
const folder = mediaType === 'video' ? 'videos' : 'images';
|
||||
const gbaiName = GBUtil.getGBAIPath(min.botId);
|
||||
const fileUrl = urlJoin(process.env.BOT_URL, 'kb', gbaiName, `${min.botId}.gbkb`, folder, mediaFile);
|
||||
|
@ -669,9 +665,18 @@ export class GBConversationalService {
|
|||
|
||||
let data: any = {
|
||||
name: template,
|
||||
components: [ ]
|
||||
components: [
|
||||
{
|
||||
type: 'header',
|
||||
parameters: [
|
||||
{
|
||||
type: mediaType
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
//data['components'][0]['parameters'][0][mediaType] = { link: urlMedia };
|
||||
data['components'][0]['parameters'][0][mediaType] = { link: urlMedia };
|
||||
|
||||
await this.sendToMobile(min, mobile, data, null);
|
||||
GBLogEx.info(min, `Sending answer file to mobile: ${mobile}. Header: ${urlMedia}`);
|
||||
|
|
|
@ -834,11 +834,9 @@ 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('.docx', '');
|
||||
template = template.replace(/\-/gi, '_');
|
||||
template = template.replace(/\./gi, '_');
|
||||
|
||||
|
||||
// Determine if media is image or video
|
||||
let isMedia =
|
||||
text.toLowerCase().endsWith('.jpg') ||
|
||||
|
@ -863,17 +861,11 @@ export class WhatsappDirectLine extends GBService {
|
|||
let data: any = {
|
||||
name: template,
|
||||
components: [
|
||||
// {
|
||||
// type: 'HEADER',
|
||||
// format: 'TEXT',
|
||||
// text: 'General Bots',
|
||||
// example: {
|
||||
// header_text: [
|
||||
// "General Bots"
|
||||
// ]
|
||||
// }
|
||||
// },
|
||||
|
||||
{
|
||||
type: 'HEADER',
|
||||
format: mediaType.toUpperCase(), // Use IMAGE or VIDEO format
|
||||
example: { header_handle: [handleMedia] }
|
||||
},
|
||||
{
|
||||
type: 'BODY',
|
||||
text: text
|
||||
|
@ -1388,26 +1380,17 @@ export class WhatsappDirectLine extends GBService {
|
|||
|
||||
while (startOffset < fileSize) {
|
||||
const endOffset = Math.min(startOffset + CHUNK_SIZE, fileSize);
|
||||
const fileStream = createReadStream(filePath, { start: startOffset, end: endOffset - 1 });
|
||||
const chunkSize = endOffset - startOffset;
|
||||
|
||||
// Read the chunk into a buffer to get accurate size
|
||||
const buffer = new Uint8Array(chunkSize);
|
||||
const fd = await fs.open(filePath, 'r');
|
||||
const { bytesRead } = await fd.read(buffer, 0, chunkSize, startOffset);
|
||||
await fd.close();
|
||||
|
||||
// Trim buffer to actual bytes read
|
||||
const chunk = buffer.subarray(0, bytesRead);
|
||||
|
||||
const uploadResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `OAuth ${userAccessToken}`,
|
||||
'file_offset': startOffset.toString(),
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'Content-Length': bytesRead.toString()
|
||||
Authorization: `OAuth ${userAccessToken}`,
|
||||
file_offset: startOffset.toString(),
|
||||
'Content-Length': chunkSize.toString()
|
||||
},
|
||||
body: chunk
|
||||
body: fileStream
|
||||
});
|
||||
|
||||
const uploadData = await uploadResponse.json();
|
||||
|
@ -1415,7 +1398,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
h = uploadData.h;
|
||||
}
|
||||
if (!uploadResponse.ok) {
|
||||
throw new Error(`Upload failed: ${uploadData.error?.message || 'Unknown error'}`);
|
||||
throw new Error(uploadData.error.message);
|
||||
}
|
||||
|
||||
startOffset = endOffset;
|
||||
|
@ -1425,20 +1408,19 @@ export class WhatsappDirectLine extends GBService {
|
|||
const finalizeResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `OAuth ${userAccessToken}`
|
||||
Authorization: `OAuth ${userAccessToken}`
|
||||
}
|
||||
});
|
||||
|
||||
const finalizeData = await finalizeResponse.json();
|
||||
if (!finalizeResponse.ok) {
|
||||
throw new Error(`Finalize failed: ${finalizeData.error?.message || 'Unknown error'}`);
|
||||
throw new Error(finalizeData.error.message);
|
||||
}
|
||||
|
||||
console.log('Upload completed successfully with file handle:', finalizeData.h);
|
||||
return finalizeData.h; // Return the final handle from the response
|
||||
return h;
|
||||
} catch (error) {
|
||||
console.error('Error during file upload:', error);
|
||||
throw error; // Re-throw to allow caller to handle
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue