Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/they4kman/slack-channeler
Power a Slack bot with Django Channels v2
https://github.com/they4kman/slack-channeler
django-channels python slack
Last synced: 13 days ago
JSON representation
Power a Slack bot with Django Channels v2
- Host: GitHub
- URL: https://github.com/they4kman/slack-channeler
- Owner: theY4Kman
- License: mit
- Created: 2019-04-25T06:07:01.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-07T08:37:21.000Z (over 4 years ago)
- Last Synced: 2024-11-24T15:44:52.411Z (2 months ago)
- Topics: django-channels, python, slack
- Language: Python
- Homepage: https://pypi.org/project/slack-channeler/
- Size: 59.6 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# slack-channeler
Power a Slack bot with Django Channels v2# Installation
```bash
pip install slack-channeler
```# Usage
slack-channeler relies on the channel layer. First, ensure it's setup. [channels-redis](https://github.com/django/channels_redis) is recommended.
```python
# settings.pyCHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('localhost', 6379)],
},
},
}
```Create a consumer to handle Slack events
```python
# consumers.pyfrom channels.consumer import AsyncConsumer, get_handler_name
class SlackConsumer(AsyncConsumer):
async def dispatch(self, message):
handler = getattr(self, get_handler_name(message), None)
if handler:
await handler(**message['data'])async def slack_message(self, channel, text, **kwargs):
# Simply echo back message
await self.channel_layer.send('slack', {
'type': 'message',
'channel': channel,
'text': text,
})
```Route Slack events to the consumer
```python
# routing.pyfrom channels.routing import ProtocolTypeRouter, ChannelNameRouter
from .consumers import SlackConsumer
application = ProtocolTypeRouter({
'channel': ChannelNameRouter({
'slack': SlackConsumer,
}),
})
```Start a Channels worker to handle Slack events from the channel layer
```bash
python manage.py runworker slack
```Lastly, run slack-channeler
```bash
SLACK_CHANNELER_TOKEN=xoxb-12345678900-098765432100-DeadBeefFeed90iIJjYsf3ay slack_channeler
```# Building package
Currently, poetry does not support dynamic generation of version files, nor custom hooks to do so. To keep `pyproject.toml` the source of authority for version numbers, a custom `build.py` script is used to dynamically generate `version.py`.To build slack-channeler, run `python build.py`. This has the same semantics as `poetry build`.