Skip to main content
https://api.travis-ci.org/iky/nameko-slack.svg?branch=master

Slack Extensions for Nameko

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

Quick Start

Install from PyPI:

pip install nameko-slackclient

Real Time Messaging Client

“nameko>=2.7.0”, “slackclient>=2”

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}

More Examples

Listen for events of a particular type:

from nameko_slackclient 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_slackclient 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_slackclient 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_slackclient 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

Respond back to the channel by returning a string in the message handling entrypoint:

from nameko_slackclient import rtm

class Service:

    name = 'some-service'

    @rtm.handle_message
    def sure(self, event, message):
        return 'sure, {}'.format(message)

Run multiple RTM bots:

# config.yml

SLACK:
    BOTS:
        alice: ${ALICE_BOT_TOKEN}
        bob: ${BOB_BOT_TOKEN}
# service.py

from nameko_slackclient import rtm

class Service:

    name = 'some-service'

    @rtm.handle_message(bot_name='alice')
    def listen_as_alice(self, event, message):
        pass

    @rtm.handle_message(bot_name='bob')
    def listen_as_bob(self, event, message):
        pass
$ ALICE_BOT_TOKEN=xoxb-aaa-111 BOB_BOT_TOKEN=xoxb-bbb-222 nameko run --config ./config.yaml service
starting services: some-service

WEB API Client

A simple dependency provider wrapping Slack WEB API client.

Usage

The dependency provider uses the same config key as the RTM extension:

# config.yml

AMQP_URI: 'pyamqp://guest:guest@localhost'
SLACK:
    TOKEN: ${SLACK_BOT_TOKEN}
# service.py

from nameko.rpc import rpc
from nameko_slackclient import web


class Service:

    name = 'some-service'

    slack = web.Slack()

    @rpc
    def say_hello(self, name):
        self.slack.chat_postMessage(
            channel="#nameko",
            text="Hello from Nameko! :tada:")

You can also use multiple bots:

# config.yml

AMQP_URI: 'pyamqp://guest:guest@localhost'
SLACK:
    BOTS:
        alice: ${ALICE_BOT_TOKEN}
        bob: ${BOB_BOT_TOKEN}
# service.py

from nameko.rpc import rpc
from nameko_slackclient import web


class Service:

    name = 'some-service'

    alice = web.Slack('alice')
    bob = web.Slack('bob')

    @rpc
    def say_hello(self):
        self.alice.chat_postMessage(
            channel="#nameko",
            text="Hello from Alice! :tada:")
        self.bob.chat_postMessage(
            channel="#nameko",
            text="Hello from Bob! :tada:")

Metadata

Release files for nameko-slackclient 0.0.3

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

Source distribution (sdist)

Source distribution for nameko-slackclient 0.0.3
File Size Uploaded
nameko-slackclient-0.0.3.tar.gz 8.4 kB Details

Release files / nameko-slackclient-0.0.3.tar.gz

Download URL nameko-slackclient-0.0.3.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c198334dcae4c7931b74c5b174b311e72c3c7adcec78bf6d83db85bfefbbd456
BLAKE2b-256 checksum
How to use checksums
3b23a4b9a320e17bebbae8a2392481752626ef87914ddd39aa25d3e7852b9a1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release history Release notifications | RSS feed

This release

0.0.3 This release

1 release file

0.0.2

1 release file

0.0.1

1 release file

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