Package: nameko-slack
Slack Extensions for NamekoNamekoextension for interaction withSlack APIs. UsesSlack Developer Kit for Python.Real Time Messaging ClientThe RTM extension is a Websocket client for Slack’sReal Time Messaging APIthat allows you to receive events from Slack in real time. Thertmmodule
contains two Nameko entrypoints for handling such events -handle_eventandhandle_message.UsageProvide Slack bot API token in your Nameko service config file:# config.ymlSLACK:TOKEN:"xoxb-abc-1232"Or using environment variable within your config:# config.ymlSLACK:TOKEN:${SLACK_BOT_TOKEN}Define your service with an entrypoint which will listen for and fire on any
event coming from Slack:# service.pyfromnameko_slackimportrtmclassService:name='some-service'@rtm.handle_eventdefon_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 ExamplesListen for events of a particular type:fromnameko_slackimportrtmclassService:name='some-service'@rtm.handle_event('presence_change')defon_presence_change(self,event):passListen for any message type event:fromnameko_slackimportrtmclassService:name='some-service'@rtm.handle_messagedefon_any_message(self,event,message):passUse regular expressions to fire on matching messages only:fromnameko_slackimportrtmclassService:name='some-service'@rtm.handle_message('^spam')defon_message_starting_with(self,event,message):passParse message and pass matching groups as positional or named arguments
to the entrypoint:fromnameko_slackimportrtmclassService:name='some-service'@rtm.handle_message('^spam (\w*)')defon_spam(self,event,message,egg):pass@rtm.handle_message('^egg (?P<ham>\w+)')defon_egg(self,event,message,ham=None):passRespond back to the channel by returning a string in the message handling
entrypoint:fromnameko_slackimportrtmclassService:name='some-service'@rtm.handle_messagedefsure(self,event,message):return'sure,{}'.format(message)Run multiple RTM bots:# config.ymlSLACK:BOTS:alice:${ALICE_BOT_TOKEN}bob:${BOB_BOT_TOKEN}# service.pyfromnameko_slackimportrtmclassService:name='some-service'@rtm.handle_message(bot_name='alice')deflisten_as_alice(self,event,message):pass@rtm.handle_message(bot_name='bob')deflisten_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-serviceWEB API ClientA simple dependency provider wrappingSlack WEB API client.UsageThe dependency provider uses the same config key as the RTM extension:# config.ymlAMQP_URI:'pyamqp://guest:guest@localhost'SLACK:TOKEN:${SLACK_BOT_TOKEN}# service.pyfromnameko.rpcimportrpcfromnameko_slackimportwebclassService:name='some-service'slack=web.Slack()@rpcdefsay_hello(self,name):self.slack.api_call('chat.postMessage',channel="#nameko",text="Hello from Nameko! :tada:")You can also use multiple bots:# config.ymlAMQP_URI:'pyamqp://guest:guest@localhost'SLACK:BOTS:alice:${ALICE_BOT_TOKEN}bob:${BOB_BOT_TOKEN}# service.pyfromnameko.rpcimportrpcfromnameko_slackimportwebclassService:name='some-service'alice=web.Slack('alice')bob=web.Slack('bob')@rpcdefsay_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:")