res_users.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import models, fields, _
  4. class Users(models.Model):
  5. _inherit = 'res.users'
  6. odoobot_state = fields.Selection(
  7. [
  8. ('not_initialized', 'Not initialized'),
  9. ('onboarding_emoji', 'Onboarding emoji'),
  10. ('onboarding_attachement', 'Onboarding attachment'),
  11. ('onboarding_command', 'Onboarding command'),
  12. ('onboarding_ping', 'Onboarding ping'),
  13. ('idle', 'Idle'),
  14. ('disabled', 'Disabled'),
  15. ], string="OdooBot Status", readonly=True, required=False) # keep track of the state: correspond to the code of the last message sent
  16. odoobot_failed = fields.Boolean(readonly=True)
  17. @property
  18. def SELF_READABLE_FIELDS(self):
  19. return super().SELF_READABLE_FIELDS + ['odoobot_state']
  20. def _init_messaging(self):
  21. if self.odoobot_state in [False, 'not_initialized'] and self._is_internal():
  22. self._init_odoobot()
  23. return super()._init_messaging()
  24. def _init_odoobot(self):
  25. self.ensure_one()
  26. odoobot_id = self.env['ir.model.data']._xmlid_to_res_id("base.partner_root")
  27. channel_info = self.env['mail.channel'].channel_get([odoobot_id, self.partner_id.id])
  28. channel = self.env['mail.channel'].browse(channel_info['id'])
  29. message = _("Hello,<br/>Odoo's chat helps employees collaborate efficiently. I'm here to help you discover its features.<br/><b>Try to send me an emoji</b> <span class=\"o_odoobot_command\">:)</span>")
  30. channel.sudo().message_post(body=message, author_id=odoobot_id, message_type="comment", subtype_xmlid="mail.mt_comment")
  31. self.sudo().odoobot_state = 'onboarding_emoji'
  32. return channel