Skip to main content

A pure Python implementation of the Session Messenger protocol client

Project description

session-python

PyPI version License: MIT

A pure Python implementation of the Session Messenger programmatic client protocol.

This library is a 1-to-1 port of the official @session.js (Bun) client modules to Python, allowing you to build Session bots, send encrypted messages, upload/download attachments, handle read receipts, typing indicators, message reactions, and unsends natively in Python, without wrapping any Node/Bun subprocesses.


🚀 Features

  • Zero JS Subprocesses: Natively implemented in Python using PyNaCl, Cryptography, and Protobuf.
  • Oxen Mnemonics: Ported 13-word Electrum/Monero style mnemonic decoder & encoder (Oxen standard) with CRC32 checksums.
  • Session Protocol Encryption: SealedBox encryption, sender identity signatures, envelope wrapping, and 160-byte block size padding.
  • Proxy Support: SOCKS5 and HTTP proxy support out of the box (uses socks5h:// to perform remote DNS resolution for high privacy).
  • Redundant Node Routing: Swarm lookup and node rotation (automatically retries other nodes in the swarm if a connection fails).
  • In-Memory Swarm Cache: Caches resolved swarms to reduce network calls and decrease message sending latency by up to 4x.
  • Attachments Support: Encrypts (AES-CBC + HMAC-SHA256) and uploads/downloads attachments to the Session file server.
  • Typing Indicators: Show or hide typing indicators (show_typing_indicator, hide_typing_indicator).
  • Message Reactions: React to messages with emojis (add_reaction, remove_reaction).
  • Read Receipts: Mark messages as read (mark_messages_as_read).
  • Message Deletion (Unsend): Delete messages locally and propagate deletion commands (delete_message, delete_messages).
  • SOGS Support: Sign and send requests to SOGS (Open Groups) (send_sogs_request, sign_sogs_request).
  • Pythonic Interface: Exposes context managers (with Session() as s:) and static generators (Session.generate_mnemonic()).

📦 Installation

To install session-python:

pip install session-python

🛠️ Quick Start

1. Basic Send Message

from session_python import Session

# Initialize with SOCKS5 proxy
PROXY = "socks5h://user:pass@host:port"
RECIPIENT_ID = "059ce57868de2b93dc56e3bce3780db7a7aadc91d8e236f4a8f972f92e609ab609"

with Session(proxy=PROXY) as session:
    # Set your account mnemonic or generate a new one
    mnemonic = Session.generate_mnemonic()
    print(f"Generated new mnemonic: {mnemonic}")
    
    session.set_mnemonic(mnemonic, display_name="Python Bot")
    print(f"Your Session ID: {session.get_session_id()}")

    # Send a text message
    result = session.send_message(to=RECIPIENT_ID, text="Hello from Python!")
    print(f"Message Hash: {result['messageHash']}")

2. Polling for Messages

from session_python import Session, Poller

with Session(proxy=PROXY) as session:
    session.set_mnemonic("your 13 word mnemonic here...")
    
    poller = Poller(session)
    print("Listening for messages...")
    
    while True:
        messages = poller.poll()
        for msg in messages:
            if msg["type"] == "data":
                print(f"Received from {msg['from']}: {msg['body']}")
        time.sleep(5)

3. File Attachments

with Session(proxy=PROXY) as session:
    session.set_mnemonic(mnemonic)

    # 1. Send file attachment
    with open("photo.jpg", "rb") as f:
        file_bytes = f.read()

    attachments = [{
        "data": file_bytes,
        "name": "photo.jpg",
        "content_type": "image/jpeg"
    }]
    session.send_message(to=RECIPIENT_ID, text="Here is a file!", attachments=attachments)

    # 2. Download and decrypt attachment from a polled message pointer
    # file_ptr is extracted from incoming messages: msg["attachments"][0]
    decrypted_file = session.get_file(file_ptr)
    with open("downloaded_photo.jpg", "wb") as f:
        f.write(decrypted_file)

4. Advanced Chat Operations

with Session(proxy=PROXY) as session:
    session.set_mnemonic(mnemonic)
    
    # Typing Indicators
    session.show_typing_indicator(RECIPIENT_ID)
    time.sleep(2)
    session.hide_typing_indicator(RECIPIENT_ID)
    
    # Message Reactions
    session.add_reaction(message_timestamp=1783586415070, message_author=RECIPIENT_ID, emoji="🔥")
    session.remove_reaction(message_timestamp=1783586415070, message_author=RECIPIENT_ID, emoji="🔥")
    
    # Read Receipts
    session.mark_messages_as_read(conversation=RECIPIENT_ID, timestamps=[1783586415070])
    
    # Delete / Unsend Messages
    session.delete_message(conversation=RECIPIENT_ID, timestamp=1783586415070, hash_val="IXFgLeoj...")

💻 Command Line Interface (CLI)

The package installs a command-line tool sscli to manage mnemonics and generate accounts directly from the terminal.

1. Generate a new Session ID and Mnemonic

sscli mnemonic gen

2. Generate a Vanity Session ID (Prefix Brute-force)

Find custom Session IDs. By default, this uses all available CPU threads for maximum performance:

# Find a Session ID starting with '05abc...'
sscli mnemonic gen -p abc

# Generate 3 accounts starting with '0577...' using 2 threads
sscli mnemonic gen -p 77 -n 3 -t 2

3. Derive Session ID from existing mnemonic

sscli mnemonic gen "your 13 word mnemonic here..."

4. Verify a mnemonic

Validates the checksum and words of a mnemonic, and outputs its hex seed and Session ID:

sscli mnemonic verify "your 13 word mnemonic here..."

⚖️ License

This project is licensed under the MIT License - see the LICENSE file for details.

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

session_python-1.0.5.tar.gz (33.4 kB view details)

Uploaded Source

Built Distribution

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

session_python-1.0.5-py3-none-any.whl (32.6 kB view details)

Uploaded Python 3

File details

Details for the file session_python-1.0.5.tar.gz.

File metadata

  • Download URL: session_python-1.0.5.tar.gz
  • Upload date:
  • Size: 33.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for session_python-1.0.5.tar.gz
Algorithm Hash digest
SHA256 7e14dc6b40ec5508065dc8426628a4d34627a2eccc9b281c9b250fd42a5ece51
MD5 17b8f5c3fed573eb6d8a84c6ba2f370e
BLAKE2b-256 4cc6a99427a527b25a09a38be2695e8495b3c0d613be8b5be94802ac05206cd7

See more details on using hashes here.

File details

Details for the file session_python-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: session_python-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 32.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for session_python-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8191e3650e28c3b34a1aaac8467300ac5ba26cc63d891a0f5533d52bcf6a03f2
MD5 1ad0ed39e37311c2b9614466f3c9b03f
BLAKE2b-256 dea18a5f5009dafd4c59a9d2caa360cf34349a6dae273ba9221c907b222515f7

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