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

Uploaded CPython 3.13+Windows x86-64

ferogram-0.1.7-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.0 MB view details)

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

ferogram-0.1.7-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.8 MB view details)

Uploaded CPython 3.13+manylinux: glibc 2.17+ ARM64

ferogram-0.1.7-cp313-abi3-macosx_11_0_arm64.whl (6.6 MB view details)

Uploaded CPython 3.13+macOS 11.0+ ARM64

ferogram-0.1.7-cp313-abi3-macosx_10_12_x86_64.whl (7.0 MB view details)

Uploaded CPython 3.13+macOS 10.12+ x86-64

ferogram-0.1.7-cp313-abi3-android_24_x86_64.whl (7.0 MB view details)

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

ferogram-0.1.7-cp313-abi3-android_24_x86.whl (6.8 MB view details)

Uploaded Android API level 24+ x86CPython 3.13+

ferogram-0.1.7-cp313-abi3-android_24_armeabi_v7a.whl (6.0 MB view details)

Uploaded Android API level 24+ ARM EABI v7aCPython 3.13+

ferogram-0.1.7-cp313-abi3-android_24_arm64_v8a.whl (6.6 MB view details)

Uploaded Android API level 24+ ARM64 v8aCPython 3.13+

File details

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

File metadata

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

File hashes

Hashes for ferogram-0.1.7.tar.gz
Algorithm Hash digest
SHA256 d38162506a6ba98871fbe94b6fd480e03ffe23ae0f96797549372e79ed51e886
MD5 e1a7158800a1cfbae11753975b91d0ce
BLAKE2b-256 9d6d3619fa132eb05397bd2157ecd622130972e14acd2c1ede8314f5a637027e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ferogram-0.1.7-cp313-abi3-win_amd64.whl
  • Upload date:
  • Size: 9.2 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.7-cp313-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d6b84049a1a7a45855a71e48a72f84791d30428790a9efac12eccdcc4cae6ebc
MD5 38eeda05b8a4a61a9bad25750f54b35b
BLAKE2b-256 22a05cd7ad284c841f6789b2959cc8b149638e5b20f3ad2e44c9c0a7d8a09c85

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.7-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 28c43b3541ff7ea710dcdbfdd33bc828f70ce557fdfca9c9076b4695433e15c2
MD5 1a9a94012277aa49d336059c9e62e8a6
BLAKE2b-256 4813a86ac199e10d658df604809edaed8b0e1d024f5fef005f1f0a6d7ea6efad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.7-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8530035c0fd03773034eb636908deb163d92a819853fe8e0b2a35b41d1cb7ede
MD5 ffc63b500ebe6ca595393880a2455ee0
BLAKE2b-256 d23af1c93f83f7673ae5ae8287b562b7d50fdbbc5051579289aaf1dee73a91a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.7-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 97fb7753af0ecb3399aa57abbb0cb6aa40656f40bc0caeb04741c2e6331b52dd
MD5 9c9ae87aec050ecea1b943bd64fbdb5a
BLAKE2b-256 31f256211dff3e0ba8dd6092bf465e4e800058e8662f6238e66ec4e7baa2c617

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.7-cp313-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f0ee5472e155c56dba374e457399ac3daaa6603e058971999532a48e592bd9e5
MD5 8ddfc9425c6018fc9fd482932392c370
BLAKE2b-256 712f8c562c4cf7c87a208be9a8aeb938a4ed0c35ea38565c4c90dfe5145e30e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.7-cp313-abi3-android_24_x86_64.whl
Algorithm Hash digest
SHA256 02482ea3ac476dd261449528f08198070ec3cb8b95836610dd1cfbe5d85d8020
MD5 99e49adfaf2019d5c979d8df477ff1b1
BLAKE2b-256 edef8c61ec120aa5e47e3cb69a13aa9964b43c4d08be03653dc6b08c4d947856

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.7-cp313-abi3-android_24_x86.whl
Algorithm Hash digest
SHA256 469f4c8979edbc2bb0cee250574b2837b7283af4724716b0192d1f2825fdc057
MD5 b57d30efd272e895891f533f1b1d4b1d
BLAKE2b-256 8ec8ed8873c78c578f32d9c1586131f9c0a542e6aff270e193d27d982c3eed12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.7-cp313-abi3-android_24_armeabi_v7a.whl
Algorithm Hash digest
SHA256 bdde7372335d50f896fc847f9146c5ad5bd7d910b3bb62f6a3415b4303ccdbe9
MD5 4e8750f6d050ee053636e89834450f58
BLAKE2b-256 0c6b2ad30889c1485f55fba232be69cc7753ff6207ee2122df89a16a6fcac282

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.7-cp313-abi3-android_24_arm64_v8a.whl
Algorithm Hash digest
SHA256 df64101dab9cae7e6b96b988654e1de3e912ec755d1df0901c237d80b0887ca8
MD5 9271a01893965297706591ee1daa49f5
BLAKE2b-256 0814a383e12aab001b294b1a5cd2c2d0b6b222393864e5daa2290c562deb4fed

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