javascript Node.js API同步hubspot和Pipedrive

lzfw57am  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(88)

我正在尝试整合hubspot和piepdrive。我在尝试同步两个数据列表。问题是它同步,但不更新在pipedrive。
在pipedrive中,我有一个客户端列表,我想从hubspot更新,所以我使用node来自动化这个过程。我想使用node.js代码将hubspot与piepdrive集成。
下面是我的代码:

const axios = require('axios');
require('dotenv').config(); // Load environment variables

// Pipedrive API credentials
const pipedriveApiKey = process.env.PIPEDRIVE_API_TOKEN; // Use dotenv for secret management
const pipedriveApiUrl = 'https://Rian-Sandbox.pipedrive.com/v1';

// HubSpot API credentials
const hubspotApiKey = process.env.HUBSPOT_API_KEY; // Use dotenv for secret management
const hubspotApiUrl = 'https://api.hubapi.com/crm/v3/objects/contacts';

// Axios configurations for headers
const pipedriveConfig = {
  headers: {
    'Authorization': `Bearer ${pipedriveApiKey}`,
  },
};

const hubspotConfig = {
  headers: {
    'Authorization': `Bearer ${hubspotApiKey}`,
  },
};

// Function to get contacts from HubSpot
async function getHubSpotContacts() {
  try {
    const response = await axios.get(`${hubspotApiUrl}`, {
      params: {
        limit: 100, // Adjust the limit as needed
      },
      headers: hubspotConfig.headers,
    });

    const hubSpotContacts = response.data.results; // Adjust the property based on the actual response structure

    if (!Array.isArray(hubSpotContacts)) {
      console.error('HubSpot contacts response is not an array:', hubSpotContacts);
      return; // Exit the function or handle the error as needed
    }

    return hubSpotContacts;
  } catch (error) {
    console.error('Error fetching HubSpot contacts:', error.message);
    throw error;
  }
}

// Function to create or update a contact in Pipedrive
async function createOrUpdatePipedriveContact(contact) {
  try {
    // Check if the contact already exists in Pipedrive based on email
    const response = await axios.get(`${pipedriveApiUrl}/persons/find`, {
      params: {
        term: contact.email,
      },
      headers: pipedriveConfig.headers,
    });

    const existingPerson = response.data.data[0];

    if (existingPerson) {
      // Update the existing person
      const updatedResponse = await axios.put(`${pipedriveApiUrl}/persons/${existingPerson.id}`, contact, {
        headers: pipedriveConfig.headers,
      });
      console.log('Contact updated in Pipedrive:', contact.firstname, contact.lastname);
      return updatedResponse.data.data;
    } else {
      // Create a new person in Pipedrive
      const createdResponse = await axios.post(`${pipedriveApiUrl}/persons`, contact, {
        headers: pipedriveConfig.headers,
      });
      console.log('Contact created in Pipedrive:', contact.firstname, contact.lastname);
      return createdResponse.data.data;
    }
  } catch (error) {
    console.error('Error creating/updating Pipedrive contact:', error.message);
    throw error;
  }
}

// Function to sync contacts from HubSpot to Pipedrive
async function syncHubSpotToPipedrive() {
  try {
    const hubSpotContacts = await getHubSpotContacts();

    if (!Array.isArray(hubSpotContacts)) {
      console.error('HubSpot contacts response is not an array:', hubSpotContacts);
      return; // Exit the function or handle the error as needed
    }

    for (const contact of hubSpotContacts) {
      if (
        contact.properties &&
        contact.properties.firstname &&
        contact.properties.lastname &&
        contact.properties.email
      ) {
        // Customize the contact mapping according to your needs
        const pipedriveContact = {
          name: contact.properties.firstname.value + ' ' + contact.properties.lastname.value,
          email: contact.properties.email.value,
          phone: contact.properties.phone ? contact.properties.phone.value : '',
        };

        await createOrUpdatePipedriveContact(pipedriveContact);
      } else {
        console.error('Contact data is incomplete or missing:', contact);
      }
    }

    console.log('HubSpot to Pipedrive sync completed.');
  } catch (error) {
    console.error('Error during HubSpot to Pipedrive sync:', error.message);
  }
}

// Function to start the synchronization
async function startSync() {
  await syncHubSpotToPipedrive();
}

// Call the function to start the synchronization
startSync();

终端输出:

HubSpot to Pipedrive sync completed.
rianc@Rians-MacBook-Pro hubspot % node synchro
Contact data is incomplete or missing: {
  id: '1',
  properties: {
    createdate: '2020-11-29T18:27:35.318Z',
    email: '[email protected]',
    firstname: 'Maria',
    hs_object_id: '1',
    lastmodifieddate: '2022-04-26T19:31:14.645Z',
    lastname: 'Johnson (Sample Contact)'
  },
  createdAt: '2020-11-29T18:27:35.318Z',
  updatedAt: '2022-04-26T19:31:14.645Z',
  archived: false
}
3bygqnnd

3bygqnnd1#

我也在尝试同样的问题。您正在使用电子邮件将Hubspot联系人与Pipedrive进行匹配,因为电子邮件地址可能会更改,因此电子邮件并不是唯一的标识符。为了解决这个问题,我在Pipedrive中创建了一个名为hubspotID的属性,我已经有了一个脚本,可以在Pipedrive中从Hubspot创建联系人。所以Pipedrive接收hubSpot ID并将其插入该属性。你怎么知道你的脚本同步两个标准物质?

相关问题