Send private messages to friends

140,739

Solution 1

Sending private message through api is now possible.

Fire this event for sending message(initialization of facebook object should be done before).

to:user id of facebook

function facebook_send_message(to) {
    FB.ui({
        app_id:'xxxxxxxx',
        method: 'send',
        name: "sdfds jj jjjsdj j j ",
        link: 'https://apps.facebook.com/xxxxxxxaxsa',
        to:to,
        description:'sdf sdf sfddsfdd s d  fsf s '

    });
}

Properties

  • app_id
    Your application's identifier. Required, but automatically specified by most SDKs.

  • redirect_uri
    The URL to redirect to after the user clicks the Send or Cancel buttons on the dialog. Required, but automatically specified by most SDKs.

  • display
    The display mode in which to render the dialog. This is automatically specified by most SDKs.

  • to
    A user ID or username to which to send the message. Once the dialog comes up, the user can specify additional users, Facebook groups, and email addresses to which to send the message. Sending content to a Facebook group will post it to the group's wall.

  • link
    (required) The link to send in the message.

  • picture
    By default a picture will be taken from the link specified. The URL of a picture to include in the message. The picture will be shown next to the link.

  • name By default a title will be taken from the link specified. The name of the link, i.e. the text to display that the user will click on.

  • description
    By default a description will be taken from the link specified. Descriptive text to show below the link.

See more here

@VishwaKumar:

For sending message with custom text, you have to add 'message' parameter to FB.ui, but I think this feature is deprecated. You can't pre-fill the message anymore. Though try once.

FB.ui({
  method: 'send',
  to: '1234',
  message: 'A request especially for one person.',
  data: 'tracking information for the user'
});

See this link: http://fbdevwiki.com/wiki/FB.ui

Solution 2

This is not possible now, but there is a work around. You can engage with the user in the public realm and ask them to send you private messages, but you can't send private messages back, only public ones. Of course, this all depends on if the user gives you the correct permissions.

If you have given permission to access a person's friends, you can then theoretically post on that users wall with references to each one of the friends, asking them to publicly interact with you and then potentially privately message you.

Get Friends

#if authenticated
https://graph.facebook.com/me/friends
http://developers.facebook.com/docs/reference/api/user/

Post in the Public Domain

http://developers.facebook.com/docs/reference/api/status/

Get Messages sent to that user (if given permission)

http://developers.facebook.com/docs/reference/api/message/

Solution 3

You can use Facebook Chat API to send private messages, here is an example in Ruby using xmpp4r_facebook gem:

sender_chat_id = "-#{sender_uid}@chat.facebook.com"
receiver_chat_id = "-#{receiver_uid}@chat.facebook.com"
message_body = "message body"
message_subject = "message subject"

jabber_message = Jabber::Message.new(receiver_chat_id, message_body)
jabber_message.subject = message_subject

client = Jabber::Client.new(Jabber::JID.new(sender_chat_id))
client.connect
client.auth_sasl(Jabber::SASL::XFacebookPlatform.new(client,
   ENV.fetch('FACEBOOK_APP_ID'), facebook_auth.token,
   ENV.fetch('FACEBOOK_APP_SECRET')), nil)
client.send(jabber_message)
client.close

Solution 4

No, this isn't possible. In order for you to send messages of any kind to a Facebook user, you need that user's permission to do so.

If someone logs into your site with Facebook Connect, they are explicitly agreeing to share their Facebook data with your site, and you will then be able to send that person a message through the normal channels. You would also be able to fetch their friend list. However, you can not send messages to the friends.

Solution 5

One workaround, though not a great one, is to use the new @facebook.com email address. There are a few downsides to this:

1) Not everyone (as of this posting) has the new messages application enabled in their account.

2) Not everyone will have setup their @facebook.com email in their messages app.

3) Not everyone will choose their username (if they even have a facebook username) as their email address.

Share:
140,739
yspro
Author by

yspro

Updated on November 02, 2020

Comments

  • yspro
    yspro over 3 years

    I need to get via Facebook connect user's info and send a private message to all of his friends. Is it possible?

  • Randell
    Randell almost 13 years
    My Google-fu is failing me. Got links on how to do this?
  • Somnath Muluk
    Somnath Muluk about 12 years
    :Now we can send private message now. I have given answer.
  • Scott
    Scott almost 12 years
    This solution has been there a while. It does not completely solve the problem as you have to use Facebook's popup and can only populate a single person (at this point, track bug here). A full solution allows you to use the standard API to send a message however you want, though it seems that Facebook may have taken this away for good.
  • CyberJunkie
    CyberJunkie almost 12 years
    @Somnath, thanks for posting the solution! Do you know if there is a way to bypass the write message window and just send the message directly?
  • Somnath Muluk
    Somnath Muluk almost 12 years
    @CyberJunkie: I think facebook doesn't provide such bypassing functionality. User have to click send msg button. Other wise your inbox will be full of advertisements.
  • VishwaKumar
    VishwaKumar over 11 years
    @Somnath: I tried your solution and it works, But is there a way to populate the Message in the popup with custom messages?
  • Somnath Muluk
    Somnath Muluk over 11 years
    @VishwaKumar: Do you mean message box should be filled with custom message when it is opened?
  • Tim Tisdall
    Tim Tisdall over 11 years
    I managed to get it to <b>send to more than one person</b>! You can make the value for 'to' a JSON array. I put an example up here: fbdevwiki.com/wiki/FB.ui#method:_.27send.27
  • Dmitry Khryukin
    Dmitry Khryukin over 11 years
    the blog post about this - dalibornasevic.com/posts/…
  • Wayfarer
    Wayfarer over 11 years
    Does anyone know if this works consistently for every facebook user ? I have a website where people can log in with their fb accounts. UserA is not a friend of userB. userA sends a message to userB. sometimes it works, sometimes the link gets stripped. sometimes it doesn't work... doesn't seem consistent :/
  • Kyle Clegg
    Kyle Clegg about 11 years
    @SomnathMuluk have an objective-c answer for this?
  • Somnath Muluk
    Somnath Muluk about 11 years
    @Kyle: sorry, I don't know objective-C. You can ask one linked question to this or raise bounty for same question. You might get good answers.
  • confiq
    confiq about 11 years
    @SomnathMuluk do you know if i need special permission for app for this? does user need to install it?
  • Somnath Muluk
    Somnath Muluk almost 11 years
    @confiq: I think there is no any requirement for any permissions... Just you need to have FB.init initialised for Javascript.
  • kumar
    kumar almost 11 years
    hello somnath, as my requirement is to send the message by custom, I've created some custom functionality to get the list of friends & message,later I called u r function, but I was failed to send the message to the selected users..can u help me
  • Somnath Muluk
    Somnath Muluk almost 11 years
    @Nagendra: what did happened? Any error? For sending message with custom text, you have to add 'message' parameter to FB.ui, but I think this feature is Deprecated. :( You can't pre-fill the message anymore.
  • kumar
    kumar almost 11 years
    @SomnathMuluk: I've added, but not displaying the message in the users message inbox, u can see here stackoverflow.com/questions/16465119/…
  • skaz
    skaz almost 11 years
    @SomnathMuluk: It looks like you can't even set a "Name" or "Description" text anymore either. It gets pulled from the website automatically :(
  • Totty.js
    Totty.js over 10 years
    can you explain more about this?
  • Alex
    Alex about 10 years
    @Totty can you show your realization? I very need it for my iOS app.
  • Snehal S
    Snehal S about 10 years
    do you have the similar code in php to send private message?
  • Daniel Cukier
    Daniel Cukier almost 9 years
    this is deprecated since v2.0 after May 1st 2015