Created
April 8, 2022 14:07
-
-
Save jackcoldrick90/914b48bcf05d23f475890d940ad80d83 to your computer and use it in GitHub Desktop.
Using the Conversations Inbox & Messaging API (https://developers.hubspot.com/docs/api/conversations/conversations) we can pull the body of an email, parse the content and create a contact in the CRM using the Contact API (https://developers.hubspot.com/docs/api/crm/contacts).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. Import required libraries | |
const hubspot = require('@hubspot/api-client'); | |
const axios = require('axios'); | |
exports.main = async (event, callback) => { | |
//2. Store threadID in variable, we'll use this to pull information | |
const threadID = event.inputFields['hs_thread_id']; | |
//3. Make a request to the conversations API to retrieve text - https://developers.hubspot.com/docs/api/conversations/conversations | |
axios.get("https://api.hubapi.com/conversations/v3/conversations/threads/" + threadID + "/messages/?hapikey=" + process.env.HAPIKEY, {}) | |
.then((response) => { // Run code when we get response | |
console.log(response.data.results[1].text) //debugging can be removed | |
//4. Parse text and store data in variables | |
//4.1 Get email address | |
var email = response.data.results[1].text.match(/[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+/); | |
console.log("Email address is: " + email); | |
//4.2 Get Name - (?<=TERM: )(.*) - JS doesn't support positive lookbehinds... | |
var name = response.data.results[1].text.match(/(?=name: )(.*)/); | |
console.log("Name is: " + name[1].split(" ")[1] + " " + name[1].split(" ")[2]); | |
var fname = name[1].split(" ")[1]; | |
var lname = name[1].split(" ")[2]; | |
//4.3 Get phone number - (?<=Phone: )(.*)(?= Email:) | |
var phone = response.data.results[1].text.match(/(?=Phone: )(.*)/); | |
console.log("Phone is: " + phone[0].split(" ")[1]); | |
//4.4 Get message - (?<=Message: )(.*) - Could create engagement with this information | |
var message = response.data.results[1].text.match(/(?=Message: )(.*)/); | |
console.log("Message is: " + message[1].split(" ")[1]); | |
console.log(message[0]); | |
//5. Create Contact using email address - https://developers.hubspot.com/docs/api/crm/contacts | |
const hubspotClient = new hubspot.Client({ | |
apiKey: process.env.HAPIKEY | |
}); | |
const properties = { | |
"email": email[0], | |
"firstname": fname, | |
"lastname": lname, | |
"phone": phone[1].split(" ")[1] | |
}; | |
const SimplePublicObjectInput = { properties }; | |
console.log('CONTACT CREATE'); | |
hubspotClient.crm.contacts.basicApi.create(SimplePublicObjectInput).then(results => { | |
console.log("SUCCESS: Contact created with email address: " + email[0]) // Success | |
}).catch(error =>{ | |
console.log("ERROR: Contact not created"); // Error | |
}); | |
}).catch(error=>{ | |
console.log(error); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment