Skip to content

Instantly share code, notes, and snippets.

@shuyuhey shuyuhey/LICENSE
Last active Aug 27, 2019

Embed
What would you like to do?
AppStoreのレビューをSlackに通知する
var TARGET_RSS_URL = 'レビューのXML';
var SLACK_URL = 'SLACKのWebhookURL';
function run() {
var response = UrlFetchApp.fetch(TARGET_RSS_URL);
var xml = XmlService.parse(response.getContentText());
var namespace = xml.getRootElement().getNamespace();
var entries = xml.getRootElement().getChildren("entry", namespace);
var appleNameSpace = xml.getRootElement().getNamespace("im");
var today = Moment.moment();
var lastUpdated = fetchLastUpdated();
var reviews = entries
.filter(function(entry, index) {
var updated = new Date(entry.getChild("updated", namespace).getText());
Logger.log(updated);
return index !== 0 && updated >= lastUpdated;
})
.map(function(entry) {
return {
author: entry.getChild("author", namespace).getChild("name", namespace).getText(),
title: entry.getChild("title", namespace).getText(),
content: entry.getChild("content", namespace).getText(),
rating: entry.getChild("rating",appleNameSpace).getText(),
updated: entry.getChild("updated", namespace).getText()
};
})
.map(makeAttachment);
if (reviews.length <= 0) { return; }
postSlack(reviews);
saveLastUpdated(today);
}
function postSlack(content) {
var options = {
method: 'post',
payload: JSON.stringify(payload(content))
};
var res = UrlFetchApp.fetch(SLACK_URL, options);
if (res.getResponseCode() != 200) {
Logger.log(res);
}
}
function payload(attachments) {
return {
username: "AppStoreレビュー",
icon_emoji: ":bow:",
attachments: attachments
}
}
function makeAttachment(review) {
return {
color: choiceColor(review.rating),
title: review.title,
author_name: review.author,
text: review.content,
fields: [
{
title: "評価",
value: star(review.rating),
short: true
},
{
title: "更新日",
value: Moment.moment(review.updated).format('YYYY/MM/DD hh:mm:ss'),
short: true
}
]
}
}
function choiceColor(rating) {
switch (Number(rating)) {
case 1: return 'danger';
case 2: return 'warning';
case 3: return '#89CEEB';
default: return 'good';
}
}
function star(rating) {
var rate = Number(rating);
return Array(rate + 1).join('') + Array((5 - rate) + 1).join('');
}
function saveLastUpdated(today) {
PropertiesService.getScriptProperties().setProperty('last_updated', today.toString());
}
function fetchLastUpdated() {
var lastUpdated = PropertiesService.getScriptProperties().getProperty('last_updated');
return Moment.moment(lastUpdated);
}
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
@shuyuhey

This comment has been minimized.

Copy link
Owner Author

commented Dec 8, 2017

moment.jsを使っていますので、下記の記事を参考に導入してください。

https://tonari-it.com/gas-moment-js-moment/

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.