From 4b72cd0e3f77af8ff9210f9a7b3e0484bd046a12 Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Mon, 4 Sep 2023 11:44:26 +0200 Subject: [PATCH] [FIX] im_livechat: fix chatbot test page MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR fixes a server error when trying to test a chatbot that appeared with the [livechat refactoring]. Steps to reproduce: - Go to a livechat channel form view - Click on the "chatbot" smart button - Click on a chatbot - Click on the "test" button - An internal server error occurs task-3485014 [livechat refactoring]: https://github.com/odoo/odoo/pull/122834 closes odoo/odoo#134227 X-original-commit: dfc50ea4e4fb327876458afee1b807e36ce80c2b Signed-off-by: Alexandre Kühn (aku) --- .../src/embed/chatbot/chatbot_service.js | 18 +++++++++++++++++- .../static/src/embed/core/livechat_service.js | 13 ++++++++----- .../views/im_livechat_chatbot_templates.xml | 8 ++++---- addons/website_livechat/controllers/chatbot.py | 3 ++- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/addons/im_livechat/static/src/embed/chatbot/chatbot_service.js b/addons/im_livechat/static/src/embed/chatbot/chatbot_service.js index fcd1beddcdc..ca081cd6f6e 100644 --- a/addons/im_livechat/static/src/embed/chatbot/chatbot_service.js +++ b/addons/im_livechat/static/src/embed/chatbot/chatbot_service.js @@ -25,6 +25,7 @@ export class ChatBotService { currentStep; /** @type {number} */ nextStepTimeout; + hasPostedWelcomeSteps = false; shouldRestore = false; isTyping = false; @@ -55,6 +56,16 @@ export class ChatBotService { this._processUserAnswer.bind(this), MULTILINE_STEP_DEBOUNCE_DELAY ); + if (this.livechatService.options.isTestChatbot) { + this.livechatService.rule.chatbot = { + ...this.livechatService.options.testChatbotData, + welcomeStepIndex: this.livechatService.options.testChatbotData.welcomeSteps.length, + }; + this.currentStep = new ChatbotStep( + this.livechatService.options.testChatbotData.welcomeSteps.at(-1) + ); + this.livechatService.updateSession(this.livechatService.options.testChatbotChannelData); + } this.livechatService.initializedDeferred.then(() => { this.chatbot = this.livechatService.rule?.chatbot ? new Chatbot(this.livechatService.rule.chatbot) @@ -77,7 +88,11 @@ export class ChatBotService { /** * Start the chatbot script. */ - start() { + async start() { + if (this.livechatService.options.isTestChatbot && !this.hasPostedWelcomeSteps) { + await this.postWelcomeSteps(); + this.save(); + } if (this.shouldRestore && this.livechatService.state !== SESSION_STATE.PERSISTED) { // We need to repost the welcome steps as they were not saved. this.chatbot.welcomeStepIndex = 0; @@ -135,6 +150,7 @@ export class ChatBotService { this.livechatService.thread?.messages.push(message); } } + this.hasPostedWelcomeSteps = true; } // ============================================================================= diff --git a/addons/im_livechat/static/src/embed/core/livechat_service.js b/addons/im_livechat/static/src/embed/core/livechat_service.js index 082e560a5d7..3f709386089 100644 --- a/addons/im_livechat/static/src/embed/core/livechat_service.js +++ b/addons/im_livechat/static/src/embed/core/livechat_service.js @@ -64,11 +64,14 @@ export class LivechatService { } async initialize() { - const init = await this.rpc("/im_livechat/init", { - channel_id: this.options.channel_id, - }); - this.available = init.available_for_me ?? this.available; - this.rule = init.rule; + let init; + if (!this.options.isTestChatbot) { + init = await this.rpc("/im_livechat/init", { + channel_id: this.options.channel_id, + }); + } + this.available = init?.available_for_me ?? this.available; + this.rule = init?.rule ?? {}; this.initialized = true; this.initializedDeferred.resolve(); } diff --git a/addons/im_livechat/views/im_livechat_chatbot_templates.xml b/addons/im_livechat/views/im_livechat_chatbot_templates.xml index 0da13bf3d9c..05b89bfb8bb 100644 --- a/addons/im_livechat/views/im_livechat_chatbot_templates.xml +++ b/addons/im_livechat/views/im_livechat_chatbot_templates.xml @@ -7,19 +7,19 @@ - +
diff --git a/addons/website_livechat/controllers/chatbot.py b/addons/website_livechat/controllers/chatbot.py index e3784fffa14..483bee0075f 100644 --- a/addons/website_livechat/controllers/chatbot.py +++ b/addons/website_livechat/controllers/chatbot.py @@ -37,5 +37,6 @@ class WebsiteLivechatChatbotScriptController(http.Controller): return request.render("im_livechat.chatbot_test_script_page", { 'server_url': chatbot_script.get_base_url(), 'channel_data': discuss_channel._channel_info()[0], - 'chatbot_data': chatbot_script._format_for_frontend() + 'chatbot_data': chatbot_script._format_for_frontend(), + 'current_partner_id': request.env.user.partner_id.id, })