Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/be-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const V2Agent = require('./v2-agent');
const debug = require('debug')('dialogflow:debug');

class BEAgent extends V2Agent {
sendResponses_(requestSource) {
let responseJson = this.responseJson_;
if (!responseJson) {
throw new Error(`No responses defined for platform: ${requestSource}`);
}

responseJson.outputContexts = this.agent.context.getV2OutputContextsArray();
if (this.agent.followupEvent_) {
responseJson.followupEventInput = this.agent.followupEvent_;
}
if (this.agent.endConversation_) {
responseJson.triggerEndOfConversation = this.agent.endConversation_;
}

debug('Response to Dialogflow: ' + JSON.stringify(responseJson));
return responseJson;
}
}

module.exports = BEAgent;
16 changes: 3 additions & 13 deletions src/dialogflow-fulfillment.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const {
SUPPORTED_RICH_MESSAGE_PLATFORMS,
} = require('./rich-responses/rich-response');
const V1Agent = require('./v1-agent');
const V2Agent = require('./v2-agent');
const BEAgent = require('./be-agent');

const RESPONSE_CODE_BAD_REQUEST = 400;

Expand All @@ -58,9 +58,6 @@ class WebhookClient {
if (!options.request) {
throw new Error('Request can NOT be empty.');
}
if (!options.response) {
throw new Error('Response can NOT be empty.');
}

/**
* The Express HTTP request that the endpoint receives from the Assistant.
Expand All @@ -69,13 +66,6 @@ class WebhookClient {
*/
this.request_ = options.request;

/**
* The Express HTTP response the endpoint will return to Assistant.
* @private
* @type {Object}
*/
this.response_ = options.response;

/**
* The agent version (v1 or v2) based on Dialogflow webhook request
* https://dialogflow.com/docs/reference/v2-comparison
Expand Down Expand Up @@ -225,7 +215,7 @@ class WebhookClient {
}

if (this.agentVersion === 2) {
this.client = new V2Agent(this);
this.client = new BEAgent(this);
} else if (this.agentVersion === 1) {
this.client = new V1Agent(this);
} else {
Expand Down Expand Up @@ -502,7 +492,7 @@ class WebhookClient {
if (payload) {
this.client.addPayloadResponse_(payload, requestSource);
}
this.client.sendResponses_(requestSource);
return this.client.sendResponses_(requestSource);
}

/**
Expand Down