Skip to main content

slacktools-commands

A simple framework for working with Slack slash-commands (https://api.slack.com/interactivity/slash-commands).

Register you custom Command class with the ComandFactory and when you receive a command request from Slack simply grab the command instance from the factory and execute it.

Install

pip install slacktools-commands

Basic Usage

Define your command:

from commands import register_command, Command, CommandValidationError

from myproject import get_status, post_status


@register_command("/status")
class StatusCommand(Command):
    def _validate(self):
        if not get_status(id=self.payload.text):
            raise CommandValidationError("Not a valid id.")

    def _execute(self):
        post_status(id=self.payload.text)

Handle the Slack command request:

from rest_framework.views import APIView
from rest_framework.response import Response

from commands import CommandFactory

class CommandsView(APIView):
    def post(self, request):
        command = CommandFactory.make_commmand(request.data)
        command.execute()
        return Response()

Action Commands

Action commands allow you execute many different actions from a single Slack command. The text following the command is used to determine which action should be performed. The text is split by spaces, the first character set determines the action and the remain character sets are passed to the action as options/parameters.

Example

The below class definitions will handle the following command: /status service api

from commands import ActionCommand, Action, CommandValidationError, register_command

from myproject import post_status_msg


class ServiceStatus(Action):
    def validate(self):
        if len(self.options) == 0:
            CommandValidationError(self.payload, "Missing service name")

    def execute(self):
        post_status_msg(self.options[0])


@register_command("/status")
class StatusCommand(ActionCommand):
    ACTIONS = {
        "service": ServiceStatus
    }

Metadata

Release files for slacktools-commands 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slacktools-commands 1.0.0
File Size Uploaded
slacktools-commands-1.0.0.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slacktools-commands 1.0.0
File Interpreter ABI Platform
slacktools_commands-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.0 kB

Release files / slacktools-commands-1.0.0.tar.gz

Download URL slacktools-commands-1.0.0.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c6248e9363eb4c555cdcb7e46c0e42dee3bba646e67f084e51082b1463efe589
BLAKE2b-256 checksum
How to use checksums
3101c0b70cb46d1d63be1e8b1fb4fc8994d8921f1cc45b32cff9bab6d76a4e1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

Release files / slacktools_commands-1.0.0-py3-none-any.whl

Download URL slacktools_commands-1.0.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e204d0cb373f84e2676264f044a7927348bd6c8f80e527ac132962349262501
BLAKE2b-256 checksum
How to use checksums
c574c8562059294f9191dc243946b51008ba2610b9df137b5355434b6d2dbdb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page