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.6.tar.gz (568.8 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.6-cp313-abi3-win_amd64.whl (7.1 MB view details)

Uploaded CPython 3.13+Windows x86-64

ferogram-0.1.6-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.8 MB view details)

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

ferogram-0.1.6-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.6 MB view details)

Uploaded CPython 3.13+manylinux: glibc 2.17+ ARM64

ferogram-0.1.6-cp313-abi3-macosx_11_0_arm64.whl (5.4 MB view details)

Uploaded CPython 3.13+macOS 11.0+ ARM64

ferogram-0.1.6-cp313-abi3-macosx_10_12_x86_64.whl (5.7 MB view details)

Uploaded CPython 3.13+macOS 10.12+ x86-64

ferogram-0.1.6-cp313-abi3-android_24_x86_64.whl (5.7 MB view details)

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

ferogram-0.1.6-cp313-abi3-android_24_x86.whl (5.5 MB view details)

Uploaded Android API level 24+ x86CPython 3.13+

ferogram-0.1.6-cp313-abi3-android_24_armeabi_v7a.whl (4.9 MB view details)

Uploaded Android API level 24+ ARM EABI v7aCPython 3.13+

ferogram-0.1.6-cp313-abi3-android_24_arm64_v8a.whl (5.4 MB view details)

Uploaded Android API level 24+ ARM64 v8aCPython 3.13+

File details

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

File metadata

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

File hashes

Hashes for ferogram-0.1.6.tar.gz
Algorithm Hash digest
SHA256 4f9e870cbec67c390df17cd10e0b40a7f6dbf9a2361865fab6b2c49d0e0ec78f
MD5 19fcdd4dba3876ae0db28ad36a64bfa0
BLAKE2b-256 a58f168477c3b1f5d883b2a3dcc551b195417c29f79414ff817fedff1c8bc06c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ferogram-0.1.6-cp313-abi3-win_amd64.whl
  • Upload date:
  • Size: 7.1 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.6-cp313-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ee671021a81ee89a6937577d2d4b4d13d1f90c36197f28be1ed4231677a0b503
MD5 09dce31a27509d94efcea3cc2cfa4437
BLAKE2b-256 4f657b71086d341d16c7a5b3f426fa4e21db24886563bbacd211e2949c015a0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.6-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1edd6b54c59c4301eb08f8ba33d0225486632c531017c30c88fa3a716fc9cf05
MD5 260028d513ae6c7b2ffcdceb95ec4c64
BLAKE2b-256 cd198c49cbcbeb639a7f193426b567b95dfc88f494fd502b45c2dd1baf8a2913

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.6-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 95348fc439cde8ff581dd3b0c9563733a1c15e97072c667ee061928ed6193516
MD5 1c112599ecad649c7799f5e13ddcf1b7
BLAKE2b-256 501f2d7aa9acd4ad6aac0bc73d49bb0df5948bb8c9e6eed7c78c2efea3488015

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.6-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e2cc91c1c57e9f5ab3dc67e0b42a3dc478f7440b3c91488c1aa4c3a81ac9d9eb
MD5 aadb2abaa991549cf40bb98394c3c4d5
BLAKE2b-256 1efe8a539114b5283827d3e1bb21081483df5eceb3a7696a0d47bc4df44e007c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.6-cp313-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 57abfca829cbfeb7de6f26eb906ede9b732044ad2212954eb3c9ce27e9fa9d88
MD5 e8355ca26dce14f2112dc3d9bf1bdf5f
BLAKE2b-256 0600a54fe28e46b89d864fd2c431a9dbbb32dc5df6c60ff377707491031cb802

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.6-cp313-abi3-android_24_x86_64.whl
Algorithm Hash digest
SHA256 d305867dcd8567c877fab57eae2c2f96cbe141a87c4a23235bf35a5d37b7fda6
MD5 c0b84eec97eaffa5cf2ef7cda95b18fe
BLAKE2b-256 1dcc350d64adc56913c7fda4f0469e8db65618413301238df52b9bf680f7cdb2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.6-cp313-abi3-android_24_x86.whl
Algorithm Hash digest
SHA256 053dce7dd99b4fab9e989b4826dd3c20bae7f730488e09972b531321099f39a0
MD5 afa1d5b5d9c75d921632ccd0b9a62909
BLAKE2b-256 00c3e33aa3c325028019546a8b49b26c21a190a0797463074820b6521b0af9c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.6-cp313-abi3-android_24_armeabi_v7a.whl
Algorithm Hash digest
SHA256 87b6ba700c69f1314ebdb745a427ebe234e77dcaf3b7114b40046eae8a6d62c4
MD5 9f4540e88098a42f9f07b560d9d3b797
BLAKE2b-256 c10ef023fef8dfda96e70ff188be5b8a3c8260b73247d35ff1264262cc3a5b5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.6-cp313-abi3-android_24_arm64_v8a.whl
Algorithm Hash digest
SHA256 a61f0bb5dffb9a716d32ff27f63ffe83fcbcdd57dcf80db62098313790f36fa3
MD5 e3bd9644963b8b2d548aa4ef86686497
BLAKE2b-256 30dac0e801be2e726e7d5e0aedf9d91ab14c8de6cbbfbb50e9c8080f04c1cc29

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