From 563385d07bee7fc072b032e04eca2510642a9d15 Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Fri, 29 Mar 2024 06:19:08 +0100 Subject: [PATCH] [FIX] {im, website}_livechat: fix chatbot redirection Before this PR, the chat bot would stop after redirecting a user on the same page that the current one (e.g. anchors) because it was counting on the page reload that do not occur in this case. This PR fixes the issue. opw-3801598 closes odoo/odoo#159769 X-original-commit: 3d7c2b87e59b261a2d3eeceb0c6de012e8b02a2e Signed-off-by: Matthieu Stockbauer (tsm) --- .../embed/common/chatbot/chatbot_service.js | 10 +++- .../website_livechat_chatbot_redirect.js | 53 +++++++++++++++++++ .../website_livechat/tests/test_chatbot_ui.py | 44 ++++++++++++++- 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 addons/website_livechat/static/tests/tours/website_livechat_chatbot_redirect.js diff --git a/addons/im_livechat/static/src/embed/common/chatbot/chatbot_service.js b/addons/im_livechat/static/src/embed/common/chatbot/chatbot_service.js index b6009e72331..01f62497276 100644 --- a/addons/im_livechat/static/src/embed/common/chatbot/chatbot_service.js +++ b/addons/im_livechat/static/src/embed/common/chatbot/chatbot_service.js @@ -257,15 +257,21 @@ export class ChatBotService { } this.currentStep.hasAnswer = true; this.save(); + let isRedirecting = false; if (answer) { + if (answer.redirectLink && URL.canParse(answer.redirectLink, window.location.href)) { + const url = new URL(window.location.href); + const nextURL = new URL(answer.redirectLink, window.location.href); + isRedirecting = url.pathname !== nextURL.pathname || url.origin !== nextURL.origin; + browser.location.assign(answer.redirectLink); + } await this.rpc("/chatbot/answer/save", { channel_uuid: this.livechatService.thread.uuid, message_id: stepMessage.id, selected_answer_id: answer.id, }); } - if (answer?.redirectLink) { - browser.location.assign(answer.redirectLink); + if (isRedirecting) { return; } this._triggerNextStep(); diff --git a/addons/website_livechat/static/tests/tours/website_livechat_chatbot_redirect.js b/addons/website_livechat/static/tests/tours/website_livechat_chatbot_redirect.js new file mode 100644 index 00000000000..f6381d7302a --- /dev/null +++ b/addons/website_livechat/static/tests/tours/website_livechat_chatbot_redirect.js @@ -0,0 +1,53 @@ +/* @odoo-module */ + +import { registry } from "@web/core/registry"; +import { contains } from "@web/../tests/utils"; + +registry.category("web_tour.tours").add("website_livechat.chatbot_redirect", { + shadow_dom: ".o-livechat-root", + test: true, + url: "/", + steps: () => [ + { + trigger: ".o-livechat-LivechatButton", + }, + { + trigger: ".o-mail-Message:contains(Hello, were do you want to go?)", + }, + { + trigger: "li:contains(Go to the #chatbot-redirect anchor)", + }, + { + trigger: ".o-mail-Message:contains(Tadam, we are on the page you asked for!)", + run() { + const url = new URL(location.href); + if (url.pathname !== "/" || url.hash !== "#chatbot-redirect") { + throw new Error( + "Chatbot should have redirected to the #chatbot-redirect anchor." + ); + } + }, + }, + { + trigger: "button[title='Restart Conversation']", + }, + { + trigger: "li:contains(Go to the /chabtot-redirect page)", + }, + { + trigger: ".o-mail-Message:contains(Tadam, we are on the page you asked for!)", + async run() { + await contains(".o-mail-Message", { + text: "Tadam, we are on the page you asked for!", + target: this.$anchor[0], + }); + const url = new URL(location.href); + if (url.pathname !== "/chatbot-redirect") { + throw new Error( + "Chatbot should have redirected to the /chatbot-redirect page." + ); + } + }, + }, + ], +}); diff --git a/addons/website_livechat/tests/test_chatbot_ui.py b/addons/website_livechat/tests/test_chatbot_ui.py index 5ca524de2b9..2057a67dc18 100644 --- a/addons/website_livechat/tests/test_chatbot_ui.py +++ b/addons/website_livechat/tests/test_chatbot_ui.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import tests +from odoo import Command, tests from odoo.addons.im_livechat.tests.chatbot_common import ChatbotCase from odoo.addons.website_livechat.tests.common import TestLivechatCommon @@ -97,3 +97,45 @@ class TestLivechatChatbotUI(TestLivechatCommon, ChatbotCase): def test_chatbot_available_after_reload(self): self.start_tour("/", "website_livechat_chatbot_after_reload_tour", step_delay=100) + + def test_chatbot_redirect(self): + chatbot_redirect_script = self.env["chatbot.script"].create( + {"title": "Redirection Bot"} + ) + question_step, _ = tuple( + self.env["chatbot.script.step"].create([ + { + "chatbot_script_id": chatbot_redirect_script.id, + "message": "Hello, were do you want to go?", + "step_type": "question_selection", + }, + { + "chatbot_script_id": chatbot_redirect_script.id, + "message": "Tadam, we are on the page you asked for!", + "step_type": "text", + } + ]) + ) + self.env["chatbot.script.answer"].create([ + { + "name": "Go to the #chatbot-redirect anchor", + "redirect_link": "#chatbot-redirect", + "script_step_id": question_step.id, + }, + { + "name": "Go to the /chabtot-redirect page", + "redirect_link": "/chatbot-redirect", + "script_step_id": question_step.id, + }, + ]) + livechat_channel = self.env["im_livechat.channel"].create({ + 'name': 'Redirection Channel', + 'rule_ids': [Command.create({ + 'regex_url': '/', + 'chatbot_script_id': chatbot_redirect_script.id, + })] + }) + default_website = self.env.ref("website.default_website") + default_website.channel_id = livechat_channel.id + self.env.ref("website.default_website").channel_id = livechat_channel.id + self.start_tour("/", "website_livechat.chatbot_redirect")