[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) <tsm@odoo.com>
This commit is contained in:
tsm-odoo
2024-03-29 10:44:22 +00:00
parent 0509aa467e
commit 563385d07b
3 changed files with 104 additions and 3 deletions
@@ -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();
@@ -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."
);
}
},
},
],
});
@@ -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")