Skip to content

Twilio Studio Integration

Twilio Studio is a visual interface for building communication workflows. If you use Studio to power your SMS workflow, you can insert dynamic offers from Advertising.chat using the same graphical tools you use to design your workflow.

This guide will walk you through the steps of creating a Twilio Function that will enable you to deliver offers from within your workflow. You'll only have to do this initial configuration one time; after you've created the Function, you can add it to any part of your workflows with drag-and-drop.

Create a Fetch Function

  1. From the menu on the left side of the Twilio Console, select the Functions option under Runtime.

  2. Click the Create a Function button from the Functions page.

  3. In the New Function dialog, select the "+ Blank" option and click Create.

  4. In the box labeled FUNCTION NAME, enter "Ad Fetch".

  5. In the box labeled PATH, enter "/ad-fetch".

  6. Copy the following code block into the CODE editor under Configuration:

var axios = require('axios');
exports.handler = function (context, event, callback) {
  delete context['AUTH_TOKEN'];
  axios
    .post(
      'https://webhooks.advertising.chat/twilio',
      { context, event },
      { params: { apiKey: context.ADCHAT_API_KEY || event.ADCHAT_API_KEY } }
    )
    .then(function (response) {
      if (response.status !== 200) return callback();
      var twiml = new Twilio.twiml.MessagingResponse();
      twiml.message(response.data);
      callback(null, twiml);
    })
    .catch(callback);
};

Click Save to save the new Function.

One last thing:

  1. Click on Configure in the Functions menu to go to the Functions Configuration page.

  2. In the Environment section, add an environment variable named ADCHAT_API_KEY and give it the API key value from the Advertising.chat Console.

  3. Scroll down to the section labeled Dependencies, and click the "plus" (+) button. Add an entry for axios with version 0.19.2.

Click Save and we're done!

You've now created a Function that can be used from within your Twilio Studio workflow any time you want to fetch a dynamic offer from Advertising.chat. In the future, you can just start with the next section.

Add the Fetch Function to your Workflow

Now that you've created a function that calls Advertising.chat, you can add it to your workflow easily.

Head over to the Studio Dashboard and select the flow you want to integrate Advertising.chat into. (If you don't already have a flow, you can use the button to create a flow, and select the Blank template from the New Flow dialog so that you can follow along.)

  1. From the WIDGET LIBRARY on the right hand side, select Run Function from the TOOLS & EXECUTE CODE section. (You need to drag the Run Function onto the canvas)

  2. Select the new widget, and in the RUN FUNCTION settings select the "Ad Fetch" function from the FUNCTION URL dropdown.

  3. Drag a line from the Trigger block's "Incoming Message" connector to the new widget.

  4. Click Publish to publish your updated workflow.

Customizing Offer Selection

By default, using the Ad Fetch Function in a widget will fetch an offer from Advertising.chat using the API key that was set up in the Environment section. Advertising.chat has the concept of Moments, which are similar to "ad units" in web-based advertising platforms, and let you control the selection of offers in your workflow based on "where" in the conversation you are with a user.

You can specify any of these Function Parameters:

KEY VALUE
moment the moment code to fetch
subscriber a unique identifier for the current subscriber
apiKey can be used to override the API key specified in the Environment