Skip to content

Commit

Permalink
修正注释并补全类型声明 (#34)
Browse files Browse the repository at this point in the history
* ✨ feat: 更新卡片内容时支持其它参数

* 🐛 fix: CardReplier 和 CallbackHandler 补充属性类型; ChatbotHandler.update_card 修正更新卡片注释; ChatbotHandler.reply_card 支持传入其他参数

* upgrade version to 0.17.1

---------

Co-authored-by: ginping <[email protected]>
  • Loading branch information
ginping and ginping authored Mar 18, 2024
1 parent cd2e4bb commit 216dd9a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
8 changes: 5 additions & 3 deletions dingtalk_stream/card_replier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

import platform, requests, copy, hashlib
from .utils import DINGTALK_OPENAPI_ENDPOINT
from .chatbot import ChatbotMessage
from .stream import DingTalkStreamClient
from .log import setup_default_logger
from enum import Enum, unique


class CardReplier(object):

def __init__(self, dingtalk_client, incoming_message):
self.dingtalk_client = dingtalk_client
self.incoming_message = incoming_message
def __init__(self, dingtalk_client: DingTalkStreamClient, incoming_message: ChatbotMessage):
self.dingtalk_client: DingTalkStreamClient = dingtalk_client
self.incoming_message: ChatbotMessage = incoming_message
self.conversation_type = incoming_message.conversation_type
self.open_conversation_id = incoming_message
self.logger = setup_default_logger('dingtalk_stream.handler')
Expand Down
42 changes: 23 additions & 19 deletions dingtalk_stream/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __init__(self):
self.image_content = None
self.rich_text_content = None
self.sender_staff_id = None
self.hosting_context = None
self.hosting_context: HostingContext = None
self.conversation_msg_context = None

self.extensions = {}
Expand Down Expand Up @@ -669,14 +669,16 @@ def reply_card(self,
card_data: dict,
incoming_message: ChatbotMessage,
at_sender: bool = False,
at_all: bool = False) -> str:
at_all: bool = False,
**kwargs) -> str:
"""
回复互动卡片。由于sessionWebhook不支持发送互动卡片,所以需要使用OpenAPI,当前仅支持自建应用。
机器人回复互动卡片。由于 sessionWebhook 不支持发送互动卡片,所以需要使用 OpenAPI,当前仅支持自建应用。
https://open.dingtalk.com/document/orgapp/robots-send-interactive-cards
:param card_data: 卡片数据内容,interactive_card.py中有一些简单的样例,高阶需求请至卡片搭建平台:https://card.dingtalk.com/card-builder
:param card_data: 卡片数据内容,interactive_card.py 中有一些简单的样例,高阶需求请至卡片搭建平台:https://card.dingtalk.com/card-builder
:param incoming_message: 回调数据源
:param at_sender: 是否at发送人
:param at_all: 是否at所有人
:param kwargs: 其他参数,具体可参考文档。
:return:
"""
access_token = self.dingtalk_client.get_access_token()
Expand All @@ -695,17 +697,18 @@ def reply_card(self,
}

card_biz_id = self._gen_card_id(incoming_message)
body = {"cardTemplateId": "StandardCard",
"robotCode": self.dingtalk_client.credential.client_id,
"cardData": json.dumps(card_data),
"sendOptions": {
# "atUserListJson": "String",
# "atAll": at_all,
# "receiverListJson": "String",
# "cardPropertyJson": "String"
},
"cardBizId": card_biz_id
}
body = {
"cardTemplateId": "StandardCard",
"robotCode": self.dingtalk_client.credential.client_id,
"cardData": json.dumps(card_data),
"sendOptions": {
# "atUserListJson": "String",
# "atAll": at_all,
# "receiverListJson": "String",
# "cardPropertyJson": "String"
},
"cardBizId": card_biz_id,
}

if incoming_message.conversation_type == '2':
body["openConversationId"] = incoming_message.conversation_id
Expand All @@ -728,6 +731,7 @@ def reply_card(self,
}
]
body["sendOptions"]["atUserListJson"] = json.dumps(user_list_json, ensure_ascii=False)
body.update(**kwargs)

url = DINGTALK_OPENAPI_ENDPOINT + '/v1.0/im/v1.0/robot/interactiveCards/send'
try:
Expand All @@ -743,10 +747,10 @@ def reply_card(self,

def update_card(self, card_biz_id: str, card_data: dict):
"""
回复互动卡片。由于sessionWebhook不支持发送互动卡片,所以需要使用OpenAPI,当前仅支持自建应用
https://open.dingtalk.com/document/orgapp/robots-send-interactive-cards
:param card_data: 卡片数据内容,interactive_card.py中有一些简单的样例,高阶需求请至卡片搭建平台:https://card.dingtalk.com/card-builder
:param incoming_message: 回调数据源,主要是为了取到card_biz_id
更新机器人发送互动卡片(普通版)
https://open.dingtalk.com/document/orgapp/update-the-robot-to-send-interactive-cards
:param card_biz_id: 唯一标识一张卡片的外部ID(卡片幂等ID,可用于更新或重复发送同一卡片到多个群会话)。需与 self.reply_card 接口返回的 card_biz_id 保持一致。
:param card_data: 要更新的卡片数据内容。详情参考卡片搭建平台:https://card.dingtalk.com/card-builder
:return:
"""
access_token = self.dingtalk_client.get_access_token()
Expand Down
9 changes: 5 additions & 4 deletions dingtalk_stream/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
from .frames import SystemMessage
from .frames import EventMessage
from .frames import CallbackMessage
from .stream import DingTalkStreamClient
from .log import setup_default_logger


class CallbackHandler(object):
TOPIC_CARD_CALLBACK = '/v1.0/card/instances/callback'

def __init__(self):
self.dingtalk_client = None
self.dingtalk_client: DingTalkStreamClient = None
self.logger = setup_default_logger('dingtalk_stream.handler')

def pre_start(self):
return

async def process(self, message):
async def process(self, message: CallbackMessage):
return AckMessage.STATUS_NOT_IMPLEMENT, 'not implement'

async def raw_process(self, callback_message: CallbackMessage):
Expand All @@ -33,7 +34,7 @@ async def raw_process(self, callback_message: CallbackMessage):

class EventHandler(object):
def __init__(self):
self.dingtalk_client = None
self.dingtalk_client: DingTalkStreamClient = None
self.logger = setup_default_logger('dingtalk_stream.handler')

def pre_start(self):
Expand All @@ -55,7 +56,7 @@ async def raw_process(self, event_message: EventMessage):

class SystemHandler(object):
def __init__(self):
self.dingtalk_client = None
self.dingtalk_client: DingTalkStreamClient = None
self.logger = setup_default_logger('dingtalk_stream.handler')

def pre_start(self):
Expand Down
2 changes: 1 addition & 1 deletion dingtalk_stream/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION_STRING = '0.17.0'
VERSION_STRING = '0.17.1'

0 comments on commit 216dd9a

Please sign in to comment.