Skip to content

Instantly share code, notes, and snippets.

@ngxson
Last active November 13, 2017 08:27
Show Gist options
  • Save ngxson/18aca4287702c8ac30aeb5c35fb24ed6 to your computer and use it in GitHub Desktop.
Save ngxson/18aca4287702c8ac30aeb5c35fb24ed6 to your computer and use it in GitHub Desktop.
chatbot beta webhook
var HOOK_FB_ID = "267312947032250";
var TARGET_FB_ID = "182794865548469";
var TARGET_FB_TOKEN = "token có quyền publish bài";
function doGet(e) {
if (e.parameter['hub.verify_token'] === 'o3OfDSJc7qrRkiDbCoLJeHKGHQl81a') {
var textContent = ContentService.createTextOutput(e.parameter['hub.challenge']);
textContent.setMimeType(ContentService.MimeType.TEXT);
return textContent;
}
}
function doPost(e) {
try {
var msgdata = JSON.parse(e['postData'].contents);
msgdata.entry.forEach(function(entry) {
processRequest(entry);
});
/*var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty('log', JSON.stringify(msgdata));*/
} catch (err) {
//var doc = DocumentApp.openById("1kuRay06fB4EkVv_J1YnoZaz-_wMqo9u1KD_wrUSuCXI");
//doc.getBody().setText(JSON.stringify(err));
}
}
function processRequest(data) {
try {
data.changes.forEach(function (action) {
if (action.field === "feed" &&
action.value.verb === "add" &&
action.value.sender_id === HOOK_FB_ID) {
if (action.value.item === "comment") {
saveCmt(action.value.comment_id,
(action.value.parent_id ? action.value.parent_id : action.value.post_id),
action.value.message);
}
}
});
} catch (err) {}
}
function saveCmt(cmt_id, post_id, cmt) {
if (!cmt) return;
var options1 = {
'method' : 'delete',
'contentType' : "application/json",
'muteHttpExceptions' : true
};
UrlFetchApp.fetch('https://graph.facebook.com/v2.10/'+cmt_id+'?access_token='+TARGET_FB_TOKEN, options1);
var payload_data = {message: cmt};
var options2 = {
'method' : 'post',
'payload' : JSON.stringify(payload_data),
'contentType' : "application/json",
'muteHttpExceptions' : true
};
UrlFetchApp.fetch('https://graph.facebook.com/v2.10/'+post_id+'/comments?access_token='+TARGET_FB_TOKEN, options2);
}
function viewLog() {
var userProperties = PropertiesService.getUserProperties();
Logger.log(userProperties.getProperty('log'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment