2012-12-29 46 views
2

video we made in 2010一样,今年我们又一次做了一个新年快乐视频,其中包含我们所有Facebook和非Facebook好友的名字。向所有Facebook好友发送自定义消息

在2010年,我们有一个自定义应用程序,用于在我们每个朋友的墙上贴上一个自定义消息,如“亲爱的XXX,祝您新年快乐,这部影片在1分24秒出现“。

今年我们也想做同样的事情,也就是发布一个自定义的消息来向我们的每个朋友展示特定的信息,但是我们注意到Facebook在墙上和私人消息上张贴有限制。

http://www.facebook.com/help/326534794098501/

我们可能会使用的GreaseMonkey/Javascript和对话框API脚本中使用我们的应用程序,但我们仍然担心收到一些安全警告。

那么这个项目是否可行?我们的朋友喜欢我们每年都在做的视频。

+0

结帐用37票的答案:http://facebook.stackoverflow.com/questions/2943297/how-send-message-facebook- friend-through-graph-api-using-accessstoken –

+0

我们使用https://developers.facebook.com/docs/reference/dialogs/send/中的“直接URL”方法,所以我们只使用我们的API密钥应用程序,但其余的都是脚本。如果我们省略了API密钥,它会显示“发生错误”。使用该方法发布给我们的朋友是否有限制? –

回答

1

一个确认:试图通过直接Send Dialog API销售线索(在发送大约60条消息之后)发送消息到来自Facebook服务器端的错误(错误500,甚至空白页)。

的工作方式,以达致这是

  1. 尝试通过直接Feed Dialog API送饲料的消息,
  2. 测试如果可能的话或做的,
  3. 如果没有成功(时间轴是被用户阻止),通过Send Dialog API发送正常消息。

在所有情况下,必须有对话URL的redirect_uri参数,该参数必须指向应用程序拥有的URL。所以app_id也是强制性的。

这样,你不会发送太多“正常”的信息,因为没有很多人阻挡他们的墙,因此你的信息没有被阻止。

以下是一个示例的GreaseMonkey/TamperMonkey代码:

// ==UserScript== 
// @name  NameOfYourScript 
// @namespace NamespaceOfYourScript 
// @version VersionOfYourScript 
// @description enter something useful 
// @match  https://*/* 
// @copyright 2012+, You 
// @require  https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js 
// @grant  GM_getValue 
// @grant  GM_setValue 
// @grant  GM_openInTab 
// ==/UserScript== 
var baseapppage = "<URL of a page owned by your application>"; 
var baseapppagehost = "apps.facebook.com"; var baseapppagepath = "<path to your app>"; 
//Call baseapppage+"?startfbscript=0" to launch the script 
//0 is the index at which you want to start 
var appid = "<APP ID of the application>"; 
var titleofvideo ="<title of video>"; 

var facebook_ids = [ 
    //put the list of the people to contact here 
{"id":"<facebook id of one person>","":"<other arguments for the message>"}, 
//... 
]; 
var video_url = "<URL of the video to share>"; 

var message = "<Template of the message to share, with <placeholders> for the person objects>"+ 
    "\n\nLink of video:"+ 
    "\n"+video_url+ 
    ""; 
