Facebook App邀请请求 [英] Facebook App invitation request

查看:224
本文介绍了Facebook App邀请请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将App req发送给5个人,我设法获得5个人的UserID,但始终都是5个人,是否有办法随机化我从Facebook获得的USERID's? >

I want to send the App req to 5 people , i have managed to get the UserID's of 5 people but its always those same 5 people , isnt there a way to randomise the USERID's which i get from Facebook ?

<script>
var profilePicsDiv = document.getElementById('profile_pics');

FB.getLoginStatus(function(response) {


  FB.api({ method: 'friends.get' }, function(result) {
    Log.info('friends.get response', result);
   var user_ids="" ;
    var numFriends = result ? Math.min(5, result.length) : 0;
    if (numFriends > 0) {
      for (var i=0; i<numFriends; i++) {
        user_ids+= (

                          ',' + result[i] 

        );
      }
    }
    profilePicsDiv.innerHTML = user_ids;
alert(user_ids);

  });
});


 function sendRequestToRecipients() {
        var user_ids = document.getElementsByName("user_ids")[0].value;
        FB.ui({method: 'apprequests',
          message: 'My Great Request',
          to: user_ids,                        ///  How to Fill the ID's HERE ?
        }, requestCallback);
      }


</script>

推荐答案

我还没有测试过,但这应该可以工作,

I havent tested, but this should work,

FB.getLoginStatus(function(response) {
 FB.api({ method: 'friends.get' }, function(result) {
   Log.info('friends.get response', result);
   var user_ids="" ;
   var totalFriends = result.length;
   var numFriends = result ? Math.min(5, result.length) : 0;
   if (numFriends > 0) {
      for (var i=0; i<numFriends; i++) {
        var randNo = Math.floor(Math.random() * totalFriends)
        user_ids+= (',' + result[randNo]);
      }
    }
    profilePicsDiv.innerHTML = user_ids;
alert(user_ids);

  });
});

在这里,在循环中,我生成一个从0到result.length的随机数(即当前响应中的所有好友),然后使用该随机数从给定列表中获取随机ID.

Here, in loop I generate a random no from 0 to result.length (i.e. total friends in current response) And then I use that random no to fetch random id from the given list.

(在OP询问了非重复随机变量之后),

(after OP asked about non-repeating randoms),

根据您的要求,这应该可以工作...

Based on your requirement, this should work...

   FB.getLoginStatus(function(response) {
     FB.api({ method: 'friends.get' }, function(result) {
       Log.info('friends.get response', result);
       var user_ids="" ;
       var totalFriends = result.length;
       var randNo = Math.floor(Math.random() * totalFriends);
       var numFriends = result ? Math.min(5,totalFriends) : 0;
       if (numFriends > 0) {
          for (var i=0; i<numFriends; i++) { 
            user_ids+= (',' + result[randNo]);
            randNo ++;
            if(randNo >= totalFriends){
                randNo = 0;
            } 
          }
        }
        profilePicsDiv.innerHTML = user_ids;
    alert(user_ids);

      });
    });

在这里,我不是每次都生成随机数,而是生成一次随机数,然后将其递增.如果随机数不超过好友总数,那么我将从0开始.

Here, instead of generating random no each time, I generate a random no once and then increment it. If random no exceeds the total no of friends, then I start from 0.

这总是每次都会给随机的朋友:)

This will always give random friends each time :)

代码未经测试,如果有错误,我深表歉意(但是代码一定会为您提供思考的方向)

Code is not tested, I apologize if has errors ( But code surely gives you a direction to think)

这篇关于Facebook App邀请请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