# Slack Cluster Manager (EN)
This bot manages "clusters" in slack.
*"cluster" is like groups or lists in the slack.
This bot provides two functions. First one is to invite a cluster to a slack channel or a group. Second one is to evacuate a cluster from a slack channel or a group.
###### tags: `Slack` `EN` `Bot` `API`
## About :fries: update command
+ How to use this command
1. First, write down `:fries:`
2. Next, write down `update` eg.`:fires: update`
3. Next, write down `cluster name` eg. `:fires: update b-7`
4. Finally, write down `mentions` eg. `:fires: update @ryuji @waricoma`
+ When you skip the fourth sequence, the cluster which is written in the third sequence will be deleted.
5. Enter
+ The function of this command
+ Resister, update, or delete a cluster
## About :fries: mention command
+ How to use this command
1. First, write down `:fries:`
2. Next, write down `mention` eg.`:fires: mention`
3. Next, write down `cluster name` eg. `:fires: mention b-7`
4. Next, write down `message` eg. `:fries: mention b-7 Today is not Morimga but Party Tent`
5. Enter
+ Bot sends DM to all members registered in b-7 cluster. The contents of the DM include the URL of the user's post sent in the 5th sequence and the message entered in the 4th sequence.
+ function of this command
+ Send notification for the chosen cluster.
## About :fries: invite command
+ How to use this command
1. First, write down `:fries:`
2. Next, write down `invite` eg.`:fries: invite`
3. Next, write down `cluster name`eg.`:fries: invite b-7`
4. Enter
+ The Bot invites all members registered in a cluster to a channel or a group which a user entered through this command.
+ function this command
+ Invite registered members in a cluster to a channel which a user entered through this command.
## About:fries: kick command
+ How to use this command
1. First, write down `:fries:`
2. Next, write down `kick` eg.`:fries: kick`
3. Next, write down `cluster name`eg.`:fries: kick b-7`
4. Enter
+ Function of this command
+ Evacuate all registered members in a cluster from the channel which a user entered through this command.
## About:fries: list command
+ How to use this command
1. First, write down `:fries:`
2. Next, write down `list` eg. `:fries: list`
3. Enter
+ Function of this command
+ Displays all registered cluster names and member IDs.
## About cluster name
Only small letter alphabet,0,1,2,3,4,5,6,7,8,9,'-' and '_' are available like naming rule of channel rule of Slack.
## About the icon to fire :fries:
This can be changed by the environment variable.
## About :fries: update command 【Implementation】
### Precondition
+ This command can be without checking differences of the channel, group, thread, and DM.
+ This command can only be used by users with the privileges set by environment variables. We use the authority which exists in the official function of Slack.
+ This command will fire when catching the massage event of text type.
+ The regular expression of the message text of the firing condition is '^:fries:( |)update( |)((<@U(([A-Z]|[0-9])+)>)*)$`
### sequence
1. Create an array of strings with as many elements as this bot can extract `member id` from the text parameter of the event.
2. Get `cluster name` and strings from the text parameter of the event.
4. Pass the values obtained in the first and second sequences to Nedb's `cluster model` update method.
5. Send the result of the third sequence to the user of the event using a thread.
## About :fries: mention command 【Implementation】
### Precondition
+ Receive slack event by Slack RTM Client
+ Reply slack event by Slack Web Client
+ This command is available in channel, group, and thread
+ This command will fire when catching massage event of text type
+ The regular expression for the message text of the firing condition is `/ ^: fries :( |) mention (|) (([az] ||-| _ | [0-9]) +) (|) (. +) $ / i`
### sequence
1. Get `cluster name` and string from the text parameter of the event.
2. Pass the value obtained in the first sequence to the find method of Nedb's `cluster model`.
3. Bot sends DM to all members registered in the b-7(for exapmle) cluster. The contents of the DM include the URL of the user's post sent in the 5th sequence and the message entered in the 4th sequence. (https://api.slack.com/methods/im.open https://api.slack.com/methods/chat.postMessage)
4. If the corresponding cluster does not exist in the second sequence, a message about it is sent to the event user using a thread.
## About :fries: invite command 【Implementation】
### Precondition
+ Catch slack event for Slack RTM Client
+ Reply by Slack Web Client
+ This command is available in channel, group, and thread
+ This will fire when catching massage event of text type
+ The regular expression of the message text of the firing condition is `/^:fries:( |)invite( |)(([a-z]||-|_|[0-9])+)$/i`
### sequence
1. Get `cluster name` and string from the text parameter of the event.
2. Get `channel id` from the channel parameter of the event.
3. Pass `cluster name` to the find method of Nedb's` cluster model`.
4. If the corresponding cluster does not exist in the third sequence, a message to that effect is sent back to the event user. And this command is terminated.
5. Get the channel member list of `channel id`. (https://api.slack.com/methods/conversations.members)
6. Check if the result obtained in the fifth sequence matches the environment variable SLACK_USER_ID.
7. If the confirmation result of the 6th sequence is “Not present”, make SLACK_USER_ID member participate. (https://api.slack.com/methods/conversations.join)
8. Invite all members except the member of environment variable SLACK_USER_ID of the cluster. (https://api.slack.com/methods/conversations.invite)
9. If the confirmation result of the 6th sequence is “Not present”, the member of the environment variable SLACK_USER_ID is withdrawn. (https://api.slack.com/methods/conversations.leave)
10. A thread is sent back to the event user.It is about the sequences of this command have been completed successfully.
## About :fries: kick command 【Implementation】
### Precondition
+ Catch slack event for Slack RTM Client
+ Reply slack event by Slack Web Client
+ This command is available in channel, group and slead
+ This will fire when catching massage event of text type
+ The regular expression of the message text of the firing condition is `/^:fries:( |)kick( |)(([a-z]||-|_|[0-9])+)$/i`
### sequence
1. Get `cluster name` and string from the text parameter of the event.
2. Get `channel id` from the channel parameter of the event.
3. Pass `cluster name` to the find method of Nedb's` cluster model`.
4. If the corresponding cluster does not exist in the third sequence, a message to that effect is sent back to the event user. And this command is terminated.
5. Get the channel member list of `channel id`. (https://api.slack.com/methods/conversations.members)
6. Check if the result obtained in the fifth sequence matches the environment variable SLACK_USER_ID.
7. If the confirmation result of the 6th sequence is “Not present”, make SLACK_USER_ID member participate. (https://api.slack.com/methods/conversations.join)
8. Exclude all members except the member of environment variable SLACK_USER_ID of the relevant cluster. (https://api.slack.com/methods/conversations.kick)
9. If the confirmation result of the sixth sequence is “Not Existent”, or if the environment variable SLACK_USER_ID exists in the corresponding cluster, the member of the environment variable SLACK_USER_ID will be withdrawn. (https://api.slack.com/methods/conversations.leave)
10. A thread is sent back to the event user.It is about all the sequences of this command have been completed successfully.
## About :fries: list command 【Implementation】
### Precondition
+ Catch slack event for Slack RTM Client
+ Reply slack event by Slack Web Client
+ This command is available only for DM(with Bot).
+ It is because this bot sends mention to all member practically.
+ This will fire when catching massage event of text type
+ The regular expression of the message text of the firing condition is `/^:fries:( |)list$/i`
### sequence
## About NeDB (DateBase)
### sample
```json
{
"_id": "",
"cluster_name": "b-7",
"members": ["U......", "U......", "U......"]
}
```
### model list
+ cluster
+ cluster update method ( Catch`cluster name` as `String` , Get a list of `member` `id` as arguments of `String[]` )
1. Find the corresponding record based on `cluster name`.
2. If `length` in the` id` list array of `members` is 0, the corresponding record is deleted. And this method ends. Returns `{'message': 'deleted', cluster_name: '__received_cluster_name __'}` as return value.
3. If the search result is 0, a new record is created. And this method ends. The return value is `{'message': 'created', cluster_name: '__received_cluster_name __'}`.
4. Update the `members` of the corresponding record to` String [] `in the` id` list of `members` received as an argument. And this method ends. Returns `{'message': 'updated', cluster_name: '__received_cluster_name __'}` as return value.
+ cluster find method (Receives `cluster name` as` String`.)
1. Find the corresponding record based on `cluster name`.
2. If you do not set `cluster name`, all clusters are displayed as return values.