A slash command for Slack that displays an xkcd comic in the channel, either by comic number (e.g. /xkcd 702
) , description (e.g. /xkcd battery horse
) or a getting a random comic by simply typing /xkcd
.
- Slash Command for our command (
/xkcd
) - Incoming Webhook to display the results of the command
- Custom Google Search Engine to consume search results via the API
- Tiny web application to tie it all together (I'm using a Nancy application, but can be anything)
- Somewhere to host the web application (e.g. Azure or AppHarbor, both offer free tier)
Perform the following steps to set up the command correctly.
Create a new Slash Command. Call it /xkcd
, and leave the page open to put in a URL later. Copy the value of the Token field, this is your SLACK_SLASH_COMMAND_TOKEN
.
Create a new Incoming Webhook. You can choose any channel from the list; it doesn't matter. The channel will be overridden on each request with the channel from which the request originated. After creating, you'll see a Webhook URL field. This is your SLACK_WEBHOOK_URL
.
Unfortunately, Google Web Search API was deprecated some time ago, and scraping the results of a search is against the Google TOS (which can get you banned on Google). However, it is possible to use the Google Custom Search API. We can define our own Custom Search Engine (CSE) to only search the xkcd.com site, and that is allowed with a C# API.
Note: CSE allows 100 queries per day for free!
- Make sure you're logged in with a Google Account.
- Go to https://cse.google.com/cse/create/new
1. In Sites to search type
xkcd.com
2. Press Create - Press Control Panel
- Turn off Image search, make sure Search only included sites is selected.
- Click the Search engine ID button, and copy the code. This is your
GOOGLE_SEARCH_ENGINE_ID
.
Finally, to enable access via the C# API, we need to enable the Custom Search API:
- Go to https://console.developers.google.com/project
- Press Create Project, and give it a name
xkcd
- In the newly created project, go to the API & auth menu, selecting APIs
- Under Other popular APIs select Custom Search API
- Press the Enable API button
- Under APIs & auth menu, select Credentials
- Press Add credentials dropdown, and select API key
- In the new popup, press Browser key, give it a name (e.g.
xkcd-key
) - When you're done, you'll get a popup with the API key. This is your
GOOGLE_SEARCH_API_KEY
Phew!
Clone this project, and open it with Visual Studio. This is a Nancy web application, which can be deployed as Azure Web App for free. After deploying it to Azure, go to the Azure Management Portal, navigate to Web Apps, select your application and go to Configure. Scroll down until the app settings section, and add the keys as mentioned above:
Finally, copy the URL of your Azure application, and paste it into the Slash Command page from above, in the URL field, and make sure the selected method is POST. The full URL should be e.g.:
http://my-xkcd-slack-bot.azurewebsites.net/xkcd
Add in some additional descriptions and usage hints for the command, customize the icon and name of the incoming webhook, and you're all set!