有没有其他方法可以将消息发送给多个收件人。 我们已尝试集成逻辑,此处描述 Facebook send dialog to multiple friends using a recipients arrays 但它现在看起来不起作用。它只允许将信息发送给 ID 列表中的第一个收件人。
感谢您的帮助。
最佳答案
我找到了向多个 friend 发送 Facebook 消息的解决方法。
每个 Facebook 用户都会自动获得一个@facebook.com 电子邮件地址。
该地址与公共(public)用户名或公共(public)用户 ID 相同。
因此,您只需向该电子邮件地址发送一封普通电子邮件即可。
邮件将像普通消息一样显示在 Facebook 收件箱中。
使用已连接用户的电子邮件作为发件人很重要,否则将无法正常工作。
下面是一个获取所有 friend 电子邮件地址并调用 Web 服务的示例。
<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>
关于javascript - 使用 Facebook API 向多个收件人发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9947434/