Skip to content

Commit

Permalink
fix(basic.gblib): #227 - HEAR AS FILE defining where to save, per bot.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Feb 3, 2023
1 parent bc5c1b0 commit 473cd98
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
13 changes: 9 additions & 4 deletions packages/basic.gblib/services/DialogKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import sgMail from '@sendgrid/mail';
import mammoth from 'mammoth';
import qrcode from 'qrcode';
import { json } from 'body-parser';
import { WebAutomationKeywords } from './WebAutomationKeywords.js';

/**
* Default check interval for user replay
Expand Down Expand Up @@ -743,6 +744,10 @@ export class DialogKeywords {
// return await beginDialog('/t',{ to: to });
}

public static getFileByHandle (hash) {
return GBServer.globals.files[hash];
}

/**
* Hears something from user and put it in a variable
*
Expand Down Expand Up @@ -795,7 +800,6 @@ export class DialogKeywords {
// ],
// 'Please select a product'
// );

// let i = 0;
// await CollectionUtil.asyncForEach(args, async arg => {
// i++;
Expand All @@ -818,7 +822,7 @@ export class DialogKeywords {
setTimeout(resolve, ms);
});
};
min.cbMap[userId] = {};
min.cbMap[userId] = {};
min.cbMap[userId]['promise'] = '!GBHEAR';

while (min.cbMap[userId].promise === '!GBHEAR') {
Expand All @@ -829,8 +833,9 @@ export class DialogKeywords {

if (kind === 'file') {
GBLog.info(`BASIC (${min.botId}): Upload done for ${answer.filename}.`);
// TODO: answer.filename, answer.data.

const handle = WebAutomationKeywords.cyrb53(this.min.botId + answer.filename);
GBServer.globals.files[handle] = answer;
result = handle;
} else if (kind === 'boolean') {
if (isIntentYes('pt-BR', answer)) {
result = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/basic.gblib/services/WebAutomationKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class WebAutomationKeywords {

pageMap = {};

cyrb53 = (str, seed = 0) => {
public static cyrb53 = (str, seed = 0) => {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
Expand Down Expand Up @@ -131,7 +131,7 @@ export class WebAutomationKeywords {
}
await page.goto(url);

const handle = this.cyrb53(this.min.botId + url);
const handle = WebAutomationKeywords.cyrb53(this.min.botId + url);

this.pageMap[handle] = page;

Expand Down
31 changes: 21 additions & 10 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,10 +1060,13 @@
await step.continueDialog();
}
}


/**
* Private handler which receives the Attachment and persists to disk.
* during a HEAR attachment AS FILE upload.
*/
private static async downloadAttachmentAndWrite(attachment) {
const url = attachment.contentUrl;
// TODO: https://github.com/GeneralBots/BotServer/issues/195 - '${botId}','uploads');
const localFolder = Path.join('work');
const localFileName = Path.join(localFolder, `${this['min'].botId}.gbai`, 'uploads', attachment.name);

Expand Down Expand Up @@ -1156,7 +1159,7 @@
}

// Prepare Promises to download each attachment and then execute each Promise.

const promises = step.context.activity.attachments.map(
GBMinService.downloadAttachmentAndWrite.bind({ min, user, params })
);
Expand All @@ -1170,18 +1173,26 @@
await this.sendActivity('Error uploading file. Please,start again.');
}
}
// Prepare Promises to reply to the user with information about saved attachments.
// The current TurnContext is bound so `replyForReceivedAttachments` can also send replies.
const replyPromises = successfulSaves.map(replyForReceivedAttachments.bind(step.context));
await Promise.all(replyPromises);
if (successfulSaves.length > 0) {
const result = {
data: Fs.readFileSync(successfulSaves[0]['localPath']),
filename: successfulSaves[0]['fileName']
};

class GBFile {data:Buffer; filename: string};

const results = successfulSaves.reduce((accum:GBFile[], item)=>{
const result: GBFile = {
data: Fs.readFileSync(successfulSaves[0]['localPath']),
filename: successfulSaves[0]['fileName']
};
accum.push(result);
}, []) as GBFile[];

if (min.cbMap[userId] && min.cbMap[userId].promise == '!GBHEAR') {
min.cbMap[userId].promise = result;
if (results.length>1)
{
throw new Error('It is only possible to upload one file per message, right now.');
}
min.cbMap[userId].promise = results[0];
}
}

Expand Down
1 change: 1 addition & 0 deletions src/RootData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { GBMinService } from '../packages/core.gbapp/services/GBMinService.js';

export class RootData {
public processes: {}; // List of .gbdialog active executions.
public files: {}; // List of uploaded files handled.
public publicAddress: string; // URI for BotServer.
public server: any; // Express reference.
public sysPackages: any[]; // Loaded system package list.
Expand Down

0 comments on commit 473cd98

Please sign in to comment.