feat: add Telegram file attachment support (inbound + outbound)
Inbound:
- tg-ingest detects document/photo/audio/video/voice attachments
- Downloads files via Telegram Bot API (getFile + download)
- Inlines small text files (<50KB) directly in the prompt
- Stores binary/large files to S3 (attachments/{chat_id}/{update_id}/{filename})
- agent-runner appends file context to the AgentCore prompt
Outbound:
- New send_file tool for the agent to send documents back to users
- TelegramAdapter.send_document uses multipart/form-data POST
- CDK grants tg-ingest S3 write access and passes bucket name env var
This commit is contained in:
@@ -63,6 +63,43 @@ class TelegramAdapter:
|
||||
import traceback
|
||||
print(f'[telegram] send_typing failed: {e}\n{traceback.format_exc()}')
|
||||
|
||||
def send_document(self, file_bytes: bytes, filename: str, caption: str = '') -> str:
|
||||
"""Send a file as a Telegram document using multipart/form-data. Returns message_id."""
|
||||
import io
|
||||
token = self._get_token()
|
||||
url = f'https://api.telegram.org/bot{token}/sendDocument'
|
||||
|
||||
boundary = '----AgentClawBoundary'
|
||||
body = io.BytesIO()
|
||||
|
||||
def add_field(name: str, value: str):
|
||||
body.write(f'--{boundary}\r\n'.encode())
|
||||
body.write(f'Content-Disposition: form-data; name="{name}"\r\n\r\n'.encode())
|
||||
body.write(f'{value}\r\n'.encode())
|
||||
|
||||
def add_file(name: str, fname: str, data: bytes):
|
||||
body.write(f'--{boundary}\r\n'.encode())
|
||||
body.write(f'Content-Disposition: form-data; name="{name}"; filename="{fname}"\r\n'.encode())
|
||||
body.write(b'Content-Type: application/octet-stream\r\n\r\n')
|
||||
body.write(data)
|
||||
body.write(b'\r\n')
|
||||
|
||||
add_field('chat_id', self.chat_id)
|
||||
if self.thread_id is not None:
|
||||
add_field('message_thread_id', str(self.thread_id))
|
||||
if caption:
|
||||
add_field('caption', caption)
|
||||
add_file('document', filename, file_bytes)
|
||||
body.write(f'--{boundary}--\r\n'.encode())
|
||||
|
||||
req = urllib.request.Request(
|
||||
url, data=body.getvalue(),
|
||||
headers={'Content-Type': f'multipart/form-data; boundary={boundary}'},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=60) as resp:
|
||||
result = json.loads(resp.read())
|
||||
return str(result.get('result', {}).get('message_id', ''))
|
||||
|
||||
def edit(self, message_id: str, text: str) -> None:
|
||||
"""Edit an existing message in-place."""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user