Created
June 22, 2021 06:55
-
-
Save finatext-shibuya/3d9129e43d4264c5de430ebee850c250 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 members = getMembers(); | |
const hasNot2FAMembers = filterHasNot2FAMembers(members); | |
if (hasNot2FAMembers.length === 0) { | |
Logger.log("no 2FA members, ok"); | |
return; | |
} | |
const attachments = createAttachments(hasNot2FAMembers); | |
postSlackAttachments(attachments); | |
inviteMembers(hasNot2FAMembers); | |
} | |
function getMembers() { | |
const properties = PropertiesService.getScriptProperties(); | |
const token = properties.getProperty('SLACK_TOKEN'); | |
const url = "https://slack.com/api/users.list"; | |
const options = { | |
"headers": { "Authorization": `Bearer ${token}` }, | |
"contentType": "application/json" | |
}; | |
const response = UrlFetchApp.fetch(url, options); | |
const json = JSON.parse(response.getContentText()); | |
return json.members; | |
} | |
function filterHasNot2FAMembers(members) { | |
const result = members.filter(a => !a.deleted && !a.is_bot && !a.is_restricted && !a.has_2fa && a.name !== "slackbot"); | |
Logger.log({ | |
members: members.length, | |
result: result.length | |
}); | |
return result; | |
} | |
function createAttachments(members) { | |
return members.map(function(a) { | |
return { | |
"color": "good", | |
"fields": [ | |
{ | |
"title": "real_name", | |
"value": a.real_name || "-", | |
"short": true | |
}, | |
{ | |
"title": "display_name", | |
"value": a.diaplay_name || "-", | |
"short": true | |
}, | |
{ | |
"title": "has_2fa", | |
"value": a.has_2fa ? "true" : "false", | |
"short": true | |
}, | |
], | |
}; | |
}); | |
} | |
function postSlackAttachments(attachments) { | |
const webhookUrl = PropertiesService.getScriptProperties().getProperty('SLACK_WEBHOOK'); | |
var msgJson = { | |
"username": "", | |
"attachments": attachments | |
}; | |
var payload = JSON.stringify(msgJson); | |
var options = { | |
"method": "post", | |
"contentType": "application/json", | |
"payload": payload | |
}; | |
const result = UrlFetchApp.fetch(webhookUrl, options); | |
} | |
function inviteMembers(members) { | |
const properties = PropertiesService.getScriptProperties(); | |
const token = properties.getProperty('SLACK_TOKEN'); | |
const channel = properties.getProperty('SLACK_CHANNEL'); | |
const url = "https://slack.com/api/conversations.invite"; | |
const options = { | |
"headers": { "Authorization": `Bearer ${token}` }, | |
"contentType": "application/json", | |
"method": 'post', | |
"payload": JSON.stringify({"channel": channel, "users": members.map(a => a.id).join(",")}) | |
}; | |
const response = UrlFetchApp.fetch(url, options); | |
const json = JSON.parse(response.getContentText()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment