Package: django-feishu-sdk
django_feishu_sdk使用本三方库可参考以下网址：https://gitee.com/li-pf/django_feishu_sdkhttps://github.com/lpfandr/django_feishu_sdkRequired1.Install or add django-feishu-sdk to your python path.pipinstalldjango-feishu-sdk2.Add feishu to your setting.# 飞书配置FEISHU_V2_SDK_CONFIGS={'FEISHU_APP_ID':"xxx",# 企业应用的id'FEISHU_APP_SECRET':"xxx",# 企业应用的SECRET'FEISHU_VERIFY_TOKEN':"xxx",# 企业应用的事件加密token'FEISHU_ENCRYPT_KEY':"xxx",# 企业应用的事件加密key}3.If you use events, you'd better make the following settingsALLOWED_HOSTS=['*']MIDDLEWARE=['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware',# 'django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',]Usage⚠ 您应该优先创建一个app,完善相关url路径，startapp xxx除事件外的数据获取1.在view.py中调用飞书数据fromdjango.httpimportJsonResponsefromdjango_feishu_sdkimport*defsend_text_messages(request):"""发送文本消息"""fs_client=FeishuClient()res=fs_client.send_text(text='测试消息',receive_id_type='open_id',receive_id='ou_63f6845d304e7a1a3df99d6fce291d58')print(res)returnJsonResponse(res)defget_user_info(request):"""按照用户个人信息"""fs_client=FeishuClient()res=fs_client.get_user_info(user_id='77g56381',user_id_type='user_id')print(res)returnJsonResponse()2.url.pyfromdjango.urlsimportpathfrom.viewsimport*urlpatterns=[path('get_user_info/',get_user_info),# 获取用户的信息path('event_process/',event_process),# 接收事件订阅的消息]3.当前页面json显示事件的数据获取1.在app中的统计目录新建feishu.py文件,内容如下fromdjango.httpimportJsonResponsefromdjango_feishu_sdkimportUrlVerificationEvent,MessageReceiveEvent,FeishuClient,MessageReadEventfromdjango_feishu_sdk.apis.eventimportEventManagerevent_manager=EventManager()# 处理首次事件配置@event_manager.register("url_verification")defrequest_url_verify_handler(req_data:UrlVerificationEvent):# url verification, just need return challengereturnJsonResponse({"challenge":req_data.event.challenge})# 消息已读事件@event_manager.register("im.message.message_read_v1")defmessage_read_event_handler(req_data:MessageReadEvent):print('sender',req_data.event.get('reader'))print('req_data',req_data.header.event_id)receive_id=req_data.event.get('reader').get('reader_id').get('open_id')fs_client=FeishuClient()res=fs_client.send_text(text='监测到消息已读',receive_id_type='open_id',receive_id=receive_id)print(res)returnJsonResponse(res)2.view.py文件如下fromdjango_feishu_sdkimport*from.feishuimportevent_managerdefevent_process(request):"""与飞书事件挂载"""event_handler,event=event_manager.get_handler_with_event(request)returnevent_handler(event)事件扩展1.在app中的统计目录新建feishu.py文件,内容如下fromdjango.httpimportJsonResponsefromdjango_feishu_sdkimportUrlVerificationEvent,MessageReceiveEvent,FeishuClient,MessageReadEvent,EventContentfromdjango_feishu_sdk.apis.eventimportEventManagerclassMessageReadEvent(EventContent):"""事件类型：已读消息飞书文档地址：https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/events/message_read"""@staticmethoddefevent_type():return"im.message.message_read_v1"classMyEventManager(EventManager):_event_list=[MessageReceiveEvent,UrlVerificationEvent,MessageReadEvent]event_manager=MyEventManager()@event_manager.register("im.message.receive_v1")defmessage_receive_event_handler(req_data:MessageReceiveEvent):# sender_id = req_data.event.sender.sender_id# message = req_data.event.messagefs_client=FeishuClient()res=fs_client.send_text(text='测试消息',receive_id_type='open_id',receive_id='ou_63f6845d304e7a1a3df99d6fce291d58')print(res)returnJsonResponse(res)2.注意事项
feishu.py文件中类继承时，_event_list已经给出了默认的三个，应该保留# 原始_event_list=[MessageReceiveEvent,UrlVerificationEvent,MessageReadEvent]# 重写后_event_list=[MessageReceiveEvent,UrlVerificationEvent,MessageReadEvent,yournewEvent]