mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-26 03:44:58 +08:00
Merge pull request #1184 from wangcham/master
feat: add support for sending active messages in dingtalk
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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,
|
||||
@@ -198,6 +199,53 @@ class DingTalkClient:
|
||||
|
||||
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 连接,监听消息"""
|
||||
await self.client.start()
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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'],
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user