//"feed" or "send" (note that "send" only will block you...) 
var default_mode = "feed"; 
//if you lower this, it will be quicker, but may lead to an error 
var temporisation = 5*1000; 
//updating placeholders in message 
function updatemessage(o) { 
    var str = message; 
    for (var p in o) { 
     str = str.replace(new RegExp("<"+p+">","ig"),o[p]) 
    } 
    return str; 
} 
var automatic = true; 
//http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript 
function getQueryVariable(variable) { 
    var query = document.location.search.substring(1); 
    var vars = query.split('&'); 
    for (var i = 0; i < vars.length; i++) { 
     var pair = vars[i].split('='); 
     if (decodeURIComponent(pair[0]) == variable) { 
      return decodeURIComponent(pair[1]); 
     } 
    } 
    console.log('Query variable %s not found', variable); 
} 
//creating URLs 
function createURL(baseurl,data) { 
    var datastr = ""; 
    for (var k in data) { 
     if (datastr.length) datastr += "&"; 
     datastr += encodeURIComponent(k)+"="+ encodeURIComponent(data[k]); 
    } 
    var separator = baseurl.lastIndexOf("?") >= 0 ? "&" : "?"; 
    return baseurl + separator + datastr; 
} 
//arguments for feed page 
var feed_arguments = { 
    "app_id":appid, 
    "name":titleofvideo, 
    "link":video_url, 
    "redirect_uri":createURL(baseapppage,{"currentfbscript":"1"}), 
    //"":"", //caption, description... 
}; 
//arguments for send page 
var send_arguments = { 
    "app_id":appid, 
    "name":titleofvideo, 
    "link":video_url, 
    "redirect_uri":createURL(baseapppage,{"currentfbscript":"1"}), 
    //"":"", 
}; 
//function to open direct dialog API, in mode "feed" or "send", directed to some facebook id 
function relocateto(mode, to) { 
    var arguments = mode == "feed" ? feed_arguments : send_arguments; 
    var baseurl = mode == "feed" ? "https://www.facebook.com/dialog/feed" : "https://www.facebook.com/dialog/send"; 
    arguments['to'] = to; 
    var new_url = createURL(baseurl,arguments); 

    if (parseInt(""+GM_getValue("indice",-1)) % 20 == 0) { 
     //note : fake reload in other tab because of Chrome memory "leak". 
     //Close finished tags to keep memory low. 
     console.log("run 'fake reload'..."); 
     function fake_reload() { 
      console.log("...now"); 
      GM_openInTab(new_url, {active: false, insert: true}); 
      // close the current window some ms later to allow the insert magic to detect this' tab position 
      //note that it unfortunately doesn't work 
      window.setTimeout(window.close, 1); 
     } 
     window.setTimeout(fake_reload, 3000); 
    } else { 
     document.location = new_url; 
    } 
} 
//wall post page 
if (document.location.host == "www.facebook.com" && document.location.pathname == "/dialog/feed") { 
    GM_setValue("mode","feed"); 
    var indice = parseInt(""+GM_getValue("indice",-1)); 
    if (indice < 0 || indice >= facebook_ids.length) return; 
    if (jQuery("input[name=publish]").length) { 
     var mes = updatemessage(facebook_ids[indice]); 
     setTimeout(function() { 
      jQuery("textarea#feedform_user_message").html(mes).val(mes); 
      if (automatic) jQuery("input[name=publish]").click(); 
     },temporisation); 
    } else { 
     //impossible to send wall message -> send dialog 
     relocateto("send", getQueryVariable("to")); 
    } 
} 
//send post page 
if (document.location.host == "www.facebook.com" && document.location.pathname == "/dialog/send") { 
    GM_setValue("mode","send"); 
    var indice = parseInt(""+GM_getValue("indice",-1)); 
    if (indice < 0 || indice >= facebook_ids.length) return; 
    if (jQuery("input[name=publish]").length) { 
     var mes = updatemessage(facebook_ids[indice]); 
     setTimeout(function() { 
      jQuery("textarea#feedform_user_message").html(mes).val(mes); 
      if (automatic) jQuery("input[name=publish]").click(); 
     },temporisation); 
    } else { 
     //impossible to send normal message -> dialogue 
     alert("Impossible to send message... index="+indice+" for "+getQueryVariable("to")+"-"+facebook_ids[indice].id); 
    } 
} 
//start or end page 
if (document.location.host == baseapppagehost && document.location.pathname == baseapppagepath) { 
    if (getQueryVariable("startfbscript")) { 
     GM_setValue("mode",default_mode); 
     var i = getQueryVariable("startfbscript") 
     GM_setValue("indice",i) 
     relocateto(default_mode, facebook_ids[i].id); 
    } else if (getQueryVariable("currentfbscript") && GM_getValue("mode","feed") == "feed" && document.location.search.indexOf("post_id") < 0) { 
     //it didn't work -> sending normal message 
     relocateto("send", GM_getValue("lastname",facebook_ids[0].id)); 
    } else if (getQueryVariable("currentfbscript") && (
       (GM_getValue("mode","feed") == "feed" && getQueryVariable("post_id")) 
       || (GM_getValue("mode","feed") == "send" && getQueryVariable("success")))) { 
     //it worked -> next name ! 
     var indice = parseInt(""+GM_getValue("indice",0)); 
     indice++; 
     GM_setValue("indice",indice) 
     if (indice >= facebook_ids.length) { 
      jQuery("#pagelet_iframe_canvas_content").html("<br/><br/>Finished!"); 
      return; 
     } else { 
      console.log("Next id to send to: "+facebook_ids[indice].id); 
      jQuery("#pagelet_iframe_canvas_content").html("<br><br/> Running script : " 
                  + indice +"/"+facebook_ids.length 
                  +"<br> Next : " 
                  +facebook_ids[indice].id); 
     } 
     var nextname = facebook_ids[indice].id; 
     //next post 
     setTimeout (function(){ 
      relocateto(default_mode, nextname); 
     }, temporisation); 
    } else { 
     //why are we here if the script is running ? Problem with facebook... 
     if (parseInt(""+GM_getValue("indice",0)) < facebook_ids.length) { 
      alert("Impossible to post message. Current index = "+GM_getValue("indice",0)+" for "+facebook_ids[parseInt(""+GM_getValue("indice",0))].id); 
     } 
    } 
} 
+0

请注意,通过电子邮件从Facebook登录电子邮件发送消息到 @ facebook.com是可行的,但我偶然发现了字符集问题。 – Arglanir

相关问题