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

Add fulfillmentText to json when requestSource is null and a text message is found in responseMessages #337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
23 changes: 22 additions & 1 deletion src/v2-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,30 @@ class V2Agent {
*/
addMessagesResponse_(requestSource) {
let messages = this.buildResponseMessages_(requestSource);
if (messages.length > 0) {
if (messages.length > 0 && requestSource) {
this.addJson_({fulfillmentMessages: messages});
} else if (messages.length > 0) {
const fulfillmentText = this.getFulfillmentText_(requestSource);
this.addJson_({
fulfillmentMessages: messages,
...(fulfillmentText && {fulfillmentText}),
});
}
}

/**
* Get v2 fulfillment text based on developer defined response messages
*
* @private
* @param {string} requestSource string indicating the source of the initial request
* @return {string} message objects
*/
getFulfillmentText_() {
const textMessages = this.agent.responseMessages_.filter((message) => (message.ssml || message.text));
if (textMessages.length > 0) {
return (textMessages[0].ssml || textMessages[0].text);
}
return null;
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/webhook-integration-v2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ const mockSimulatorV2ResponseOther = {
quickReplies: {quickReplies: ['Quick Reply', 'Suggestion']},
},
],
fulfillmentText: 'This message is from Dialogflow\'s Cloud Functions for Firebase editor!',
outputContexts: [
{
name:
Expand Down