Nameko extension for interaction with Slack APIs. Uses Slack Developer Kit for Python.
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
.
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'}
...
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
A simple dependency provider wrapping Slack WEB API client.
The dependency provider uses the same config key as the RTM extension:
You can also use multiple bots:
# 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:")