Skip to main content

Python wrapper for ferogram, blazing-fast Rust MTProto library for Telegram.

Project description

ferogram-py

Python bindings for ferogram, a Telegram MTProto client written in Rust.

The Rust core handles crypto, transport, session management, and update processing. The Python layer gives you async/await methods and decorator-based event handlers with no boilerplate.

Built with PyO3 and maturin. Works on Linux, macOS, Windows, and Android (Termux).

Install

pip install ferogram

Pre-built wheels are available for:

Platform Arch
Linux (manylinux) x86_64, aarch64
macOS x86_64, arm64
Windows x86_64
Android / Termux aarch64, x86_64

On Termux, pip install ferogram picks the correct Android wheel automatically.

Force local build

If the pre-built wheel does not work, or you want to compile for your exact machine:

# from PyPI source
pip install ferogram --no-binary ferogram

# from cloned repo
git clone https://github.com/ankit-chaubey/ferogram-py
cd ferogram-py
pip install . --no-binary ferogram

Termux source build prerequisites:

pkg install rust clang python
pip install ferogram --no-binary ferogram

Quick start

from ferogram import Client, filters

app = Client("mybot", bot_token="123:TOKEN")

@app.on_message(filters.command("start"))
async def start(client, message):
    await message.reply("Hello!")

app.run()

Handlers

ferogram uses decorator-based handlers. Each handler receives the Client and the update object.

@app.on_message(filters.command("start"))
async def on_start(client, message):
    await message.reply("Hello!")

@app.on_message(filters.command("help"))
async def on_help(client, message):
    await message.reply("Commands: /start /help")

@app.on_edited_message(filters.text)
async def on_edit(client, message):
    await message.reply("you edited a message")

@app.on_callback_query(filters.data("btn_ok"))
async def on_btn(client, query):
    await query.answer(text="OK!")

@app.on_inline_query()
async def on_inline(client, query):
    pass  # handle inline queries

@app.on_user_status()
async def on_status(client, status):
    print(status.user_id, status.status)

@app.on_chat_action()
async def on_action(client, action):
    pass  # user joined, left, pinned, etc.

@app.on_message_deleted()
async def on_delete(client, event):
    pass

@app.on_participant_update()
async def on_participant(client, event):
    pass

@app.on_message_reaction()
async def on_reaction(client, event):
    pass

@app.on_poll_vote()
async def on_vote(client, event):
    pass

@app.on_raw_update()
async def on_raw(client, update):
    pass  # every raw TL update

Filters

Combine filters by passing multiple to the decorator.

filters.all             # every update
filters.text            # message has text
filters.photo           # message has a photo
filters.media           # message has any media
filters.private         # private chat
filters.group           # group or channel
filters.incoming        # not sent by you
filters.outgoing        # sent by you
filters.mentioned       # bot was mentioned
filters.album           # grouped media

filters.command("start")        # /start
filters.regex(r"hello|hi")      # text matches pattern
filters.user(123456)            # from a specific user
filters.chat(-100123456)        # in a specific chat
filters.data("btn_ok")          # callback query data

filters.and_(f1, f2)    # both must pass
filters.or_(f1, f2)     # either passes
filters.not_(f1)        # inverts a filter

Messages

# send
msg = await client.send_message("me", "hello")
await client.send_html("me", "<b>bold</b>")
await client.send_markdown("me", "**bold**")

# on the message object
await message.reply("got it")
await message.forward_to("me")
await message.pin()
await message.edit("updated text")
await message.delete()

Media

await client.send_photo("me", "photo.jpg", caption="look")
await client.send_document("me", "file.pdf", caption="report")

Edit and delete

msg = await client.send_message("me", "draft")
await client.edit_message("me", msg.id, "updated")
await client.delete_message(msg.id)
await client.delete_messages([1, 2, 3])

Dialogs and account

me = await client.get_me()
print(me.first_name, me.username)

dialogs = await client.get_dialogs(limit=20)
for d in dialogs:
    print(d.title, d.unread_count)

Userbot

