It was created with the help of maxdeviant and the work on Nouvion
Wrriten up as of May 2, 2015
Named after the Roman poet... Virgil is a Slack bot, created in Ruby, that you can incorporate into your Slack.
2022 note
THIS IS DEPRECATED. THERE ARE BETTER WAYS TO DO SLACK BOTS. THIS WAS LIKE ONE OF THE FIRST THINGS I EVER DID, AND IS KEPT FOR NOSTALGIA ONLY
######Structure ######Getting Started ######ForecastIO ######Adding on / contributing ######Deployment
.
|-- Gemfile
|-- Gemfile.lock
|-- LICENSE
|-- README.md
|-- Rakefile
|-- app.rb
|-- lib
| |-- interpreter.rb
| |-- modules
| |-- module.calc.rb
| |-- module.forecast.rb
| |-- module.help.rb
| |-- module.pathfind.rb
| |-- module.recite.rb
| |-- module.reference.rb
| |-- module.self.rb
| |-- module.whois.rb
| |-- slack.rb
| |-- virgil.rb
|-- spec
| |-- slack
| |-- slack_spec.rb
| |-- spec_helper.rb
| |-- virgil
4 directories, 19 files
Virgil is actually pretty simple. He runs out of app.rb
at the root level. interpreter.rb
is effectively your controller. It parses the message and acts accordingly, telling Virgil how to respond. slack.rb
is what is doing the leg work in connecting to Slack's API.
When you boot the app up and get your server going, virgil is effectively listening and able to connect to Slack's RTM API via a Faye websocket.
Under lib
in the modules
folder is where things are broken out (e.g. module.forecast.rb
is relevant to forecasting weather, module.calc.rb
pertains to mathematical calculations and so on).
See Adding on / contributing for more on this.
For now, to get Virgil up and running locally, you're going to need a few things.
- Ensure that Ruby 2.2.1 is installed, this repo runs that locally.
- You need to set up the bot user on your Slack. Obviously, name your bot virgil. Yes, it MUST be virgil.
To get Virgil's virgil forecast [city]
working... you'll need a ForecastIO key. To get one, head over to their API and sign up, which will give you a key. It looks something like 380xxxxxa3xxxxx17xxxxxcb9
.
In your .env
that you previously made
FORECAST_TOKEN="380xxxxxa3xxxxx17xxxxxcb9"
And you should be good to go.
Hypothetically, you have Virgil up and running on your own. Now, you're wondering how to add on to him or, make him something else to work on your own.
First, figure out what term(s) and what text you expect. For instance virgil [method] [relevant text]
is the format for pretty much everything.
Once you have a method, add that method to the case statement in interpreter.rb
under lib
. The [relevant text]
is text[1..-1].join(' ')
. Looking at the other cases is a good idea for reference. As noted in the comments above the case statement, keep your code here concise, with most of the work going on in your module.
Create another file under modules
with the relevant name (e.g. forecast.rb
is for forecasting the weather). Add the correct require_relative
in references.rb
under modules
as well.
Test it out... prove it works... and you're good to go. If you really want to commit to this, a pull request will suffice.
At time of writing this, I do not have virgil deployed anywhere. einhorn
, recommneded by maxdeviant paired with deploying via Linode (or another 99.9% up service) seems to be the way to go. If you ping virgil multiple times while its running, you can potentially create multiple instances. einhorn allows you to push code while live and Linode has a good reputation for staying up and a relatively cheap cost. Heroku apps, while popular for deploying with Ruby, go to sleep and using something like kaffeine, could have negative effects as mentioned before.
Fortune favors the bold