Skip to content

Request offers from Twilio Studio

A Twilio Studio flow can send Offer Manager offers over SMS. A Twilio Function calls the Fetch API and returns the offer's text, and the flow sends it with a Send Message widget. This document describes how to create the function and add it to a flow. For the request in general, see Request an offer.

Before you begin

  • Create a moment, such as after-check-in, and a rule with at least one offer for it.
  • Get an API key from API Keys in the Advertising.chat console's account menu, and the ID of the agent your moments belong to.
  • You need a Twilio account with a Studio flow that handles incoming SMS.

Create the function

  1. In the Twilio Console, open Functions and Assets, and create a service, such as adchat.
  2. Add a function with the path /offer-fetch, and replace its code with the following:

    const axios = require('axios');
    const crypto = require('crypto');
    
    exports.handler = async function (context, event, callback) {
      try {
        const response = await axios.post(
          'https://ads.advertising.chat/v1/fetch/v2',
          {},
          {
            params: {
              key: context.ADCHAT_API_KEY,
              application: context.ADCHAT_APPLICATION,
              adUnit: event.moment,
              format: 'sms',
              subscriber: crypto.createHash('sha256').update(event.from || '').digest('hex'),
            },
            validateStatus: status => status === 200 || status === 204,
          }
        );
        if (response.status !== 200) return callback(null, { text: '' });
        return callback(null, {
          text: response.data.payload.text,
          messageId: response.data.messageId,
        });
      } catch (err) {
        return callback(null, { text: '' });
      }
    };
    
  3. In the service's environment variables, add ADCHAT_API_KEY with your API key as the value, and ADCHAT_APPLICATION with the agent's ID.

  4. In the service's dependencies, add axios.
  5. Deploy the service.

The function hashes the sender's phone number, so Advertising.chat receives an opaque subscriber identifier for frequency caps instead of the number itself. On any error, the function returns empty text, so the flow continues without an offer.

Add the function to a flow

  1. In Studio, open the flow, and drag a Run Function widget onto the canvas at the point where an offer could fit.
  2. In the widget's settings, select your service, its environment, and the /offer-fetch function.
  3. Add two function parameters: moment with the moment's code, such as after-check-in, and from with the value {{trigger.message.From}}.
  4. Connect the widget's success transition to a Split Based On... widget that tests whether {{widgets.YOUR_WIDGET_NAME.parsed.text}} is empty.
  5. On the not-empty branch, add a Send Message widget whose body is {{widgets.YOUR_WIDGET_NAME.parsed.text}}.
  6. Publish the flow.

The SMS text is the offer's Alt Text followed by a click-tracking link, so taps are recorded automatically.

What's next