Implement continuous typing indicator in message handling
- Added a new private method `_keep_typing` to send a typing indicator continuously while processing messages, refreshing every 4 seconds to comply with Telegram/Discord limitations. - Updated the `handle_message` method to initiate the typing indicator at the start of message processing and ensure it stops once processing is complete, improving user experience during message handling.
This commit is contained in:
parent
7eac4ee9fe
commit
a09b018bd5
1 changed files with 25 additions and 4 deletions
|
|
@ -171,20 +171,34 @@ class BasePlatformAdapter(ABC):
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
async def _keep_typing(self, chat_id: str, interval: float = 4.0) -> None:
|
||||||
|
"""
|
||||||
|
Continuously send typing indicator until cancelled.
|
||||||
|
|
||||||
|
Telegram/Discord typing status expires after ~5 seconds, so we refresh every 4.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
await self.send_typing(chat_id)
|
||||||
|
await asyncio.sleep(interval)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass # Normal cancellation when handler completes
|
||||||
|
|
||||||
async def handle_message(self, event: MessageEvent) -> None:
|
async def handle_message(self, event: MessageEvent) -> None:
|
||||||
"""
|
"""
|
||||||
Process an incoming message.
|
Process an incoming message.
|
||||||
|
|
||||||
Calls the registered message handler and sends the response.
|
Calls the registered message handler and sends the response.
|
||||||
|
Keeps typing indicator active throughout processing.
|
||||||
"""
|
"""
|
||||||
if not self._message_handler:
|
if not self._message_handler:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Start continuous typing indicator (refreshes every 4 seconds)
|
||||||
|
typing_task = asyncio.create_task(self._keep_typing(event.source.chat_id))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Send typing indicator
|
# Call the handler (this can take a while with tool calls)
|
||||||
await self.send_typing(event.source.chat_id)
|
|
||||||
|
|
||||||
# Call the handler
|
|
||||||
response = await self._message_handler(event)
|
response = await self._message_handler(event)
|
||||||
|
|
||||||
# Send response if any
|
# Send response if any
|
||||||
|
|
@ -196,6 +210,13 @@ class BasePlatformAdapter(ABC):
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[{self.name}] Error handling message: {e}")
|
print(f"[{self.name}] Error handling message: {e}")
|
||||||
|
finally:
|
||||||
|
# Stop typing indicator
|
||||||
|
typing_task.cancel()
|
||||||
|
try:
|
||||||
|
await typing_task
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass
|
||||||
|
|
||||||
def build_source(
|
def build_source(
|
||||||
self,
|
self,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue