I am new with Node.js, and I have trouble to retrieve the facebook user "first name" using the SDK. I have a story call "greeting" in which I would like to answer "Hello {userName}".
I have defined a getUserName name action which is related to "greeting" story:
getUserName({context, entities}) {
if(context.fbid) {
return new Promise(function(resolve, reject) {
if (context.userName) {
return requestUserName(context)
.then((json) => {
context.userName = json.first_name;
return context;
})
.catch((err) => {
console.error('Oops! An error occurred while asking the name of the user: ',
err.stack || err);
});
} else {
return resolve(context);
}
});
} else {
console.error('Oops! Couldn\'t find user for session:', sessionId);
// Giving the wheel back to our bot
return Promise.resolve()
}
}
where requestUserName is defined as follows:
const requestUserName = (id) => {
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/v2.8/' + id +'?' + qs)
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
Even if the action seems to be triggered, it doesn't work and I have no idea of the reason :( Surprisingly, another action is triggered but I don't know why since this action is not related to the "greeting" story. I think I missed some key concept of the Node.js SDK.
Help would be greatly appreciated :)