Skip to content

Commit

Permalink
Added webhook pre-script functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dakavisa committed May 28, 2020
1 parent b7a4318 commit 12d6e1b
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 3 deletions.
1 change: 1 addition & 0 deletions api/lib/models/webhook.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions api/lib/services/agent/agent.converse-generate-response.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion api/lib/services/agent/agent.test.train.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions api/lib/validators/agent.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ class AgentValidate {
}),
webhookUser: WebhookSchema.webhookUser,
webhookPassword: WebhookSchema.webhookPassword,
postScript: WebhookSchema.postScript
postScript: WebhookSchema.postScript,
preScript: WebhookSchema.preScript
};
})()
};
Expand All @@ -379,6 +380,7 @@ class AgentValidate {
webhookUser: WebhookSchema.webhookUser,
webhookPassword: WebhookSchema.webhookPassword,
postScript: WebhookSchema.postScript,
preScript: WebhookSchema.preScript,
creationDate: WebhookSchema.creationDate,
modificationDate: WebhookSchema.modificationDate
};
Expand Down Expand Up @@ -407,7 +409,8 @@ class AgentValidate {
}),
webhookUser: WebhookSchema.webhookUser,
webhookPassword: WebhookSchema.webhookPassword,
postScript: WebhookSchema.postScript
postScript: WebhookSchema.postScript,
preScript: WebhookSchema.preScript
};
})()
};
Expand Down Expand Up @@ -435,6 +438,7 @@ class AgentValidate {
webhookUser: WebhookSchema.webhookUser,
webhookPassword: WebhookSchema.webhookPassword,
postScript: WebhookSchema.postScript,
preScript: WebhookSchema.preScript,
creationDate: WebhookSchema.creationDate,
modificationDate: WebhookSchema.modificationDate
};
Expand Down Expand Up @@ -973,6 +977,7 @@ class AgentValidate {
webhookUser: WebhookSchema.webhookUser,
webhookPassword: WebhookSchema.webhookPassword,
postScript: WebhookSchema.postScript,
preScript: WebhookSchema.preScript,
creationDate: WebhookSchema.creationDate,
modificationDate: WebhookSchema.modificationDate
},
Expand Down Expand Up @@ -1059,6 +1064,7 @@ class AgentValidate {
webhookUser: WebhookSchema.webhookUser,
webhookPassword: WebhookSchema.webhookPassword,
postScript: WebhookSchema.postScript,
preScript: WebhookSchema.preScript,
creationDate: ActionSchema.creationDate,
modificationDate: ActionSchema.modificationDate
},
Expand Down
3 changes: 3 additions & 0 deletions api/server/plugins/redis/models/webhook.redis-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const schema = {
postScript: {
type: 'string'
},
preScript: {
type: 'string'
},
creationDate: {
type: 'timestamp'
},
Expand Down
36 changes: 36 additions & 0 deletions ui/app/components/WebhookSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,42 @@ export class WebhookSettings extends React.Component {
]
: null}
</Grid>
<Grid key="grid-editor-prescript" item xs={12}>
<Grid className={classes.bodyTitleContainer} item xs={12}>
<Typography variant="h2">
<FormattedMessage {...messages.preScript} />
</Typography>
</Grid>
<AceEditor
key="preScript"
width="100%"
height="300px"
mode={'javascript'}
theme="terminal"
name="preScript"
readOnly={isReadOnly}
onLoad={this.onLoad}
onChange={this.props.onChangeWebhookData.bind(null, 'preScript')}
fontSize={14}
showPrintMargin
showGutter
highlightActiveLine
value={webhook.preScript}
editorProps={{
$blockScrolling: Infinity,
}}
setOptions={{
useWorker: false,
showLineNumbers: true,
tabSize: 2,
}}
/>
{this.props.errorState.postScript ? (
<Typography key="preScriptError" variant="caption" className={classes.errorLabel}>
<FormattedMessage {...messages.preScriptError} />
</Typography>
) : null}
</Grid>
<Grid key="grid-editor-postscript" item xs={12}>
<Grid className={classes.bodyTitleContainer} item xs={12}>
<Typography variant="h2">
Expand Down
4 changes: 4 additions & 0 deletions ui/app/components/WebhookSettings/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,8 @@ export default defineMessages({
id: 'app.components.WebhookSettings.postScript',
defaultMessage: 'Postscript',
},
preScript: {
id: 'app.components.WebhookSettings.preScript',
defaultMessage: 'Prescript',
},
});
2 changes: 2 additions & 0 deletions ui/app/containers/App/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ const initialState = Immutable({
webhookPayloadType: 'None',
webhookPayload: '',
postScript: '',
preScript: '',
webhookHeaders: [],
webhookUser: '',
webhookPassword: '',
Expand Down Expand Up @@ -660,6 +661,7 @@ const initialState = Immutable({
webhookPayloadType: 'None',
webhookPayload: '',
postScript: '',
preScript: '',
webhookHeaders: [],
webhookUser: '',
webhookPassword: '',
Expand Down

0 comments on commit 12d6e1b

Please sign in to comment.