【问题标题】:Node.js Wit.ai SDK - Retrieve the user first name on messengerNode.js Wit.ai SDK - 在 messenger 上检索用户名
【发布时间】:2016-10-23 15:00:27
【问题描述】:

我是 Node.js 的新手,我无法使用 SDK 检索 facebook 用户“名字”。 我有一个故事叫“问候”,我想在其中回答“你好 {userName}”。

我定义了一个与“问候”故事相关的 getUserName 名称操作:

  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()
        } 
      }

其中 requestUserName 定义如下:

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;
  });
};

即使动作似乎被触发,它也不起作用,我不知道原因:( 令人惊讶的是,触发了另一个动作,但我不知道为什么,因为这个动作与“问候”故事无关。我想我错过了 Node.js SDK 的一些关键概念。

帮助将不胜感激:)

【问题讨论】:

    标签: node.js wit.ai


    【解决方案1】:

    我解决了我的问题。如果你有兴趣,这里是我的代码。

    getUserName 操作:

      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()
        } 
      },
    

    用户名请求:

    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;
      });
    };
    

    【讨论】:

    • 您好...您知道通过 Facebook Messenger 的内置 NLP 集成 wit.ai 时我们是否可以获取用户名?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 2016-12-02
    • 2017-05-08
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多