botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Facebook Messenger user name

Open Joffers opened this issue 10 years ago • 18 comments

Hi

How do you get the facebook users name that sent the message to the bot?

I cant seem to get the users name who is sending the message to my bot. I have tried the From fields (ID, Address, Name, etc), as well as the participant fields but they all return a number which I cant map back to a user.

I need to know who the user is as I send custom messages based on the permissions that are on a backend server.

Thanks in advanced Jeff

Joffers avatar May 16 '16 14:05 Joffers

@tomlm does the facebook messenger api provide the user name, or would the bot need to somehow resolve it through the facebook graph api?

willportnoy avatar May 16 '16 19:05 willportnoy

It is in the From.Name property of the message

-Tom

tomlm avatar May 16 '16 19:05 tomlm

Hi

I just tested this and it is still returning a number and not my user profile name. Do i need to update something on my side?

It returns 1277941615949513 (some numbers changed just in case) in the From.Name property

Thanks Jeff

Joffers avatar May 16 '16 19:05 Joffers

If you are looking at the incoming message it will be in the From.Name field If you are looking the outgoing message it will be the To.Name field

tomlm avatar May 16 '16 19:05 tomlm

Hi

Please see code below

///

/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>


public async Task<Message> Post([FromBody]Message message)
{
  if( message.Type == "Message" )
  {
    return message.CreateReplyMessage( message.To.Name + " || " + message.From.Name + " == This is the response" );
  }
  else
  {
    return HandleSystemMessage( message );
  }
}

As you can see it just looks for a message and then returns the To and From name properties with a "This is the response" message appended.

What is returned to me is "Skype Bot || 1277941615949513 == This is the response". So the To.Name property is correct as my bot is called Skype bot, but the From.Name is still returning a number.

So not sure what is happening.

Thanks Jeff

Joffers avatar May 17 '16 05:05 Joffers

Right now the implementation currently passes the Facebook userid rather than the user name. In a future update we will look at changing it to request the user's name and pass that instead.

In the interim, you can (using your bot app credentials from Facebook) call the Facebook graph API to request user profile information using this userid.

jameslew avatar May 21 '16 18:05 jameslew

Hi Is this issue resolved? I am using bot builder and it gives me user-name in the From.Name field. On the contrary I want to know how to get the FB userid from message.

Thanks Aniket

aniketisin avatar May 24 '16 10:05 aniketisin

In the interim, you can (using your bot app credentials from Facebook) call the Facebook graph API to request user profile information using this userid.

Hi, I'm having an issue similar to what started this thread. Sending a message to our Bot from the FB channel, this is the data in the message. Address: 1025934197521552, Channel: facebook, Id: obzBUren4G, Name: Is it possible to retrieve the user profile using the value passed in the address field? If so, could someone share sample code for calling the FB Graph API? Thanks in advance, Ron

ronsmith2 avatar Jun 21 '16 19:06 ronsmith2

Posting this update so it may help someone else.

The number value being passed in the Address field appears to be the page scoped user id from FB. Using this request I was able to retrieve the user's name.

https://graph.facebook.com/v2.6/1025934197521552? fields=first_name,last_name&access_token=<PAGE_ACCESS_TOKEN>

Regards, Ron

ronsmith2 avatar Jun 22 '16 13:06 ronsmith2

that's exactly the api we hit to populate the from.name property in the message when it passes through our system. We pass from.Id = {facebookId} from.Name = {facebook.first_name} {facebook.last_name}

tomlm avatar Jun 24 '16 22:06 tomlm

@aniketisin hi, may I know which object the From.Name belongs to? is it event.message?

heiseish avatar Jul 22 '16 04:07 heiseish

@heiseish Hi,

public async Task Post([FromBody]Message message) { if( message.Type == "Message" ) { //here message.From.Name gives me the Facebook name of the user } else { return HandleSystemMessage( message ); } }

aniketisin avatar Jul 22 '16 10:07 aniketisin

@aniketisin: hi, thanks for the reply. I've alrdy figured out a way to retrieve user's name by facebook graph api

heiseish avatar Jul 22 '16 10:07 heiseish

Just tell me how we can retrieve the user name using 'event' or 'sender' var event = req.body.entry[0].messaging[i]; var sender = event.sender.id; Does the above code can help in retrieving. I am working in nodeJS? Thank you.

gvkhirwadkar avatar Jul 23 '16 17:07 gvkhirwadkar

@gvkhirwadkar we can only get the user ID by that way. Oh and i assume by user name, u mean user's name, not username? You can use module fbgraph in nodejs to get user's name.

heiseish avatar Jul 25 '16 01:07 heiseish

For, me there is some id value coming at From.Id, while From.Name has null value. So, what is the Value shown in From.Id and why is From.Name null? I am using "Facebook at work"

PrasadGautham avatar May 31 '17 12:05 PrasadGautham

OKay, I got it. Its the facebook userid that comes at from.id and from.name is null. I have to get that by passing the userid and page acess token to graph api for user profile. At first I was confused, as I was expecting the id at from.Name

PrasadGautham avatar Jun 01 '17 01:06 PrasadGautham

Hi, I am facing a similar issue, my bot doesn't receives the user name, it is null, but if the user from facebook is an admin of the app it does receive it, what could it be?

fercom avatar Feb 12 '21 00:02 fercom