Building a Customizable Slack Bot with the Slack Machine

Setting Up the Slack Machine

To get started, we need to generate two tokens: the app-level token and the bot token. We can do this by creating a new Slack app on the Slack API website and following the instructions to generate the tokens. Once we have the tokens, we can add them to our local_settings.py file.

Configuring Slack

Next, we need to configure our Slack workspace to use the Slack Machine. We can do this by adding the Slack Machine app to our workspace and configuring the app settings.

Creating a Virtual Environment

To isolate our dependencies and avoid conflicts with other projects, we’ll create a virtual environment using the venv Python module.

python -m venv myenv

Activate the virtual environment:

source myenv/bin/activate

Interacting with the Slack Machine

Now that we have our virtual environment set up, we can install the Slack Machine module using pip:

pip install slack-machine

We can then run the bot using the Slack Machine and interact with it in our Slack workspace.

Building Custom Plugins

One of the most powerful features of the Slack Machine is its ability to support custom plugins. We can create our own plugins to extend the functionality of our bot and make it more useful for our specific use case.

Example Plugin: Dad Jokes

Let’s create a simple plugin that responds to the command “Make me laugh” with a random dad joke. We can do this by creating a new file called customPlugin.py and adding the following code:


import requests
import json

class DadJokePlugin(MachineBasePlugin):
    def dadjoke(self, msg):
        response = requests.get("undefinedicanhazdadjoke.com/")
        joke = response.json()["joke"]
        self.say(joke)

We can then add this plugin to our local_settings.py file and run the bot again to test it out.

Storage Capabilities

The Slack Machine also provides storage capabilities that allow us to store data in key-value pairs. We can use this feature to store tokens or other data that we need to access later.

Leave a Reply

Your email address will not be published. Required fields are marked *