Last active
June 22, 2021 06:57
-
-
Save finatext-shibuya/3c63dde95ca899c59283ca6dc45e3837 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
function myFunction() { | |
const users = getUsers(); | |
const pendingUsers = filterPendingUsers(users); | |
const attachments = createAttachments(pendingUsers); | |
postSlackAttachments(attachments); | |
} | |
function getUsers() { | |
const url = PropertiesService.getScriptProperties().getProperty('BACKLOG_URL') + "/api/v2/users?apiKey=" + PropertiesService.getScriptProperties().getProperty('BACKLOG_API_TOKEN'); | |
var options = { | |
"method": "get", | |
"contentType": "application/json" | |
}; | |
const response = UrlFetchApp.fetch(url, options); | |
return JSON.parse(response.getContentText()); | |
} | |
function filterPendingUsers(users) { | |
return users.filter(a => a.lang == null); // 招待完了していないユーザーはlang nullのため判定に利用する | |
} | |
function createAttachments(pendingUsers) { | |
return pendingUsers.map(user => { | |
return { | |
"color": "good", | |
"fields": [{ | |
"title": "email", | |
"value": user.mailAddress, | |
"short": false | |
}] | |
}; | |
}); | |
} | |
function postSlackAttachments(attachments) { | |
const slackUrl = PropertiesService.getScriptProperties().getProperty('SLACK_WEBHOOK'); | |
const webhookUrl = slackUrl; | |
for (var i = 0; i < attachments.length; i++) { | |
if (0 < i) { | |
Utilities.sleep(1000); | |
} | |
var attachment = attachments[i]; | |
var username = "backlog bot"; | |
var msgJson = { | |
"username": username, | |
"attachments": [attachment] | |
}; | |
var payload = JSON.stringify(msgJson); | |
var options = { | |
"method": "post", | |
"contentType": "application/json", | |
"payload": payload | |
}; | |
UrlFetchApp.fetch(webhookUrl, options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment