Creating an Elixir module
Creating an Elixir module
To get a better handle on Elixir, I developed a simple CLI tool for sending files in Slack.
To create a new project, run
This creates a new Elixir project which looks like this
Navigate to the lib folder and create a folder inside it called slack_bot. We will use this folder for our code (slack_bot.exs) will remain empty. Then, create the files bot.ex and cli.ex. cli.ex will be the entry point to our application. bot.ex will contain the code for the specific Slack functions.
Our app is going to need to make HTTP requests and parse JSON into Elixir data structures. We can use HTTPoison and Poison for these functions, respectively.
Open mix.exs and modify the application and deps functions to look like this:
These lines define our app dependencies and instruct our program to use them.
Install the dependencies with
Now open the mix.ex file. Since we are creating a CLI, modify the project function to look like this:
First, we will create our CLI parser, which is the entry point we defined in the mix.exs file. Open lib/slack_bot/cli.ex and add the following code:
This module defines our CLI behavior. Our main function is the entry point of the module. We take the input arguments to the program and pipe (|>) them through a series of functions to validate and execute the real work of the program.We use an OptionParser and parse for two flags, --channel and --file as defined in the switches list. We also alias these flags to the abbreviated versions -c and -f respectively. Finally, we process the arguments and if they meet our expected conditions (both flags present), we call our function to upload the file (SlackBot.Bot.upload_file), which we will define now.
Open lib/slack_bot/bot.ex and add the following code:
According to the Slack API documentation for files.upload, we pass three query parameters in our POST request to their API: a token, a file and a comma seperated value list of channels. When we call upload_file from our CLI module, we grab our Slack API token (assumed to be set as an environment variable SLACK_TOKEN; visit Slack to get a token) and pass in our file path and channel arguments from the CLI. Next, we build our URL, injecting the query parameters:
Examples for parameters:
Once we have our URL, we use HTTPoison to make a POST request to Slack, using an enctype of multipart/form-data. I had no idea what this meant, but got some help from this HTTPoison Github issue.
Lastly, run
to compile the script to binary and run it like this:
Hope you enjoy!
Discussion in the ATmosphere