0

I have created a Twilio function that I would like to use to send my affiliate referral link to subscribers of an application that come through my channel.

It works fine with a static to / from number, however I would like to make the "to" field a dynamic variable that can be manipulated via a HTTP/Webhook POST when a Zapier detects a new subscriber to my Mailchimp mailing list and pass their phone number as the variable.

I am also unclear what I need to do to authenticate the client (Zapier) that is making the POST as I do not want the function open to the world to use, if any insights can be shared on this it would be sincerely appreciated - I am a very inexperienced programmer trying to learn very quickly!

@philnash - thanks for your suggestion, implementing it slowly!

Many thanks in advance!

exports.handler = function(context, event, callback) {
  const appCodes = ['code1', 'code2', 'code3', 'code4']
  var smsBody = refCode ();

function refCode () {
    return appCodes[Math.floor((Math.random() * appCodes.length))];
};
  
  context.getTwilioClient().messages.create({
    to: '+11112223333', // How do I make this dynamic from HTTP/Zapier Webhook POST???
    from: '+1444555666',
    body: `Get the App: ${smsBody}`
  }).then(msg => {
    callback(null, msg.sid);
  }).catch(err => callback(err));
}

3 Answers3

0

Twilio developer evangelist here.

I presume the Zapier webhook is sending the details, including the phone number, as the body of the POST request.

All the parameters in a request body appear on the event object that is passed into your handler. You probably want to run a test where you print out the contents of the event object to see what you are being passed. You can do this with:

exports.handler = function(context, event, callback) {
  for (let key in event) {
    console.log(`${key}: ${event[key]}`);
  }
  // ... rest of the function
}

Then, when you figure out what parameter is storing the number, you can use that in the call to create the message.

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • can I actually put this in my twilio function code?? or do I have to run this in a standalone node.js app? – xXUnderToesXx Jan 30 '19 at 09:25
  • I am having trouble figuring out where to actually put that to make it work! – xXUnderToesXx Jan 30 '19 at 09:43
  • Oh, I'd just throw it in the top of your Twilio Function, within the handler function. I'll edit the answer to be more clear. – philnash Jan 30 '19 at 22:57
  • Thanks Phil, ill give it another go! Once I implement this code, im publishing it and refreshing the URL in my browser to try and see the output - will let you know how I go @philnash – xXUnderToesXx Jan 31 '19 at 08:02
  • I keep getting this error message when i run it within the function "runtime application timed out" when I call it from the browser - I am literally using the same code above in a standalone function just so I can see what parameters are available. – xXUnderToesXx Jan 31 '19 at 08:08
  • Oh, if you just use that code in a standalone function it will time out because you never call the `callback`. If it is standalone, just add `callback(null, "OK");` as the last line of the function and it won't time out. Did you find out what parameters the webhook is sending you? – philnash Jan 31 '19 at 12:24
  • Thanks Phil, yes that fixed the timeout, thank you! apologies for the drawn out queries, but the console.log command doesnt appear to be posting anything to the webpage, im not sure how I am supposed to see the outputto view the available parameters? just to clarify, this is a twilio function in "runime -> functions -> manage" that you create within the twilio console, not in a standalone node.js application and I am executing the function by loading the respective URL in a web browser @philnash – xXUnderToesXx Feb 01 '19 at 08:17
  • Yup, the `console.log` should log things out to the log that is underneath the Function code within the Twilio console. – philnash Feb 01 '19 at 14:03
  • Ok unfortunately I couldnt list the available parameters, but i figured out how to do what I was trying to achieve a slightly different way... I realised I could create my own variables in the "event" context passed through via POST, so I just created another variable, sourced it from the event context and everything worked perfectly!! @philnash – xXUnderToesXx Feb 03 '19 at 06:03
  • Brilliant, glad you got it sorted! – philnash Feb 03 '19 at 22:15
0

Try this:

exports.handler = function(context, event, callback) {
  for (let key in event) {
    console.log(`${key}: ${event[key]}`);
  }
  // ... rest of the function
  callback(null, 'complete');
};
Alan
  • 10,465
  • 2
  • 8
  • 9
  • Thank you for your suggestion, when I refresh the URL for my function it just returns "complete" on the webpage, but it gets rid of the timeout error! Will console.log actually display output on a webpage? – xXUnderToesXx Feb 01 '19 at 08:14
0

Thanks everyone for your input, it was sincerely appreciated! I was able to solve this with the following code:

exports.handler = function(context, event, callback) {
  const appCodes = ['code1', 'code2', 'code3', 'code4']
  var smsBody = refCode ();
  var subNum = event.primaryPhone || 'There is no subscriber number'; // primaryPhone sent via HTTP post to twilio function

function refCode () {
    return appCodes[Math.floor((Math.random() * appCodes.length))];
};

  context.getTwilioClient().messages.create({
    to: `${subNum}`, // parameters & values recieved from HTTP POST are available within the twilio functions "event" context
    from: '+1444555666',
    body: `Get the App: ${smsBody}`
  }).then(msg => {
    callback(null, msg.sid);
  }).catch(err => callback(err));
}