Skip to main content

Nameko extension for interaction with Slack APIs

Project description

https://travis-ci.org/iky/nameko-slack.svg?branch=master

Nameko extension for interaction with Slack APIs. Uses Slack Developer Kit for Python.

Real Time Messaging Client

The RTM extension is a Websocket client for Slack’s Real Time Messaging API that allows you to receive events from Slack in real time. The rtm module contains two Nameko entrypoints for handling such events - handle_event and handle_message.

Usage

Provide Slack bot API token in your Nameko service config file:

# config.yml

SLACK:
    TOKEN: "xoxb-abc-1232"

Or using environment variable within your config:

# config.yml

SLACK:
    TOKEN: ${SLACK_BOT_TOKEN}

Define your service with an entrypoint which will listen for and fire on any event coming from Slack:

# service.py

from nameko_slack import rtm

class Service:

    name = 'some-service'

    @rtm.handle_event
    def on_any_event(self, event):
        print(event)

Finally, run the service:

$ SLACK_BOT_TOKEN=xoxb-abc-1232 nameko run --config ./config.yaml service
starting services: some-service
{'type': 'hello'}
{'type': 'presence_change', 'user': 'ABCDE1234', 'presence': 'active'}
{'type': 'user_typing', 'user': 'ABCDE1234', 'channel': 'ABCDE1234'}
{'type': 'message', 'text': 'spam', 'channel': 'ABCDE1234', 'user': 'ABC...
{'type': 'presence_change', 'user': 'ABCDE1234', 'presence': 'active'}
...

More Examples

Listen for events of a particular type:

from nameko_slack import rtm

class Service:

    name = 'some-service'

    @rtm.handle_event('presence_change')
    def on_presence_change(self, event):
        pass

Listen for any message type event:

from nameko_slack import rtm

class Service:

    name = 'some-service'

    @rtm.handle_message
    def on_any_message(self, event, message):
        pass

Use regular expressions to fire on matching messages only:

from nameko_slack import rtm

class Service:

    name = 'some-service'

    @rtm.handle_message('^spam')
    def on_message_starting_with(self, event, message):
        pass

Parse message and pass matching groups as positional or named arguments to the entrypoint:

from nameko_slack import rtm

class Service:

    name = 'some-service'

    @rtm.handle_message('^spam (\w*)')
    def on_spam(self, event, message, egg):
        pass

    @rtm.handle_message('^egg (?P<ham>\w+)')
    def on_egg(self, event, message, ham=None):
        pass

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nameko-slack-0.0.2.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

nameko_slack-0.0.2-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file nameko-slack-0.0.2.tar.gz.

File metadata

File hashes

Hashes for nameko-slack-0.0.2.tar.gz
Algorithm Hash digest
SHA256 9b77e5996b6113f4ecd3a8febad5d9c49ff2be859e6b4b9fcaf746ece4ac7374
MD5 1de5cc9fc1e29ee7bdb65ac1eb9fb54e
BLAKE2b-256 e8a114ecc3381c4c46b38ddbb4b8558c54fad34a9199a4c8925718a98c308730

See more details on using hashes here.

File details

Details for the file nameko_slack-0.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for nameko_slack-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c2042ffffe7fe1fa2df6686c9e9858cb684190b9f76fa87259485ae63e063682
MD5 119ad258d5682e1260863ceb8272da47
BLAKE2b-256 4bd36fa15d1d82884ddafb57467b307cc0417fcb259a336c6cd149087f4896b8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page