Using functions in Twilio Studio

732 views
Skip to first unread message

tomasz.k...@docplanner.com

unread,
Mar 28, 2018, 7:45:36 AM3/28/18
to Twilio Functions


Hello,

I am trying to build flow in Studio which will:

- receive SMS, parse message body, use parsed number as parameter for my http request (client's PABX is sending to my Twilio number sms with info about non answered call like 'You have unanswered call from 227636555 at 14:45')
- i want to get 227636555 from text messages and use it as one of parameters of my http request (in studio project below for test reason i just try to send back sms with number as message body)





When i am using my function_1 defined as

exports.handler = function(context, event, callback) {
  
  let regexp = /[1-9]{9}/;
  var str =  event.Body;  
  var matches_array = str.match(regexp);
  
  var numer = matches_array[0];
//  callback(null, {"numer": numer});
  
var client = context.getTwilioClient();
client.messages.create({
  to: event.From,
  from: event.To,
  body: numer
}, function(err, res) {
   callback(null,twiml);              
})
 
};

and connected directly to Twilio number it is working just FINE and i am receiving SMS from Twilio with just number as body.


When i am using my function_1 defined as

exports.handler = function(context, event, callback) {
  
  let regexp = /[1-9]{9}/;
  var str =  event.Body;  
  var matches_array = str.match(regexp);
  
  var numer = matches_array[0];
  callback(null, {"numer": numer});
 
};

testing it with browser and Twilio function link (https://ultra-xxx.twil.io/numer?Body=You%20have%20unanswered%20call%20from%20227636555%20at%2014:45' i receive proper info like {"numer":"227636555"}.

When using it in flow it produces error - which i can recreate when i will change .twil.io/numer?Body=You%20h  to .twil.io/numer?body=You%20h


 "widgets": {
    "send_message_1": {},
    "function_1": {
      "status_code": 500,
      "content_type": "application/json",
      "parsed": {
        "message": "Cannot read property 'match' of undefined",
        "name": "TypeError",
        "stack": "TypeError: Cannot read property 'match' of undefined\n    at Object.exports.handler (/var/task/handlers/ZFf75157b5c8bf2e3fb63edec4132b4f3e.js:8:26)\n    at Object.exports.handler (/var/task/node_modules/enigma-lambda/index.js:268:10)\n    at exports.handler (/var/task/enigma.js:17:9)"
      },
      "body": "{\"message\":\"Cannot read property 'match' of undefined\",\"name\":\"TypeError\",\"stack\":\"TypeError: Cannot read property 'match' of undefined\\n    at Object.exports.handler (/var/task/handlers/ZFf75157b5c8bf2e3fb63edec4132b4f3e.js:8:26)\\n    at Object.exports.handler (/var/task/node_modules/enigma-lambda/index.js:268:10)\\n    at exports.handler (/var/task/enigma.js:17:9)\"}"
    }

Do you have any suggestion what i am doing wrong and how to set up my function to return value also in Twilio Studio?


regards
Tomek

shane elliott

unread,
Jul 27, 2018, 10:31:05 PM7/27/18
to Twilio Functions
I changed your function al little to get it to work.  

1) your regex needs to include 0's and at least in US a number has 10 numbers. 
2) I added some error processing I believe you were getting the undefined because you were not passing anything back.
3) you need to pass the message from step 1 in studio to the function
        Add: Body={{trigger.message.Body}} to the function_1 parameters:

Here is the modified function:

exports.handler = function(context, event, callback) {

let regexp = /[0-9]{10}/;
  var str =  event.Body;  
  var matches_array = str.match(regexp);
  console.log(event.Body)
  if(matches_array){
  var numer = matches_array[0];
  callback(null, {"nummer": numer});
   }
   else{
        callback(null, {"nummer": "no number found"});
   }

};

gives me just the number back.  Is that what you were trying to do?

Reply all
Reply to author
Forward
0 new messages