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 4, 2024
1 parent 25245a0 commit 0b10992
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
75 changes: 41 additions & 34 deletions packages/basic.gblib/services/DialogKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1421,15 +1421,15 @@ export class DialogKeywords {
GBLogEx.info(min, `TALK '${text} step:${step}'.`);

if (user) {

text = await min.conversationalService.translate(
min,
text,
user.locale ? user.locale : min.core.getParam(min.instance, 'Locale', GBConfigService.get('LOCALE'))
);
GBLog.verbose(`Translated text(playMarkdown): ${text}.`);

if (!isNaN(user.userSystemId)){
if (!isNaN(user.userSystemId)) {
await min.conversationalService.sendText(min, step, text, user);
} else {
await min.conversationalService['sendOnConversation'](min, user, text);
Expand All @@ -1453,41 +1453,14 @@ export class DialogKeywords {
const element = filename._page ? filename._page : filename.screenshot ? filename : null;
let url;
let nameOnly;
let localName;
const gbaiName = GBUtil.getGBAIPath(min.botId);

if (filename.endsWith('.pdf')) {
const gbdriveName = GBUtil.getGBAIPath(min.botId, 'gbdrive');
const pdf = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbdriveName, filename);

const pngs = await GBUtil.pdfPageAsImage(min, pdf, undefined);

await CollectionUtil.asyncForEach(pngs, async png => {
await GBUtil.sleep(2000);
// Prepare a cache to be referenced by Bot Framework.

url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(png.localName));

const contentType = mime.lookup(url);
const reply = { type: ActivityTypes.Message, text: caption };
reply['attachments'] = [];
reply['attachments'].push({
name: nameOnly,
contentType: contentType,
contentUrl: url
});

if (channel === 'omnichannel' || channel === 'whatsapp' || !user) {
await min.whatsAppDirectLine.sendFileToDevice(mobile, url, filename, caption, undefined, true);
} else {
await min.conversationalService['sendOnConversation'](min, user, reply);
}
});
}

// Web automation.

if (element) {
const localName = path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
localName = path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
nameOnly = path.basename(localName);
await element.screenshot({ path: localName, fullPage: true });

Expand Down Expand Up @@ -1539,16 +1512,50 @@ export class DialogKeywords {
const driveUrl = template['@microsoft.graph.downloadUrl'];
const res = await fetch(driveUrl);
let buf: any = Buffer.from(await res.arrayBuffer());
let localName1 = path.join(
localName = path.join(
'work',
gbaiName,
'cache',
`${fileOnly.replace(/\s/gi, '')}-${GBAdminService.getNumberIdentifier()}.${ext}`
);
await fs.writeFile(localName1, buf, { encoding: null });
await fs.writeFile(localName, buf, { encoding: null });

url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
}
else {
const gbdriveName = GBUtil.getGBAIPath(min.botId, 'gbdrive');
localName = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbdriveName, filename);
}

if (localName.endsWith('.pdf')) {

url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName1));
const pngs = await GBUtil.pdfPageAsImage(min, localName, undefined);

await CollectionUtil.asyncForEach(pngs, async png => {
await GBUtil.sleepRandom();
// Prepare a cache to be referenced by Bot Framework.

url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(png.localName));

const contentType = mime.lookup(url);
const reply = { type: ActivityTypes.Message, text: caption };
reply['attachments'] = [];
reply['attachments'].push({
name: nameOnly,
contentType: contentType,
contentUrl: url
});

if (channel === 'omnichannel' || channel === 'whatsapp' || !user) {
await min.whatsAppDirectLine.sendFileToDevice(mobile, url, filename, caption, undefined, true);
} else {
await min.conversationalService['sendOnConversation'](min, user, reply);
}
});

return;
}

}

if (!url) {
Expand Down
6 changes: 5 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,9 @@ export class GBUtil {

return generatedFiles.length > 0 ? generatedFiles : null;
}


public static async sleepRandom(min = 1, max = 5) {
const randomDelay = Math.floor(Math.random() * (max - min + 1) + min) * 1000;
await new Promise(resolve => setTimeout(resolve, randomDelay));
}
}

0 comments on commit 0b10992

Please sign in to comment.