Skip to content

Commit 3f42aa2

Browse files
CopilotBukeLy
andcommitted
Apply reply_to_message_id fix to local command handlers as well
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
1 parent 70ea250 commit 3f42aa2

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

agent-sdk-client/consumer.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,29 @@ async def process_message(message_data: dict) -> None:
5656
logger.warning("Received update with no message or edited_message")
5757
return
5858

59+
# Use message_data fields for SQS message (allows handler to override text/thread_id)
60+
user_message = message_data.get('text') or message.text
61+
thread_id = message_data.get('thread_id') or message.message_thread_id
62+
5963
cmd = config.get_command(message.text)
6064
if cmd:
6165
if config.is_local_command(cmd):
6266
logger.info(
6367
"Handling local command in consumer (fallback path)",
6468
extra={'chat_id': message.chat_id, 'message_id': message.message_id},
6569
)
70+
# Only reply to original message if we're in the same thread
71+
reply_to_id = (
72+
message.message_id
73+
if thread_id == message.message_thread_id
74+
else None
75+
)
6676
try:
6777
await bot.send_message(
6878
chat_id=message.chat_id,
6979
text=config.local_response(cmd),
70-
message_thread_id=message.message_thread_id,
71-
reply_to_message_id=message.message_id,
80+
message_thread_id=thread_id,
81+
reply_to_message_id=reply_to_id,
7282
)
7383
except Exception:
7484
logger.warning("Failed to send local command response", exc_info=True)
@@ -83,12 +93,18 @@ async def process_message(message_data: dict) -> None:
8393
'message_id': message.message_id,
8494
},
8595
)
96+
# Only reply to original message if we're in the same thread
97+
reply_to_id = (
98+
message.message_id
99+
if thread_id == message.message_thread_id
100+
else None
101+
)
86102
try:
87103
await bot.send_message(
88104
chat_id=message.chat_id,
89105
text=config.unknown_command_message(),
90-
message_thread_id=message.message_thread_id,
91-
reply_to_message_id=message.message_id,
106+
message_thread_id=thread_id,
107+
reply_to_message_id=reply_to_id,
92108
)
93109
except Exception:
94110
logger.warning("Failed to send local command response", exc_info=True)
@@ -98,7 +114,7 @@ async def process_message(message_data: dict) -> None:
98114
await bot.send_chat_action(
99115
chat_id=message.chat_id,
100116
action=ChatAction.TYPING,
101-
message_thread_id=message.message_thread_id,
117+
message_thread_id=thread_id,
102118
)
103119

104120
# Initialize result with default error response
@@ -109,10 +125,6 @@ async def process_message(message_data: dict) -> None:
109125
'error_message': 'Failed to get response from Agent Server'
110126
}
111127

112-
# Use message_data fields for SQS message (allows handler to override text/thread_id)
113-
user_message = message_data.get('text') or message.text
114-
thread_id = message_data.get('thread_id') or message.message_thread_id
115-
116128
# Call Agent Server
117129
try:
118130
async with httpx.AsyncClient(timeout=600.0) as client:

0 commit comments

Comments
 (0)