A Python 3 port of python-slack-events-api to pure Django.


License
MIT
Install
pip install django-slack-events==1.0.1

Documentation

Django Slack Events

A light, Python 3 (only) reimplementation of python-slack-events-api in pure Django leveraging Signals.

Installation

pip install django_slack_events

Getting Started

You should be able to follow along with the app setup and development workflow portions of the original package directions to get things going on the Slack side. When you reach the Flask instructions, jump back over here to see how to proceed with Django.

Quick Start

Once you've installed the package, you add it to your INSTALLED_APPS as you might expect:

    INSTALLED_APPS = [
        ...
        'django_slack_events',
    ]

Then in your project's URLs you can set your endpoint name. Following the /slack/events convention, that looks like:

    from django_slack_events import SlackEventHandler

    ...

    path('slack/events/', SlackEventHandler.as_view())

That's it! Your Django project is ready to handle incoming events from Slack.

Listening to Events

Events are handled as Django Signals, like so:

    from django.dispatch import receiver

    from django_slack_events import SlackEvent

    @receiver(SlackEvent)
    def my_callback(sender, **kw):
        payload = kw['slack_payload']
        ...

The sender is helpfully set to the event type for you, so you can leverage Django's built-in filtering to listen to specific events:

    from django.dispatch import receiver

    from django_slack_events import SlackEvent, SlackEventType

    @receiver(SlackEvent, sender=SlackEventType.REACTION_ADDED)
    def reaction_added(sender, **kw):
        payload = kw['slack_payload']
        ...

Full Example

Put this code in any file available in your app's ready context and set the appropriate environment variables, et voila!

    import os

    from django.dispatch import receiver
    from django_slack_events import SlackEvent, SlackEventType
    import slack


    slack_client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])


    @receiver(SlackEvent, sender=SlackEventType.APP_HOME_OPENED)
    def app_home_opened(sender, **kw):
        event_data = kw['slack_payload']
        response = slack_client.views_publish(view={
            "type": "home",
            "blocks": [{
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "[GENERAL KENOBI VOICE] Hello, there! ;)"
                }
            }]
        }, user_id=event_data['event']['user'])
        assert response['ok']

BUT WAIT, THERE'S MORE

Slack Interactions are extremely similar to events, in that they have a type, a payload, and can be handled in many cases by a simple function with access to the Slack API client.

Here's the above example extended to provide and handle a button:

    import os

    from django.dispatch import receiver
    from django_slack_events import SlackEvent, SlackInteractionType
    import slack


    slack_client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])


    @receiver(SlackEvent, sender=SlackEventType.APP_HOME_OPENED)
    def app_home_opened(sender, **kw):
        event_data = kw['slack_payload']
        response = slack_client.views_publish(view={
            "type": "home",
            "blocks": [{
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "You can add a button alongside text in your message. "
                },
                "accessory": {
                    "type": "button",
                    "text": {
                            "type": "plain_text",
                            "text": "Button",
                            "emoji": True
                    },
                    "value": "click_me_123"
                }
            }]
        }, user_id=event_data['event']['user'])
        assert response['ok']


    @receiver(SlackEvent, sender=SlackInteractionType.BLOCK_ACTIONS)
    def block_actions(sender, **kw):
        event_data = kw['slack_payload']
        response = slack_client.views_publish(view={
            "type": "home",
            "blocks": [{
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "[GENERAL KENOBI VOICE] Hello there!",
                },
            }]
        }, user_id=event_data['user']['id'])
        assert response['ok']

Note: For now, only BLOCK_ACTIONS, VIEW_CLOSED, and VIEW_SUBMISSION are supported. More functionality is planned in the future.