# How to Integrate ConnectiveOne with Odoo CRM

> **Important:** Odoo CRM integration (module, iframe, webhooks) is **implemented by the client's IT team**. ConnectiveOne provides public guides and standard mechanisms (autologin, `event_webhook_url`); it is **not** part of the CO implementation estimate.

This guide describes typical ways to integrate ConnectiveOne with Odoo CRM: embedding the operator panel, webhook events, and contact/lead synchronization. It is intended for teams building an Odoo module on the client side.

## What Can Be Integrated

| Capability | Direction | Documentation |
|------------|-----------|---------------|
| Operator panel on contact / lead card | Odoo → ConnectiveOne (iframe + autologin) | [OP widget in Odoo](./integrate-operator-panel-as-widget-odoo.md) |
| Chat events (created, operator connected, closed) | ConnectiveOne → Odoo (webhook) | [Operator panel webhook](../webhook/configure-operator-panel-webhook.md) |
| Lead create/update from bot | ConnectiveOne → Odoo (XML-RPC / JSON-RPC) | This doc, § «Leads and Contacts» |
| Custom fields to CRM | Two-way via scenario + Odoo API | § «Custom fields» |
| Discuss messages | Usually **not** duplicated — embed OP recommended | [Why embed](#why-embed-not-discuss) |

---

## Recommended Architecture

```mermaid
flowchart LR
  subgraph Odoo["Odoo CRM"]
    Partner["res.partner / crm.lead"]
    Iframe["OP iframe"]
    WebhookCtrl["HTTP controller<br/>/connectiveone/events"]
  end
  subgraph CO["ConnectiveOne"]
    Bot["Bot / scenario"]
    OP["Operator Panel"]
    AJ["Action Jail"]
  end
  Partner --> Iframe
  Iframe -->|"autologin"| OP
  Bot --> OP
  OP -->|"event_webhook_url"| WebhookCtrl
  AJ -->|"XML-RPC"| Partner
```

**Principle:** dialogues and routing stay in ConnectiveOne; Odoo holds CRM cards, activities, and manager directories.

---

## Step 1. Embed Operator Panel

Full guide: [How to integrate operator panel as widget in Odoo CRM](./integrate-operator-panel-as-widget-odoo.md).

Summary:

1. Match manager email in Odoo and ConnectiveOne.
2. Store `login_key` for autologin in Odoo secure storage.
3. Add iframe tab on `res.partner` / `crm.lead`.
4. Build URL **server-side** with client phone.

---

## Step 2. Operator Panel Webhook in Odoo

### ConnectiveOne Configuration

In `operator_panel__connect_to_operator_with_msg`:

```json
{
  "auto_connect_operator": true,
  "subject_alias": "support",
  "event_webhook_url": "https://your-odoo.example.com/connectiveone/events"
}
```

Event list and payload format — [webhook configuration](../webhook/configure-operator-panel-webhook.md).

### Example Odoo Controller (Python)

```python
from odoo import http
from odoo.http import request

class ConnectiveOneWebhook(http.Controller):

    @http.route("/connectiveone/events", type="json", auth="public", methods=["POST"], csrf=False)
    def receive_event(self, **kwargs):
        data = request.jsonrequest
        event_name = data.get("event_name")
        client = data.get("client") or {}
        phone = client.get("phone")
        partner = False
        if phone:
            partner = request.env["res.partner"].sudo().search([
                "|", ("phone", "ilike", phone[-9:]),
                ("mobile", "ilike", phone[-9:]),
            ], limit=1)

        if event_name == "chat_created" and partner:
            partner.activity_schedule(
                "mail.mail_activity_data_todo",
                summary="New ConnectiveOne chat",
                note=data.get("text", ""),
            )
        elif event_name == "chat_closed_by_operator" and partner:
            partner.message_post(body="ConnectiveOne chat closed by operator")

        return {"status": "ok"}
```

> Add **authentication** for production (shared secret header, VPN, or mTLS).

### Typical CRM Event Actions

| Event | Suggested Odoo action |
|-------|----------------------|
| `chat_created` | Activity on partner / lead |
| `operator_connected` | Chatter message |
| `chat_closed_by_operator` | Close activity, update stage |
| `chat_transferred_to_operator` | Reassign responsible (optional) |

---

## Step 3. Leads and Contacts from Bot

### Create Lead via Odoo External API

From Action Jail or scenario call [Odoo External API](https://www.odoo.com/documentation/19.0/developer/reference/external_api.html):

```javascript
// Example in custom action (pseudocode)
// authenticate → execute_kw('crm.lead', 'create', [{ name, phone, description }])
```

**Recommended lead fields:**

| Odoo field | Bot source |
|------------|------------|
| `name` | Client name or request subject |
| `phone` / `mobile` | Scenario constant |
| `description` | Request text / product |
| `user_id` | Assigned manager (if known) |
| `tag_ids` | Channel source (Telegram, Viber) |

---

## Step 4. Custom Fields and Identifiers

| ConnectiveOne (OP client) | Odoo |
|---------------------------|------|
| `external_id` / phone | `res.partner.id` |
| Custom field `odoo_partner_id` | Partner ID |
| `chat_room_id` | Optional field on partner |

This enables correct dialogue deep links from iframe and webhook updates on the same card.

---

## Step 5. Odoo System Parameters

| Key | Example | Description |
|-----|---------|-------------|
| `connectiveone.instance_url` | `https://company.connectiveone.io` | Base URL |
| `connectiveone.login_key` | *(secret)* | Autologin |
| `connectiveone.default_bot_id` | `12` | Bot for `init_dialog` |
| `connectiveone.default_channel` | `telegram` | Default channel |
| `connectiveone.webhook_secret` | *(secret)* | Webhook verification |

---

## Why Embed, Not Discuss

Duplicating chat in Odoo Discuss requires full message, attachment, and routing sync. That is costly to maintain and conflicts with ConnectiveOne skill groups.

**Recommendation:** operator panel iframe + webhooks for status and activities.

---

## Implementation Checklist

- [ ] Odoo users = ConnectiveOne operators (email)
- [ ] Autologin URL tested on staging
- [ ] iframe renders in partner / lead form view
- [ ] Webhook endpoint handles `chat_created` and creates activity
- [ ] (Optional) Action Jail creates `crm.lead` on bot request
- [ ] CSP / cookies verified in production browsers
- [ ] Secrets not in Odoo module repository

---

## Limitations

- Odoo Online may restrict custom controllers — check hosting plan
- Autologin requires valid `login_key`
- Webhook URL must be reachable from ConnectiveOne servers
- Full two-way message sync is a separate large scope

## Related Articles

- [Operator panel widget in Odoo](./integrate-operator-panel-as-widget-odoo.md)
- [Integrate operator panel as widget (general)](./integrate-operator-panel-as-widget.md)
- [Configure operator panel webhook](../webhook/configure-operator-panel-webhook.md)
- [Integrate via Custom Channel](../custom-channel/integrate-via-custom-channel.md)
- [What are integrations](../../explanation/what-are-integrations.md)
