使用django channels v2为slack bot供电
slack-channeler的Python项目详细描述
松弛通道器
使用django channels v2为slack bot供电
安装
pip install slack-channeler
用法
松弛通道依赖于通道层。首先,确保它是设置好的。channels-redis是推荐的。
# settings.pyCHANNEL_LAYERS={'default':{'BACKEND':'channels_redis.core.RedisChannelLayer','CONFIG':{"hosts":[('localhost',6379)],},},}
创建消费者以处理松弛事件
# consumers.pyfromchannels.consumerimportAsyncConsumer,get_handler_nameclassSlackConsumer(AsyncConsumer):asyncdefdispatch(self,message):handler=getattr(self,get_handler_name(message),None)ifhandler:awaithandler(**message['data'])asyncdefslack_message(self,channel,text,**kwargs):# Simply echo back messageawaitself.channel_layer.send('slack',{'type':'message','channel':channel,'text':text,})
将松弛事件路由到消费者
# routing.pyfromchannels.routingimportProtocolTypeRouter,ChannelNameRouterfrom.consumersimportSlackConsumerapplication=ProtocolTypeRouter({'channel':ChannelNameRouter({'slack':SlackConsumer,}),})
启动通道工作进程以处理通道层的空闲事件
python manage.py runworker slack
最后,运行松弛通道器
SLACK_CHANNELER_TOKEN=xoxb-12345678900-098765432100-DeadBeefFeed90iIJjYsf3ay slack_channeler
建筑包
目前,poetry不支持动态生成版本文件,也不支持自定义挂钩。为了保持版本号的权威源pyproject.toml
,使用自定义的build.py
脚本动态生成version.py
。
要构建松弛通道器,请运行python build.py
。这与poetry build
具有相同的语义。