Files
odoo_source/addons/im_livechat/controllers/chatbot.py
T
Sébastien Theys 90cb44e1e1 [REF] mail, *: rename mail.channel to discuss.channel
* = bus, calendar, crm_livechat, hr, hr_holidays, im_livechat, mail_bot,
    mass_mailing, privacy_lookup, test_discuss_full, test_mail,
    test_mail_full, website_crm_livechat, website_livechat, base

In preparation of splitting discuss and mail modules.

Part of task-3265211

closes odoo/odoo#118354

Related: odoo/upgrade#4553
Related: odoo/enterprise#39661
Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
2023-04-21 02:21:53 +02:00

105 lines
5.1 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import http
from odoo.http import request
from odoo.tools import is_html_empty, plaintext2html
class LivechatChatbotScriptController(http.Controller):
@http.route('/chatbot/restart', type="json", auth="public", cors="*")
def chatbot_restart(self, channel_uuid, chatbot_script_id):
discuss_channel = request.env['discuss.channel'].sudo().search([('uuid', '=', channel_uuid)], limit=1)
chatbot = request.env['chatbot.script'].browse(chatbot_script_id)
if not discuss_channel or not chatbot.exists():
return None
return discuss_channel._chatbot_restart(chatbot).message_format()[0]
@http.route('/chatbot/post_welcome_steps', type="json", auth="public", cors="*")
def chatbot_post_welcome_steps(self, channel_uuid, chatbot_script_id):
discuss_channel = request.env['discuss.channel'].sudo().search([('uuid', '=', channel_uuid)], limit=1)
chatbot = request.env['chatbot.script'].sudo().browse(chatbot_script_id)
if not discuss_channel or not chatbot.exists():
return None
return chatbot._post_welcome_steps(discuss_channel).message_format()
@http.route('/chatbot/answer/save', type="json", auth="public", cors="*")
def chatbot_save_answer(self, channel_uuid, message_id, selected_answer_id):
discuss_channel = request.env['discuss.channel'].sudo().search([('uuid', '=', channel_uuid)], limit=1)
chatbot_message = request.env['chatbot.message'].sudo().search([
('mail_message_id', '=', message_id),
('discuss_channel_id', '=', discuss_channel.id),
], limit=1)
selected_answer = request.env['chatbot.script.answer'].sudo().browse(selected_answer_id)
if not discuss_channel or not chatbot_message or not selected_answer.exists():
return
if selected_answer in chatbot_message.script_step_id.answer_ids:
chatbot_message.write({'user_script_answer_id': selected_answer_id})
@http.route('/chatbot/step/trigger', type="json", auth="public", cors="*")
def chatbot_trigger_step(self, channel_uuid, chatbot_script_id=None):
discuss_channel = request.env['discuss.channel'].sudo().search([('uuid', '=', channel_uuid)], limit=1)
if not discuss_channel:
return None
next_step = False
if discuss_channel.chatbot_current_step_id:
chatbot = discuss_channel.chatbot_current_step_id.chatbot_script_id
user_messages = discuss_channel.message_ids.filtered(
lambda message: message.author_id != chatbot.operator_partner_id
)
user_answer = request.env['mail.message'].sudo()
if user_messages:
user_answer = user_messages.sorted(lambda message: message.id)[-1]
next_step = discuss_channel.chatbot_current_step_id._process_answer(discuss_channel, user_answer.body)
elif chatbot_script_id: # when restarting, we don't have a "current step" -> set "next" as first step of the script
chatbot = request.env['chatbot.script'].sudo().browse(chatbot_script_id)
if chatbot.exists():
next_step = chatbot.script_step_ids[:1]
if not next_step:
return None
posted_message = next_step._process_step(discuss_channel)
return {
'chatbot_posted_message': posted_message.message_format()[0] if posted_message else None,
'chatbot_step': {
'chatbot_operator_found': next_step.step_type == 'forward_operator' and len(
discuss_channel.channel_member_ids) > 2,
'chatbot_script_step_id': next_step.id,
'chatbot_step_answers': [{
'id': answer.id,
'label': answer.name,
'redirect_link': answer.redirect_link,
} for answer in next_step.answer_ids],
'chatbot_step_is_last': next_step._is_last_step(discuss_channel),
'chatbot_step_message': plaintext2html(next_step.message) if not is_html_empty(next_step.message) else False,
'chatbot_step_type': next_step.step_type,
}
}
@http.route('/chatbot/step/validate_email', type="json", auth="public", cors="*")
def chatbot_validate_email(self, channel_uuid):
discuss_channel = request.env['discuss.channel'].sudo().search([('uuid', '=', channel_uuid)], limit=1)
if not discuss_channel or not discuss_channel.chatbot_current_step_id:
return None
chatbot = discuss_channel.chatbot_current_step_id.chatbot_script_id
user_messages = discuss_channel.message_ids.filtered(
lambda message: message.author_id != chatbot.operator_partner_id
)
if user_messages:
user_answer = user_messages.sorted(lambda message: message.id)[-1]
result = chatbot._validate_email(user_answer.body, discuss_channel)
if result['posted_message']:
result['posted_message'] = result['posted_message'].message_format()[0]
return result