chatbot.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import http
  4. from odoo.http import request
  5. from odoo.tools import get_lang, is_html_empty, plaintext2html
  6. class LivechatChatbotScriptController(http.Controller):
  7. @http.route('/chatbot/restart', type="json", auth="public", cors="*")
  8. def chatbot_restart(self, channel_uuid, chatbot_script_id):
  9. chatbot_language = self._get_chatbot_language()
  10. mail_channel = request.env['mail.channel'].sudo().with_context(lang=chatbot_language).search([('uuid', '=', channel_uuid)], limit=1)
  11. chatbot = request.env['chatbot.script'].browse(chatbot_script_id)
  12. if not mail_channel or not chatbot.exists():
  13. return None
  14. return mail_channel._chatbot_restart(chatbot).message_format()[0]
  15. @http.route('/chatbot/post_welcome_steps', type="json", auth="public", cors="*")
  16. def chatbot_post_welcome_steps(self, channel_uuid, chatbot_script_id):
  17. mail_channel = request.env['mail.channel'].sudo().search([('uuid', '=', channel_uuid)], limit=1)
  18. chatbot_language = self._get_chatbot_language()
  19. chatbot = request.env['chatbot.script'].sudo().with_context(lang=chatbot_language).browse(chatbot_script_id)
  20. if not mail_channel or not chatbot.exists():
  21. return None
  22. return chatbot._post_welcome_steps(mail_channel).message_format()
  23. @http.route('/chatbot/answer/save', type="json", auth="public", cors="*")
  24. def chatbot_save_answer(self, channel_uuid, message_id, selected_answer_id):
  25. mail_channel = request.env['mail.channel'].sudo().search([('uuid', '=', channel_uuid)], limit=1)
  26. chatbot_message = request.env['chatbot.message'].sudo().search([
  27. ('mail_message_id', '=', message_id),
  28. ('mail_channel_id', '=', mail_channel.id),
  29. ], limit=1)
  30. selected_answer = request.env['chatbot.script.answer'].sudo().browse(selected_answer_id)
  31. if not mail_channel or not chatbot_message or not selected_answer.exists():
  32. return
  33. if selected_answer in chatbot_message.script_step_id.answer_ids:
  34. chatbot_message.write({'user_script_answer_id': selected_answer_id})
  35. @http.route('/chatbot/step/trigger', type="json", auth="public", cors="*")
  36. def chatbot_trigger_step(self, channel_uuid, chatbot_script_id=None):
  37. chatbot_language = self._get_chatbot_language()
  38. mail_channel = request.env['mail.channel'].sudo().with_context(lang=chatbot_language).search([('uuid', '=', channel_uuid)], limit=1)
  39. if not mail_channel:
  40. return None
  41. next_step = False
  42. if mail_channel.chatbot_current_step_id:
  43. chatbot = mail_channel.chatbot_current_step_id.chatbot_script_id
  44. user_messages = mail_channel.message_ids.filtered(
  45. lambda message: message.author_id != chatbot.operator_partner_id
  46. )
  47. user_answer = request.env['mail.message'].sudo()
  48. if user_messages:
  49. user_answer = user_messages.sorted(lambda message: message.id)[-1]
  50. next_step = mail_channel.chatbot_current_step_id._process_answer(mail_channel, user_answer.body)
  51. elif chatbot_script_id: # when restarting, we don't have a "current step" -> set "next" as first step of the script
  52. chatbot = request.env['chatbot.script'].sudo().with_context(lang=chatbot_language).browse(chatbot_script_id)
  53. if chatbot.exists():
  54. next_step = chatbot.script_step_ids[:1]
  55. if not next_step:
  56. return None
  57. posted_message = next_step._process_step(mail_channel)
  58. return {
  59. 'chatbot_posted_message': posted_message.message_format()[0] if posted_message else None,
  60. 'chatbot_step': {
  61. 'chatbot_operator_found': next_step.step_type == 'forward_operator' and len(
  62. mail_channel.channel_member_ids) > 2,
  63. 'chatbot_script_step_id': next_step.id,
  64. 'chatbot_step_answers': [{
  65. 'id': answer.id,
  66. 'label': answer.name,
  67. 'redirect_link': answer.redirect_link,
  68. } for answer in next_step.answer_ids],
  69. 'chatbot_step_is_last': next_step._is_last_step(mail_channel),
  70. 'chatbot_step_message': plaintext2html(next_step.message) if not is_html_empty(next_step.message) else False,
  71. 'chatbot_step_type': next_step.step_type,
  72. }
  73. }
  74. @http.route('/chatbot/step/validate_email', type="json", auth="public", cors="*")
  75. def chatbot_validate_email(self, channel_uuid):
  76. mail_channel = request.env['mail.channel'].sudo().search([('uuid', '=', channel_uuid)], limit=1)
  77. if not mail_channel or not mail_channel.chatbot_current_step_id:
  78. return None
  79. chatbot = mail_channel.chatbot_current_step_id.chatbot_script_id
  80. user_messages = mail_channel.message_ids.filtered(
  81. lambda message: message.author_id != chatbot.operator_partner_id
  82. )
  83. if user_messages:
  84. user_answer = user_messages.sorted(lambda message: message.id)[-1]
  85. result = chatbot._validate_email(user_answer.body, mail_channel)
  86. if result['posted_message']:
  87. result['posted_message'] = result['posted_message'].message_format()[0]
  88. return result
  89. def _get_chatbot_language(self):
  90. return request.httprequest.cookies.get('frontend_lang', request.env.user.lang or get_lang(request.env).code)