PURPOSE This commit introduces a chatbot operator that works based on a user-defined script with various steps. SPECS A im_livechat.chatbot.script can be defined on a livechat rule. When a end-user reaches a website page that matches the rule, the chat window opens and the script of the bot starts iterating through its steps. The chatbot code is currently directly integrated with the existing livechat Javascript code. It defines extra conditions and layout elements to be able to automate the conversation and register user answers. AVAILABLE STEPS A script is defined with several steps that can currently be one of the following types: "text" A simple text step where the bot posts a message without expecting an answer e.g: "Hello! I'm a friendly robot!" "question_selection" The bot will ask a question and suggest answers, the end-user will have to click on the answer he chooses e.g: "How can I help you? -> Create a Ticket -> Create a Lead -> Speak with a human" "question_email" That step will ask the end user's email address (and validate it) The result is saved on the linked im_livechat.im_livechatchatbot.mail.message "question_phone" Same logic as the 'question_email' for a phone number We don't validate the input this time as it's a complicated process (requires country, ...) "forward_operator" Special type of step that will add a human operator to the conversation when reached, which stops the script and allow the visitor to discuss with a real person. The operator will be chosen among the available operators on the livechat.channel. If there is no operator available, the script continues normally which allows to automate an "answering machine" that will redirect the user in case no operator is available. e.g: "I'm sorry, no operator is available right now, please contact us by email at 'info@company.com', we will try to respond as soon as possible!". (Or even something more complex with multiple questions / paths). "free_input_single" Will ask the visitor for a single line of text. This text is not saved anywhere else than in the conversation, but it's still useful when combined with steps that create leads / tickets since those print the whole conversation into the description. "free_input_multi" Same as "free_input_single" but lets the user input multiple lines of text. The frontend implementation is made by waiting a few seconds (currently 10) for either the next submitted message or the next character typed into the input. This lets visitors explain their issue / question with multiple messages. Which is very useful since new messages are sent every time you press "Enter". LINKS Task-2030386 Part-of: odoo/odoo#84000 Co-authored-by: Patrick Hoste <pko@odoo.com> Co-authored-by: Aurélien Warnon <awa@odoo.com>
66 lines
2.6 KiB
Python
66 lines
2.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, models, fields
|
|
from odoo.osv import expression
|
|
|
|
import textwrap
|
|
|
|
|
|
class ChatbotScriptAnswer(models.Model):
|
|
_name = 'chatbot.script.answer'
|
|
_description = 'Chatbot Script Answer'
|
|
_order = 'script_step_id, sequence, id'
|
|
|
|
name = fields.Char(string='Name', required=True, translate=True)
|
|
sequence = fields.Integer(string='Sequence', default=1)
|
|
redirect_link = fields.Char('Redirect Link',
|
|
help="The visitor will be redirected to this link upon clicking the option "
|
|
"(note that the script will end if the link is external to the livechat website).")
|
|
script_step_id = fields.Many2one(
|
|
'chatbot.script.step', string='Script Step', required=True, ondelete='cascade')
|
|
chatbot_script_id = fields.Many2one(related='script_step_id.chatbot_script_id')
|
|
|
|
def name_get(self):
|
|
if self._context.get('chatbot_script_answer_display_short_name'):
|
|
return super().name_get()
|
|
|
|
result = []
|
|
for answer in self:
|
|
answer_message = answer.script_step_id.message.replace('\n', ' ')
|
|
shortened_message = textwrap.shorten(answer_message, width=26, placeholder=" [...]")
|
|
|
|
result.append((
|
|
answer.id,
|
|
"%s: %s" % (shortened_message, answer.name)
|
|
))
|
|
|
|
return result
|
|
|
|
@api.model
|
|
def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
|
|
"""
|
|
Search the records whose name or step message are matching the ``name`` pattern.
|
|
The chatbot_script_id is also passed to the context through the custom widget
|
|
('chatbot_triggering_answers_widget') This allows to only see the question_answer
|
|
from the same chatbot you're configuring.
|
|
"""
|
|
force_domain_chatbot_script_id = self.env.context.get('force_domain_chatbot_script_id')
|
|
|
|
if name and operator == 'ilike':
|
|
if not args:
|
|
args = []
|
|
|
|
# search on both name OR step's message (combined with passed args)
|
|
name_domain = [('name', operator, name)]
|
|
step_domain = [('script_step_id.message', operator, name)]
|
|
domain = expression.AND([args, expression.OR([name_domain, step_domain])])
|
|
|
|
else:
|
|
domain = args or []
|
|
|
|
if force_domain_chatbot_script_id:
|
|
domain = expression.AND([domain, [('chatbot_script_id', '=', force_domain_chatbot_script_id)]])
|
|
|
|
return self._search(domain, limit=limit, access_rights_uid=name_get_uid)
|