new(basic.gblib): PUT keyword.
This commit is contained in:
parent
29b1975dab
commit
d3769e5bf3
5 changed files with 101 additions and 40 deletions
|
@ -52,11 +52,10 @@ export class HearDialog {
|
||||||
|
|
||||||
private static async downloadAttachmentAndWrite(attachment) {
|
private static async downloadAttachmentAndWrite(attachment) {
|
||||||
|
|
||||||
// Retrieve the attachment via the attachment's contentUrl.
|
|
||||||
const url = attachment.contentUrl;
|
|
||||||
|
|
||||||
// Local file path for the bot to save the attachment.
|
const url = attachment.contentUrl;
|
||||||
const localFileName = Path.join(__dirname, attachment.name);
|
const localFolder = Path.join('work', 'dev-rodriguez.gbai', 'uploads');
|
||||||
|
const localFileName = Path.join(localFolder, attachment.name);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// arraybuffer is necessary for images
|
// arraybuffer is necessary for images
|
||||||
|
@ -135,17 +134,17 @@ export class HearDialog {
|
||||||
if (step.activeDialog.state.options['kind'] === "file") {
|
if (step.activeDialog.state.options['kind'] === "file") {
|
||||||
|
|
||||||
// Prepare Promises to download each attachment and then execute each Promise.
|
// Prepare Promises to download each attachment and then execute each Promise.
|
||||||
const promises = step.context.activity.attachments.map(HearDialog.downloadAttachmentAndWrite);
|
const promises = step.context.activity.attachments.map(
|
||||||
|
HearDialog.downloadAttachmentAndWrite);
|
||||||
const successfulSaves = await Promise.all(promises);
|
const successfulSaves = await Promise.all(promises);
|
||||||
|
|
||||||
async function replyForReceivedAttachments(localAttachmentData) {
|
async function replyForReceivedAttachments(localAttachmentData) {
|
||||||
if (localAttachmentData) {
|
if (localAttachmentData) {
|
||||||
// Because the TurnContext was bound to this function, the bot can call
|
// Because the TurnContext was bound to this function, the bot can call
|
||||||
// `TurnContext.sendActivity` via `this.sendActivity`;
|
// `TurnContext.sendActivity` via `this.sendActivity`;
|
||||||
await this.sendActivity(`Attachment "${localAttachmentData.fileName}" ` +
|
await this.sendActivity(`Upload OK.`);
|
||||||
`has been received and saved to "${localAttachmentData.localPath}".`);
|
|
||||||
} else {
|
} else {
|
||||||
await this.sendActivity('Attachment was not successfully saved to disk.');
|
await this.sendActivity('Error uploading file. Please, start again.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,6 +153,11 @@ export class HearDialog {
|
||||||
const replyPromises = successfulSaves.map(replyForReceivedAttachments.bind(step.context));
|
const replyPromises = successfulSaves.map(replyForReceivedAttachments.bind(step.context));
|
||||||
await Promise.all(replyPromises);
|
await Promise.all(replyPromises);
|
||||||
|
|
||||||
|
result = {
|
||||||
|
data: fs.readFileSync(successfulSaves[0].localPath),
|
||||||
|
filename: successfulSaves[0].fileName
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (step.activeDialog.state.options['kind'] === "boolean") {
|
else if (step.activeDialog.state.options['kind'] === "boolean") {
|
||||||
if (isIntentYes('pt-BR', step.result)) {
|
if (isIntentYes('pt-BR', step.result)) {
|
||||||
|
|
|
@ -407,8 +407,16 @@ export class DialogKeywords {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getCoded(text){
|
public getCoded(value){
|
||||||
return Buffer.from(text).toString("base64");
|
|
||||||
|
// Checks if it is a GB FILE object.
|
||||||
|
|
||||||
|
if (value.data && value.filename)
|
||||||
|
{
|
||||||
|
value = value.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Buffer.from(value).toString("base64");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -79,7 +79,7 @@ export class GBVMService extends GBService {
|
||||||
const vbsFile = filename.substr(0, filename.indexOf('docx')) + 'vbs';
|
const vbsFile = filename.substr(0, filename.indexOf('docx')) + 'vbs';
|
||||||
const fullVbsFile = urlJoin(folder, vbsFile);
|
const fullVbsFile = urlJoin(folder, vbsFile);
|
||||||
const docxStat = fs.statSync(urlJoin(folder, wordFile));
|
const docxStat = fs.statSync(urlJoin(folder, wordFile));
|
||||||
const interval = 30000; // If compiled is older 30 seconds, then recompile.
|
const interval = 3000; // If compiled is older 30 seconds, then recompile.
|
||||||
let writeVBS = true;
|
let writeVBS = true;
|
||||||
if (fs.existsSync(fullVbsFile)) {
|
if (fs.existsSync(fullVbsFile)) {
|
||||||
const vbsStat = fs.statSync(fullVbsFile);
|
const vbsStat = fs.statSync(fullVbsFile);
|
||||||
|
@ -324,9 +324,7 @@ export class GBVMService extends GBService {
|
||||||
});
|
});
|
||||||
|
|
||||||
code = code.replace(/(\w+)\s*\=\s*get\s(.*)/gi, ($0, $1, $2, $3) => {
|
code = code.replace(/(\w+)\s*\=\s*get\s(.*)/gi, ($0, $1, $2, $3) => {
|
||||||
if ($2.indexOf('http') !== -1) {
|
|
||||||
return `${$1} = sys().getByHttp (${$2}, headers, httpUsername, httpPs)`;
|
|
||||||
} else {
|
|
||||||
const count = ($2.match(/\,/g) || []).length;
|
const count = ($2.match(/\,/g) || []).length;
|
||||||
const values = $2.split(',');
|
const values = $2.split(',');
|
||||||
|
|
||||||
|
@ -348,11 +346,20 @@ export class GBVMService extends GBService {
|
||||||
|
|
||||||
else {
|
else {
|
||||||
|
|
||||||
return `${$1} = sys().get (${$2})`;
|
return `${$1} = sys().get (${$2}, headers)`;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
code = code.replace(/\= NEW OBJECT/gi, ($0, $1, $2, $3) => {
|
||||||
|
return ` = {}`;
|
||||||
|
});
|
||||||
|
|
||||||
|
code = code.replace(/\= NEW ARRAY/gi, ($0, $1, $2, $3) => {
|
||||||
|
return ` = []`;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
code = code.replace(/(go to)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
code = code.replace(/(go to)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
||||||
return `gotoDialog(step, ${$3})\n`;
|
return `gotoDialog(step, ${$3})\n`;
|
||||||
});
|
});
|
||||||
|
@ -401,6 +408,10 @@ export class GBVMService extends GBService {
|
||||||
return `${$1} = sys().postByHttp (${$2}, ${$3}, headers)`;
|
return `${$1} = sys().postByHttp (${$2}, ${$3}, headers)`;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
code = code.replace(/(\w+)\s*\=\s*put\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
|
||||||
|
return `${$1} = sys().putByHttp (${$2}, ${$3}, headers)`;
|
||||||
|
});
|
||||||
|
|
||||||
code = code.replace(/(\w+)\s*\=\s*download\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
|
code = code.replace(/(\w+)\s*\=\s*download\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
|
||||||
return `${$1} = sys().download (${$2}, ${$3})`;
|
return `${$1} = sys().download (${$2}, ${$3})`;
|
||||||
});
|
});
|
||||||
|
|
|
@ -196,12 +196,12 @@ export class SystemKeywords {
|
||||||
let isObject = false;
|
let isObject = false;
|
||||||
|
|
||||||
if (Array.isArray(data)) {
|
if (Array.isArray(data)) {
|
||||||
isObject = Object.keys(data[0]) !== null;
|
isObject = Object.keys(data[1]) !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isObject || JSON.parse(data) !== null) {
|
if (isObject || JSON.parse(data) !== null) {
|
||||||
|
|
||||||
let keys = Object.keys(data[0]);
|
let keys = Object.keys(data[1]);
|
||||||
|
|
||||||
|
|
||||||
if (headers) {
|
if (headers) {
|
||||||
|
@ -286,7 +286,7 @@ export class SystemKeywords {
|
||||||
// Guess fields from data variable into Tabulator fields collection.
|
// Guess fields from data variable into Tabulator fields collection.
|
||||||
|
|
||||||
let fields = [];
|
let fields = [];
|
||||||
let keys = Object.keys(data[1]);
|
let keys = Object.keys(data[0]);
|
||||||
for (let i = 0; i < keys.length; i++) {
|
for (let i = 0; i < keys.length; i++) {
|
||||||
fields.push({ field: keys[i], title: keys[i] });
|
fields.push({ field: keys[i], title: keys[i] });
|
||||||
}
|
}
|
||||||
|
@ -526,6 +526,12 @@ export class SystemKeywords {
|
||||||
const botId = this.min.instance.botId;
|
const botId = this.min.instance.botId;
|
||||||
const path = `/${botId}.gbai/${botId}.gbdata`;
|
const path = `/${botId}.gbai/${botId}.gbdata`;
|
||||||
|
|
||||||
|
// Checks if it is a GB FILE object.
|
||||||
|
|
||||||
|
if (data.data && data.filename) {
|
||||||
|
data = data.data;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await client
|
await client
|
||||||
.api(`${baseUrl}/drive/root:/${path}/${file}:/content`)
|
.api(`${baseUrl}/drive/root:/${path}/${file}:/content`)
|
||||||
|
@ -587,8 +593,14 @@ export class SystemKeywords {
|
||||||
* @example value = GET "file.xlsx", "A2"
|
* @example value = GET "file.xlsx", "A2"
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public async get(file: string, address: string): Promise<any> {
|
public async get(file: string, addressOrHeaders: string, httpUsername, httpPs, qs, streaming): Promise<any> {
|
||||||
GBLog.info(`BASIC: GET '${address}' in '${file}'.`);
|
|
||||||
|
if (file.startsWith('http')) {
|
||||||
|
|
||||||
|
return await this.getByHttp(file, addressOrHeaders, httpUsername, httpPs, qs, streaming);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
GBLog.info(`BASIC: GET '${addressOrHeaders}' in '${file}'.`);
|
||||||
let [baseUrl, client] = await GBDeployer.internalGetDriveClient(this.min);
|
let [baseUrl, client] = await GBDeployer.internalGetDriveClient(this.min);
|
||||||
const botId = this.min.instance.botId;
|
const botId = this.min.instance.botId;
|
||||||
const path = `/${botId}.gbai/${botId}.gbdata`;
|
const path = `/${botId}.gbai/${botId}.gbdata`;
|
||||||
|
@ -602,13 +614,14 @@ export class SystemKeywords {
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
let results = await client
|
let results = await client
|
||||||
.api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets('${sheets.value[0].name}')/range(address='${address}')`)
|
.api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets('${sheets.value[0].name}')/range(address='${addressOrHeaders}')`)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
let val = results.text[0][0];
|
let val = results.text[0][0];
|
||||||
GBLog.info(`BASIC: Getting '${file}' (GET). Value= ${val}.`);
|
GBLog.info(`BASIC: Getting '${file}' (GET). Value= ${val}.`);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public isValidDate(dt) {
|
public isValidDate(dt) {
|
||||||
const contentLocale = this.min.core.getParam<string>(
|
const contentLocale = this.min.core.getParam<string>(
|
||||||
|
@ -1356,11 +1369,10 @@ export class SystemKeywords {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public async getByHttp(url: string, headers: any, username: string, ps: string, qs: any, streaming = false) {
|
public async getByHttp(url: string, headers: any, username: string, ps: string, qs: any, streaming = false) {
|
||||||
let options = {
|
let options = { url: url };
|
||||||
encoding: "binary",
|
if (headers) {
|
||||||
url: url,
|
options['headers'] = headers;
|
||||||
headers: headers
|
}
|
||||||
};
|
|
||||||
if (username) {
|
if (username) {
|
||||||
options['auth'] = {
|
options['auth'] = {
|
||||||
user: username,
|
user: username,
|
||||||
|
@ -1387,6 +1399,28 @@ export class SystemKeywords {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls any REST API by using POST HTTP method.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* user = post "http://server/path", "data"
|
||||||
|
* talk "The updated user area is" + user.area
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public async putByHttp(url: string, data, headers) {
|
||||||
|
const options = {
|
||||||
|
uri: url,
|
||||||
|
json: true,
|
||||||
|
body: data,
|
||||||
|
headers: headers
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = await request.put(options);
|
||||||
|
GBLog.info(`[PUT]: ${url} (${data}): ${result}`);
|
||||||
|
return typeof (result) === 'object' ? result : JSON.parse(result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls any REST API by using POST HTTP method.
|
* Calls any REST API by using POST HTTP method.
|
||||||
*
|
*
|
||||||
|
|
|
@ -255,6 +255,10 @@ export class GBMinService {
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
mkdirp.sync(dir);
|
mkdirp.sync(dir);
|
||||||
}
|
}
|
||||||
|
dir = `work/${min.botId}.gbai/uploads`;
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
mkdirp.sync(dir);
|
||||||
|
}
|
||||||
|
|
||||||
// Loads Named Entity data for this bot.
|
// Loads Named Entity data for this bot.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue