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('.png') ||
|
||||||
text.toLowerCase().endsWith('.mp4') ||
|
text.toLowerCase().endsWith('.mp4') ||
|
||||||
text.toLowerCase().endsWith('.mov');
|
text.toLowerCase().endsWith('.mov');
|
||||||
|
let mediaFile = !isMedia ? /(.*)\n/gim.exec(text)[0].trim() : text;
|
||||||
let text1= /(.*)\n/gim.exec(text);
|
|
||||||
|
|
||||||
let mediaFile = !isMedia ? (text1? text1[0].trim() : text):text;
|
|
||||||
let mediaType = mediaFile.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mov') ? 'video' : 'image';
|
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 folder = mediaType === 'video' ? 'videos' : 'images';
|
||||||
const gbaiName = GBUtil.getGBAIPath(min.botId);
|
const gbaiName = GBUtil.getGBAIPath(min.botId);
|
||||||
const fileUrl = urlJoin(process.env.BOT_URL, 'kb', gbaiName, `${min.botId}.gbkb`, folder, mediaFile);
|
const fileUrl = urlJoin(process.env.BOT_URL, 'kb', gbaiName, `${min.botId}.gbkb`, folder, mediaFile);
|
||||||
|
@ -669,9 +665,18 @@ export class GBConversationalService {
|
||||||
|
|
||||||
let data: any = {
|
let data: any = {
|
||||||
name: template,
|
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);
|
await this.sendToMobile(min, mobile, data, null);
|
||||||
GBLogEx.info(min, `Sending answer file to mobile: ${mobile}. Header: ${urlMedia}`);
|
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
|
// Function to create or update a template using WhatsApp Business API
|
||||||
|
|
||||||
public async createOrUpdateTemplate(min: GBMinInstance, template, text) {
|
public async createOrUpdateTemplate(min: GBMinInstance, template, text) {
|
||||||
template = template.replace('.docx', '');
|
|
||||||
template = template.replace(/\-/gi, '_');
|
template = template.replace(/\-/gi, '_');
|
||||||
template = template.replace(/\./gi, '_');
|
template = template.replace(/\./gi, '_');
|
||||||
|
|
||||||
|
|
||||||
// Determine if media is image or video
|
// Determine if media is image or video
|
||||||
let isMedia =
|
let isMedia =
|
||||||
text.toLowerCase().endsWith('.jpg') ||
|
text.toLowerCase().endsWith('.jpg') ||
|
||||||
|
@ -863,17 +861,11 @@ export class WhatsappDirectLine extends GBService {
|
||||||
let data: any = {
|
let data: any = {
|
||||||
name: template,
|
name: template,
|
||||||
components: [
|
components: [
|
||||||
// {
|
{
|
||||||
// type: 'HEADER',
|
type: 'HEADER',
|
||||||
// format: 'TEXT',
|
format: mediaType.toUpperCase(), // Use IMAGE or VIDEO format
|
||||||
// text: 'General Bots',
|
example: { header_handle: [handleMedia] }
|
||||||
// example: {
|
},
|
||||||
// header_text: [
|
|
||||||
// "General Bots"
|
|
||||||
// ]
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
|
|
||||||
{
|
{
|
||||||
type: 'BODY',
|
type: 'BODY',
|
||||||
text: text
|
text: text
|
||||||
|
@ -1388,26 +1380,17 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
while (startOffset < fileSize) {
|
while (startOffset < fileSize) {
|
||||||
const endOffset = Math.min(startOffset + CHUNK_SIZE, fileSize);
|
const endOffset = Math.min(startOffset + CHUNK_SIZE, fileSize);
|
||||||
|
const fileStream = createReadStream(filePath, { start: startOffset, end: endOffset - 1 });
|
||||||
const chunkSize = endOffset - startOffset;
|
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}`, {
|
const uploadResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `OAuth ${userAccessToken}`,
|
Authorization: `OAuth ${userAccessToken}`,
|
||||||
'file_offset': startOffset.toString(),
|
file_offset: startOffset.toString(),
|
||||||
'Content-Type': 'application/octet-stream',
|
'Content-Length': chunkSize.toString()
|
||||||
'Content-Length': bytesRead.toString()
|
|
||||||
},
|
},
|
||||||
body: chunk
|
body: fileStream
|
||||||
});
|
});
|
||||||
|
|
||||||
const uploadData = await uploadResponse.json();
|
const uploadData = await uploadResponse.json();
|
||||||
|
@ -1415,7 +1398,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
h = uploadData.h;
|
h = uploadData.h;
|
||||||
}
|
}
|
||||||
if (!uploadResponse.ok) {
|
if (!uploadResponse.ok) {
|
||||||
throw new Error(`Upload failed: ${uploadData.error?.message || 'Unknown error'}`);
|
throw new Error(uploadData.error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
startOffset = endOffset;
|
startOffset = endOffset;
|
||||||
|
@ -1425,20 +1408,19 @@ export class WhatsappDirectLine extends GBService {
|
||||||
const finalizeResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
|
const finalizeResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `OAuth ${userAccessToken}`
|
Authorization: `OAuth ${userAccessToken}`
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const finalizeData = await finalizeResponse.json();
|
const finalizeData = await finalizeResponse.json();
|
||||||
if (!finalizeResponse.ok) {
|
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);
|
console.log('Upload completed successfully with file handle:', finalizeData.h);
|
||||||
return finalizeData.h; // Return the final handle from the response
|
return h;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error during file upload:', 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