@@ -47,57 +47,68 @@ async def process_message(message_data: dict) -> None:
4747 config = Config .from_env ()
4848 bot = Bot (config .telegram_token )
4949
50- # Reconstruct Update object from stored data
51- update = Update .de_json (message_data ['telegram_update' ], bot )
52- message = update .message or update .edited_message
53-
54- if not message :
55- logger .warning ("Received update with no message or edited_message" )
56- return
57-
58- cmd = config .get_command (message .text )
59- if cmd :
60- if config .is_local_command (cmd ):
61- logger .info (
62- "Handling local command in consumer (fallback path)" ,
63- extra = {'chat_id' : message .chat_id , 'message_id' : message .message_id },
64- )
65- try :
66- await bot .send_message (
67- chat_id = message .chat_id ,
68- text = config .local_response (cmd ),
69- message_thread_id = message .message_thread_id ,
70- reply_to_message_id = message .message_id ,
71- )
72- except Exception :
73- logger .warning ("Failed to send local command response" , exc_info = True )
50+ is_newchat = message_data .get ('is_newchat' , False )
51+
52+ if is_newchat :
53+ # newchat 消息直接使用 message_data
54+ chat_id = message_data ['chat_id' ]
55+ thread_id = message_data ['thread_id' ]
56+ user_message = message_data ['text' ]
57+ message_id = message_data .get ('message_id' )
58+ else :
59+ # 正常消息解析 Update
60+ update = Update .de_json (message_data ['telegram_update' ], bot )
61+ message = update .message or update .edited_message
62+
63+ if not message :
64+ logger .warning ("Received update with no message or edited_message" )
7465 return
7566
76- if not config .is_agent_command (cmd ):
77- # Defensive guard: producer should already block non-agent commands.
78- logger .info (
79- "Skipping non-agent command (consumer fallback)" ,
80- extra = {
81- 'chat_id' : message .chat_id ,
82- 'message_id' : message .message_id ,
83- },
84- )
85- try :
86- await bot .send_message (
87- chat_id = message .chat_id ,
88- text = config .unknown_command_message (),
89- message_thread_id = message .message_thread_id ,
90- reply_to_message_id = message .message_id ,
67+ chat_id = message .chat_id
68+ thread_id = message .message_thread_id
69+ user_message = message .text
70+ message_id = message .message_id
71+
72+ cmd = config .get_command (user_message )
73+ if cmd :
74+ if config .is_local_command (cmd ):
75+ logger .info (
76+ "Handling local command in consumer (fallback path)" ,
77+ extra = {'chat_id' : chat_id , 'message_id' : message_id },
9178 )
92- except Exception :
93- logger .warning ("Failed to send local command response" , exc_info = True )
94- return
79+ try :
80+ await bot .send_message (
81+ chat_id = chat_id ,
82+ text = config .local_response (cmd ),
83+ message_thread_id = thread_id ,
84+ reply_to_message_id = message_id ,
85+ )
86+ except Exception :
87+ logger .warning ("Failed to send local command response" , exc_info = True )
88+ return
89+
90+ if not config .is_agent_command (cmd ):
91+ # Defensive guard: producer should already block non-agent commands.
92+ logger .info (
93+ "Skipping non-agent command (consumer fallback)" ,
94+ extra = {'chat_id' : chat_id , 'message_id' : message_id },
95+ )
96+ try :
97+ await bot .send_message (
98+ chat_id = chat_id ,
99+ text = config .unknown_command_message (),
100+ message_thread_id = thread_id ,
101+ reply_to_message_id = message_id ,
102+ )
103+ except Exception :
104+ logger .warning ("Failed to send local command response" , exc_info = True )
105+ return
95106
96107 # Send typing indicator
97108 await bot .send_chat_action (
98- chat_id = message . chat_id ,
109+ chat_id = chat_id ,
99110 action = ChatAction .TYPING ,
100- message_thread_id = message . message_thread_id ,
111+ message_thread_id = thread_id ,
101112 )
102113
103114 # Initialize result with default error response
@@ -118,31 +129,31 @@ async def process_message(message_data: dict) -> None:
118129 'Content-Type' : 'application/json' ,
119130 },
120131 json = {
121- 'user_message' : message . text ,
122- 'chat_id' : str (message . chat_id ),
123- 'thread_id' : str (message . message_thread_id ) if message . message_thread_id else None ,
132+ 'user_message' : user_message ,
133+ 'chat_id' : str (chat_id ),
134+ 'thread_id' : str (thread_id ) if thread_id else None ,
124135 },
125136 )
126137 response .raise_for_status ()
127138 result = response .json ()
128139
129140 except httpx .TimeoutException :
130- logger .warning (f"Agent Server timeout for chat_id={ message . chat_id } " )
141+ logger .warning (f"Agent Server timeout for chat_id={ chat_id } " )
131142 await bot .send_message (
132- chat_id = message . chat_id ,
143+ chat_id = chat_id ,
133144 text = "Request timed out." ,
134- message_thread_id = message . message_thread_id ,
145+ message_thread_id = thread_id ,
135146 )
136147 raise # Re-raise to trigger SQS retry for transient errors
137148
138149 except Exception as e :
139- logger .exception (f"Agent Server error for chat_id={ message . chat_id } " )
150+ logger .exception (f"Agent Server error for chat_id={ chat_id } " )
140151 error_text = f"Error: { str (e )[:200 ]} "
141152 try :
142153 await bot .send_message (
143- chat_id = message . chat_id ,
154+ chat_id = chat_id ,
144155 text = error_text ,
145- message_thread_id = message . message_thread_id ,
156+ message_thread_id = thread_id ,
146157 )
147158 except Exception as send_error :
148159 logger .error (f"Failed to send error message to Telegram: { send_error } " )
@@ -160,21 +171,21 @@ async def process_message(message_data: dict) -> None:
160171 # Send response to Telegram
161172 try :
162173 await bot .send_message (
163- chat_id = message . chat_id ,
174+ chat_id = chat_id ,
164175 text = text ,
165176 parse_mode = ParseMode .MARKDOWN_V2 ,
166- message_thread_id = message . message_thread_id ,
167- reply_to_message_id = message . message_id ,
177+ message_thread_id = thread_id ,
178+ reply_to_message_id = message_id ,
168179 )
169180 except BadRequest as e :
170181 if "parse entities" in str (e ).lower ():
171182 safe_text = escape_markdown (text , version = 2 )
172183 await bot .send_message (
173- chat_id = message . chat_id ,
184+ chat_id = chat_id ,
174185 text = safe_text ,
175186 parse_mode = ParseMode .MARKDOWN_V2 ,
176- message_thread_id = message . message_thread_id ,
177- reply_to_message_id = message . message_id ,
187+ message_thread_id = thread_id ,
188+ reply_to_message_id = message_id ,
178189 )
179190 else :
180191 raise
0 commit comments