Hubspot custom code SMS integration

Updated 1 week ago

This tutorial will shows how to send automated SMS using ClickSend API using the Hubspot CRM. For troubleshooting purposes please contact our support team.

Prequisites:

To get started you will need:

Configuring the trigger/s for the workflow:

  1. Click on the "Automation" tab from the menu bar and open the "Workflows" in a new tab from the drpdown menu.

    SelectWorkflow
  2. Click on "Create workflow" button.

    CreateWorkflow
  3. To keep this guide simple we will select "Contact-based" blank workflow, you can select any trigger you like as long as it contains the phone number field. After selecting the trigger and naming the workflow click on the "Next" button.

    ContactWorkflow
  4. Click on the "Set up triggers" button and filter on how the contacts enter the workflow.

    Filter
  5. For simplicity purposes we will filter contacts based on if the phone number field does not contain the alphabet 'a', so that we can select every contact. After setting up the filter click on the "Apply filter" button.

    FilterPhone
  6. If you want to cross check the filter setup click on "Test criteria" button. Click on "Save" button to save filter/s after the configuration.

    TestSaveFilter

Configuring the action for the workflow:

  1. Switch to the previous tab and click on the "Settings" icon.

    Settings
  2. Click and copy the API key from the Settings menu.

    API
  3. Switch to workflows tab and click on the "+" button to add the action.

    AddAction
  4. Click on "Custom code" from the action list.

    Code
  5. Click on "Choose a secret" dropdown menu and select "Add secret" to add your Hubspot API Key.

    HSS
  6. Type "HPKEY" as "Secret name" and paste the Hubspot API Key under the "Secret value". Click on "Save" button.

    HSP
  7. We will need your ClickSend credentials to send SMS - ClickSend Credentials

  8. Copy your username from the ClickSend dashboard and click on "Add secret".

    CSUD
  9. Type "CSUSERNAME" as "Secret name" and paste the username under the "Secret value". Click on "Save" button.

    CSUH
  10. Repeat the steps 14 and 15 to add your ClickSend API Key, use "CSKEY" as "Secret name" and API Key as "Secret value".

    CSUK
  11. Remove the existing code, then copy and paste the code from "Hubspot_CustomCode.js" in the "Code" section.

    CSUC
  12. Copy and change the content of message body as per the need from the code block below. Click on the "Save" button after making the changes.

const hubspot = require('@hubspot/api-client');
var axios = require('axios');

exports.main = async (event, callback) => {

try {
let firstName;
let phone;
let lastName;
const username = process.env.CSUSERNAME; // Your CS username via secret
const key = process.env.CSKEY; /// Your CS apikey via secret

const result = Buffer.from(username+":"+key).toString('base64');

const hubspotClient = new hubspot.Client({ apiKey: process.env.HPKEY });
const ApiResponse = await hubspotClient.crm.contacts.basicApi.getById(
event.object.objectId,
['phone', 'firstname', 'lastname']
);
phone = ApiResponse.body.properties.phone;
firstName = (ApiResponse.body.properties.firstname!=undefined) ? ApiResponse.body.properties.firstname : '';
lastName = (ApiResponse.body.properties.lastname!=undefined)? ApiResponse.body.properties.lastname : '';
// You can GET more fields such as company, email, using the Husbspot API

const message = 'Hi '+firstName+ ' '+'Hubspottesting'; // Add a custom message here

var data = JSON.stringify({
"messages": [
{
"body": message,
"to": phone,
"from": "", // Add your Sender ID (Optional)
"source":"Hubspot"
}
]
});

var config = {
method: 'post',
url: 'https://rest.clicksend.com/v3/sms/send',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic '+result
},
data : data
};

axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
} catch (err) {
throw err;
}
const email = event.inputFields['email'];
const phone = event.inputFields['phone'];

callback({
outputFields: {
email: email,
phone: phone,
},
});
};
CSUM

Testing (Optional)

  1. To test the action select a contact from the drop-down list and click on the "Test" button.

    TA
  2. The output should have a "SUCCESS" value.

    SUV

Publishing the Workflow

  1. Click on "Review and publish" button.

    RAP
  2. Review the Enrollment, Unenrollment and Timing options. Click on the "Turn on" button.

    WTO
  3. The workflow will now automatically send the SMS whenever a contact triggers the workflow.


How Did We Do?


Powered by HelpDocs (opens in a new tab)