From 216dd9aefd0b03b5a0642043059514e3c33afeac Mon Sep 17 00:00:00 2001 From: ginping Date: Tue, 19 Mar 2024 07:26:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=B3=A8=E9=87=8A=E5=B9=B6?= =?UTF-8?q?=E8=A1=A5=E5=85=A8=E7=B1=BB=E5=9E=8B=E5=A3=B0=E6=98=8E=20(#34)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ feat: 更新卡片内容时支持其它参数 * 🐛 fix: CardReplier 和 CallbackHandler 补充属性类型; ChatbotHandler.update_card 修正更新卡片注释; ChatbotHandler.reply_card 支持传入其他参数 * upgrade version to 0.17.1 --------- Co-authored-by: ginping --- dingtalk_stream/card_replier.py | 8 ++++--- dingtalk_stream/chatbot.py | 42 ++++++++++++++++++--------------- dingtalk_stream/handlers.py | 9 +++---- dingtalk_stream/version.py | 2 +- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/dingtalk_stream/card_replier.py b/dingtalk_stream/card_replier.py index 1800ed7..1471f8f 100644 --- a/dingtalk_stream/card_replier.py +++ b/dingtalk_stream/card_replier.py @@ -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') diff --git a/dingtalk_stream/chatbot.py b/dingtalk_stream/chatbot.py index 702b0ca..d730ff1 100644 --- a/dingtalk_stream/chatbot.py +++ b/dingtalk_stream/chatbot.py @@ -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 = {} @@ -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() @@ -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 @@ -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: @@ -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() diff --git a/dingtalk_stream/handlers.py b/dingtalk_stream/handlers.py index 8e5dd74..44f1394 100644 --- a/dingtalk_stream/handlers.py +++ b/dingtalk_stream/handlers.py @@ -5,6 +5,7 @@ from .frames import SystemMessage from .frames import EventMessage from .frames import CallbackMessage +from .stream import DingTalkStreamClient from .log import setup_default_logger @@ -12,13 +13,13 @@ 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): @@ -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): @@ -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): diff --git a/dingtalk_stream/version.py b/dingtalk_stream/version.py index 599c045..2916be1 100644 --- a/dingtalk_stream/version.py +++ b/dingtalk_stream/version.py @@ -1 +1 @@ -VERSION_STRING = '0.17.0' +VERSION_STRING = '0.17.1'