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.1.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.1-cp39-abi3-win_amd64.whl (5.2 MB view details)

Uploaded CPython 3.9+Windows x86-64

ferogram-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.7 MB view details)

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

ferogram-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

ferogram-0.1.1-cp39-abi3-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

ferogram-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

ferogram-0.1.1-cp39-abi3-android_24_x86_64.whl (4.7 MB view details)

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

ferogram-0.1.1-cp39-abi3-android_24_x86.whl (4.5 MB view details)

Uploaded Android API level 24+ x86CPython 3.9+

ferogram-0.1.1-cp39-abi3-android_24_armeabi_v7a.whl (4.0 MB view details)

Uploaded Android API level 24+ ARM EABI v7aCPython 3.9+

ferogram-0.1.1-cp39-abi3-android_24_arm64_v8a.whl (4.4 MB view details)

Uploaded Android API level 24+ ARM64 v8aCPython 3.9+

File details

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

File metadata

  • Download URL: ferogram-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 73480531108fa4556ad0f3e7d0a2457b29ad5105d5253140d264a812234537b0
MD5 6974f0545e7496643c9aa46a89166870
BLAKE2b-256 c0a5b53ea572cd8b27cc11e1eceeb4f8ac224d757f757e5f3a7c800b7401450b

See more details on using hashes here.

File details

Details for the file ferogram-0.1.1-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: ferogram-0.1.1-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 5.2 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for ferogram-0.1.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e7aeeab27f08ead814b5ca2192b83089ac18a60e3661c4b0f255f41896fb9058
MD5 d51101187cef568116490563e7fd8d24
BLAKE2b-256 83e3379b5fdf2f683b0b1ee56c279d60240e0fa269d7e45ab14378f8a1f84a4b

See more details on using hashes here.

File details

Details for the file ferogram-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 435d44a2be2f0f61cdeda69ef774498b86b40b39a935ec4cb24ab84079e6f0d3
MD5 21be604f3df9cbaa555eab67d1db6c48
BLAKE2b-256 f6ddb85ba4945d8177c8b3b1d5e485cb78d967b801e2d21f9d4354c94e554c9d

See more details on using hashes here.

File details

Details for the file ferogram-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c98841b21de6e510db33946303df6bceea285bdde93fe1d2f77f4d405db94441
MD5 3912dd9a485ed2cda3f4e37adedbb092
BLAKE2b-256 7a34664551cf04fe93971dfdf6631823591dd0a8fe28e724c1512b0aac20517a

See more details on using hashes here.

File details

Details for the file ferogram-0.1.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 241a90e7b4617e76dfe9fd5d11a0610c9ffe623021dbc26e9278dfcb0ec2761b
MD5 cd7f87def5515f907097504670292429
BLAKE2b-256 7d37814f32c14d2a6590b72fb7bf0707bd95cbdb05e5cc3cfe28671c2cf76801

See more details on using hashes here.

File details

Details for the file ferogram-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 50d79287deb49a0b7f9290ee356cfff5307ad7ab8064bb23f60e3b609f2e5724
MD5 3ee9d438ba892efa5e0ffe1cb0dbf555
BLAKE2b-256 618c301215e55259e1e569effc81a72d400ae1dcad281ed0ebfe4d0102e606e3

See more details on using hashes here.

File details

Details for the file ferogram-0.1.1-cp39-abi3-android_24_x86_64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.1-cp39-abi3-android_24_x86_64.whl
Algorithm Hash digest
SHA256 f74c07e9f2f636c1e7c410092bcf4f2cfaecbdac79ef2a8154afc4aba251b153
MD5 22a21084120089db4e7a331df2193d43
BLAKE2b-256 c6d49a0341ebc0f4f364e649aab57086fbc21dc62d002ac8f4e8012c32b6018d

See more details on using hashes here.

File details

Details for the file ferogram-0.1.1-cp39-abi3-android_24_x86.whl.

File metadata

File hashes

Hashes for ferogram-0.1.1-cp39-abi3-android_24_x86.whl
Algorithm Hash digest
SHA256 c7167691ce15890185215b1838f9c0e466b72894bc7ecc6dd6a3c4bfb87f4727
MD5 0900894aad4264d67dde4cbbd396d0d0
BLAKE2b-256 9e3e048827f0fec39ef7a743415fb7cc8b62cc7365654c21cc094f9e54176c11

See more details on using hashes here.

File details

Details for the file ferogram-0.1.1-cp39-abi3-android_24_armeabi_v7a.whl.

File metadata

File hashes

Hashes for ferogram-0.1.1-cp39-abi3-android_24_armeabi_v7a.whl
Algorithm Hash digest
SHA256 fe0b546ef9e481c62690d55ee28e39706d06ccad3bcab56f33e8891a6b584f0c
MD5 fbfdc9652cad5e5cd3a577091b061ab7
BLAKE2b-256 0a1cd5725b95dda3e94fece3eb2a76240c770caf5c73f81601f9ed791c7f170e

See more details on using hashes here.

File details

Details for the file ferogram-0.1.1-cp39-abi3-android_24_arm64_v8a.whl.

File metadata

File hashes

Hashes for ferogram-0.1.1-cp39-abi3-android_24_arm64_v8a.whl
Algorithm Hash digest
SHA256 676a130ff9f5ccc346bc13406a0031e7e79d3c96cd1cad5d787e83bc71a11082
MD5 d43708f2b856cb290e0b28979f1226b7
BLAKE2b-256 64bf472ab6f96e275077fa55462bdcfa006b4cd93921a2cd946f9c029176bba3

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