From ee1ca070d59b167f2e2007bee65d6824dba22648 Mon Sep 17 00:00:00 2001 From: cchardeau Date: Tue, 13 Apr 2021 14:26:12 +0200 Subject: [PATCH] Add fulfillmentText to MessagesResponse when requestSource is other --- src/v2-agent.js | 23 ++++++++++++++++++++++- test/webhook-integration-v2-test.js | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/v2-agent.js b/src/v2-agent.js index ecfa517..7291604 100644 --- a/src/v2-agent.js +++ b/src/v2-agent.js @@ -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; } /** diff --git a/test/webhook-integration-v2-test.js b/test/webhook-integration-v2-test.js index f4e1a9c..5705959 100644 --- a/test/webhook-integration-v2-test.js +++ b/test/webhook-integration-v2-test.js @@ -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: