From 12d6e1b1e90e1d793a136bfc51f11bf50d1c907f Mon Sep 17 00:00:00 2001 From: dakavisa Date: Thu, 28 May 2020 16:49:21 -0600 Subject: [PATCH] Added webhook pre-script functionality --- api/lib/models/webhook.model.js | 1 + ...erse-generate-response-fallback.service.js | 15 ++++++++ ...verse-generate-response-welcome.service.js | 15 ++++++++ ...gent.converse-generate-response.service.js | 15 ++++++++ .../agent/agent.test.train.service.js | 5 ++- api/lib/validators/agent.validator.js | 10 ++++-- .../redis/models/webhook.redis-model.js | 3 ++ ui/app/components/WebhookSettings/index.js | 36 +++++++++++++++++++ ui/app/components/WebhookSettings/messages.js | 4 +++ ui/app/containers/App/reducer.js | 2 ++ 10 files changed, 103 insertions(+), 3 deletions(-) diff --git a/api/lib/models/webhook.model.js b/api/lib/models/webhook.model.js index d4eab8564..517adb91b 100644 --- a/api/lib/models/webhook.model.js +++ b/api/lib/models/webhook.model.js @@ -23,6 +23,7 @@ class WebhookModel { webhookUser: Joi.string().allow(''), webhookPassword: Joi.string().allow(''), postScript: Joi.string().trim().allow(''), + preScript: Joi.string().trim().allow(''), creationDate: Joi .string(), modificationDate: Joi diff --git a/api/lib/services/agent/agent.converse-generate-response-fallback.service.js b/api/lib/services/agent/agent.converse-generate-response-fallback.service.js index 9459f44dd..20080d174 100644 --- a/api/lib/services/agent/agent.converse-generate-response-fallback.service.js +++ b/api/lib/services/agent/agent.converse-generate-response-fallback.service.js @@ -39,6 +39,21 @@ module.exports = async function ({ CSO }) { ]; webhook = await globalService.findInModelPath({ modelPath, isFindById: false, isSingleResult: true, skip: 0, limit: -1 }); } + if (webhook.preScript) { + const vm = new VM({ + timeout: 1000, + sandbox: { + CSO + } + }); + + const script = new VMScript(webhook.preScript); + try { + CSO = vm.run(script); + } catch (err) { + console.error(`Failed to execute preScript of the webhook ${webhook.webhookKey}`, err); + } + } const webhookResponse = await agentService.converseCallWebhook({ url: webhook.webhookUrl, templatePayload: webhook.webhookPayload, diff --git a/api/lib/services/agent/agent.converse-generate-response-welcome.service.js b/api/lib/services/agent/agent.converse-generate-response-welcome.service.js index ee1fb03f8..338008acc 100644 --- a/api/lib/services/agent/agent.converse-generate-response-welcome.service.js +++ b/api/lib/services/agent/agent.converse-generate-response-welcome.service.js @@ -39,6 +39,21 @@ module.exports = async function ({ CSO }) { ]; webhook = await globalService.findInModelPath({ modelPath, isFindById: false, isSingleResult: true, skip: 0, limit: -1 }); } + if (webhook.preScript) { + const vm = new VM({ + timeout: 1000, + sandbox: { + CSO + } + }); + + const script = new VMScript(webhook.preScript); + try { + CSO = vm.run(script); + } catch (err) { + console.error(`Failed to execute preScript of the webhook ${webhook.webhookKey}`, err); + } + } const webhookResponse = await agentService.converseCallWebhook({ url: webhook.webhookUrl, templatePayload: webhook.webhookPayload, diff --git a/api/lib/services/agent/agent.converse-generate-response.service.js b/api/lib/services/agent/agent.converse-generate-response.service.js index 7b5f1151b..46e73a03e 100644 --- a/api/lib/services/agent/agent.converse-generate-response.service.js +++ b/api/lib/services/agent/agent.converse-generate-response.service.js @@ -96,6 +96,21 @@ module.exports = async function ({ actionData, CSO }) { ]; webhook = await globalService.findInModelPath({ modelPath, isFindById: false, isSingleResult: true }); } + if (webhook.preScript) { + const vm = new VM({ + timeout: 1000, + sandbox: { + CSO + } + }); + + const script = new VMScript(webhook.preScript); + try { + CSO = vm.run(script); + } catch (err) { + console.error(`Failed to execute preScript of the webhook ${webhook.webhookKey}`, err); + } + } const webhookResponse = await agentService.converseCallWebhook({ url: webhook.webhookUrl, templatePayload: webhook.webhookPayload, diff --git a/api/lib/services/agent/agent.test.train.service.js b/api/lib/services/agent/agent.test.train.service.js index 209dd0ea0..e6679f3b1 100644 --- a/api/lib/services/agent/agent.test.train.service.js +++ b/api/lib/services/agent/agent.test.train.service.js @@ -84,6 +84,9 @@ const upsertResultAction = function (result, sayingAction, recognizedAction, say condition = 'good'; } + if (recognizedAction.indexOf('+__+') !== -1 || sayingAction.indexOf('+__+') !== -1) { + const a = 1; + } let actionIndex = result.actions.findIndex(action => { return action.actionName === sayingAction }) if (actionIndex === -1) { result.actions.push({ @@ -117,7 +120,7 @@ const upsertResultKeywords = function (result, sayingKeywords, recognizedKeyword recognizedKeyword.keyword == sayingKeyword.keyword }); let condition = 'bad'; - if (recognizedKeywordsIndex !== -1) { + if (recognizedKeywordsIndex !== -1 && recognizedKeywords.length !== 0) { condition = 'good'; } else { errorPresent = true; diff --git a/api/lib/validators/agent.validator.js b/api/lib/validators/agent.validator.js index 94a5dc67e..8a356e9df 100644 --- a/api/lib/validators/agent.validator.js +++ b/api/lib/validators/agent.validator.js @@ -352,7 +352,8 @@ class AgentValidate { }), webhookUser: WebhookSchema.webhookUser, webhookPassword: WebhookSchema.webhookPassword, - postScript: WebhookSchema.postScript + postScript: WebhookSchema.postScript, + preScript: WebhookSchema.preScript }; })() }; @@ -379,6 +380,7 @@ class AgentValidate { webhookUser: WebhookSchema.webhookUser, webhookPassword: WebhookSchema.webhookPassword, postScript: WebhookSchema.postScript, + preScript: WebhookSchema.preScript, creationDate: WebhookSchema.creationDate, modificationDate: WebhookSchema.modificationDate }; @@ -407,7 +409,8 @@ class AgentValidate { }), webhookUser: WebhookSchema.webhookUser, webhookPassword: WebhookSchema.webhookPassword, - postScript: WebhookSchema.postScript + postScript: WebhookSchema.postScript, + preScript: WebhookSchema.preScript }; })() }; @@ -435,6 +438,7 @@ class AgentValidate { webhookUser: WebhookSchema.webhookUser, webhookPassword: WebhookSchema.webhookPassword, postScript: WebhookSchema.postScript, + preScript: WebhookSchema.preScript, creationDate: WebhookSchema.creationDate, modificationDate: WebhookSchema.modificationDate }; @@ -973,6 +977,7 @@ class AgentValidate { webhookUser: WebhookSchema.webhookUser, webhookPassword: WebhookSchema.webhookPassword, postScript: WebhookSchema.postScript, + preScript: WebhookSchema.preScript, creationDate: WebhookSchema.creationDate, modificationDate: WebhookSchema.modificationDate }, @@ -1059,6 +1064,7 @@ class AgentValidate { webhookUser: WebhookSchema.webhookUser, webhookPassword: WebhookSchema.webhookPassword, postScript: WebhookSchema.postScript, + preScript: WebhookSchema.preScript, creationDate: ActionSchema.creationDate, modificationDate: ActionSchema.modificationDate }, diff --git a/api/server/plugins/redis/models/webhook.redis-model.js b/api/server/plugins/redis/models/webhook.redis-model.js index 8298492b3..76377219f 100644 --- a/api/server/plugins/redis/models/webhook.redis-model.js +++ b/api/server/plugins/redis/models/webhook.redis-model.js @@ -29,6 +29,9 @@ const schema = { postScript: { type: 'string' }, + preScript: { + type: 'string' + }, creationDate: { type: 'timestamp' }, diff --git a/ui/app/components/WebhookSettings/index.js b/ui/app/components/WebhookSettings/index.js index e97d221ed..2ef16470b 100644 --- a/ui/app/components/WebhookSettings/index.js +++ b/ui/app/components/WebhookSettings/index.js @@ -381,6 +381,42 @@ export class WebhookSettings extends React.Component { ] : null} + + + + + + + + {this.props.errorState.postScript ? ( + + + + ) : null} + diff --git a/ui/app/components/WebhookSettings/messages.js b/ui/app/components/WebhookSettings/messages.js index 2730e78ea..3fa99d415 100644 --- a/ui/app/components/WebhookSettings/messages.js +++ b/ui/app/components/WebhookSettings/messages.js @@ -103,4 +103,8 @@ export default defineMessages({ id: 'app.components.WebhookSettings.postScript', defaultMessage: 'Postscript', }, + preScript: { + id: 'app.components.WebhookSettings.preScript', + defaultMessage: 'Prescript', + }, }); diff --git a/ui/app/containers/App/reducer.js b/ui/app/containers/App/reducer.js index 8da061777..825173539 100644 --- a/ui/app/containers/App/reducer.js +++ b/ui/app/containers/App/reducer.js @@ -586,6 +586,7 @@ const initialState = Immutable({ webhookPayloadType: 'None', webhookPayload: '', postScript: '', + preScript: '', webhookHeaders: [], webhookUser: '', webhookPassword: '', @@ -660,6 +661,7 @@ const initialState = Immutable({ webhookPayloadType: 'None', webhookPayload: '', postScript: '', + preScript: '', webhookHeaders: [], webhookUser: '', webhookPassword: '',