from ferogram import Client, filters

app = Client("session", api_id=123456, api_hash="abc123")

@app.on_message(filters.private, filters.incoming, filters.text)
async def echo(client, message):
    await message.reply(message.text)

app.run()

Raw API

Access any Telegram API method directly using the generated TL types.

from ferogram.raw.api.functions import GetHistory
from ferogram.raw.api.types import InputPeerUsername

result = await app.invoke(GetHistory(
    peer=InputPeerUsername(username="durov").to_dict(),
    offset_id=0,
    offset_date=0,
    add_offset=0,
    limit=10,
    max_id=0,
    min_id=0,
    hash=0,
))
for msg in result.get("messages", []):
    print(msg.get("id"), msg.get("message", "")[:80])

758 functions and 1559 types are available under ferogram.raw.api.functions and ferogram.raw.api.types.

The generated/ directory is internal codegen output. Always import from ferogram.raw.api.

Context manager

async with Client("session", api_id=..., api_hash=...) as app:
    await app.send_message("me", "hello")

Architecture

Python caller
    |  asyncio await
    v
ferogram-py  (PyO3 .so extension)
    |  FFI, Rust holds GIL only at call boundary
    v
ferogram  (Rust, tokio async runtime)
    |  TCP / TLS
    v
Telegram MTProto

License

MIT OR Apache-2.0

Developed by Ankit Chaubey

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

ferogram-0.1.4.tar.gz (559.7 kB view details)

Uploaded Source

Built Distributions

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

ferogram-0.1.4-cp313-abi3-win_amd64.whl (5.3 MB view details)

Uploaded CPython 3.13+Windows x86-64

ferogram-0.1.4-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.13+manylinux: glibc 2.17+ x86-64

ferogram-0.1.4-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.5 MB view details)

Uploaded CPython 3.13+manylinux: glibc 2.17+ ARM64

ferogram-0.1.4-cp313-abi3-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.13+macOS 11.0+ ARM64

ferogram-0.1.4-cp313-abi3-macosx_10_12_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.13+macOS 10.12+ x86-64

ferogram-0.1.4-cp313-abi3-android_24_x86_64.whl (4.7 MB view details)

Uploaded Android API level 24+ x86-64CPython 3.13+

ferogram-0.1.4-cp313-abi3-android_24_x86.whl (4.5 MB view details)

Uploaded Android API level 24+ x86CPython 3.13+

ferogram-0.1.4-cp313-abi3-android_24_armeabi_v7a.whl (3.9 MB view details)

Uploaded Android API level 24+ ARM EABI v7aCPython 3.13+

ferogram-0.1.4-cp313-abi3-android_24_arm64_v8a.whl (4.4 MB view details)

Uploaded Android API level 24+ ARM64 v8aCPython 3.13+

File details

Details for the file ferogram-0.1.4.tar.gz.

File metadata

  • Download URL: ferogram-0.1.4.tar.gz
  • Upload date:
  • Size: 559.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for ferogram-0.1.4.tar.gz
Algorithm Hash digest
SHA256 64b5c0f8569701cc344831252186bc260c1cc5c7da63b7873899ee1693990cf6
MD5 cb31b670dc8a7608d978778a174af827
BLAKE2b-256 ca822edf7273ae44c2f023a0b2454048385b6d4006aea1a2702652a1def665c4

See more details on using hashes here.

File details

Details for the file ferogram-0.1.4-cp313-abi3-win_amd64.whl.

File metadata

  • Download URL: ferogram-0.1.4-cp313-abi3-win_amd64.whl
  • Upload date:
  • Size: 5.3 MB
  • Tags: CPython 3.13+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for ferogram-0.1.4-cp313-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 5d0a4e8327352684e4f3fb6a5ce6f9acc6abf212d4c5a7157846eb1ca927389e
MD5 83bf0c0f251b2934e378d299a3869850
BLAKE2b-256 91dd095e3ddd956cf69f34b66d4eab1a7d0d7999eff0c13a1d9aadaf5013d678

