Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Source code for a Google Script App which. 1) Creates a Google form. 2) Sets triggers on that form. 3) Spits out the code to be used on websites on which you have installed Facebook comments social plugin. 4) Once the thus generated code is placed on the blog/site, the admin will get email whenever a facebook comment is made. App: https://script…
function sendCommentEmail (event)
{
var profile_link;
var name_value;
var reply_link;
var page_url;
var page_title;
var time;
var text;
var responses = event.response.getItemResponses();
for (var i = 0; i < responses.length; i++) {
var response = responses [i];
var value = response.getResponse();
switch (response.getItem().getTitle()) {
case "Time" :
time = value;
break;
case "Commenter Profile" :
profile_link = value;
break;
case "Commenter Name" :
name_value = value;
break;
case "Comment Link" :
reply_link = "<a style='text-decoration:none;color:#15c;' href='"+value+"'> Reply to comment</a>.";
break;
case "Page URL" :
page_url = value;
break;
case "Page Title" :
page_title = value;
break;
case "Comment Text" :
text = value.toString();
break;
}
}
var name = "<a style='text-decoration:none;color:#15c;' href='"+profile_link+"'>"+name_value+"</a>";
var post_link = "<a style='text-decoration:none;color:#15c;' href='"+page_url+"'>"+page_title+"</a>";
var regex = new RegExp ("\n","g");
text = text.replace (regex, "<br/>");
var message =
"On " + time + ",<br/><br/>"
+ name + " commentd on " + post_link + " :<br/>"
+ '<div style="'
+ 'font-size: 20px;'
+ 'font-family: \'ff-tisa-web-pro\',Georgia,Cambria;'
+ 'padding-top: 10px;'
+ 'padding-bottom: 10px;'
+ 'padding-left: 20px;'
+ '">' + text + '</div>'
+ reply_link;
Logger.log (message);
MailApp.sendEmail({
to:Session.getEffectiveUser().getEmail(),
subject: name_value + " commented on blog.",
htmlBody:message
});
}
function makeForm ()
{
// Create the form and add the fields we need.
var form = FormApp.create ("Facebook comments mail notifications.");
var time_field = form.addTextItem ();
time_field.setTitle ("Time");
var profile_link_field = form.addTextItem ();
profile_link_field.setTitle ("Commenter Profile");
var commenter_name_field = form.addTextItem ();
commenter_name_field.setTitle ("Commenter Name");
var page_title_field = form.addTextItem ();
page_title_field.setTitle ("Page Title");
var page_url_field = form.addTextItem ();
page_url_field.setTitle ("Page URL");
var comment_text_field = form.addParagraphTextItem ();
comment_text_field.setTitle ("Comment Text");
var comment_link_field = form.addTextItem ();
comment_link_field.setTitle ("Comment Link");
// Create a trigger which will be triggered whenever the
// form is submitted. (in our case, we will send a mail)
var trigger = ScriptApp.newTrigger ('sendCommentEmail')
.forForm (form)
.onFormSubmit ()
.create ();
return form.getPublishedUrl();
}
function main () {
var form_url = makeForm ();
var response_url = form_url.replace ("viewform", "formResponse");
var form_code = UrlFetchApp.fetch (form_url).getContentText ();
// The source code of the following function should be added
// to the page where facebook comments plugin is installed.
function codeToAdd () {
FB.Event.subscribe(
"comment.create",
function(response) {
var pageURL = response.href;
var pageTitle = $("title").html();
var ID = "";
// PERMISSION ISSUE: replies aren"t queriable through
// fql without access token
// so, just save the parent comment again :(
if (response.parentCommentID == undefined) {
ID = response.commentID;
}
else {
ID = response.parentCommentID;
}
var comment_query =
"SELECT fromid, time, text, id,"
+ " post_fbid, fromid "
+ "FROM comment "
+ "WHERE object_id IN ("
+ " SELECT comments_fbid"
+ " FROM link_stat"
+ " WHERE url ='"+pageURL+"'"
+ ") "
+ "AND post_fbid IN ("+ID+")";
var user_query =
"SELECT name, url "
+ "FROM profile "
+ "WHERE id IN (SELECT fromid FROM #comment)";
FB.api(
{
method: "fql.multiquery",
queries: {
comment: comment_query,
user: user_query
}
},
function(response) {
console.log (response);
var comment = response[0].fql_result_set [0];
var user = response[1].fql_result_set [0];
var commentTime = new Date (comment.time * 1000);
commentTime = commentTime.toString();
var commentURL =
pageURL
+ "?fb_comment_id=fbc_"
+ comment.id
+ "_"
+ comment.post_fbid;
$.post(
"<form_submit_url>",
{
"<Time>": commentTime,
"<Commenter Profile>": user.url,
"<Commenter Name>": user.name,
"<Page Title>": pageTitle,
"<Page URL>": pageURL,
"<Comment Text>": comment.text,
"<Comment Link>": commentURL
}
);
}
);
}
);
};
var put_request_param_names = form_code.match (/entry\.[0-9]*/g);
// The array put_request_param_names contains the names of the POST request
// parameters to be submitted to the form submit url
// in the following order:
var tags_to_replace = ["<Time>",
"<Commenter Profile>",
"<Commenter Name>",
"<Page Title>",
"<Page URL>",
"<Comment Text>",
"<Comment Link>"];
var code_to_add = codeToAdd.toString ();
for (var i = 0; i < put_request_param_names.length; i++) {
code_to_add = code_to_add.replace (tags_to_replace [i],
put_request_param_names [i]);
}
code_to_add = code_to_add.replace ("<form_submit_url>", response_url);
return code_to_add;
}
function doGet() {
var code_to_add = main ();
return HtmlService.createHtmlOutput ("<html><head></head><body><pre>"+code_to_add+"</pre></body></pre>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.