slackm8 will randomly shuffle a team into a specific amount of groups & invite members to Slack channels.
Written in elm-lang.
My workplace likes to set up random groups of members for quick chats to promote communication throughout the team. This app was created to automate the process having a member specifically create channels and invite random users. Plus I wanted to build another thing with elm.
% git clone firstname.lastname@example.org:chrisbuttery/slackm8.git % cd slackm8 % open dist/index.html
Slack API authorization test token
To try this example out you'll need to have the administrator of your Slack team create an API authorization test token here. https://api.slack.com/web.
This will allow slackm8 to make 3 types of http requests to your slack team.
- users.list: Listing existing users
- channels.create: Creating a channel
- channels.invite: Inviting a User to a Channel
slackm8 will store your test token as 'slackm8Model' in localStorage along with the rest of the model. Your token is only accessed from localStorage or the model directly.
Here are just a few things I learnt along the way.
Decoding JSON objects.
Decoding JSON objects seemed a bit daunting to me. I think it's because you have to already know the structure that's going to be returned before you can attempt to decode it. You can't just debug the response in a console like you can in JS.
To get familiar with decoding various JSON structures in elm, I created this sandbox.
Currying Http Task
I needed to perform specific Http Tasks in order, such as creating a channel and then populating this channel with users. This meant both the tasks needed to have access to the same data.
Once I had my 'group' of users I passed them to my
createRoom request. I could then partially apply the
CreateRoomsSuccess Task with the sames users. This meant when
CreateRoomsSuccess was handled at a later stage it had access to the same data.
Thank you Chad Gilbert.
createRoom : Group -> Cmd Msg createRoom group = Task.perform CreateRoomsFail (CreateRoomsSuccess group.users) (Http.post decodeCreateRoomResponse ("https://some_api?room=" ++ group.title) Http.empty ) update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of ... CreateRoomsSuccess users roomID -> model ! List.map (addUser roomID << .name) users ...
Splitting a list into a list of multiple lists
I won't lie. This continues to blow my mind.
I wanted to split a list into lists of specific amount of values.
So for example, split a list into groups of 2.
limit = 2 myList = [1,2,3,4,5] // desired output [[1,2], [2,3], ]
I've commented on how I understand this function to work. I could have got it awfully wrong, so I'm happy for anyone to correct me.
Source files located in
% npm install && elm package install -y % npm run build