chatbot.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. class WebsiteLivechatChatbotScriptController(http.Controller):
  6. @http.route('/chatbot/<model("chatbot.script"):chatbot_script>/test',
  7. type="http", auth="user", website=True)
  8. def chatbot_test_script(self, chatbot_script):
  9. """ Custom route allowing to test a chatbot script.
  10. As we don't have a im_livechat.channel linked to it, we pre-emptively create a mail.channel
  11. that will hold the conversation between the bot and the user testing the script. """
  12. mail_channel_values = {
  13. 'channel_member_ids': [(0, 0, {
  14. 'partner_id': chatbot_script.operator_partner_id.id,
  15. 'is_pinned': False
  16. }, {
  17. 'partner_id': request.env.user.partner_id.id
  18. })],
  19. 'livechat_active': True,
  20. 'livechat_operator_id': chatbot_script.operator_partner_id.id,
  21. 'chatbot_current_step_id': chatbot_script._get_welcome_steps()[-1].id,
  22. 'anonymous_name': False,
  23. 'channel_type': 'livechat',
  24. 'name': chatbot_script.title,
  25. }
  26. visitor_sudo = request.env['website.visitor']._get_visitor_from_request()
  27. if visitor_sudo:
  28. mail_channel_values['livechat_visitor_id'] = visitor_sudo.id
  29. mail_channel = request.env['mail.channel'].create(mail_channel_values)
  30. return request.render("im_livechat.chatbot_test_script_page", {
  31. 'server_url': chatbot_script.get_base_url(),
  32. 'channel_data': mail_channel.channel_info()[0],
  33. 'chatbot_data': chatbot_script._format_for_frontend()
  34. })