0

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 :)

Nitsuja
  • 73
  • 6

1 Answers1

0

I solved my issue. If you are interested, here is my code.

The getUserName action:

  getUserName({sessionId, context, entities}) {
    const recipientId = sessions[sessionId].fbid;
    const name = sessions[sessionId].name;
    if(recipientId) {
      return new Promise(function(resolve, reject) {
        if (!name) {
          return requestUserName(recipientId)
          .then((json) => {
            context.userName = json.first_name;
            sessions[sessionId].name = json.first_name;
            resolve(context);
          })
          .catch((err) => {
            console.error('Oops! An error occurred while asking the name of the user: ',
              err.stack || err);
          });
        } else {
          // Retrieve the name of the user 
          context.userName = name;
          return resolve(context);
        }
      });
    } else {
      console.error('Oops! Couldn\'t find user for session:', sessionId);
      // Giving the wheel back to our bot
      return Promise.resolve()
    } 
  },

The user name request:

const requestUserName = (id) => {
  const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
  return fetch('https://graph.facebook.com/v2.8/' + encodeURIComponent(id) +'?' + qs)
  .then(rsp => rsp.json())
  .then(json => {
    if (json.error && json.error.message) {
      throw new Error(json.error.message);
    }
    return json;
  });
};
Nitsuja
  • 73
  • 6
  • Hi...Do you know if there is a way we can get the user name when the wit.ai is integrated via Facebook Messenger's Built In NLP? – SamT Dec 23 '17 at 22:48