[FIX] im_livechat, *: Fix broken test page of chatbot

*: website_livechat

Task-3006246
Task-3010911

closes odoo/odoo#102985

X-original-commit: 59002571344405ce4fb691761f62d4fb909ffc17
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
This commit is contained in:
Alexandre Kühn
2022-10-11 14:10:02 +02:00
parent 47acff4b16
commit cf34560383
8 changed files with 47 additions and 75 deletions
@@ -367,7 +367,7 @@ registerModel({
data: attr({
compute() {
if (this.messaging.publicLivechatGlobal.isTestChatbot) {
return this.messaging.publicLivechatGlobal.testChatbotData.chatbot;
return this.messaging.publicLivechatGlobal.testChatbotData;
}
if (this.state === 'init') {
return this.messaging.publicLivechatGlobal.rule.chatbot;
@@ -450,9 +450,6 @@ registerModel({
}),
serverUrl: attr({
compute() {
if (this.messaging.publicLivechatGlobal.chatbot.isActive) {
return this.messaging.publicLivechatGlobal.chatbot.serverUrl;
}
return this.messaging.publicLivechatGlobal.serverUrl;
},
}),
@@ -192,6 +192,12 @@ registerModel({
default: false,
}),
isTestChatbot: attr({
compute() {
if (!this.options) {
return clear();
}
return Boolean(this.options.isTestChatbot);
},
default: false,
}),
lastMessage: one('PublicLivechatMessage', {
@@ -204,7 +210,7 @@ registerModel({
}),
livechatButtonView: one('LivechatButtonView', {
compute() {
if (this.isAvailable && this.isAvailableForMe && this.hasLoadedQWebTemplate && this.env.services.public_livechat_service) {
if (this.isAvailable && (this.isAvailableForMe || this.isTestChatbot) && this.hasLoadedQWebTemplate && this.env.services.public_livechat_service) {
return {};
}
return clear();
@@ -227,7 +233,14 @@ registerModel({
default: '',
}),
sessionCookie: attr(),
testChatbotData: attr(),
testChatbotData: attr({
compute() {
if (!this.options) {
return clear();
}
return this.options.testChatbotData;
},
}),
welcomeMessages: many('PublicLivechatMessage', {
compute() {
return this.messages.filter((message) => {
@@ -2,15 +2,17 @@
<odoo>
<data>
<template id="chatbot_test_script_page">
<script>
<t t-call="im_livechat.loader">
<t t-set="info" t-value="{ 'available': True, 'options': { 'isTestChatbot': True, 'testChatbotChannelData': channel_data, 'testChatbotData': chatbot_data }, 'server_url': server_url }"/>
</t>
</script>
<t t-call="web.frontend_layout">
<t t-set="no_livechat" t-value="True"/>
<t t-set="title" t-value="chatbot_data['chatbot_name']"/>
<div id="wrap" class="o_livechat_js_chatbot_test_script"
t-att-data-chatbot="json.dumps(chatbot_data)"
t-att-data-channel="json.dumps(channel_data)"
t-att-data-server-url="server_url">
<div groups="im_livechat.im_livechat_group_user" t-ignore="true"
<div id="wrap">
<div groups="im_livechat.im_livechat_group_user" t-ignore="true"
class="alert alert-info alert-dismissible rounded-0 fade show d-print-none css_editable_mode_hidden mb-0">
<div t-ignore="true" class="text-center">
<a t-attf-href="/web#view_type=form&amp;model=chatbot.script&amp;id=#{chatbot_data['chatbot_script_id']}&amp;action=im_livechat.chatbot_script_action">
-1
View File
@@ -37,7 +37,6 @@ Allow website visitors to chat with the collaborators. This module also brings a
],
'web.assets_frontend': [
'website_livechat/static/src/legacy/public_livechat.js',
'website_livechat/static/src/legacy/website_livechat_chatbot_test_script.js',
'website_livechat/static/src/legacy/public_livechat.scss',
],
'website.assets_wysiwyg': [
@@ -1,61 +0,0 @@
/** @odoo-module alias=website_livechat.chatbot_test_script **/
import publicWidget from 'web.public.widget';
import {deleteCookie} from 'web.utils.cookies';
import LivechatButton from '@im_livechat/legacy/widgets/livechat_button';
/**
* Override of the LivechatButton to create a testing environment for the chatbot script.
*
* The biggest difference here is that we don't have a 'im_livechat.channel' to work with.
* The 'mail.channel' holding the conversation between the bot and the testing user has been created
* by the 'chatbot/<model("chatbot.script"):chatbot>/test' endpoint.
*/
const LivechatButtonTestChatbot = LivechatButton.extend({
/**
* Initialize various data received from the 'chatbot_test_script_page' template.
*/
init: function (parent, messaging, chatbotData) {
this._super(...arguments);
this.messaging.publicLivechatGlobal.livechatButtonView.update({
rule: {
'action': 'auto_popup',
'auto_popup_timer': 0,
},
});
this.messaging.publicLivechatGlobal.update({ isTestChatbot: true });
this.messaging.publicLivechatGlobal.update({ testChatbotData: chatbotData.chatbot });
this.messaging.publicLivechatGlobal.chatbot.update({
currentStep: {
data: this.messaging.publicLivechatGlobal.chatbot.lastWelcomeStep,
},
});
},
});
publicWidget.registry.livechatChatbotTestScript = publicWidget.Widget.extend({
selector: '.o_livechat_js_chatbot_test_script',
init(parent) {
this._super(...arguments);
this.env = parent.env;
},
/**
* Remove any existing session cookie to start fresh
*/
async start() {
deleteCookie('im_livechat_session');
deleteCookie('im_livechat_auto_popup');
deleteCookie('im_livechat_history');
deleteCookie('im_livechat_previous_operator_pid');
const messaging = await this.env.services.messaging.get();
return this._super(...arguments).then(() => {
messaging.update({
publicLivechatGlobal: { isAvailable: true, chatbot: { serverUrl: this.$el.data().serverUrl } },
});
this.livechatButton = new LivechatButtonTestChatbot(this, messaging, this.$el.data());
this.livechatButton.appendTo(document.body);
});
}
});
@@ -40,7 +40,7 @@ registerPatch({
return this._super();
}
this.messaging.publicLivechatGlobal.update({
publicLivechat: { data: this.messaging.publicLivechatGlobal.testChatbotData.channel },
publicLivechat: { data: this.messaging.publicLivechatGlobal.options.testChatbotChannelData },
});
await this.openChatWindow();
this.widget._sendWelcomeMessage();
@@ -2,7 +2,7 @@
import { registerPatch } from '@mail/model/model_core';
import {setCookie} from 'web.utils.cookies';
import { deleteCookie, setCookie } from 'web.utils.cookies';
registerPatch({
name: 'PublicLivechatGlobal',
@@ -19,6 +19,28 @@ registerPatch({
*/
willStart() {
if (this.isTestChatbot) {
/**
* Override of the LivechatButton to create a testing environment for the chatbot script.
*
* The biggest difference here is that we don't have a 'im_livechat.channel' to work with.
* The 'mail.channel' holding the conversation between the bot and the testing user has been created
* by the 'chatbot/<model("chatbot.script"):chatbot>/test' endpoint.
*/
deleteCookie('im_livechat_session');
deleteCookie('im_livechat_auto_popup');
deleteCookie('im_livechat_history');
deleteCookie('im_livechat_previous_operator_pid');
this.update({
rule: {
'action': 'auto_popup',
'auto_popup_timer': 0,
},
});
this.chatbot.update({
currentStep: {
data: this.chatbot.lastWelcomeStep,
},
});
/**
* Overridden to avoid calling the "init" endpoint as it
* requires a im_livechat.channel linked to work properly.