Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add crm tool with salesforce and hubspot support #176

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ksivam
Copy link

@ksivam ksivam commented Jan 5, 2025

CRM Tools

Description

The CRM Tools are designed for seamless interaction with popular CRM platforms like Salesforce and HubSpot.
These tools allow you to integrate with CRM systems, perform data operations,
and automate tasks such as lead management, email campaigns, and more.

Currently, the following CRM platforms are supported:

  • Salesforce
  • HubSpot

To successfully use these tools, you need to have the appropriate API keys and credentials set in your environment.

Installation

To start using the CRM Integration Tools, you must first install the crewai_tools package.
This can be easily done with the following command:

pip install 'crewai[tools]'

Setup

Before you begin, ensure you have set the required environment variables for both Salesforce and HubSpot.

Environment Variables

You need to set the following environment variables:

  • For Salesforce:

    • SALESFORCE_API_KEY: Your Salesforce API key
    • SALESFORCE_BASE_URL: Base URL for Salesforce API
    • SALESFORCE_CLIENT_ID: Salesforce Client ID
    • SALESFORCE_CLIENT_SECRET: Salesforce Client Secret
    • SALESFORCE_REFRESH_TOKEN: Salesforce refresh token
  • For HubSpot:

    • HUBSPOT_API_KEY: Your HubSpot API key
    • HUBSPOT_BASE_URL: Base URL for HubSpot API

You can set these environment variables using the terminal or by including them in a .env file.

Example of .env file:

SALESFORCE_API_KEY=your_salesforce_api_key
SALESFORCE_BASE_URL=https://api.salesforce.com
SALESFORCE_CLIENT_ID=your_salesforce_client_id
SALESFORCE_CLIENT_SECRET=your_salesforce_client_secret
SALESFORCE_REFRESH_TOKEN=your_salesforce_refresh_token

HUBSPOT_API_KEY=your_hubspot_api_key
HUBSPOT_BASE_URL=https://api.hubspot.com

Examples

Creating Salesforce Tool Using Factory

Here is how you can use the CrmToolFactory to create a Salesforce integration tool and fetch data from Salesforce:

from crewai_tools.tools import CrmDataType, CrmToolFactory, CrmType

# Create Salesforce tool instance using the factory
sf = CrmToolFactory.create_crm_tool(CrmType.SALESFORCE)

# Fetch Salesforce data (e.g., contacts)
contacts = sf.fetch_data(CrmDataType.CONTACTS)
print(contacts)

Creating HubSpot Tool Using Factory

Similarly, you can create a HubSpot tool instance using the factory and fetch data from HubSpot:

from crewai_tools.tools import CrmDataType, CrmToolFactory, CrmType

# Create HubSpot tool instance using the factory
hs = CrmToolFactory.create_crm_tool(CrmType.HUBSPOT)

# Fetch HubSpot data (e.g., contacts)
contacts = hs.fetch_data(CrmDataType.CONTACTS)
print(contacts)

@joaomdmoura
Copy link
Collaborator

Disclaimer: This review was made by a crew of AI Agents.

Code Review Comment: CRM Tools Implementation

Overview

The PR integrates CRM tools for Salesforce and HubSpot, implementing a factory pattern alongside abstract base classes for better extensibility. While the architecture appears well-structured, several areas require improvement to enhance maintainability, reliability, and code quality.

Code Quality Findings

1. Import Organization

  • Issue: Relative imports are present in crm_tool_factory.py, posing potential issues across varying contexts.
  • Suggestion: Use absolute imports instead. Example:
    from crewai_tools.tools.crm_tool.crm_tool import CrmBaseTool, CrmType

2. Error Handling

  • Issue: Generic exception handling in crm_tool.py may mask specific errors and lacks adequate logging.
  • Suggestion: Implement specific exception handling with logging. Example adjustment:
    except requests.exceptions.HTTPError as http_err:
        logging.error(f"HTTP error occurred: {http_err}")

3. Configuration Management

  • Issue: Scattered environment variables without centralized validation can lead to misconfigurations.
  • Suggestion: Use a dataclass for managing configuration, integrating validation checks. Example:
    @dataclass
    class CrmConfig:
        ...
        @classmethod
        def from_env(cls, crm_type: CrmType) -> "CrmConfig":
            ...

4. Type Safety

  • Issue: Many methods lack type hints, which can lead to confusion.
  • Suggestion: Ensure all functions and methods include type hints. Example for hubspot_tool.py:
    def fetch_crm_data(self, data_type: CrmDataType) -> Dict[str, Any]:
        ...

5. Documentation

  • Issue: Several methods lack docstrings, making it harder for future developers to understand their usage.
  • Suggestion: Add comprehensive docstrings for methods, including parameters and return types as demonstrated:
    def fetch_crm_data(self, data_type: CrmDataType) -> Dict[str, Any]:
        """Fetches CRM data based on type.
        Args:
            data_type (CrmDataType): Type of data to fetch.
        Returns:
            Dict[str, Any]: Fetched data.
        """

6. Testing Considerations

  • Recommendation: Introduce unit and integration tests, particularly around API interactions and factory functionality. Utilize mock responses to validate behavior without making actual HTTP calls.

Historical Context

Moving forward, consider the context provided by previous pull requests that implemented similar patterns or tackled related challenges. For example, reviewing documentation updates in past PRs may provide insights into improving documentation practices for this implementation.

Implications for Related Files

The enhancements suggested in this review directly impact the cohesion and robustness of the entire CRM tools integration. As the base class CrmBaseTool is extended by HubSpotTool and SalesforceTool, improvements in error handling and type safety will propagate through these classes, bolstering the overall reliability.

Conclusion

Implementing these changes will significantly improve code maintainability, enhance developer experience, and ensure a more robust integration with the CRM platforms. Following established best practices in error handling, documentation, and configuration management will create a solid foundation for future enhancements.

Thank you for your efforts in integrating these CRM tools; looking forward to the refined version of this implementation!

@ksivam ksivam changed the title Add crm tool with salesforce and hubspot support feature: Add crm tool with salesforce and hubspot support Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants