Skip to content

Instantly share code, notes, and snippets.

@kn1cht kn1cht/new-page.gs
Last active Mar 16, 2019

Embed
What would you like to do?
new-page.gs
var properties = PropertiesService.getScriptProperties();
// Scrapbox settings
var sid = properties.getProperty('SCRAPBOX_SID');
var projectName = properties.getProperty('SCRAPBOX_PROJECT');
// Slack settings
var webHookUrl = properties.getProperty('SLACK_WEBHOOK_URL');
// JSON data settings
var jsonName = 'scrapbox-titles.json';
function doPost(e) {
var attachment = JSON.parse(e.postData.getDataAsString()).attachments[0];
var titles = JSON.parse(getJsonFileFromDrive(jsonName).getBlob().getDataAsString() || '[]');
if(titles.length === 0) // fetch the list of the page titles
titles = getPageListFromApi().pages.map(function(p){ return p.title; });
console.log(attachment.title);
if(titles.indexOf(attachment.title) === -1) { // New page!
titles.push(attachment.title);
var message = projectName + 'に記事"' + attachment.title + '"が作成されました';
console.log(message);
UrlFetchApp.fetch(webHookUrl, options(message, attachment));
}
getJsonFileFromDrive(jsonName).setContent(JSON.stringify(titles));
}
function options(message, attachment) {
return {
method : 'post',
contentType : 'application/json',
payload : JSON.stringify({
username : 'Scrapbox',
icon_emoji : ':scrapbox:',
text : message,
attachments : [attachment]
})
};
}
function getPageListFromApi() {
var headers = { 'Cookie' : 'connect.sid=' + sid };
var response = UrlFetchApp.fetch('https://scrapbox.io/api/pages/' + projectName + '?limit=1000', {
method : 'get',
headers : headers
});
return JSON.parse(response);
}
function getJsonFileFromDrive(name) {
var files = DriveApp.searchFiles('title = "' + name + '"');
if (files.hasNext()) return files.next();
return DriveApp.createFile(name, '');
}
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.