Skip to main content

Secret chats support for Telegram (MTProto) client libraries

Project description

tg-secret

This library provides secret chat support for python Telegram (MTProto) client libraries. Currently, tg-secret supports Pyrogram and Telethon (not tested), but any library can be supported by creating client adapter implementing tg_secret.SecretClientAdapter class.

It is work-in-progress and not recommended to use in production yet. Currently, following critical features are not implemented:

  • Media downloading
  • Some security checks (check if dh_config.p is safe prime, inconsistent seq_no in terms of parity)

Example with pyrogram

from asyncio import sleep
from io import BytesIO

from pyrogram import Client, filters
from pyrogram.types import User, Message

from tg_secret import TelegramSecretClient, ChatRequestResult, SecretChat, SecretMessage
from tg_secret.client_adapters.pyrogram_adapter import PyrogramClientAdapter

client = Client(
    "secret_client",
    api_id=...,  # Your api id
    api_hash=...,  # Your api hash
)
secret = TelegramSecretClient(PyrogramClientAdapter(client))


@client.on_message(filters.private & filters.command("start_secret_chat"))
async def on_user_requested_secret_chat(_: Client, message: Message):
    print(f"Requesting new secret chat with {message.from_user.first_name} ({message.from_user.id})")
    await secret.request_encryption(message.from_user.id)


@secret.on_request
async def secret_chat_request(chat: SecretChat) -> ChatRequestResult:
    user: User = await client.get_users(chat.peer_id)
    print(f"Accepting new secret chat from {user.first_name} ({user.id})")
    return ChatRequestResult.ACCEPT


@secret.on_chat_ready
async def secret_chat_ready(chat: SecretChat) -> None:
    print(f"Secret chat with {chat.peer_id} is ready!")
    await chat.send_message("Hello!")


@secret.on_new_message
async def new_secret_message(message: SecretMessage) -> None:
    print(f"New message from {message.chat.peer_id}: {message.text}")
    if message.text == "/delete_chat":
        await message.reply("Discarding chat...")
        await message.chat.delete(delete_history=False)
        return
    elif message.text == "/file":
        await secret.send_document(
            message.chat.id, BytesIO(b"test 123"), caption="Here's your file", file_name="test.txt"
        )
        return
    elif message.text == "/delete_message":
        message_to_delete = await message.reply("This message will be deleted in 5 seconds")
        await sleep(5)
        await message_to_delete.delete()
        return
    elif message.text == "/delete_history":
        await message.chat.delete_history()
        await message.chat.send_message("History was deleted")
        return

    await message.reply(f"**{message.text}**")


@secret.on_messages_deleted
async def secret_messages_deleted(chat: SecretChat, random_ids: list[int]):
    print(f"Messages were deleted: {random_ids} in chat with {chat.peer_id}")


@secret.on_chat_deleted
async def secret_chat_deleted(chat: SecretChat, history_deleted: bool):
    print(f"Secret chat with {chat.peer_id} was deleted, with history: {history_deleted}")


if __name__ == "__main__":
    client.run(secret.pyrogram_start())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tg_secret-0.1.0.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tg_secret-0.1.0-py3-none-any.whl (32.7 kB view details)

Uploaded Python 3

File details

Details for the file tg_secret-0.1.0.tar.gz.

File metadata

  • Download URL: tg_secret-0.1.0.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.13.3 Linux/6.14.9-200.fc41.x86_64

File hashes

Hashes for tg_secret-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aa736aa8d40ab86930c527ca1f25d70444a1be95e85c5b2acb0e2e86c4c34089
MD5 cd3e5f025a7f7b9014aa93e318fa3e7f
BLAKE2b-256 9b9fc5d6f0e18ce449759fff5fe8dc681c631dfdc2ab31430e3e7e051df40f02

See more details on using hashes here.

File details

Details for the file tg_secret-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tg_secret-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.13.3 Linux/6.14.9-200.fc41.x86_64

File hashes

Hashes for tg_secret-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40882fb95fb3329388051337e1c234e03e123bf45a0d55f1606b92475e51d641
MD5 f36ff43c7a85ad57566edb4e44383b50
BLAKE2b-256 fe358253bd8be2f0c27cdae5b69761f3b6985874ac984afbb8c0eed924210c21

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page