mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-25 19:37:36 +08:00
feat(dingtalk): add supports for audio receiving
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
import dingtalk_stream
|
import dingtalk_stream
|
||||||
from dingtalk_stream import AckMessage
|
from dingtalk_stream import AckMessage
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ class EchoTextHandler(dingtalk_stream.ChatbotHandler):
|
|||||||
await self.client.update_incoming_message(incoming_message)
|
await self.client.update_incoming_message(incoming_message)
|
||||||
|
|
||||||
return AckMessage.STATUS_OK, 'OK'
|
return AckMessage.STATUS_OK, 'OK'
|
||||||
|
|
||||||
async def get_incoming_message(self):
|
async def get_incoming_message(self):
|
||||||
"""异步等待消息的到来"""
|
"""异步等待消息的到来"""
|
||||||
while self.incoming_message is None:
|
while self.incoming_message is None:
|
||||||
|
|||||||
@@ -92,8 +92,31 @@ class DingTalkClient:
|
|||||||
base64_str = base64.b64encode(file_bytes).decode('utf-8') # 返回字符串格式
|
base64_str = base64.b64encode(file_bytes).decode('utf-8') # 返回字符串格式
|
||||||
return base64_str
|
return base64_str
|
||||||
else:
|
else:
|
||||||
raise Exception("获取图片失败")
|
raise Exception("获取文件失败")
|
||||||
|
|
||||||
|
async def get_audio_url(self,download_code:str):
|
||||||
|
if not await self.check_access_token():
|
||||||
|
await self.get_access_token()
|
||||||
|
url = 'https://api.dingtalk.com/v1.0/robot/messageFiles/download'
|
||||||
|
params = {
|
||||||
|
"downloadCode":download_code,
|
||||||
|
"robotCode":self.robot_code
|
||||||
|
}
|
||||||
|
headers ={
|
||||||
|
"x-acs-dingtalk-access-token": self.access_token
|
||||||
|
}
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.post(url, headers=headers, json=params)
|
||||||
|
if response.status_code == 200:
|
||||||
|
result = response.json()
|
||||||
|
download_url = result.get("downloadUrl")
|
||||||
|
if download_url:
|
||||||
|
return await self.download_url_to_base64(download_url)
|
||||||
|
else:
|
||||||
|
raise Exception("获取音频失败")
|
||||||
|
else:
|
||||||
|
raise Exception(f"Error: {response.status_code}, {response.text}")
|
||||||
|
|
||||||
async def update_incoming_message(self, message):
|
async def update_incoming_message(self, message):
|
||||||
"""异步更新 DingTalkClient 中的 incoming_message"""
|
"""异步更新 DingTalkClient 中的 incoming_message"""
|
||||||
message_data = await self.get_message(message)
|
message_data = await self.get_message(message)
|
||||||
@@ -160,10 +183,14 @@ class DingTalkClient:
|
|||||||
message_data['Picture'] = await self.download_image(incoming_message.get_image_list()[0])
|
message_data['Picture'] = await self.download_image(incoming_message.get_image_list()[0])
|
||||||
|
|
||||||
message_data['Type'] = 'image'
|
message_data['Type'] = 'image'
|
||||||
|
elif incoming_message.message_type == 'audio':
|
||||||
|
message_data['Audio'] = await self.get_audio_url(incoming_message.to_dict()['content']['downloadCode'])
|
||||||
|
|
||||||
|
message_data['Type'] = 'audio'
|
||||||
|
|
||||||
# 删掉开头的@消息
|
# 删掉开头的@消息
|
||||||
if message_data["Content"].startswith("@"+self.robot_name):
|
if 'Content' in message_data and message_data["Content"].startswith("@"+self.robot_name):
|
||||||
message_data["Content"][len("@"+self.robot_name):]
|
message_data["Content"] = message_data["Content"][len("@"+self.robot_name):]
|
||||||
except Exception:
|
except Exception:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,10 @@ class DingTalkEvent(dict):
|
|||||||
@property
|
@property
|
||||||
def picture(self):
|
def picture(self):
|
||||||
return self.get("Picture","")
|
return self.get("Picture","")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def audio(self):
|
||||||
|
return self.get("Audio","")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def conversation(self):
|
def conversation(self):
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ class DingTalkMessageConverter(adapter.MessageConverter):
|
|||||||
yiri_msg_list.append(platform_message.Plain(text=event.content))
|
yiri_msg_list.append(platform_message.Plain(text=event.content))
|
||||||
if event.picture:
|
if event.picture:
|
||||||
yiri_msg_list.append(platform_message.Image(base64=event.picture))
|
yiri_msg_list.append(platform_message.Image(base64=event.picture))
|
||||||
|
if event.audio:
|
||||||
|
yiri_msg_list.append(platform_message.Voice(base64=event.audio))
|
||||||
|
|
||||||
chain = platform_message.MessageChain(yiri_msg_list)
|
chain = platform_message.MessageChain(yiri_msg_list)
|
||||||
|
|
||||||
@@ -167,7 +169,7 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter):
|
|||||||
self.bot.on_message("GroupMessage")(on_message)
|
self.bot.on_message("GroupMessage")(on_message)
|
||||||
|
|
||||||
async def run_async(self):
|
async def run_async(self):
|
||||||
|
self.ap.logger.debug(f'钉钉机器人启动')
|
||||||
await self.bot.start()
|
await self.bot.start()
|
||||||
|
|
||||||
async def kill(self) -> bool:
|
async def kill(self) -> bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user