Last active
April 12, 2019 01:28
-
-
Save matsuokah/8d491b56a0fe7118fb96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var slackUrl = "WEBHOOK URL" | |
var selfAddress = "EMAIL ADDRESS"; | |
var defaultPayload = { | |
"channel": "notification", | |
"username": "iTunes Connect", | |
"text": "アプリのステータスに変更がありました", | |
"icon_emoji": ":iOS:" | |
}; | |
function clone(obj) { | |
if (null == obj || "object" != typeof obj) return obj; | |
var copy = obj.constructor(); | |
for (var attr in obj) { | |
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; | |
} | |
return copy; | |
} | |
function getAttachment(subject) { | |
var statuses = [ | |
{ | |
"token": "Processing for App Store", | |
"text": "アップロードが完了しました。アプリの処理中です。", | |
"color": "#00BBAA" | |
}, | |
{ | |
"token": "has completed processing", | |
"text": "アップロード後の処理が完了しました。", | |
"color": "#00BBAA" | |
}, | |
{ | |
"token": "Waiting For Review", | |
"text": "申請が完了しました。レビュー待ちです。", | |
"color": "#00BBAA" | |
}, | |
{ | |
"token": "Developer Rejected", | |
"text": "アプリの申請を取り下げました。", | |
"color": "#FFBB44" | |
}, | |
{ | |
"token": "In Review", | |
"text": "レビューに入りました。", | |
"color": "#FFBB44" | |
}, | |
{ | |
"token": "New message from App Review", | |
"text": "レビューアプリに対して、Appleより連絡がありました。", | |
"color": "#FFBB44" | |
}, | |
{ | |
"token": "Pending Developer Release", | |
"text": "レビューが通りました。おめでとうございます~:age:", | |
"color": "#00BBAA" | |
}, | |
{ | |
"token": "Ready for Sale", | |
"text": "公開処理が完了しました。反映までしばらくお待ちください。", | |
"color": "#00BBAA" | |
}, | |
]; | |
for (var status in statuses) { | |
if (~subject.indexOf(statuses[status].token)) { | |
return statuses[status]; | |
} | |
} | |
return null; | |
} | |
function makePayload(message) { | |
var payload = clone(defaultPayload); | |
var field = { | |
"title": message.getSubject(), | |
"value": message.getPlainBody(), | |
"short": false | |
} | |
var attachment = getAttachment(message.getSubject()); | |
if (!attachment) { | |
return null | |
} | |
attachment["fields"] = [field]; | |
payload["attachments"] = [attachment]; | |
return payload; | |
} | |
function postSlack(payload) { | |
var payloadStr = JSON.stringify(payload); | |
// :は\:にエスケープする必要がある. | |
var escapedStr = payloadStr.replace(/":"/g, "\"\:\""); | |
Logger.log(escapedStr); | |
var options = { | |
'method': 'post', | |
'contentType': 'Content-type: application/json; charset=utf-8', | |
'payload': escapedStr | |
} | |
var response = UrlFetchApp.fetch(slackUrl, options) || false; | |
var ret = true; | |
if (!response || response.getResponseCode() != 200) { | |
ret = false; | |
} | |
return {status: ret, code: response.getResponseCode(), response: response}; | |
} | |
function postError(message) { | |
GmailApp.sendEmail(selfAddress, "Slackへのポストに失敗しました", "スクリプトを確認してください。失敗メールの件名:" + message.getSubject()); | |
} | |
function main() { | |
var threads = GmailApp.search('from:no_reply@email.apple.com'); | |
for (var i = 0; i < threads.length; i++) { | |
var thread = threads[i]; | |
if (!thread.isUnread()) { | |
continue; | |
} | |
var messages = thread.getMessages(); | |
for (var j = 0; j < messages.length; j++) { | |
var message = messages[j]; | |
if (!message || !message.isUnread()) { | |
continue; | |
} | |
var payload = makePayload(message); | |
if (!payload) { | |
continue; | |
} | |
res = postSlack(payload); | |
if (!res.status) { | |
postError(message); | |
continue; | |
} | |
message.markRead(); | |
} | |
thread.markRead(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment