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.3.tar.gz (31.4 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.3-py3-none-any.whl (43.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tg_secret-0.1.3.tar.gz
Algorithm Hash digest
SHA256 62511e1308c1ac7d1b32a09ab92b5d90ba484a3c12b9c52a8798614b16e61d3a
MD5 c7d01472745426f72370f1364dd9cd8f
BLAKE2b-256 93ba057a813ad46db7a0b426e0d967461c02cf27f05b7b5e38977b99b9d049f5

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tg_secret-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3bf52aa3a60f29cf16c3c5a3a07547106f55a7ab23eb8a9c7689c5686516bd42
MD5 b71207e0f0f63a52db55d9204d2beeba
BLAKE2b-256 d2a8a9ba1739a85d35e9c2d9d4b1c5dcb48b9de47cc6e9208cb29ed61c2e784b

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