Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Add support for alternative query results and ending conversations (b…
Browse files Browse the repository at this point in the history
…oth v2 only)

Change-Id: I9c1947da5dfdb6219232bcdd265ac2bb6c3c356e
  • Loading branch information
Matt Carroll committed Jul 16, 2018
1 parent d8396fa commit da41fcb
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/V1Agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
Class representing a v1 Dialogflow agent

**Kind**: global class

* [V1Agent](#V1Agent)
* [new V1Agent(agent)](#new_V1Agent_new)
* [.end_()](#V1Agent+end_)

<a name="new_V1Agent_new"></a>

### new V1Agent(agent)
Expand All @@ -15,3 +20,10 @@ To be used in with WebhookClient class
| --- | --- | --- |
| agent | <code>Object</code> | instance of WebhookClient class |

<a name="V1Agent+end_"></a>

### v1Agent.end_()
Add a response or list of responses to be sent to Dialogflow and end the conversation
Note: not support on v1

**Kind**: instance method of [<code>V1Agent</code>](#V1Agent)
17 changes: 17 additions & 0 deletions docs/V2Agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
Class representing a v2 Dialogflow agent

**Kind**: global class

* [V2Agent](#V2Agent)
* [new V2Agent(agent)](#new_V2Agent_new)
* [.end_(responses)](#V2Agent+end_)

<a name="new_V2Agent_new"></a>

### new V2Agent(agent)
Expand All @@ -15,3 +20,15 @@ To be used in with WebhookClient class
| --- | --- | --- |
| agent | <code>Object</code> | instance of WebhookClient class |

<a name="V2Agent+end_"></a>

### v2Agent.end_(responses)
Add a response or list of responses to be sent to Dialogflow and end the conversation
Note: Only supported on Dialogflow v2's telephony gateway, Google Assistant and Alexa integrations

**Kind**: instance method of [<code>V2Agent</code>](#V2Agent)

| Param | Type | Description |
| --- | --- | --- |
| responses | [<code>RichResponse</code>](#RichResponse) \| <code>string</code> \| [<code>Array.&lt;RichResponse&gt;</code>](#RichResponse) \| <code>Array.&lt;string&gt;</code> | (list) or single responses |

23 changes: 23 additions & 0 deletions docs/WebhookClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Dialogflow's simulator
* [.locale](#WebhookClient+locale) : <code>string</code>
* [.session](#WebhookClient+session) : <code>string</code>
* [.consoleMessages](#WebhookClient+consoleMessages) : [<code>Array.&lt;RichResponse&gt;</code>](#RichResponse)
* [.alternativeQueryResults](#WebhookClient+alternativeQueryResults) : <code>object</code>
* [.add(responses)](#WebhookClient+add)
* [.end(responses)](#WebhookClient+end)
* [.addResponse_(response)](#WebhookClient+addResponse_)
* [.handleRequest(handler)](#WebhookClient+handleRequest) ⇒ <code>Promise</code>
* [.setContext(context)](#WebhookClient+setContext)[<code>WebhookClient</code>](#WebhookClient)
Expand Down Expand Up @@ -117,6 +119,15 @@ https://dialogflow.com/docs/reference/api-v2/rest/v2beta1/WebhookRequest#FIELDS.
List of messages defined in Dialogflow's console for the matched intent
https://dialogflow.com/docs/rich-messages

**Kind**: instance property of [<code>WebhookClient</code>](#WebhookClient)
<a name="WebhookClient+alternativeQueryResults"></a>

### webhookClient.alternativeQueryResults : <code>object</code>
List of alternative query results
Query results can be from other Dialogflow intents or Knowledge Connectors
https://cloud.google.com/dialogflow-enterprise/alpha/docs/knowledge-connectors
Note:this feature only availbe in Dialogflow v2

**Kind**: instance property of [<code>WebhookClient</code>](#WebhookClient)
<a name="WebhookClient+add"></a>

Expand All @@ -129,6 +140,18 @@ Add a response or list of responses to be sent to Dialogflow
| --- | --- | --- |
| responses | [<code>RichResponse</code>](#RichResponse) \| <code>string</code> \| [<code>Array.&lt;RichResponse&gt;</code>](#RichResponse) \| <code>Array.&lt;string&gt;</code> | (list) or single responses |

<a name="WebhookClient+end"></a>

### webhookClient.end(responses)
Add a response or list of responses to be sent to Dialogflow and end the conversation
Note: Only supported on Dialogflow v2's telephony gateway, Google Assistant and Alexa integrations

**Kind**: instance method of [<code>WebhookClient</code>](#WebhookClient)

| Param | Type | Description |
| --- | --- | --- |
| responses | [<code>RichResponse</code>](#RichResponse) \| <code>string</code> \| [<code>Array.&lt;RichResponse&gt;</code>](#RichResponse) \| <code>Array.&lt;string&gt;</code> | (list) or single responses |

<a name="WebhookClient+addResponse_"></a>

### webhookClient.addResponse_(response)
Expand Down
30 changes: 30 additions & 0 deletions src/dialogflow-fulfillment.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class WebhookClient {
*/
this.followupEvent_ = null;


/**
* Boolean indicating whether the conversation should continue after the Dialogflow agent's response
*
* @private
* @type Boolean
*/
this.endConversation_ = false;

/**
* List of outgoing contexts defined by the developer
*
Expand Down Expand Up @@ -168,6 +177,7 @@ class WebhookClient {
* Dialogflow input contexts included in the request or null if no value
* Dialogflow v2 API only
* https://dialogflow.com/docs/reference/api-v2/rest/v2beta1/WebhookRequest#FIELDS.session
*
* @type {string}
*/
this.session = null;
Expand All @@ -180,6 +190,16 @@ class WebhookClient {
*/
this.consoleMessages = [];

/**
* List of alternative query results
* Query results can be from other Dialogflow intents or Knowledge Connectors
* https://cloud.google.com/dialogflow-enterprise/alpha/docs/knowledge-connectors
* Note:this feature only availbe in Dialogflow v2
*
* @type {object}
*/
this.alternativeQueryResults = null;

/**
* Platform contants, to define platforms, includes supported platforms and unspecified
* @example
Expand Down Expand Up @@ -226,6 +246,16 @@ class WebhookClient {
}
}

/**
* Add a response or list of responses to be sent to Dialogflow and end the conversation
* Note: Only supported on Dialogflow v2's telephony gateway, Google Assistant and Alexa integrations
*
* @param {RichResponse|string|RichResponse[]|string[]} responses (list) or single responses
*/
end(responses) {
this.client.end_(responses);
}

/**
* Add a response to be sent to Dialogflow
*
Expand Down
8 changes: 8 additions & 0 deletions src/v1-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ class V1Agent {
this.agent.followupEvent_ = eventJson;
}

/**
* Add a response or list of responses to be sent to Dialogflow and end the conversation
* Note: not support on v1
*/
end_() {
throw new Error('"end" method is not supported on Dialogflow API V1. Please migrate to Dialogflow API V2.');
}

/**
* Add an v1 Actions on Google response
*
Expand Down
31 changes: 30 additions & 1 deletion src/v2-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ class V2Agent {
this.agent.consoleMessages = [];
}
debug(`Console messages: ${JSON.stringify(this.agent.consoleMessages)}`);

/**
* Alternative query results that have a high match score
* Query results can be from other Dialogflow intents or Knowledge Connectors
* https://cloud.google.com/dialogflow-enterprise/alpha/docs/knowledge-connectors
* Note:this feature is only available in Dialogflow API V2
*
* @type {object}
*/
if (this.agent.request_.body.alternativeQueryResults) {
this.agent.alternativeQueryResults = this.agent.request_.body.alternativeQueryResults;
}
debug(`Alternative query result: ${JSON.stringify(this.agent.alternativeQueryResults)}`);
}

/**
Expand Down Expand Up @@ -202,7 +215,12 @@ class V2Agent {
*/
sendJson_(responseJson) {
responseJson.outputContexts = this.agent.outgoingContexts_;
this.agent.followupEvent_ ? responseJson.followupEventInput = this.agent.followupEvent_ : undefined;
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));
this.agent.response_.json(responseJson);
Expand Down Expand Up @@ -274,6 +292,17 @@ class V2Agent {
this.agent.followupEvent_ = event;
}

/**
* Add a response or list of responses to be sent to Dialogflow and end the conversation
* Note: Only supported on Dialogflow v2's telephony gateway, Google Assistant and Alexa integrations
*
* @param {RichResponse|string|RichResponse[]|string[]} responses (list) or single responses
*/
end_(responses) {
this.agent.endConversation_ = true;
this.agent.add(responses);
}

/**
* Add an v2 Actions on Google response
*
Expand Down
17 changes: 17 additions & 0 deletions test/webhook-v1-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ test('Test v1 no handler defined', async (t) => {
);
});

test('Test v2 end conversation', async (t) => {
let response = new ResponseMock();
let agent = new WebhookClient({
request: {body: mockSlackV1Request},
response: response,
});

const noHandlerDefinedError = await t.throws(() => {
agent.end('this will never be sent');
});

t.is(
noHandlerDefinedError.message,
'"end" method is not supported on Dialogflow API V1. Please migrate to Dialogflow API V2.'
);
});

/**
* utility function to setup webhook test
* @param {Object} request express object
Expand Down
52 changes: 51 additions & 1 deletion test/webhook-v2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ test('Test v2 consoleMessages', async (t) => {
t.is(actionsOnGoogleText2.text, 'another Actions on Google simple response');
});

test('Test v1 original request', async (t) => {
test('Test v2 original request', async (t) => {
let response = new ResponseMock();

let googleRequest = {body: mockGoogleV2Request};
Expand Down Expand Up @@ -479,6 +479,34 @@ test('Test v2 no handler defined', async (t) => {
);
});

test('Test v2 alternative query results', async (t) => {
// Request with Knowledge connector response
let request = {body: mockV2KnowledgeConnector};
let agent = new WebhookClient({
request: request,
response: new ResponseMock(),
});

t.deepEqual(mockV2KnowledgeConnector.alternativeQueryResults,
agent.alternativeQueryResults
);
});

test('Test v2 end conversation', async (t) => {
// Request with Knowledge connector response
let request = {body: mockV2KnowledgeConnector};

webhookTest(
request,
(agent) => {
agent.end('thanks for talking to me!');
},
(responseJson) => {
t.deepEqual(responseJson.triggerEndOfConversation, true);
}
);
});

/**
* Class to mock a express response object for testing
*/
Expand Down Expand Up @@ -1340,3 +1368,25 @@ const mockV2MulipleConsoleMessages = {
'languageCode': 'en',
},
};

const mockV2KnowledgeConnector = {
'responseId': 'cc3ec71d-7526-43f7-9381-128f60a5f44f',
'queryResult': {
'queryText': 'every rich response',
'parameters': {},
'allRequiredParamsPresent': true,
'fulfillmentMessages': [],
'intent': {
'name': 'projects/anotheragent-c5ea8/agent/intents/96f2305b-1cd0-4d73-97c0-3cfe669ec79b',
'displayName': 'every rich response',
},
'intentDetectionConfidence': 1,
'diagnosticInfo': {},
'languageCode': 'en',
},
'alternativeQueryResults': [
{
'queryText': 'knowledge connector text',
},
],
};

0 comments on commit da41fcb

Please sign in to comment.