feat: capture message_thread_id for Telegram topic routing
This commit is contained in:
@@ -71,7 +71,7 @@ def update_user_status(actor_id: str, name: str, status: str) -> None:
|
||||
_sent_hashes: set = set()
|
||||
|
||||
|
||||
def send_telegram_direct(chat_id: str, token: str, text: str) -> None:
|
||||
def send_telegram_direct(chat_id: str, token: str, text: str, thread_id: int | None = None) -> None:
|
||||
import hashlib
|
||||
h = hashlib.md5(f'{chat_id}:{text}'.encode()).hexdigest()[:12]
|
||||
if h in _sent_hashes:
|
||||
@@ -79,7 +79,10 @@ def send_telegram_direct(chat_id: str, token: str, text: str) -> None:
|
||||
return
|
||||
_sent_hashes.add(h)
|
||||
url = f'https://api.telegram.org/bot{token}/sendMessage'
|
||||
data = json.dumps({'chat_id': chat_id, 'text': text}).encode()
|
||||
payload: dict = {'chat_id': chat_id, 'text': text}
|
||||
if thread_id is not None:
|
||||
payload['message_thread_id'] = thread_id
|
||||
data = json.dumps(payload).encode()
|
||||
req = urllib.request.Request(url, data=data, headers={'Content-Type': 'application/json'})
|
||||
try:
|
||||
resp = urllib.request.urlopen(req, timeout=10)
|
||||
@@ -138,6 +141,7 @@ def handler(event, context):
|
||||
first = records[0]
|
||||
channel = first.get('channel', 'telegram')
|
||||
chat_id = first.get('chat_id', '')
|
||||
message_thread_id = first.get('message_thread_id') # int or None
|
||||
actor_id = f"{channel}:{chat_id}"
|
||||
|
||||
# ── User registry ─────────────────────────────────────────────────────
|
||||
@@ -161,7 +165,7 @@ def handler(event, context):
|
||||
if bot_token_secret_arn:
|
||||
sm = boto3.client('secretsmanager', region_name='us-east-1')
|
||||
bot_token = sm.get_secret_value(SecretId=bot_token_secret_arn)['SecretString']
|
||||
send_telegram_direct(chat_id, bot_token, "Hi! I don't recognize you yet. What's your name?")
|
||||
send_telegram_direct(chat_id, bot_token, "Hi! I don't recognize you yet. What's your name?", thread_id=message_thread_id)
|
||||
return
|
||||
# ── Get or create AgentCore session ──────────────────────────────────
|
||||
session_id = get_or_create_session(actor_id)
|
||||
@@ -192,6 +196,7 @@ def handler(event, context):
|
||||
'channel_adapter': {
|
||||
'type': channel,
|
||||
'target_id': str(chat_id),
|
||||
'message_thread_id': message_thread_id,
|
||||
'bot_token_secret_arn': os.environ.get('TELEGRAM_BOT_TOKEN_SECRET_ARN', ''),
|
||||
},
|
||||
}
|
||||
@@ -256,7 +261,7 @@ def handler(event, context):
|
||||
# Only flush if buffer is very large — prevents splitting multi-turn responses
|
||||
if len(text_buffer) > 1200:
|
||||
print(f'[agent-runner] send chunk {len(text_buffer)}c to {chat_id}')
|
||||
send_telegram_direct(str(chat_id), bot_token, text_buffer.strip())
|
||||
send_telegram_direct(str(chat_id), bot_token, text_buffer.strip(), thread_id=message_thread_id)
|
||||
text_buffer = ''
|
||||
|
||||
# Flush any remaining text
|
||||
@@ -267,6 +272,6 @@ def handler(event, context):
|
||||
print(f'[agent-runner] heartbeat suppressed for {actor_id}')
|
||||
return
|
||||
print(f'[agent-runner] flushing {len(text_buffer)}c to {chat_id}')
|
||||
send_telegram_direct(str(chat_id), bot_token, text_buffer.strip())
|
||||
send_telegram_direct(str(chat_id), bot_token, text_buffer.strip(), thread_id=message_thread_id)
|
||||
|
||||
print(f"[agent-runner] Completed session={session_id} actor={actor_id}")
|
||||
|
||||
Reference in New Issue
Block a user