diff --git a/.gitignore b/.gitignore index 66d0131..78cf2f9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ logs *.log npm-debug.log* +#Idea Files +.idea/ + # Runtime data pids *.pid diff --git a/README.md b/README.md index 347d2d3..86f33b1 100644 --- a/README.md +++ b/README.md @@ -530,9 +530,13 @@ You can also use this method via the `typing` option (see [`.say()`](#say) metho | Method signature | |:-----------------| -| `chat.getUserProfile()` | -| `convo.getUserProfile()` | -| `bot.getUserProfile(userId)` | +| `chat.getUserProfile(fields)` | +| `convo.getUserProfile(fields)` | +| `bot.getUserProfile(userId, fields)` | + +| Param | Type | Default | Required | +|:------|:-----|:--------|:---------| +| `fields` | array | ['id', 'name', 'first_name', 'last_name', 'profile_pic'] | `N` | This method is not technically part of the "Send" API, but it's listed here because it's also shared between the `bot`, `chat` and `convo` instances. @@ -543,6 +547,16 @@ bot.hear('hello', (payload, chat) => { chat.getUserProfile().then((user) => { chat.say(`Hello, ${user.first_name}!`); }); + + // or + + const fields = ['locale', 'timezone', 'gender']; // change permissions + + chat.getUserProfile(fields).then((user) => { + chat.say(`Hello, ${user.first_name}!`); + chat.say(`Your locale, ${user.locale}!`); + }); + }); ``` diff --git a/examples/user-profile-example.js b/examples/user-profile-example.js index 97d273f..a50e335 100644 --- a/examples/user-profile-example.js +++ b/examples/user-profile-example.js @@ -12,9 +12,15 @@ const bot = new BootBot({ bot.module(echoModule); bot.hear('hello', (payload, chat) => { - chat.getUserProfile().then((user) => { + const fields = ['locale', 'timezone', 'gender']; // change permissions + chat.getUserProfile(fields).then((user) => { chat.say(`Hello, ${user.first_name}!`); }); + + // or don't pass variable "fields" + /* chat.getUserProfile().then((user) => { + chat.say(`Hello, ${user.first_name}!`); + }); */ }); bot.start(); diff --git a/lib/BootBot.js b/lib/BootBot.js index 5df40f8..90c33bc 100644 --- a/lib/BootBot.js +++ b/lib/BootBot.js @@ -332,11 +332,20 @@ class BootBot extends EventEmitter { /** * Returns a Promise that contains the user's profile information. + * Default fields: id, name, first_name, last_name, profile_pic + * Additional fields: locale, timezone, gender + * Link: https://developers.facebook.com/docs/messenger-platform/identity/user-profile * @param {String} userId + * @param {Array} fields Additions fields (ex.: locale/timezone/gender). * @returns {Promise} */ - getUserProfile(userId) { - const url = `https://graph.facebook.com/${this.graphApiVersion}/${userId}?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=${this.accessToken}`; + getUserProfile(userId, fields) { + const defaultFields = ['id', 'name', 'first_name', 'last_name', 'profile_pic']; + if (!Array.isArray(fields)) { + throw new Error('Fields is supposed to be an array'); + } else Array.prototype.push.apply(defaultFields, fields.map(field => field.trim())); + const mergeFields = defaultFields.join(','); + const url = `https://graph.facebook.com/${this.graphApiVersion}/${userId}?fields=${mergeFields}&access_token=${this.accessToken}`; return fetch(url) .then(res => res.json()) .catch(err => console.log(`Error getting user profile: ${err}`)); diff --git a/lib/Chat.js b/lib/Chat.js index 1f6b1aa..0fd21c5 100644 --- a/lib/Chat.js +++ b/lib/Chat.js @@ -158,9 +158,11 @@ class Chat extends EventEmitter { /** * Returns a Promise that contains the user's profile information. + * @param {Array} fields + * @returns {Promise} */ - getUserProfile() { - return this.bot.getUserProfile(this.userId); + getUserProfile(fields) { + return this.bot.getUserProfile(this.userId, fields || []); } /**