In this blog we will explain how to send chat message in odoo 16 programmatically
#Sending direct chatting message in odoo mail , means send message in private channel that has "chat" type , so we have to create private channel with "chat"type for the user that we want to send him a message just as bellow
def action_send_chat_message(self,partner_id):
odoobot = self.env.ref('base.partner_root')
channel_name = odoobot.name+"/"+partner_id.name[0]+partner_id.name[1]+partner_id.name[2]+partner_id.name[3]+partner_id.name[4]+str(partner_id.id)
mail_channel_id = self.env['mail.channel'].search([('name','=',channel_name)])
if not mail_channel_id:
mail_channel_id = self.env['mail.channel'].create({
'name': channel_name,
'channel_type':'chat',
'channel_partner_ids': [(4,odoobot.id),(4,partner_id.id)]
})
self.env['mail.message'].create({
'email_from': self.env.user.partner_id.email,
'author_id': self.env.user.partner_id.id,
'model': 'mail.channel',
'message_type':'comment',
'subtype_id': self.env.ref('mail.mt_comment').id,
'subject': _('Direct Chatting'),
'body': 'This is a private direct message',
'res_id':mail_channel_id.id,
'partner_ids': [(4, partner_id.id)]
})
#note : we must pass value for "channel_ids" field in odoo 14