feat(work): add task-scoped attachment intake and durable file delivery

This commit is contained in:
ex_tianzy
2026-11-10 14:19:50 +08:00
parent ce64167627
commit 79c15ee7ce
16 changed files with 680 additions and 12 deletions
+33
View File
@@ -122,6 +122,39 @@ class Feishu:
headers={"Authorization": f"Bearer {token}"},
json={"msg_type": "text", "content": json.dumps({"text": text}, ensure_ascii=False)})
async def download_message_file(self,message_id,file_key,max_bytes):
import re
if not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',message_id)or not re.fullmatch(r'[A-Za-z0-9_-]{1,256}',file_key):raise PlatformError('invalid file reference')
token=await self.tenant_token()
try:
async with self.http.stream('GET',f'/open-apis/im/v1/messages/{message_id}/resources/{file_key}',
params={'type':'file'},headers={'Authorization':'Bearer '+token},timeout=60)as response:
if response.status_code!=200 or response.headers.get('content-type','').lower().startswith(('application/json','text/html')):raise PlatformError('message file download rejected')
raw=bytearray()
async for chunk in response.aiter_bytes():
raw.extend(chunk)
if len(raw)>max_bytes:raise PlatformError('message file size limit')
if not raw:raise PlatformError('empty message file')
return bytes(raw)
except httpx.HTTPError:raise PlatformError('message file download failed')from None
async def upload_file(self,name,content,mime,*,token):
data=await self._json('POST','/open-apis/im/v1/files',headers={'Authorization':'Bearer '+token},
data={'file_type':'stream','file_name':name},files={'file':(name,content,mime)},timeout=60)
key=data.get('data',{}).get('file_key')
import re
if not isinstance(key,str)or not re.fullmatch(r'[A-Za-z0-9_-]{1,256}',key):raise PlatformError('file upload receipt invalid')
return key
async def reply_file(self,message_id,file_key,delivery_id,*,token):
import re
if not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',message_id):raise PlatformError('invalid reply reference')
data=await self._json('POST',f'/open-apis/im/v1/messages/{message_id}/reply',
headers={'Authorization':'Bearer '+token},json={'msg_type':'file','content':json.dumps({'file_key':file_key}), 'uuid':delivery_id})
message=data.get('data',{}).get('message_id')
if not isinstance(message,str)or not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',message):raise PlatformError('file message receipt invalid')
return message
async def send(self, owner, msg_type, content, delivery_id, *, token=None):
token = token or await self.tenant_token()
data = await self._json('POST', '/open-apis/im/v1/messages',