If you’re looking for a way to handle incoming SMS messages and reply to them from your own phone, Twilio offers an easy and flexible solution. With Twilio’s programmable SMS platform, you can set up a function that receives incoming messages and responds to them automatically, making it easy to stay connected with your customers and clients.

In this tutorial, we’ll show you how to set up a Twilio function that handles incoming SMS messages and allows you to reply to them from your own phone. We’ll also show you how to modify the function to auto-respond when a HubSpot form is submitted.

Step 1: Set up a Twilio account

If you don’t already have a Twilio account, the first step is to sign up for one. You’ll need a Twilio account in order to set up the SMS function.

Step 2: Create a new Twilio function

Once you’ve set up your Twilio account, the next step is to create a new function. To do this, go to your Twilio dashboard and navigate to the “Functions” section.

Click the “Create a new Function” button and choose “Blank” as the template. Name your function something like “SMS Handler” and select “SMS” as the trigger.

Step 3: Set up the SMS handler function

Now that you’ve created a new function, it’s time to set up the SMS handler code. Here’s an example code that you can use as a starting point:

exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.MessagingResponse();
  const sender = event.From;

  // If the message is from the recipient, send the message to the sender
  if (event.From === context.MY_PHONE_NUMBER) {
    const separatorPosition = event.Body.indexOf(':');
    if (separatorPosition < 1) {
      twiml.message('You need to specify a recipient number and a ":" before the message.');
    } else {
      const recipientNumber = event.Body.substr(0, separatorPosition).trim();
      const messageBody = event.Body.substr(separatorPosition + 1).trim();
      twiml.message({ to: recipientNumber }, messageBody);
    }
  } else {
    // If the message is from someone else, forward it to the recipient
    const recipientNumber = context.MY_PHONE_NUMBER;
    const messageBody = event.Body;
    twiml.message({ to: recipientNumber }, `${sender}: ${messageBody}`);
  }

  callback(null, twiml);
};

This code handles incoming SMS messages and allows you to reply to them from your own phone. When a message comes in, the function checks if it’s coming from your phone number. If it is, it splits the message into a recipient number and message body and sends the message to the recipient. If it’s not coming from your phone number, it forwards the message to your phone with the original sender’s phone number included in the message body.

Step 4: Add your phone number to the function

In order to use the SMS handler function, you’ll need to add your phone number to the function code. To do this, find the line that says const recipientNumber = context.MY_PHONE_NUMBER; and replace MY_PHONE_NUMBER with your own phone number.

Step 5: Test the SMS handler function

Now that you’ve set up the SMS handler function, it’s time to test it out. You can do this by sending an SMS message to your Twilio phone number. If everything is set up correctly, you should receive a reply that either forwards the message to your phone or sends a message to the recipient, depending on who the message is from.

Step 6: Set up the HubSpot form submission trigger

Now that we have a functioning SMS handler function, we can modify it to auto-respond when a HubSpot form is submitted. To do this, we’ll use HubSpot’s workflow automation.

In your HubSpot account, go to the Workflows dashboard and click the “Create workflow” button. Choose the “Standard” workflow type and name your workflow. In the workflow settings, set the enrollment trigger to “Contact submits a form” and choose the form that you want to use for the trigger.

Add any other workflow actions that you need, and then click the “Add action” button and choose “HTTP request”. Set the request method to “POST” and enter the Twilio webhook URL that will receive the HTTP POST request.

Step 7: Modify the SMS handler function

Now that we have the workflow set up, we can modify the SMS handler function to handle the incoming HTTP POST request and send the auto-response message.

exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.MessagingResponse();

  // If the request is coming from the HubSpot workflow trigger, send the auto-response message
  if (event.From === "hubspot") {
    const recipientNumber = event.To;
    const messageBody = "Thanks for submitting the form!";
    twiml.message({ to: recipientNumber }, messageBody);
  } else {
    // If the message is from the recipient, send the message to the sender
    if (event.From === context.MY_PHONE_NUMBER) {
      const separatorPosition = event.Body.indexOf(':');
      if (separatorPosition < 1) {
        twiml.message('You need to specify a recipient number and a ":" before the message.');
      } else {
        const recipientNumber = event.Body.substr(0, separatorPosition).trim();
        const messageBody = event.Body.substr(separatorPosition + 1).trim();
        twiml.message({ to: recipientNumber }, messageBody);
      }
  } else {
      // If the message is from someone else, forward it to the recipient
      const sender = event.From;
      const recipientNumber = context.MY_PHONE_NUMBER;
      const messageBody = event.Body;
      twiml.message({ to: recipientNumber }, `${sender}: ${messageBody}`);
    }
  }

  callback(null, twiml);
};

In this modified code, we’re checking if the HTTP POST request is coming from the HubSpot workflow trigger. If it is, we’re sending the auto-response message to the phone number specified in the “To” field of the request. If it’s not coming from the workflow trigger, we’re handling the message as before.

Step 8: Test the modified SMS handler function

Now that we’ve modified the SMS handler function, we can test it out by submitting the form that triggers the workflow in HubSpot. If everything is set up correctly, you should receive an auto-response message from Twilio.

Using Zapier to Integrate Twilio and HubSpot

If you prefer to use Zapier to integrate Twilio and HubSpot, you can follow these steps:

Create a new Zap in Zapier:

  • Log in to your Zapier account and click the “Make a Zap!” button.
  • Choose HubSpot as the trigger app and select the “New Form Submission” trigger.
  • Connect your HubSpot account to Zapier if you haven’t already done so.
  • Choose the form that you want to use for the trigger.
  • Test the trigger and make sure it’s working correctly.
  • Set up the Twilio action in Zapier:
  • Choose Twilio as the action app and select the “Send SMS” action.
  • Connect your Twilio account to Zapier if you haven’t already done so.

Set up the Twilio SMS message with the information that you want to send. For example:

To Number: {{form.Submitted by Contact Phone Number}}
From Number: +1234567890
Body: Thanks for submitting the form!

In this example, the phone number is pulled from the form submission data in HubSpot and the message content is static.

Test the Twilio action and make sure it’s working correctly.

Turn on the Zap and test it:

Turn on the Zap and make sure it’s working correctly by submitting the form in HubSpot and verifying that the text message is sent.

With this Zapier integration, whenever someone submits the form that triggers the Zap in HubSpot, Zapier will send a text message via Twilio to the specified phone number.

Whether you choose to use the Twilio function or Zapier, integrating Twilio and HubSpot can make it easier for you to stay connected with your customers and clients.

In this tutorial, we’ve shown you how to set up a Twilio function that handles incoming SMS messages and allows you to reply to them from your own phone. We’ve also shown you how to modify the function to auto-respond when a HubSpot form is submitted. By following these steps, you can easily set up an automated SMS messaging system that will keep you connected with your customers and clients

seo-growth-chart

Boost Your Local SEO Today!

Request a free SEO audit of your website from Tusk Creative Studios! Our experienced team will review your website and provide you with customized recommendations for improving your local SEO. Fill out the form below to get started and take the first step towards online success!

Our experienced team will review your website and provide you with customized recommendations for improving your local SEO.

Get Ready To Take Your
Business to New Heights!

If you are interested in growing with Tusk, enter your info to have one of our business strategist get in touch to schedule an intro call

Our experienced team will review your website and provide you with customized recommendations for improving your local SEO.

Get Ready To Take Your
Business to New Heights!

If you are interested in growing with Tusk, enter your info to have one of our business strategist get in touch to schedule an intro call

Our experienced team will review your website and provide you with customized recommendations for improving your local SEO.