- This topic is empty.
- Post
-
- December 21, 2024 at 6:16 am
Weekend WikiKeymasterCreating a custom API integration between 3CX and Odoo allows both systems to communicate, sync data, and automate tasks like logging calls, creating leads, and more. Since 3CX and Odoo each provide APIs for integration, you can create a custom solution that connects them.Below is a general guide to create a custom API that integrates 3CX with Odoo:
Overview
You will need:
- 3CX API: To get call data and interaction details.
- Odoo API (XML-RPC or JSON-RPC): To push the data to the Odoo CRM.
The process will involve:
- Setting up API credentials for both 3CX and Odoo.
- Writing a script or service to automate the data transfer.
- Setting up event listeners to trigger actions based on calls (e.g., when a call comes in, create or update a lead in Odoo).
Step 1: Setup API Credentials
A. 3CX API
- API Access in 3CX:
- 3CX provides a REST API that can be used for interacting with its system, including call logs, contact info, and more. You’ll need access to the 3CX Management Console and relevant API endpoints.
- Enable API Access:
- In 3CX, navigate to the Settings > API Access section to generate an API Key.
- Alternatively, you can use the 3CX Call Control API (if using on-premises versions).
B. Odoo API
- XML-RPC / JSON-RPC Access:
- Odoo provides XML-RPC and JSON-RPC APIs for interaction. For this example, we’ll use JSON-RPC because it’s easier for modern applications.
- API Credentials:
- Log in to Odoo.
- Go to Settings > Users & Companies > Users, and create an API user with appropriate access.
- Obtain the Odoo Instance URL, Database Name, Client ID, and Client Secret if using OAuth, or generate an API Key.
Step 2: Create a Script for API Integration
You’ll need a script or service that listens to incoming/outgoing calls in 3CX, processes the data, and interacts with Odoo.
Here’s a basic script in Python that demonstrates how to interact with both APIs.
Install Dependencies:
Install the required libraries for API interactions:
pip install requests
Python Script for 3CX to Odoo Integration:
import requests import json from datetime import datetime # Odoo API credentials ODOO_URL = 'https://your-odoo-instance.com' ODOO_DB = 'your_odoo_database' ODOO_USER = 'api_user' ODOO_PASSWORD = 'your_password' ODOO_API_URL = f'{ODOO_URL}/jsonrpc' # 3CX API endpoint and credentials THREECX_URL = 'http://your-3cx-server.com/api' THREECX_API_KEY = 'your_3cx_api_key' # Function to get calls from 3CX def get_3cx_calls(): url = f'{THREECX_URL}/get_calls' headers = {'Authorization': f'Bearer {THREECX_API_KEY}'} response = requests.get(url, headers=headers) if response.status_code == 200: return response.json() # List of calls with relevant data else: print(f"Error fetching data from 3CX: {response.text}") return [] # Function to create or update a lead in Odoo def create_or_update_lead_in_odoo(caller_id, call_duration, call_time): headers = {'Content-Type': 'application/json'} # Odoo JSON-RPC payload to create or update a lead data = { "jsonrpc": "2.0", "method": "call", "params": { "service": "object", "method": "execute_kw", "args": [ ODOO_DB, ODOO_USER, ODOO_PASSWORD, 'crm.lead', 'create', # Use 'write' if updating an existing lead [{'name': f'Lead from {caller_id}', 'contact_name': caller_id, 'phone': caller_id, 'description': f'Call duration: {call_duration}s, Call time: {call_time}'}] ] }, "id": 1 } response = requests.post(ODOO_API_URL, data=json.dumps(data), headers=headers) if response.status_code == 200: print(f"Lead created/updated successfully for caller {caller_id}") else: print(f"Error creating/updating lead in Odoo: {response.text}") # Main function to fetch calls and sync with Odoo def sync_calls_with_odoo(): calls = get_3cx_calls() if calls: for call in calls: caller_id = call.get('caller_id', 'Unknown') call_duration = call.get('call_duration', 0) call_time = datetime.fromtimestamp(call['start_time']).strftime('%Y-%m-%d %H:%M:%S') # Sync each call with Odoo CRM create_or_update_lead_in_odoo(caller_id, call_duration, call_time) else: print("No calls to sync with Odoo.") if __name__ == '__main__': sync_calls_with_odoo()
Step 3: Customize and Test the Script
- Customize the Script:
- Adjust the 3CX API endpoint based on your setup (e.g., retrieving data via
GET
orPOST
requests). - Ensure Odoo’s API method and parameters match your Odoo version’s schema (e.g., for creating or updating leads in CRM).
- Adjust the 3CX API endpoint based on your setup (e.g., retrieving data via
- Test the Integration:
- Run the script and verify that calls from 3CX are being correctly logged or updated as leads in Odoo CRM.
- Check the Odoo interface to ensure that the lead details from the 3CX calls are correctly populated.
Step 4: Automate the Integration
To continuously sync 3CX data with Odoo, you can set up a cron job (on Linux) or a scheduled task (on Windows) to run this script periodically.
Example of setting a cron job:
crontab -e
Add a line to run the script every 5 minutes:
*/5 * * * * /usr/bin/python3 /path/to/your/script.py
Conclusion
By following these steps, you can create a custom API integration between 3CX and Odoo CRM, which:
- Retrieves call data from 3CX.
- Creates or updates Odoo CRM leads with the call details.
- Automates the process via scheduled tasks.
This will help you seamlessly integrate communication and customer management across both platforms. You can customize the script further based on your specific needs, such as adding more call-related fields or advanced actions like updating customer information.
Let me know if you need further assistance or more customization!
- You must be logged in to reply to this topic.