I’m lifelong friends with about 4 other guys. And I recently convinced them all to join a Slack channel (one of them is an Android guy, thus the dreaded green messages). Slack is great. I’m a new developer and we’ve used it at the last two places I’ve worked. One of the great features of Slack is the great selection of apps and bots that are available to install. I’m attempting to learn backend programming and I’m a big fan of Ruby and Ruby on Rails. Ruby is extremely developer friendly and after you get past it’s initial quirks, it’s fantastic.
Fortnite is one of the main ways that we all spend time together and we are always checking in to see when we can all get together and play. All of that culminated in the idea for a Slack bot written in Ruby. This bot allows any of us to ask the bot ‘Who is online?’. The bot will go to the Xbox Api and check to see what the status of the 5 guys are, and report back if they are playing Fortnite. The functionality of this is extremely simple. It is built in pure Ruby. No Rails required. There is some external setup required, but the actual program has minimal external specifications.
So let’s dive in!
Setup
As I said, I used pure Ruby so I started with a gem file and a main file that I named slack.rb. I used the following gems:
slack-ruby-bot — This gem is crucial to the success of the app as it is what connects the app and Slack
xbox-api — Xbox Doesn’t have a Ruby specific API implementation. They do have a C++ implementation for all you C++ folk out there. I used the Unofficial Xbox API provided by Alan Wynn. The docs are great and he provides different implementations for Python and Ruby.
json — JSON is pretty critical when hitting most API’s and this gem allows Ruby and JSON to play nice together.
dotenv — Last but not least, I used dotenv to hide my Slack and Xbox Api keys.
After your gems have been added to your Gemfile, make sure to run bundle install in your terminal to populate your Gemfile.lock file. External set up will require you to get an API key from both Slack and Xbox API. Both keys are fairly straight forward processes.
Xbox Api
Before we go too much further, it is important to discuss how this api obtains player data. The api uses the players accountxuid. Instead of adding a separate call to obtain the id of each player, I went ahead and did a bit of the grunt work ahead of time. I used Postman to send a request to the Xbox Api with my friends gamertags (specifically GET https://xboxapi.com/v2/xuid/{gamertag}. Make sure to set your headers with X-AUTH and your api key.) Postman then spit out their xuid and I ended up hard coding this into the program. My thinking behind this is that the program will be slightly more performant without the extra api call.
Coding
Photo by Chris Ried on Unsplash
Head back to your slack.rb file. It’s important to require all of your gems at the top of your file so that Ruby knows what to import. I then make my class an instance of the Slack Ruby Bot. After that, I put my api key into a variable named ‘client’, and I use ‘client’ as I set up variables for all of my friends. Once those are setup, it’s time for the Slack implementation. Slack uses a function called ‘command’ to provide functionality. This can all be explained much more thoroughly in the Slack Ruby Bot README. I used the word ‘pong’ as my Slack keyword for my bot. This keyword will put the bot in motion to go fetch the data and you can change it to whatever keyword you want.
Next comes the functionality. I take the player data I have put into variables and get their ‘presence’ from the Xbox API and set that to a variable. The data you get back is a Ruby object (I think ¯_(ツ)_/¯). I spent the majority of my time on this project looking for some way to parse this data without moving into JSON land. Unfortunately, I was unsuccessful. After I get this Ruby data, I use the JSON gem to get the rest of the data. I call JSON.generate on each of those new variables and set this to another variable. I then call JSON.parse now I’m in JSON land and I can pull out the data of what they are doing right now. An if statement determines what will be printed to Slack, dependent on the player data. After this, save everything, and move over to your terminal and cd into the project. Once you’re there, run the following script:
SLACK_API_TOKEN={SLACK API KEY} bundle exec ruby slack.rb
This will spin up your app on your local machine. I spent a lot of time trying to push this to Heroku with no luck. Still working on that. Move over to your Slack client and enter the following: @{SLACK BOT NAME} {YOUR COMMAND} wait a couple seconds and viola! Information!
That’s pretty much it! A couple final notes:
This is definitely not extremely performant and there are definitely better ways to do this. This is just what I got to work. Let me know what works for you!
The Xbox API free tier only allows 120 requests per hour. I ran into this limit a couple times during development. The paid tiers aren’t too expensive.
Check out my code on Github to see how I set it up.
Thanks for checking out my project! I’d love to hear your experiences and what you think! Happy gaming!
Top comments (0)