Getting the number of the caller in twilio

113 Views Asked by At

I want to know the current callers(client) Phone Number and then be able to verify it from my HubSpot CRM to see which language my client speaks. Then Enqueue the call to the agent that speaks the language or give the Caller an option to choose the preferred language.

Anyhoo

Right now my goal is to just get the number of the caller(client).

exports.handler = function(context, event, callback) {
    console.log(event);
     //const request = require('request');
     //const client = require('twilio');
     const res = new Twilio.Response();
     res.appendHeader('Access-Control-Allow-Origin', '*');
     res.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST');
     res.appendHeader('Content-Type', 'application/json');
     res.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
    const fetch = require("node-fetch");
    var phoneNumber = event.From; //|| [ "+63XXXXXXXXXX" ];
    const apiUrl = "https://myapiurl.com/contact-details.php?number="+phoneNumber;
    fetch(apiUrl,{
        method: 'GET',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded'}
    })
    .then( response =>{
        return response.json();
    })
    .then(json=>{
        console.log(json);
        res.setBody(json);
        callback(null,res);
    })
    .catch(error=>{
        console.log(error);
        callback();
    });
};

Also let me know if there is a better way to do it.

1

There are 1 best solutions below

0
On

Twilio developer evangelist here.

Your code shows you getting event.From. This is the number of the person calling, so you have already done it!