Show "typing…" on each busy session's phone/chat, at most every TYPING_EVERY seconds.
(sessions: dict[Channel, str])
| 400 | |
| 401 | |
| 402 | async def _keep_typing(sessions: dict[Channel, str]) -> None: |
| 403 | """Show "typing…" on each busy session's phone/chat, at most every TYPING_EVERY seconds.""" |
| 404 | r = await get_redis() |
| 405 | for channel, code in sessions.items(): |
| 406 | if not await r.exists(_busy_key(code, channel)): |
| 407 | continue |
| 408 | # Twilio's indicator answers an inbound message: the phone's latest one. |
| 409 | msg_id = await r.get(_last_msg_key(code, channel)) if channel == "whatsapp" else None |
| 410 | if channel == "whatsapp" and not msg_id: |
| 411 | continue |
| 412 | if not await r.set(_typed_key(code, channel), "1", nx=True, ex=TYPING_EVERY[channel]): |
| 413 | continue |
| 414 | address = await bound_address(code, channel) |
| 415 | if not address: |
| 416 | continue |
| 417 | try: |
| 418 | if channel == "whatsapp": |
| 419 | from messaging import send_whatsapp_typing |
| 420 | await send_whatsapp_typing(msg_id) |
| 421 | else: |
| 422 | from tools_router import send_telegram_typing |
| 423 | await send_telegram_typing(address) |
| 424 | except Exception as e: |
| 425 | logger.info(f"Typing indicator failed on {channel}: {e}") |
| 426 | |
| 427 | |
| 428 | # --- Shared route bodies --- |
no test coverage detected