Skip to content

Commit

Permalink
Merge pull request #463 from zhayujie/dingtalk_fix_20240103
Browse files Browse the repository at this point in the history
fix: Dingtalk support sending pictures and text
  • Loading branch information
zhayujie authored Jan 3, 2025
2 parents 7cb0f4b + 76ba00d commit 8b448f1
Showing 1 changed file with 59 additions and 15 deletions.
74 changes: 59 additions & 15 deletions channel/dingtalk/dingtalk_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,25 +203,32 @@ def build_webhook_response(self, reply, data):

def chat(self, channel, data):
reply = channel.handle(data)
type = data['conversationType']
if type == "1":
reply_json = self.build_response(reply, data)
self.notify_dingtalk(data, reply_json)
else:
# group的不清楚怎么@,先用webhook调用
reply_json = self.build_webhook_response(reply, data)
self.notify_dingtalk_webhook(reply_json)
self.notify_dingtalk(data, reply)


def notify_dingtalk(self, data, reply_json):
def notify_dingtalk(self, data, reply):
headers = {
'content-type': 'application/json',
'x-acs-dingtalk-access-token': self.get_token()
'content-type': 'application/json',
'Accept': '*/*',
}
staff_id = data.get("senderStaffId", None)
values = {
'msgtype': 'text',
'text': {
'content': reply,
}
}
if staff_id:
values['at'] = {
"atUserIds": [
staff_id
],
"isAtAll": False
}

notify_url = self.get_post_url(data)
notify_url = data.get("sessionWebhook")
try:
r = requests.post(notify_url, json=reply_json, headers=headers)
r = requests.post(notify_url, json=values, headers=headers)
resp = r.json()
log.info("[DingTalk] response={}".format(str(resp)))
except Exception as e:
Expand All @@ -230,6 +237,7 @@ def notify_dingtalk(self, data, reply_json):

class DingTalkChannel(Channel):
def __init__(self):
self.data = None
log.info("[DingTalk] started.")

def startup(self):
Expand All @@ -239,10 +247,12 @@ def handle(self, data):
reply = "您好,有什么我可以帮助您解答的问题吗?"
prompt = data['text']['content']
prompt = prompt.strip()
self.data = data
if str(prompt) != 0:
conversation_id = data['conversationId']
sender_id = data['senderId']
context = dict()
context['channel'] = self
img_match_prefix = functions.check_prefix(
prompt, channel_conf_val(const.DINGTALK, 'image_create_prefix'))
if img_match_prefix:
Expand All @@ -252,6 +262,42 @@ def handle(self, data):
context['from_user_id'] = str(id)
reply = super().build_reply_content(prompt, context)
return reply

def send(self, msg, receiver):
# 暂时无法区分是什么类型的格式,先按图片处理,后期等待有志兄弟来重构
self.reply_markdown("图片信息", f"![]({msg})")

def reply_markdown(self, title: str,text: str):
request_headers = {
'Content-Type': 'application/json',
'Accept': '*/*',
}
values = {
'msgtype': 'markdown',
'markdown': {
'title': title,
'text': text,
}
}
staff_id = self.data.get("senderStaffId", None)
if staff_id:
values['at'] = {
"atUserIds": [
staff_id
],
"isAtAll": False
}
try:
notify_url = self.data.get("sessionWebhook")
response = requests.post(notify_url,
headers=request_headers,
data=json.dumps(values))
response.raise_for_status()
except Exception as e:
log.error(
f'reply markdown failed, error={e}, response.text={response.text if "response" in locals() else ""}')
return None
return response.json()


dd = DingTalkChannel()
Expand Down Expand Up @@ -283,8 +329,6 @@ def chat():
if 'conversationTitle' in data:
group_name = data['conversationTitle']
handler = handlers.get(group_name, handlers.get(code, handlers.get('DEFAULT')))
if handler.dingtalk_post_token and token != handler.dingtalk_post_token:
return {'ret': 203}
handler.chat(dd, data)
return {'ret': 200}

Expand Down

0 comments on commit 8b448f1

Please sign in to comment.