Skip to main content

Nameko extension for interaction with Slack APIs

Project description

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.

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

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

from nameko_slack 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_slack 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_slack import web


class Service:

    name = 'some-service'

    slack = web.Slack()

    @rpc
    def say_hello(self, name):
        self.slack.api_call(
            '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_slack import web


class Service:

    name = 'some-service'

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

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

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.6.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

nameko_slack-0.0.6-py2.py3-none-any.whl (9.5 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: nameko-slack-0.0.6.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for nameko-slack-0.0.6.tar.gz
Algorithm Hash digest
SHA256 e79ffeb69dd1d4495e22e23f39d60b1aa2dcf52b0af2bfb157d35b75fbd3750b
MD5 5f532f467bdab4e65e20d986ca48eb11
BLAKE2b-256 cdd89fa2b15600e20f441789bb6f72325ea7fe05f30dc06eb0af753f28acb466

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nameko_slack-0.0.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for nameko_slack-0.0.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9fdd8cd63cffc243122719938dbfd3ebc7440ef576d40472b62b34118930c983
MD5 51abbf94a7d7ac06353bc48c6a2b5559
BLAKE2b-256 c8093075a26a0f9b3f560ba4e74d3729e3154daec04ff47644b8c6271e011102

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