【发布时间】: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 的一些关键概念。
帮助将不胜感激:)
【问题讨论】: