GithubHelp home page GithubHelp logo

slacktools-interactivity's Introduction

slacktools-interactivity

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

Install

pip install slacktools-interactivity

Commands

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

Basic Usage

Define your command:

from interactivity import CommandFactory, CommandHandler, CommandValidationError

from myproject import get_status, post_status


@CommandFactory.register("/status")
class StatusCommand(CommandHandler):
    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 interactivity import CommandFactory

class CommandsView(APIView):
    def post(self, request):
        handler = CommandFactory.make_handler(request.data)
        handler.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 interactivity import (
    ActionCommandHandler, 
    CommandAction, 
    CommandValidationError,
    CommandFactory
)

from myproject import post_status_msg


class ServiceStatus(CommandAction):
    def validate(self):
        if len(self.options) == 0:
            CommandValidationError(self.payload, "Missing service name")
    
    def execute(self):
        post_status_msg(self.options[0])


@CommandFactory.register("/status")
class StatusCommand(ActionCommandHandler):
    ACTIONS = {
        "service": ServiceStatus
    }

Views

To document

Actions

To document

slacktools-interactivity's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.