Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions src/tg_cli/cli/tg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Telegram subcommands — chats, history, sync, refresh, listen, info, whoami, status, send."""
"""Telegram subcommands — send, edit, delete, and more."""

import asyncio
import time
Expand Down Expand Up @@ -396,13 +396,19 @@ async def _run():
@click.argument("chat")
@click.argument("message")
@click.option("-r", "--reply", type=int, default=None, help="Message ID to reply to")
@click.option("--no-preview", is_flag=True, help="Disable link preview")
@structured_output_options
def tg_send(chat: str, message: str, reply: int | None, as_json: bool, as_yaml: bool):
def tg_send(
chat: str, message: str, reply: int | None,
no_preview: bool, as_json: bool, as_yaml: bool,
):
"""Send a MESSAGE to CHAT (name, username, or numeric ID)."""

async def _run():
async with connect() as client:
msg = await client.send_message(_parse_chat(chat), message, reply_to=reply)
msg = await client.send_message(
_parse_chat(chat), message, reply_to=reply, link_preview=not no_preview,
)
return msg

msg = asyncio.run(_run())
Expand All @@ -412,3 +418,54 @@ async def _run():
if emit_structured(payload, as_json=as_json, as_yaml=as_yaml):
return
console.print(f"[green]\u2713[/green] Message sent (id: {msg.id})")


@tg_group.command("edit")
@click.argument("chat")
@click.argument("msg_id", type=int)
@click.argument("new_text")
@click.option("--no-preview", is_flag=True, help="Disable link preview")
@structured_output_options
def tg_edit(chat: str, msg_id: int, new_text: str, no_preview: bool, as_json: bool, as_yaml: bool):
"""Edit a previously sent message. CHAT MSG_ID NEW_TEXT."""

async def _run():
from telethon.tl.functions.messages import EditMessageRequest

kwargs = {}
if no_preview:
kwargs["no_webpage"] = True

async with connect() as client:
entity = await client.get_input_entity(_parse_chat(chat))
await client(EditMessageRequest(
peer=entity,
id=msg_id,
message=new_text,
**kwargs,
))

asyncio.run(_run())
payload = {"edited": True, "msg_id": msg_id, "chat": chat}
if emit_structured(payload, as_json=as_json, as_yaml=as_yaml):
return
console.print(f"[green]\u2713[/green] Message {msg_id} edited")


@tg_group.command("delete")
@click.argument("chat")
@click.argument("msg_ids", nargs=-1, type=int, required=True)
@structured_output_options
def tg_delete(chat: str, msg_ids: tuple[int, ...], as_json: bool, as_yaml: bool):
"""Delete one or more messages. CHAT MSG_ID [MSG_ID ...]."""

async def _run():
async with connect() as client:
entity = await client.get_entity(_parse_chat(chat))
await client.delete_messages(entity, list(msg_ids))

asyncio.run(_run())
payload = {"deleted": True, "msg_ids": list(msg_ids), "chat": chat}
if emit_structured(payload, as_json=as_json, as_yaml=as_yaml):
return
console.print(f"[green]\u2713[/green] Deleted {len(msg_ids)} message(s)")
Loading