Skip to content

Commit 97a50f2

Browse files
committed
fix(whatsapp): Optimize typing indicator and response handling
- Fix response handling logic in EvolutionAPI provider - Modify typing indicator to send only once before sending multiple messages - Improve error handling for typing indicator failures - Prevent typing indicator errors from interrupting message sending process - Optimize logging for typing indicator and message sending
1 parent 6f7ff7f commit 97a50f2

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

agentle/agents/whatsapp/providers/evolution/evolution_api_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ async def _handle_response(
574574
if isinstance(expected_status, int):
575575
expected_status = [expected_status]
576576

577-
if response.status not in expected_status:
577+
if response.status in expected_status:
578578
try:
579579
response_data = await response.json()
580580
logger.debug(f"Response data received: {response_data}")

agentle/agents/whatsapp/whatsapp_bot.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,19 @@ async def _send_response(
21472147
messages = self._split_message_by_line_breaks(response_text)
21482148
logger.info(f"[SEND_RESPONSE] Split response into {len(messages)} parts")
21492149

2150+
# Show typing indicator ONCE before sending all messages
2151+
if self.config.typing_indicator:
2152+
try:
2153+
logger.debug(
2154+
f"[SEND_RESPONSE] Sending typing indicator to {to} before sending {len(messages)} message(s)"
2155+
)
2156+
await self.provider.send_typing_indicator(
2157+
to, self.config.typing_duration
2158+
)
2159+
except Exception as e:
2160+
# Don't let typing indicator failures break message sending
2161+
logger.warning(f"[SEND_RESPONSE] Failed to send typing indicator: {e}")
2162+
21502163
# Track sending state to handle partial failures
21512164
successfully_sent_count = 0
21522165
failed_parts: list[dict[str, Any]] = []
@@ -2156,21 +2169,6 @@ async def _send_response(
21562169
f"[SEND_RESPONSE] Sending message part {i + 1}/{len(messages)} to {to}"
21572170
)
21582171

2159-
# Show typing indicator before each message if configured
2160-
if self.config.typing_indicator:
2161-
try:
2162-
logger.debug(
2163-
f"[SEND_RESPONSE] Sending typing indicator to {to} for message {i + 1}"
2164-
)
2165-
await self.provider.send_typing_indicator(
2166-
to, self.config.typing_duration
2167-
)
2168-
except Exception as e:
2169-
# Don't let typing indicator failures break message sending
2170-
logger.warning(
2171-
f"[SEND_RESPONSE] Failed to send typing indicator: {e}"
2172-
)
2173-
21742172
# Only quote the first message if quote_messages is enabled
21752173
quoted_id = reply_to if i == 0 else None
21762174

0 commit comments

Comments
 (0)