How to use Caller phone number as a function paramiter for http request?

endzawana
endzawana
edited July 19 in Functions

I want to pass it dinamically as part of json.

exports.handler = function(context, event, callback) {
var Client = require("@paymentsds/mpesa").Client;

var client = new Client({
apiKey: 'xxxxxxxxx',
publicKey: 'yyyyyyy',
serviceProviderCode: '11221122'
});

var paymentData = {
from: '8xxxxxxxx', // input_caller phone number
reference: '111111',
transaction: '2222222',
amount: '10'
};

client.receive(paymentData).then(function(r) {
// Handle success scenario

}).catch(function(e) {
// Handle success scenario
});

Best Answer

Answers

  • qborreda
    qborreda ✭✭✭

    Hi @endzawana

    When you call a Twilio Function, any params end up being part of the event function param.
    So, find anything you are sending like event.thePhoneNumber you are sending dynamically.

    In the case of executing this from a taskRouter webhook this changes to be within the task attributes. In that case, event.task.attributes.thePhoneNumber or similar.

    Hope that helped.
    Cheers

  • hey qborreda, Can you give me an example please? You may use the funcition above.

  • qborreda
    qborreda ✭✭✭

    Ok, so here is one ..

    On the code of a Flex plugin, I need to call a Twilio Function endpoint passing some phoneNumber to get all the call recordings from it. (it's just an example)

    const data = {
      phoneNumber: '+44xxxxxxxxxx',  // this should be a real value
    }
    
    fetch(`${serverlessDomain}/search-recordings`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data),
        })
    

    Now, the search-recordings Twilio Function will have to get that phoneNumber from the event like this:

    exports.handler = function(context, event, callback) {
    
    const {phoneNumber} = event; // object destructuring
    OR
    const phoneNumber = event.phoneNumber; // plain assignment
    
    

    So basically, event refers to any payload sent to the function, both via POST or GET methods.

  • endzawana
    endzawana
    edited July 6

    Hey qborreda,

    Thank you for your reply.

    I tried event.From and I am having the error below.
    ...
    const phoneNumber = event.From; // expected to be +258843671893
    JSON.stringify(phoneNumber);
    const phoneNumNorm = phoneNumber.substring(4); // I Need 843671893 to json

    var paymentData = {};
    paymentData['from'] = phoneNumNorm; //add dynamically the value 843671893 to from key
    paymentData['reference'] = '11114';
    paymentData['transaction'] = 'T12344CC';
    paymentData['amount'] = '10';
    ...

    The error I am getting:

    "message": "Cannot read property 'substring' of undefined",
    "name": "TypeError",
    "stack": "TypeError: Cannot read property 'substring' of undefined\n ...

    Please help.

    Best regards,
    Ernesto

  • It looks like the event.From is undefined or null.
    Any idea of how to add dynamically de value?

    declaring the variable like below it works.

    const phoneNumber = event.From || '+258843671893';

  • qborreda
    qborreda ✭✭✭

    Well, if you are not sending a From param on the http request to that twilio function, then event.From would be undefined indeed.

    Where are you sending this request from? Is it a webhook?

  • Yes it is a webhook. The function is invoqued when the caller selects an option from IVR Menu (example: check balance).
    I want to use the caller phone number (+258843671893) to send on the http request without country code (843671893).

Sign In or Register to comment.