'Get the canonical private channel between some partners, create it if needed.
To reuse an old channel (conversation), this one must be private, and contains
only the given partners.
:param partners_to : list of res.partner ids to add to the conversation
:param pin : True if getting the channel should pin it for the current user
:returns a channel header, or False if the users_to was False
:rtype : dict'

@api.model
def channel_get(self, partners_to, pin=True):

    if partners_to:
        partners_to.append(self.env.user.partner_id.id)
        self.env.cr.execute("\n                                                                SELECT    P.channel_id    as    channel_id\n                                                                FROM    mail_channel    C,    mail_channel_partner    P\n                                                                WHERE    P.channel_id    =    C.id\n                                                                                AND    C.public    LIKE    'private'\n                                                                                AND    P.partner_id    IN    %s\n                                                                                AND    channel_type    LIKE    'chat'\n                                                                GROUP    BY    P.channel_id\n                                                                HAVING    COUNT(P.partner_id)    =    %s\n                                                ", (tuple(partners_to), len(partners_to)))
        result = self.env.cr.dictfetchall()
        if result:
            channel = self.browse(result[0].get('channel_id'))
            if pin:
                self.env['mail.channel.partner'].search([('partner_id', '=', self.env.user.partner_id.id), ('channel_id', '=', channel.id)]).write({'is_pinned': True})
        else:
            channel = self.create({'channel_partner_ids': [(4, partner_id) for partner_id in partners_to], 'public': 'private', 'channel_type': 'chat', 'email_send': False, 'name': ',    '.join(self.env['res.partner'].sudo().browse(partners_to).mapped('name'))})
            channel._broadcast(partners_to)
        return channel.channel_info()[0]
    return False
