Merge pull request #1184 from wangcham/master

feat: add support for sending active messages in dingtalk
This commit is contained in:
Junyan Qin (Chin)
2025-03-11 16:42:29 +08:00
committed by GitHub
5 changed files with 59 additions and 9 deletions
+1
View File
@@ -23,6 +23,7 @@ class EchoTextHandler(dingtalk_stream.ChatbotHandler):
"""异步等待消息的到来""" """异步等待消息的到来"""
while self.incoming_message is None: while self.incoming_message is None:
await asyncio.sleep(0.1) # 异步等待,避免阻塞 await asyncio.sleep(0.1) # 异步等待,避免阻塞
return self.incoming_message return self.incoming_message
async def get_dingtalk_client(client_id, client_secret): async def get_dingtalk_client(client_id, client_secret):
+48
View File
@@ -157,6 +157,7 @@ class DingTalkClient:
async def get_message(self,incoming_message:dingtalk_stream.chatbot.ChatbotMessage): async def get_message(self,incoming_message:dingtalk_stream.chatbot.ChatbotMessage):
try: try:
# print(json.dumps(incoming_message.to_dict(), indent=4, ensure_ascii=False)) # print(json.dumps(incoming_message.to_dict(), indent=4, ensure_ascii=False))
message_data = { message_data = {
"IncomingMessage":incoming_message, "IncomingMessage":incoming_message,
@@ -198,6 +199,53 @@ class DingTalkClient:
return message_data 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): async def start(self):
"""启动 WebSocket 连接,监听消息""" """启动 WebSocket 连接,监听消息"""
await self.client.start() await self.client.start()
+5 -1
View File
@@ -152,7 +152,11 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter):
async def send_message( async def send_message(
self, target_type: str, target_id: str, message: platform_message.MessageChain 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( def register_listener(
self, self,
+2 -4
View File
@@ -2,14 +2,12 @@ from __future__ import annotations
import typing import typing
import asyncio import asyncio
import traceback import traceback
import time
import datetime import datetime
from pkg.core import app from pkg.core import app
from pkg.platform.adapter import MessagePlatformAdapter from pkg.platform.adapter import MessagePlatformAdapter
from pkg.platform.types import events as platform_events, message as platform_message 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 libs.official_account_api.oaevent import OAEvent
from pkg.platform.adapter import MessagePlatformAdapter from pkg.platform.adapter import MessagePlatformAdapter
from pkg.platform.types import events as platform_events, message as platform_message 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] missing_keys = [key for key in required_keys if key not in config]
if missing_keys: if missing_keys:
raise ParamNotEnoughError("企业微信缺少相关配置项,请查看文档或联系管理员") raise ParamNotEnoughError("微信公众号缺少相关配置项,请查看文档或联系管理员")
self.bot = OAClient( self.bot = OAClient(
token=config['token'], token=config['token'],
+3 -4
View File
@@ -2,15 +2,14 @@ from __future__ import annotations
import typing import typing
import asyncio import asyncio
import traceback import traceback
import time
import datetime import datetime
import aiocqhttp
import aiohttp
from pkg.platform.adapter import MessagePlatformAdapter from pkg.platform.adapter import MessagePlatformAdapter
from pkg.platform.types import events as platform_events, message as platform_message from pkg.platform.types import events as platform_events, message as platform_message
from pkg.core import app from pkg.core import app
from .. import adapter from .. import adapter
from ...pipeline.longtext.strategies import forward
from ...core import app from ...core import app
from ..types import message as platform_message from ..types import message as platform_message
from ..types import events as platform_events from ..types import events as platform_events