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

Commit

Permalink
Add fulfillmentText to MessagesResponse when requestSource is other
Browse files Browse the repository at this point in the history
  • Loading branch information
cchardeau authored and Corentin Chardeau committed Apr 13, 2021
1 parent 86a844a commit ee1ca07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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

0 comments on commit ee1ca07

Please sign in to comment.