Skip to content

Instantly share code, notes, and snippets.

@finatext-shibuya
Last active June 22, 2021 06:57
Show Gist options
  • Save finatext-shibuya/3c63dde95ca899c59283ca6dc45e3837 to your computer and use it in GitHub Desktop.
Save finatext-shibuya/3c63dde95ca899c59283ca6dc45e3837 to your computer and use it in GitHub Desktop.
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