Skip to content

Custom Plugins

Max Bridgland edited this page Jan 15, 2020 · 3 revisions

Creating custom commands and plugins are easy for Slacky. All plugins are written in Python and you can see an example for yourself in slacky/plugins/custom/example.py. To create your own plugin follow these steps:

First, create a file in slacky/plugins/custom/ with the name of your plugin. This file can hold one or multiple commands, you can also use a folder but that will get more complicated for imports. We will call this file plugin.py for now. In this plugin.py you will want to follow the format of the current example in the repo for obtaining msg and channel data. Below is the example plugin available already:

Building Your New Plugin:

from slacky import client, config, Prefixes
from slack.errors import SlackApiError

def custom_example(**payload):
    # Get Data from Payload
    data = payload['data']
    channel_id = data['channel'] # Get Channel ID
    user = data.get('user') # Get User
    timestamp = data['ts'] # Get msg Timestamp
    if check_user(user): # Check if User == You
        web_client = client # Init Client
        text = data.get('text') # Get Text
        # Check for Command Here
        if text:
            text_split = text.split(' ')
            cmd = text_split[0]
            if cmd == config['prefix'] + 'example':
                # Command has been triggered
                print(Prefixes.event + 'Ran Command: example')
                # Do your logic here and then update the message at the end below.
                try:
                    web_client.chat_update(
                        channel=channel_id,
                        ts=timestamp,
                        text="This command is an example custom command."
                    )
                except SlackApiError as e:
                    print(Prefixes.error + str(e))

This plugin simply updates your message to say "This command is an example custom command."

Installing Your New Plugin:

Go to slacky/plugins/custom/__init__.py and import your plugin(s) inside of it. If you have a plugin inside a file called plugin.py you can import it with from .plugin import *. This will allow the main plugin file to automatically import your commands.

Now you have to register your command with your RTMClient. To do so go into slacky/__main__.py and go to the bottom where you see the example plugin loaded. You can then register your plugin in the Commands class which has a commands dictionary attribute. Each command name is mapped to a function. Simply map your custom function to a new name.

Now restart the bot and you will have loaded your custom plugin!

Clone this wiki locally