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.3.tar.gz (559.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.3-cp312-abi3-win_amd64.whl (5.3 MB view details)

Uploaded CPython 3.12+Windows x86-64

ferogram-0.1.3-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.7 MB view details)

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

ferogram-0.1.3-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.5 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

ferogram-0.1.3-cp312-abi3-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

ferogram-0.1.3-cp312-abi3-macosx_10_12_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

ferogram-0.1.3-cp312-abi3-android_24_x86_64.whl (4.7 MB view details)

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

ferogram-0.1.3-cp312-abi3-android_24_x86.whl (4.5 MB view details)

Uploaded Android API level 24+ x86CPython 3.12+

ferogram-0.1.3-cp312-abi3-android_24_armeabi_v7a.whl (3.9 MB view details)

Uploaded Android API level 24+ ARM EABI v7aCPython 3.12+

ferogram-0.1.3-cp312-abi3-android_24_arm64_v8a.whl (4.4 MB view details)

Uploaded Android API level 24+ ARM64 v8aCPython 3.12+

File details

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

File metadata

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

File hashes

Hashes for ferogram-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f6fab6a2c351cd99cbd09cd1c1e7ac176835e14e15d3a7d9c5f5bbd2a098c3ce
MD5 b35d551cb24409831c3788fb8be52696
BLAKE2b-256 da16a8a3396ad9f4ba8709ff7eb231158fd9ab527a45180ec22bbc8113d30a47

See more details on using hashes here.

File details

Details for the file ferogram-0.1.3-cp312-abi3-win_amd64.whl.

File metadata

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

File hashes

Hashes for ferogram-0.1.3-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8b9d70b6561e860e03a9828a2348db2be9e852aa297b2287a7d75ad6621c4879
MD5 c0a590dead23829b2e185bf543ce27dd
BLAKE2b-256 46d40358a51d50c48cb61eb1222607e2451e9a9e508df9cc552b309901f826cd

See more details on using hashes here.

File details

Details for the file ferogram-0.1.3-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.3-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8ad605f6bfd94538c3dbf1af22fa8ef234d5c8e4b3e4c0ae1e72df92fa8d00e5
MD5 756deae656f00de8e38e5d1c815b0f7f
BLAKE2b-256 9a19594eb471e93b6c56f6f5d7e24be3036f5c1b1ad41691f6d00e11ece6b157

See more details on using hashes here.

File details

Details for the file ferogram-0.1.3-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.3-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d2492f5c00a71ec13e4a1539424a6901a1252be2fefb8c1f73a3829be8ced9a3
MD5 be9eed86b08eaa52286a5f91bee568ed
BLAKE2b-256 bfa249e2e2597e958daa608ec53104f5d5024ed5712005aa08017241e9876819

See more details on using hashes here.

File details

Details for the file ferogram-0.1.3-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.3-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 889a0a06478b53779c2925d2ff7c12833feab6cbfd44ca86f7ecc112d1fe8d33
MD5 a9be9f0d8a58c4a86490407b911bab49
BLAKE2b-256 c2eefb28d29ed881ecf2b898ba15b0066945157c66ec8d3d31030403534ff902

See more details on using hashes here.

File details

Details for the file ferogram-0.1.3-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.3-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f5a03b34f383ee3b44f57f3d0f4040b1dd32bb024a149910154514cdb29cfc43
MD5 71c907e19189872b9bf2da8506c1292a
BLAKE2b-256 89137a85c442b3467ff96009e2d24001c89c83bf5b3e0c9d093f7aa022f7ed14

See more details on using hashes here.

File details

Details for the file ferogram-0.1.3-cp312-abi3-android_24_x86_64.whl.

File metadata

File hashes

Hashes for ferogram-0.1.3-cp312-abi3-android_24_x86_64.whl
Algorithm Hash digest
SHA256 b7aebb8ba34977c24c8c5f44f1aa2c2548810e1be45fabce295d91c24f4d83f5
MD5 eb1e8e637fe221064395ad2537d7c847
BLAKE2b-256 a0fe7355eed261edb87c8877c2453babab2f41f16fd68d6a3a67851a2e5ecbea

See more details on using hashes here.

File details

Details for the file ferogram-0.1.3-cp312-abi3-android_24_x86.whl.

File metadata

File hashes

Hashes for ferogram-0.1.3-cp312-abi3-android_24_x86.whl
Algorithm Hash digest
SHA256 0407867b91b4f0d0677fd667b12798a32e1344022638ae6ce79388caf1560d68
MD5 5b3893ebae3411c77dc0a03559e5e1c3
BLAKE2b-256 a294c4a5d33b1d75896d8ecb7b739fd7a9a8b444e37954e02bc85ee581798a8e

See more details on using hashes here.

File details

Details for the file ferogram-0.1.3-cp312-abi3-android_24_armeabi_v7a.whl.

File metadata

File hashes

Hashes for ferogram-0.1.3-cp312-abi3-android_24_armeabi_v7a.whl
Algorithm Hash digest
SHA256 e62b21e6e847c730fa666c13dad1f7d0a1e5e814de54501c1d12b8a8cd723908
MD5 0f444491d8abc058fa9c688d1c411a9c
BLAKE2b-256 c9b3830fb04f93bf99c80a30b3b62f8fbcc04d2c1976d58c77ffc8cac13d74a3

See more details on using hashes here.

File details

Details for the file ferogram-0.1.3-cp312-abi3-android_24_arm64_v8a.whl.

File metadata

File hashes

Hashes for ferogram-0.1.3-cp312-abi3-android_24_arm64_v8a.whl
Algorithm Hash digest
SHA256 f0d2b936d48af2a1796c65b654be0248b001db6104cae8265fe5f45bfc913b6f
MD5 6b010b677b5c289d16904bcfa3dda764
BLAKE2b-256 3c1332c9fd8e3f34aa70eba37065729c60e98e2a25dd8d0fa1d133924c083509

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