{
"path": "/posts/2016/2016-01-28-first-elixir-module",
"site": "at://did:plc:mracrip6qu3vw46nbewg44sm/site.standard.publication/self",
"tags": [
"code",
"elixir"
],
"$type": "site.standard.document",
"title": "Creating an Elixir module",
"updatedAt": "2016-01-28T07:23:00.000Z",
"publishedAt": "2016-01-28T07:23:00.000Z",
"textContent": "Creating an Elixir module\n\nTo get a better handle on Elixir, I developed a simple CLI tool for sending files in Slack.\n\nTo create a new project, run\n\nThis creates a new Elixir project which looks like this\n\nNavigate 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.\n\nOur 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.\n\nOpen mix.exs and modify the application and deps functions to look like this:\n\nThese lines define our app dependencies and instruct our program to use them.\n\nInstall the dependencies with\n\nNow open the mix.ex file. Since we are creating a CLI, modify the project function to look like this:\n\nFirst, 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:\n\nThis 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.\n\nOpen lib/slack_bot/bot.ex and add the following code:\n\nAccording 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:\n\nExamples for parameters:\n\nOnce 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.\n\nLastly, run\n\nto compile the script to binary and run it like this:\n\nHope you enjoy!",
"canonicalUrl": "https://www.danielcorin.com/posts/2016/2016-01-28-first-elixir-module"
}