See more details on using hashes here.

File details

Details for the file ferogram-0.1.4-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.4-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7ad6476d29ddf5f5e4fce72066dbc3b02bda3d6794402f606c88c3cf7e313f5f
MD5 1b5f95d6b9fed77a66a1862ef5fee061
BLAKE2b-256 ce681e02142f4e029d3aaede5da80f393409482f57c1831f3e11639fbd0e90fc

See more details on using hashes here.

File details

Details for the file ferogram-0.1.4-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.4-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 383190c4dcb221dae7e53ae40ad8ce41afa2b517dca68336d3a4c1fb6e1b112d
MD5 de8eb8e48c6f35027b1144484e75964c
BLAKE2b-256 1d9a9ad2bd283a9ff58eead02c9401fcccde1927e9d43bc92b719729ae854350

See more details on using hashes here.

File details

Details for the file ferogram-0.1.4-cp313-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.4-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4c6657b69969edfd98d0e09557d4c8f9379ab005e5a5aa5a4457640212c1a3a8
MD5 5b14614c40f4bdae1e4db510f6a1538e
BLAKE2b-256 da5d93db84d48de3bb7c41259d6a3873d857a86698e401fa0d94d70a25cb7174

See more details on using hashes here.

File details

Details for the file ferogram-0.1.4-cp313-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.4-cp313-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ca75e4d0dc344ecd0323bbe3c422979cfb33ab657eae478efe23c177a9c68e8f
MD5 8194d5c94bde181e282dd98a6cc8ed24
BLAKE2b-256 2ee1e5a4afbc8d9d0a3a6fc58d0e4bb60b82b81a2968e7f4605ee2efa5d634b7

See more details on using hashes here.

File details

Details for the file ferogram-0.1.4-cp313-abi3-android_24_x86_64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.4-cp313-abi3-android_24_x86_64.whl
Algorithm Hash digest
SHA256 64b68876354ecd48cb0f1bcd8a8cb1476adf0cfe26a3324a6d4664546b7e0623
MD5 aed54172d0d775a62be72a008cbd8aac
BLAKE2b-256 e89a440b8e8aa523a0431f244544aa8a5a1b115645f0e677628300638e230c89

See more details on using hashes here.

File details

Details for the file ferogram-0.1.4-cp313-abi3-android_24_x86.whl.

File metadata

File hashes

Hashes for ferogram-0.1.4-cp313-abi3-android_24_x86.whl
Algorithm Hash digest
SHA256 9ea23a670566b58c7ed9b6d7b37d9cc3102c0ed73abb88594d40851a2e501ae5
MD5 b3be9a981ea1601baa18a5c83b786927
BLAKE2b-256 16d7707619aefc048e731f12a85ba98bd8c579574d5907efea93e59a24ac1c84

See more details on using hashes here.

File details

Details for the file ferogram-0.1.4-cp313-abi3-android_24_armeabi_v7a.whl.

File metadata

File hashes

Hashes for ferogram-0.1.4-cp313-abi3-android_24_armeabi_v7a.whl
Algorithm Hash digest
SHA256 e13531a73a46fef0e8167e035f0e292b2c01ac00aacd3a69e3160b1941903268
MD5 8fa3c7111849d386d4eaf9a4733c428a
BLAKE2b-256 46537fcbe4a3b41c95bb28057e7eed5171ef7af4edef43fe0e8adf5e1678bf8c

See more details on using hashes here.

File details

Details for the file ferogram-0.1.4-cp313-abi3-android_24_arm64_v8a.whl.

File metadata

File hashes

Hashes for ferogram-0.1.4-cp313-abi3-android_24_arm64_v8a.whl
Algorithm Hash digest
SHA256 5bb4017a2a3b1e7c4535cd3bf304c77e3c2e2c31b25d7a3f995b2b8b3ba3923d
MD5 78b6f54248c56eb5dfe35015843a493b
BLAKE2b-256 fabf42d6ebdaefbe2ac939aea1af1044fca216166834029f113a21c9974e4353

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