From 7abd9994208c4b55160e00e7f0e63006158befee Mon Sep 17 00:00:00 2001 From: wangcham Date: Tue, 11 Mar 2025 04:27:17 -0400 Subject: [PATCH] feat: add support for sending active messages in dingtalk --- libs/dingtalk_api/EchoHandler.py | 1 + libs/dingtalk_api/api.py | 48 +++++++++++++++++++++++++ pkg/platform/sources/dingtalk.py | 6 +++- pkg/platform/sources/officialaccount.py | 6 ++-- pkg/platform/sources/qqofficial.py | 7 ++-- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/libs/dingtalk_api/EchoHandler.py b/libs/dingtalk_api/EchoHandler.py index 4baad637..4cf0f563 100644 --- a/libs/dingtalk_api/EchoHandler.py +++ b/libs/dingtalk_api/EchoHandler.py @@ -23,6 +23,7 @@ class EchoTextHandler(dingtalk_stream.ChatbotHandler): """异步等待消息的到来""" while self.incoming_message is None: await asyncio.sleep(0.1) # 异步等待,避免阻塞 + return self.incoming_message async def get_dingtalk_client(client_id, client_secret): diff --git a/libs/dingtalk_api/api.py b/libs/dingtalk_api/api.py index 9e5aca6d..fa4d0421 100644 --- a/libs/dingtalk_api/api.py +++ b/libs/dingtalk_api/api.py @@ -157,6 +157,7 @@ class DingTalkClient: async def get_message(self,incoming_message:dingtalk_stream.chatbot.ChatbotMessage): try: + # print(json.dumps(incoming_message.to_dict(), indent=4, ensure_ascii=False)) message_data = { "IncomingMessage":incoming_message, @@ -197,6 +198,53 @@ class DingTalkClient: traceback.print_exc() return message_data + + async def send_proactive_message_to_one(self,target_id:str,content:str): + if not await self.check_access_token(): + await self.get_access_token() + + url = 'https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend' + + headers ={ + "x-acs-dingtalk-access-token":self.access_token, + "Content-Type":"application/json", + } + + data ={ + "robotCode":self.robot_code, + "userIds":[target_id], + "msgKey": "sampleText", + "msgParam": json.dumps({"content":content}), + } + try: + async with httpx.AsyncClient() as client: + response = await client.post(url,headers=headers,json=data) + except Exception: + traceback.print_exc() + + + async def send_proactive_message_to_group(self,target_id:str,content:str): + if not await self.check_access_token(): + await self.get_access_token() + + url = 'https://api.dingtalk.com/v1.0/robot/groupMessages/send' + + headers ={ + "x-acs-dingtalk-access-token":self.access_token, + "Content-Type":"application/json", + } + + data ={ + "robotCode":self.robot_code, + "openConversationId":target_id, + "msgKey": "sampleText", + "msgParam": json.dumps({"content":content}), + } + try: + async with httpx.AsyncClient() as client: + response = await client.post(url,headers=headers,json=data) + except Exception: + traceback.print_exc() async def start(self): """启动 WebSocket 连接,监听消息""" diff --git a/pkg/platform/sources/dingtalk.py b/pkg/platform/sources/dingtalk.py index 97df77d6..aa768039 100644 --- a/pkg/platform/sources/dingtalk.py +++ b/pkg/platform/sources/dingtalk.py @@ -152,7 +152,11 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter): async def send_message( self, target_type: str, target_id: str, message: platform_message.MessageChain ): - pass + content = await DingTalkMessageConverter.yiri2target(message) + if target_type == 'person': + await self.bot.send_proactive_message_to_one(target_id,content) + if target_type == 'group': + await self.bot.send_proactive_message_to_group(target_id,content) def register_listener( self, diff --git a/pkg/platform/sources/officialaccount.py b/pkg/platform/sources/officialaccount.py index 8202946b..1e2c5ce6 100644 --- a/pkg/platform/sources/officialaccount.py +++ b/pkg/platform/sources/officialaccount.py @@ -2,14 +2,12 @@ from __future__ import annotations import typing import asyncio import traceback -import time + import datetime from pkg.core import app from pkg.platform.adapter import MessagePlatformAdapter from pkg.platform.types import events as platform_events, message as platform_message -import aiocqhttp -import aiohttp from libs.official_account_api.oaevent import OAEvent from pkg.platform.adapter import MessagePlatformAdapter from pkg.platform.types import events as platform_events, message as platform_message @@ -91,7 +89,7 @@ class OfficialAccountAdapter(adapter.MessagePlatformAdapter): ] missing_keys = [key for key in required_keys if key not in config] if missing_keys: - raise ParamNotEnoughError("企业微信缺少相关配置项,请查看文档或联系管理员") + raise ParamNotEnoughError("微信公众号缺少相关配置项,请查看文档或联系管理员") self.bot = OAClient( token=config['token'], diff --git a/pkg/platform/sources/qqofficial.py b/pkg/platform/sources/qqofficial.py index cfdf6c47..bfef2135 100644 --- a/pkg/platform/sources/qqofficial.py +++ b/pkg/platform/sources/qqofficial.py @@ -2,15 +2,14 @@ from __future__ import annotations import typing import asyncio import traceback -import time + import datetime -import aiocqhttp -import aiohttp + from pkg.platform.adapter import MessagePlatformAdapter from pkg.platform.types import events as platform_events, message as platform_message from pkg.core import app from .. import adapter -from ...pipeline.longtext.strategies import forward + from ...core import app from ..types import message as platform_message from ..types import events as platform_events