Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getUserProfile() default fields #166

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ logs
*.log
npm-debug.log*

#Idea Files
.idea/

# Runtime data
pids
*.pid
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we should change this description. The default value of the fields param is not ['id', 'name', ... ], it's an empty array. When you specify fields, you're not overwriting this default value, you're extending it. I think we should either change the behavior to allow specifying/overwriting all fields, or change this description and maybe the param name to extraFields so that it's clear that you're extending the default. What do you think?


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.

Expand All @@ -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}!`);
});

});
```

Expand Down
8 changes: 7 additions & 1 deletion examples/user-profile-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
13 changes: 11 additions & 2 deletions lib/BootBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we give fields a default value of []? Otherwise, the check on line 344 would make the method fail for people who are calling it directly without 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}`));
Expand Down
6 changes: 4 additions & 2 deletions lib/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || []);
}

/**
Expand Down