Serverless read POST Variables in sensible way

November 24, 2017, at 1:27 PM

I have the following serverless lambda function code below...

export function incoming_calls(event, context, callback) {
  var util = require("util");
  const VoiceResponse = require('twilio').twiml.VoiceResponse;
  const twiml = new VoiceResponse();
  //Determine message to give based on parameters given
  var theMessage = "Incoming Pool Service Leed";
  var querystring = require("querystring");
  var qMessage = querystring.stringify({message: theMessage});
  var whisperURL = "https://" + process.env.DOMAIN_NAME + "/twilio/whisper?" + qMessage;
  //twiml.say({ voice: 'woman' }, 'hello world!');
  twiml.dial({ record: 'true'}).number({url: whisperURL},'407-947-0503');
  //twiml.say({ voice: 'woman' }, 'This is a test!');
  //Useful Info
  var queryString = util.inspect(event.body);
  sendemail("","Incoming Call POST Request",queryString);
  callback(null, successXml(twiml.toString()));

This code works and sends me an email with the POST variables but as one long string looking like this...

'Called=%2B15622222222&ToState=CA&CallerCountry=US&Direction=inbound&CallerState=CA&ToZip=90670&CallSid=CA47e5131dd5e7375190aaf5fab773a9b0&To=%2B15622221234&CallerZip=92606&CallerName= ... etc

How do I convert the POST parameters I am receiving from Twilio into a nicer format, maybe an Array or Object instead of a String?

Answer 1

The returned string looks like a simple query string, so you could just parse the query string in vanilla js or try to find another method to use instead of toString() to get the value you need.

Rent Charter Buses Company
node.js variable inside HTML a href attribute

node.js variable inside HTML a href attribute

I have a variable of labels which is an array in Nodejs

Debug a node.js process forked via child_process.fork in VSCode

Debug a node.js process forked via child_process.fork in VSCode

I'm trying to debug a Node script forked using child_process's fork method using VSCode

Sequelize synchronization on deploy

Sequelize synchronization on deploy

I've started using Sequelize for my projects and I don't know if I'm on the right way for the model relations init