Hubspot custom code SMS integration

Updated 10 months 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
  1. Click on "Private Apps" from the Settings menu and click on "Create a private app" button.

  1. Add the "Name" and "Description" for the app.

  1. Select the scopes as shown in the image and click on "Create app" button.

  1. Click and copy the token.

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

  2. Click on "Custom code" from the action list.

  3. Click on "Choose a secret" dropdown menu and select "Add secret" to add your Private app token.

  4. Type "HPKEY" as "Secret name" and paste the Private app token under the "Secret value". Click on "Save" button.

  5. We will need your ClickSend credentials to send SMS - ClickSend Credentials.
  6. Copy your username from the ClickSend dashboard and click on "Add secret".

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


  8. Repeat the steps 8 and 9 to add your ClickSend API Key, use "CSKEY" as "Secret name" and API Key as "Secret value".

  9. Remove the existing code, then copy and paste the code from “Hubspot_CustomCode.js” in the “Code” section. Make sure Hubspot Client v3 is selected.




    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({"accessToken":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,
    },
    });
    };

  10. Change the content of message body as per the need. Click on the “Save” button after making the changes.

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)