Hi, I am new to Webhooks, API calls & request, and SDKs. I have been building Messenger Bots using ManyChat & FlowXO, but now I am required to create a Messenger bot via Facebook for Developers using Node.JS for the code. I have just finished the “Quick Start Tutorial”; and now, I am trying to personalize messages; but currently encountering problem as iterated below with the sample code.
*CODE written in NODE.JS deployed at Glitch.com
*a GET request for facebook’s Profile API using Express Http framework or (node module)
function ProfileName(senderID)
{
let JSbody;
request (
{ “uri”: “https://graph.facebook.com/v2.6/” + senderID,
“qs”: { “access_token”: process.env.PAGE_ACCESS_TOKEN,
“fields”: first_name , last_name , profile_pic },
“method”: “GET”,
},
function callback(err, res, body)
{ JSbody = JSON.parse(body);
console.log ( JSbody ); // < this will contain a json object with first_name , last_name & profile_pic data
);
console.log ( JSbody ); // < but this will contain a json object with an undefined value. WHY ?
return JSbody.first_name; // < and also this one will return an undefined value
}
*1) It seems that any value inside the ‘callback()’ function cant be used outside the ‘request()’ function even if you assigned it to a variable defined/initialized outside the request() function.
*2) can any one provide a solution on how to use the value of the “body” parameter from the ‘callback()’ function, outside the ‘request()’ function, such as returning the value of first_name as the return value of function ‘ProfileName()’.
*3) OR, is there an alternative for the ‘request()’ function in making API request & how, to avoid this problem I have?