From c23b53e3b07d0e450b00e5e4737192c4d5dcb235 Mon Sep 17 00:00:00 2001 From: Drez0 Date: Thu, 17 Jan 2019 10:45:00 +0300 Subject: [PATCH 1/9] set default fields --- lib/BootBot.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/BootBot.js b/lib/BootBot.js index 5df40f8..75ccf86 100644 --- a/lib/BootBot.js +++ b/lib/BootBot.js @@ -333,10 +333,16 @@ class BootBot extends EventEmitter { /** * Returns a Promise that contains the user's profile information. * @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.reduce((url, elem) => `${url},${elem}`); + 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}`)); From 2a738d18d81d8e4b61cd911858963577dd712e5c Mon Sep 17 00:00:00 2001 From: Drez0 Date: Thu, 17 Jan 2019 10:46:30 +0300 Subject: [PATCH 2/9] update jsdoc --- lib/BootBot.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/BootBot.js b/lib/BootBot.js index 75ccf86..9e461f5 100644 --- a/lib/BootBot.js +++ b/lib/BootBot.js @@ -332,6 +332,9 @@ 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} From 24a438efac7a331879a7154c204f722613b0b56a Mon Sep 17 00:00:00 2001 From: Drez0 Date: Thu, 17 Jan 2019 10:50:12 +0300 Subject: [PATCH 3/9] pass variable --- lib/Chat.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Chat.js b/lib/Chat.js index 1f6b1aa..443f226 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); } /** From f922f42c4771ad1daeadffbf0b51b1069d17dfd3 Mon Sep 17 00:00:00 2001 From: Drez0 Date: Thu, 17 Jan 2019 10:54:24 +0300 Subject: [PATCH 4/9] =?UTF-8?q?change=20"reduce"=20to=20"join"=20?= =?UTF-8?q?=F0=9F=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/BootBot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/BootBot.js b/lib/BootBot.js index 9e461f5..90c33bc 100644 --- a/lib/BootBot.js +++ b/lib/BootBot.js @@ -344,7 +344,7 @@ class BootBot extends EventEmitter { 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.reduce((url, elem) => `${url},${elem}`); + 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()) From 098c1e14c97645f34fd28769a8f1f4180fede8af Mon Sep 17 00:00:00 2001 From: Drez0 Date: Thu, 17 Jan 2019 10:58:15 +0300 Subject: [PATCH 5/9] change example --- examples/user-profile-example.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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(); From 785f7986ff2e4bfeeca9a209df0ade4a09851a0e Mon Sep 17 00:00:00 2001 From: Drez0 Date: Thu, 17 Jan 2019 11:07:28 +0300 Subject: [PATCH 6/9] Change README.md --- README.md | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) 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}!`); + }); + }); ``` From dee079079fa1baae5e85df2551166f45101de24d Mon Sep 17 00:00:00 2001 From: Drez0 Date: Thu, 17 Jan 2019 12:04:34 +0300 Subject: [PATCH 7/9] set default variable --- .idea/codeStyles/codeStyleConfig.xml | 5 +++++ lib/Chat.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/lib/Chat.js b/lib/Chat.js index 443f226..0fd21c5 100644 --- a/lib/Chat.js +++ b/lib/Chat.js @@ -162,7 +162,7 @@ class Chat extends EventEmitter { * @returns {Promise} */ getUserProfile(fields) { - return this.bot.getUserProfile(this.userId, fields); + return this.bot.getUserProfile(this.userId, fields || []); } /** From 7e6729bbda8bf97d8c757bf613e1e0660c39aba2 Mon Sep 17 00:00:00 2001 From: Drez0 <41549580+drezo@users.noreply.github.com> Date: Thu, 17 Jan 2019 12:06:06 +0300 Subject: [PATCH 8/9] Remove idea code style --- .idea/codeStyles/codeStyleConfig.xml | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .idea/codeStyles/codeStyleConfig.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml deleted file mode 100644 index a55e7a1..0000000 --- a/.idea/codeStyles/codeStyleConfig.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file From 74ea7b1582e6b1f79e4183a71c4973c66b173f71 Mon Sep 17 00:00:00 2001 From: Drez0 Date: Thu, 17 Jan 2019 12:14:05 +0300 Subject: [PATCH 9/9] update .gitignore (ignore IDE files) --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) 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