how to get dynamically the caller phone number and pass it as one of JSON key in Twilio run function wdget?

Viewed 33

I am new in Twilio and trying to develop an IVR that at some point runs a function (Run Fuuntion Widget). The function should send http request including the user phone number to a service provider to make payment for a product selected by the user.

I tryed the code bellow but the variable is not getting the user phone number.

exports.handler = function(context, event, callback) {
    let twiml = new Twilio.twiml.VoiceResponse();
    var Client = require("@paymentsds/mpesa").Client;
    var phoneNumber = event.From
    var phoneNumberNorm = phoneNumber.substring(4);
    
var client = new Client({
   apiKey: 'xxxxxxxxxxxxxxxxxx',             // API Key
   publicKey: 'xxxxxxxxxxxxxxxxxx',          // Public Key
   serviceProviderCode: '171717' // input_ServiceProviderCode
        });
var paymentData = {};
    paymentData['from'] = phoneNumberNorm;
    paymentData['reference'] = '11114';
    paymentData['transaction'] = 'T12344CC';
    paymentData['amount'] = '10';
client.receive(paymentData).then(function(r) {
    // Handle success scenario
        twiml.say("Your payment was successfull");
   callback(null, twiml);
}).catch(function(e) {
    // Handle success scenario
   twiml.say("Your payment failed");
   callback(null, twiml);
});
};
1 Answers

Twilio developer evangelist here.

You will need to pass the phone number into the Function via the Run Function widget.

You need to add a Function Parameter, for example From, and then the data you want to pass, which for the incoming phone number is {{contact.channel.address}}. Like so:

enter image description here

Then you can use event.From in your Function code.

Related