A card fetcher that utilizes Slack's Realtime Messaging API
This bot provides a functionality that you might have seen before on sites like Reddit and MTGSalvation. The functionality is straight forward: wrap a card’s name in double brackets and the bot will respond with the cards (e.g. [[Black Lotus]]). See the pictures below for an example.
For the application code, this project uses Node.js, MongoDB and the slackbots npm package. For deploying the bot, the project uses Packer to build an AMI with the dependencies and Terraform to start the instance on AWS. The data for the cards from the mtgjson GitHub repo.
For each of the following setups, you must perform this setup:
https://[YOUR_TEAM].slack.com/apps/manage/custom-integrations
.Bots
.Add Configuration
.config.js
in the secrets
directory. Use the Secrets Example below.npm install
the dependencies for this project.move-cards
scripts to move all the English file from the MTG JSON json
directory to the data directory in this repo like so: PATH_TO_JSON=/path/to/mtgjson/json PATH_TO_DESTINATION=/path/to/data/directory node ./tasks/move-cards.js
mongoUrl
field in the config/secrets.js
collectionName
field in the config/secrets.js
config/secrets.js
. The bot ID may be found in the URL when you’re editing the bot on Slack’s website (something like https://planeswalker.slack.com/services/BOT_ID_HERE
)PATH_TO_JSON=/path/to/data/directory ./tasks/ingest_cards
adminUser
field in the secrets Run the index script: node ./index.js
This repository also comes with Terraform and Packer resources for easy AWS cloud deployment. These directions will teach you how to run your very own instance of the bot in the cloud with just a few commands.
In my personal repository, I store secrets and other private information inside of the Terraform and Packer config files, so you’ll have to follow the steps and examples below to set up your own configurations.
AWS_SECRET_ACCESS_KEY
and AWS_ACCESS_KEY_ID
are in your environment.brew
, run brew install terraform
and brew install packer
.deploy/config/keys
directory and move the SSH keypair into it.deploy/config
directory, create a .tfvars
file. Make sure this file’s name corresponds with the name of the whatevever you name your state file. State files are created during the terraform apply process..tfvars
file and populate each of the variables with the correct values. Use the example values if you’re not sure what to put..json
file inside of the deploy/config
directory to store the AMI configuration. Name the file such that it matches the name of your .tfvars
file except with an ami-
on front (e.g. ami-us-east-2-dev.json
if you’ve name your .tfvars
file us-east-2-dev.tfvars
).json
file to, and populate each of the variables with the correct values. Use the example values if you’re not sure what to put. Note: the source_ami
will vary by region.Below is the recommended structure for your deploy/config
directory:
config
├── ami-us-east-2-dev.json
├── keys
│ ├── aws_mtg
│ └── aws_mtg.pub
└── us-east-2-dev.tfvars
Now that you have the configurations in place, deployment is very easy. Run the below commands to build the AMI and apply the terraform resources (except replace the NAME_OF_VAR_FILE
with the actual name of your .tfvars
file):
cd deploy && MTG_STATE=/path/to/state/file/directory ./utils/pack NAME_OF_VAR_FILE && ./utils/build apply NAME_OF_VAR_FILE
For example, if the name of your .tfvars
is us-east-2-dev
. Running the below command verbatim will work:
cd deploy && MTG_STATE=/path/to/state/file/directory ./utils/pack us-east-2-dev && ./utils/build apply us-east-2-dev
When it’s done packaging the dist, building the AMI, and applying the terraform resources, you’ll get a PM from the bot saying it’s fired up and ready to go.
module.exports = {
token: 'SLACK API KEY',
mongoUrl: 'MONGO URL',
collectionName: 'COLLECTION NAME',
adminUser: 'YOUR SLACK USERNAME',
bot: {
id: 'BOT ID',
icon: 'BOT EMOJI ICON',
name: 'BOT NAME'
}
};
aws_region = "AWS REGION (e.g. us-east-2)"
slackbot_instance_type = "EC2 INSTANCE TYPE (e.g. t2.micro)"
environment = "NAME OF ENVIRONMENT (e.g. dev)"
public_key_path = "RELATIVE PATH TO PUBLIC KEY FOR KEY PAIR (e.g. config/keys/aws_mtg.pub)"
private_key_path = "RELATIVE PATH TO PRIVATE KEY FOR KEY PAIR (e.g. config/keys/aws_mtg)"
key_name = "KEY NAME (e.g. mtg_slackbot)"
white_listed_ip = "YOUR PUBLIC IP"
{
"node_version": "7.6.0",
"region": "REGION TO BUILD AMI (e.g. us-east-2)",
"source_ami": "AMI TO BUILD OFF OF (e.g. ami-fcc19b99)",
"instance_type": "INSTANCE SIZE (e.g. t2.micro)",
"environment": "ENVIRONMENT NAME (e.g. dev)"
}