Skip to content

Commit

Permalink
Merge pull request #13 from open-dingtalk/features/20230814_linya_bot…
Browse files Browse the repository at this point in the history
…agent

Features/20230814 linya botagent
  • Loading branch information
hzjiangjian authored Aug 18, 2023
2 parents 9d26176 + bce9963 commit a246e85
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 32 deletions.
96 changes: 73 additions & 23 deletions dingtalk_stream/card_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ def _get_card_data(self, markdown) -> dict:

return card_data

def reply(self, markdown: str, at_sender: bool = False, at_all: bool = False):
def reply(self,
markdown: str,
at_sender: bool = False,
at_all: bool = False,
recipients: list = None,
support_forward: bool = True):
"""
回复markdown内容
:param recipients:
:param support_forward:
:param markdown:
:param title:
:param logo:
Expand All @@ -48,7 +55,9 @@ def reply(self, markdown: str, at_sender: bool = False, at_all: bool = False):
:return:
"""
self.card_instance_id = self.create_and_send_card(self.card_template_id, self._get_card_data(markdown),
at_sender=at_sender, at_all=at_all)
at_sender=at_sender, at_all=at_all,
recipients=recipients,
support_forward=support_forward)

def update(self, markdown: str):
"""
Expand Down Expand Up @@ -101,18 +110,29 @@ def _get_card_data(self, markdown, tips) -> dict:

return card_data

def reply(self, markdown: str, button_list: list, tips: str = ""):
def reply(self,
markdown: str,
button_list: list,
tips: str = "",
recipients: list = None,
support_forward: bool = True):
"""
回复markdown内容
:param support_forward:
:param recipients:
:param tips:
:param button_list: [{"text":"text", "url":"url", "iosUrl":"iosUrl", "color":"gray"}]
:param markdown:
:return:
"""
self.button_list = button_list
self.card_instance_id = self.create_and_send_card(self.card_template_id, self._get_card_data(markdown, tips))
self.card_instance_id = self.create_and_send_card(self.card_template_id, self._get_card_data(markdown, tips),
recipients=recipients, support_forward=support_forward)

def update(self, markdown: str, button_list: list, tips: str = ""):
def update(self,
markdown: str,
button_list: list,
tips: str = ""):
"""
更新markdown内容,如果你reply了多次,这里只会更新最后一张卡片
:param button_list:[{"text":"text", "url":"url", "iosUrl":"iosUrl", "color":"gray"}]
Expand Down Expand Up @@ -147,12 +167,13 @@ def set_title_and_logo(self, title: str, logo: str):
self.title = title
self.logo = logo

def ai_start(self, recipients: list = None):
def ai_start(self, recipients: list = None, support_forward: bool = True):
"""
开始执行中
:return:
"""
self.card_instance_id = self.start(self.card_template_id, {}, recipients=recipients)
self.card_instance_id = self.start(self.card_template_id, {}, recipients=recipients,
support_forward=support_forward)
self.inputing_status = False

def ai_streaming(self, markdown: str, append: bool = False):
Expand Down Expand Up @@ -180,10 +201,12 @@ def ai_streaming(self, markdown: str, append: bool = False):

order = [
"msgTitle",
"msgButtons",
"msgImages",
"msgContent",
"msgMarkdown"
"msgTextList",
"msgContent"
"msgImages",
"msgSlider",
"msgButtons",
]

card_data["sys_full_json_obj"] = json.dumps({"order": order})
Expand All @@ -200,9 +223,14 @@ def ai_streaming(self, markdown: str, append: bool = False):
self.streaming(self.card_instance_id, "msgContent", self.markdown, append=False, finished=False,
failed=False)

def ai_finish(self, markdown: str = ""):
def update(self, markdown: str = "", button_list: list = None, tips: str = ""):
return self.ai_finish(markdown=markdown, button_list=button_list, tips=tips)

def ai_finish(self, markdown: str = "", button_list: list = None, tips: str = ""):
"""
完成态
:param tips:
:param button_list:
:param markdown:
:return:
"""
Expand All @@ -215,17 +243,29 @@ def ai_finish(self, markdown: str = ""):
else:
self.markdown = markdown

order = [
"msgTitle",
"msgButtons",
"msgImages",
"msgTextList",
"msgContent"
]
sys_full_json_obj = {
"order": [
"msgTitle",
"msgContent",
"msgMarkdown"
"msgTextList",
"msgImages",
"msgSlider",
"msgButtons",
],
}

if button_list is not None and len(button_list) > 0:
sys_full_json_obj["msgButtons"] = button_list

if self.incoming_message.hosting_context is not None:
sys_full_json_obj["source"] = {
"text": "由{nick}的数字助理回答".format(nick=self.incoming_message.hosting_context.nick)
}

card_data = {
"msgContent": markdown,
"sys_full_json_obj": json.dumps({"order": order})
"sys_full_json_obj": json.dumps(sys_full_json_obj)
}

