Max Dmitrenko
Max Dmitrenko

Reputation: 41

Send message to multiple recipients using Facebook API

Is there any other way to post message to multiple recipients. We've tried to integrate logic, described here Facebook send dialog to multiple friends using a recipients arrays but it doesn't look it works now. It just allows to send information to first recipient in the list of IDs.

Thanks for your help.

Upvotes: 4

Views: 8613

Answers (2)

badrick
badrick

Reputation: 51

I’ve found a workaround for sending Facebook messages to multiple friends.

Every Facebook user automatically got an @facebook.com e-mail address. The address is the same as the public username or public user ID.

So you can simply send an regular e-mail to this e-mail addresses. The mail will be displayed in the Facebook inbox like a normal message.

It’s important that you use the connected users email as sender, otherwise it won’t work.

Here's an example that gets all friends e-mail adresses and calls a web service.

<div id="fb-root"></div>
<script type="text/javascript" src="https://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
    FB.init({
        appId: '#APP_ID#',
        status: true,
        cookie: true,
        xfbml: true
    });

    FB.getLoginStatus(function (response) {
        if (response.status === 'connected') {
            GetData();
        } else {
            Login();
        }
    });

    function Login() {
        FB.login(function (response) {
            if (response.authResponse) {
                GetData();
            }
        }, { scope: 'email' });
    }

    function GetData() {
        //Get user data
        FB.api('/me', function (response) {
            //Sender
            var sender = response.email;

            //Get friends
            FB.api('/me/friends', function (response) {

                //Recepients array
                var recipients = [];
                var length = response.data.length;
                var counter = 0;

                //Loop through friends
                for (i = 0; i < length; i++) {
                    var id = response.data[i].id;

                    //Get friend data
                    FB.api('/' + id, function (response) {
                        var recipient = "";

                        //User got a username, take username
                        if (response.username) {
                            recipient = response.username + '@facebook.com';
                        }
                        //No username, take id
                        else {
                            recipient = response.id + '@facebook.com';
                        }

                        //Add e-mail address to array
                        recipients.push(recipient);

                        counter++;
                        //last email -> send
                        if (counter == length) {
                            SendEmail(sender, recipients);
                        }
                    });
                }
            });
        });
    }

    function SendEmail(sender, recipients) {
        //Call webservice to send e-mail e.g.
        $.ajax({ type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            url: '#WEBSERVICE#',
            data: '{ sender:"' + sender + '", recipients: ["' + recipients.join('","') + '"] }',
            success: function (response) {
                //do something
            }
        });
    }
</script>

Upvotes: 5

Gil Birman
Gil Birman

Reputation: 35900

Facebook doesn't want you to do this, so you'll have to do a work-around... You can develop an app with a built-in messaging system. Then send a request to multiple recipients at the same time. When the users click on the request, your app should retrieve and display the message.

Upvotes: 1

Related Questions