Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

Add voice note cancellation feature during transcription

Alishahryar12220880671e1
diff --git a/messaging/handler.py b/messaging/handler.py
index c883a85d..c11b17ec 100644
--- a/messaging/handler.py
+++ b/messaging/handler.py
@@ -923,6 +923,26 @@ class ClaudeMessageHandler:
self.tree_queue.resolve_parent_node_id(reply_id) if tree else None
)
if not branch_root_id:
+ cancel_fn = getattr(self.platform, "cancel_pending_voice", None)
+ if cancel_fn is not None:
+ cancelled = await cancel_fn(incoming.chat_id, reply_id)
+ if cancelled is not None:
+ voice_msg_id, status_msg_id = cancelled
+ msg_ids_to_del: set[str] = {voice_msg_id, status_msg_id}
+ if incoming.message_id is not None:
+ msg_ids_to_del.add(str(incoming.message_id))
+ await self._delete_message_ids(incoming.chat_id, msg_ids_to_del)
+ msg_id = await self.platform.queue_send_message(
+ incoming.chat_id,
+ self._format_status(
+ "🗑", "Cleared.", "Voice note cancelled."
+ ),
+ fire_and_forget=False,
+ )
+ self._record_outgoing_message(
+ incoming.platform, incoming.chat_id, msg_id, "command"
+ )
+ return
msg_id = await self.platform.queue_send_message(
incoming.chat_id,
self._format_status(
diff --git a/messaging/platforms/discord.py b/messaging/platforms/discord.py
index afde38f0..f6ab5871 100644
--- a/messaging/platforms/discord.py
+++ b/messaging/platforms/discord.py
@@ -113,6 +113,37 @@ class DiscordPlatform(MessagingPlatform):
self._connected = False
self._limiter: Any | None = None
self._start_task: asyncio.Task | None = None
+ self._pending_voice: dict[tuple[str, str], tuple[str, str]] = {}
+ self._pending_voice_lock = asyncio.Lock()
+
+ async def _register_pending_voice(
+ self, chat_id: str, voice_msg_id: str, status_msg_id: str
+ ) -> None:
+ """Register a voice note as pending transcription."""
+ async with self._pending_voice_lock:
+ self._pending_voice[(chat_id, voice_msg_id)] = (voice_msg_id, status_msg_id)
+ self._pending_voice[(chat_id, status_msg_id)] = (
+ voice_msg_id,
+ status_msg_id,
+ )
+
+ async def cancel_pending_voice(
+ self, chat_id: str, reply_id: str
+ ) -> tuple[str, str] | None:
+ """Cancel a pending voice transcription. Returns (voice_msg_id, status_msg_id) if found."""
+ async with self._pending_voice_lock:
+ entry = self._pending_voice.pop((chat_id, reply_id), None)
+ if entry is None:
+ return None
+ voice_msg_id, status_msg_id = entry
+ self._pending_voice.pop((chat_id, voice_msg_id), None)
+ self._pending_voice.pop((chat_id, status_msg_id), None)
+ return (voice_msg_id, status_msg_id)
+
+ async def _is_voice_still_pending(self, chat_id: str, voice_msg_id: str) -> bool:
+ """Check if a voice note is still pending (not cancelled)."""
+ async with self._pending_voice_lock:
+ return (chat_id, voice_msg_id) in self._pending_voice
def _get_audio_attachment(self, message: Any) -> Any | None:
"""Return first audio attachment, or None."""
@@ -148,6 +179,7 @@ class DiscordPlatform(MessagingPlatform):
user_id = str(message.author.id)
message_id = str(message.id)
+ await self._register_pending_voice(channel_id, message_id, str(status_msg_id))
reply_to = (
str(message.reference.message_id)
if message.reference and message.reference.message_id
@@ -182,6 +214,14 @@ class DiscordPlatform(MessagingPlatform):
whisper_device=settings.whisper_device,
)
+ if not await self._is_voice_still_pending(channel_id, message_id):
+ await self.queue_delete_message(channel_id, str(status_msg_id))
+ return True
+
+ async with self._pending_voice_lock:
+ self._pending_voice.pop((channel_id, message_id), None)
+ self._pending_voice.pop((channel_id, str(status_msg_id)), None)
+
incoming = IncomingMessage(
text=transcribed,
chat_id=channel_id,
diff --git a/messaging/platforms/telegram.py b/messaging/platforms/telegram.py
index 1ae5bccd..bc4e5471 100644
--- a/messaging/platforms/telegram.py
+++ b/messaging/platforms/telegram.py
@@ -79,6 +79,38 @@ class TelegramPlatform(MessagingPlatform):
)
self._connected = False
self._limiter: Any | None = None # Will be MessagingRateLimiter
+ # Pending voice transcriptions: (chat_id, msg_id) -> (voice_msg_id, status_msg_id)
+ self._pending_voice: dict[tuple[str, str], tuple[str, str]] = {}
+ self._pending_voice_lock = asyncio.Lock()
+
+ async def _register_pending_voice(
+ self, chat_id: str, voice_msg_id: str, status_msg_id: str
+ ) -> None:
+ """Register a voice note as pending transcription (for /clear reply during transcription)."""
+ async with self._pending_voice_lock:
+ self._pending_voice[(chat_id, voice_msg_id)] = (voice_msg_id, status_msg_id)
+ self._pending_voice[(chat_id, status_msg_id)] = (
+ voice_msg_id,
+ status_msg_id,
+ )
+
+ async def cancel_pending_voice(
+ self, chat_id: str, reply_id: str
+ ) -> tuple[str, str] | None:
+ """Cancel a pending voice transcription. Returns (voice_msg_id, status_msg_id) if found."""
+ async with self._pending_voice_lock:
+ entry = self._pending_voice.pop((chat_id, reply_id), None)
+ if entry is None:
+ return None
+ voice_msg_id, status_msg_id = entry
+ self._pending_voice.pop((chat_id, voice_msg_id), None)
+ self._pending_voice.pop((chat_id, status_msg_id), None)
+ return (voice_msg_id, status_msg_id)
+
+ async def _is_voice_still_pending(self, chat_id: str, voice_msg_id: str) -> bool:
+ """Check if a voice note is still pending (not cancelled)."""
+ async with self._pending_voice_lock:
+ return (chat_id, voice_msg_id) in self._pending_voice
async def start(self) -> None:
"""Initialize and connect to Telegram."""
@@ -532,6 +564,7 @@ class TelegramPlatform(MessagingPlatform):
)
message_id = str(update.message.message_id)
+ await self._register_pending_voice(chat_id, message_id, str(status_msg_id))
reply_to = (
str(update.message.reply_to_message.message_id)
if update.message.reply_to_message
@@ -562,6 +595,14 @@ class TelegramPlatform(MessagingPlatform):
whisper_device=settings.whisper_device,
)
+ if not await self._is_voice_still_pending(chat_id, message_id):
+ await self.queue_delete_message(chat_id, str(status_msg_id))
+ return
+
+ async with self._pending_voice_lock:
+ self._pending_voice.pop((chat_id, message_id), None)
+ self._pending_voice.pop((chat_id, str(status_msg_id)), None)
+
incoming = IncomingMessage(
text=transcribed,
chat_id=chat_id,
diff --git a/tests/messaging/test_handler.py b/tests/messaging/test_handler.py
index e5011667..c34652f1 100644
--- a/tests/messaging/test_handler.py
+++ b/tests/messaging/test_handler.py
@@ -619,3 +619,37 @@ async def test_handle_message_clear_command_reply_to_root_clears_tree(
assert set(deleted_ids) == {"100", "101", "150"}
mock_session_store.remove_tree.assert_called_once_with("100")
assert handler.tree_queue.get_tree_count() == 0
+
+
+async def test_handle_message_clear_command_reply_pending_voice_cancels(
+ handler, mock_platform, mock_session_store, incoming_message_factory
+):
+ """Reply /clear to a voice note during transcription cancels it."""
+
+ async def cancel_pending(chat_id, reply_id):
+ if reply_id == "100":
+ return ("100", "101")
+ return None
+
+ mock_platform.cancel_pending_voice = AsyncMock(side_effect=cancel_pending)
+ mock_platform.queue_delete_message = AsyncMock()
+ deleted_ids = []
+
+ async def _capture_delete(chat_id, message_id, fire_and_forget=True):
+ deleted_ids.append(message_id)
+
+ mock_platform.queue_delete_message = AsyncMock(side_effect=_capture_delete)
+
+ incoming = incoming_message_factory(
+ text="/clear",
+ chat_id="chat_1",
+ message_id="150",
+ reply_to_message_id="100",
+ )
+ await handler.handle_message(incoming)
+
+ mock_platform.cancel_pending_voice.assert_called_once_with("chat_1", "100")
+ assert set(deleted_ids) == {"100", "101", "150"}
+ call_args = mock_platform.queue_send_message.call_args[0]
+ assert "Voice note cancelled" in call_args[1]