if self.title is not None and self.title != "":
Expand Down Expand Up @@ -280,9 +320,16 @@ def ai_start(self):
"""
self.card_instance_id = self.start(self.card_template_id, {})

def reply(self, markdown: str, image_slider_list: list, button_text: str = "submit"):
def reply(self,
markdown: str,
image_slider_list: list,
button_text: str = "submit",
recipients: list = None,
support_forward: bool = True):
"""
回复卡片
:param support_forward:
:param recipients:
:param button_text:
:param image_slider_list:
:param markdown:
Expand Down Expand Up @@ -331,7 +378,8 @@ def reply(self, markdown: str, image_slider_list: list, button_text: str = "subm

self.card_instance_id = self.create_and_send_card(self.card_template_id,
{"flowStatus": AICardStatus.PROCESSING},
callback_type="STREAM")
callback_type="STREAM", recipients=recipients,
support_forward=support_forward)

self.finish(self.card_instance_id, card_data)

Expand All @@ -357,9 +405,11 @@ def reply(self,
plugin_name: str,
ability_name: str,
plugin_args: dict,
recipients: list = None):
recipients: list = None,
support_forward: bool = True):
"""
回复markdown内容
:param support_forward:
:param ability_name:
:param recipients:
:param plugin_version:
Expand Down Expand Up @@ -389,6 +439,6 @@ def reply(self,

self.card_instance_id = self.create_and_send_card(self.card_template_id,
{"flowStatus": AICardStatus.PROCESSING},
recipients=recipients)
recipients=recipients, support_forward=support_forward)

self.finish(self.card_instance_id, card_data)
45 changes: 38 additions & 7 deletions dingtalk_stream/card_replier.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ def get_request_header(access_token):
) % platform.python_version(),
}

def create_and_send_card(self, card_template_id: str, card_data: dict, callback_type: str = "",
callback_route_key: str = "", at_sender: bool = False,
at_all: bool = False, recipients: list = None) -> str:
def create_and_send_card(self,
card_template_id: str,
card_data: dict,
callback_type: str = "",
callback_route_key: str = "",
at_sender: bool = False,
at_all: bool = False,
recipients: list = None,
support_forward: bool = True) -> str:
"""
发送卡片,两步骤:创建+投放。
https://open.dingtalk.com/document/orgapp/interface-for-creating-a-card-instance
:param support_forward:
:param callback_route_key:
:param callback_type:
:param recipients:
Expand All @@ -66,13 +73,17 @@ def create_and_send_card(self, card_template_id: str, card_data: dict, callback_
"cardParamMap": card_data
},
"imGroupOpenSpaceModel": {
"supportForward": False
"supportForward": True
},
"imRobotOpenSpaceModel": {
"supportForward": False
"supportForward": True
}
}

if not support_forward:
body["imGroupOpenSpaceModel"]["supportForward"] = False
body["imRobotOpenSpaceModel"]["supportForward"] = False

if callback_type == "STREAM":
body["callbackType"] = "STREAM"
elif callback_type == "HTTP":
Expand Down Expand Up @@ -116,13 +127,28 @@ def create_and_send_card(self, card_template_id: str, card_data: dict, callback_
if recipients is not None:
body["imGroupOpenDeliverModel"]["recipients"] = recipients

# 增加托管extension
if self.incoming_message.hosting_context is not None:
body["imGroupOpenDeliverModel"]["extension"] = {
"hostingRepliedContext": json.dumps({
"userId": self.incoming_message.hosting_context.user_id
})
}
elif self.incoming_message.conversation_type == '1':
body["openSpaceId"] = "dtv1.card//{spaceType}.{spaceId}".format(spaceType="IM_ROBOT",
spaceId=self.incoming_message.sender_staff_id)
body["imRobotOpenDeliverModel"] = {
"spaceType": "IM_ROBOT"
}

# 增加托管extension
if self.incoming_message.hosting_context is not None:
body["imRobotOpenDeliverModel"]["extension"] = {
"hostingRepliedContext": json.dumps({
"userId": self.incoming_message.hosting_context.user_id
})
}

# 投放卡片。https://open.dingtalk.com/document/orgapp/delivery-card-interface
url = DINGTALK_OPENAPI_ENDPOINT + '/v1.0/card/instances/deliver'
try:
Expand Down Expand Up @@ -184,9 +210,14 @@ class AICardReplier(CardReplier):
def __init__(self, dingtalk_client, incoming_message):
super(AICardReplier, self).__init__(dingtalk_client, incoming_message)

def start(self, card_template_id: str, card_data: dict, recipients: list = None) -> str:
def start(self,
card_template_id: str,
card_data: dict,
recipients: list = None,
support_forward: bool = True) -> str:
"""
AI卡片的创建接口
:param support_forward:
:param recipients:
:param card_template_id:
:param card_data:
Expand All @@ -195,7 +226,7 @@ def start(self, card_template_id: str, card_data: dict, recipients: list = None)
card_data_with_status = copy.deepcopy(card_data)
card_data_with_status["flowStatus"] = AICardStatus.PROCESSING
return self.create_and_send_card(card_template_id, card_data_with_status, at_sender=False, at_all=False,
recipients=recipients)
recipients=recipients, support_forward=support_forward)

def finish(self, card_instance_id: str, card_data: dict):
"""
Expand Down
Loading

0 comments on commit a246e85

Please sign in to comment.