Skip to content

iky/nameko-slack

Repository files navigation

image

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:

Or using environment variable within your config:

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

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:

Listen for any message type event:

Use regular expressions to fire on matching messages only:

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

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

Run multiple RTM bots:

$ 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:

You can also use multiple bots: