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

Uploaded CPython 3.12+Windows x86-64

ferogram-0.1.2-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.2-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.12+macOS 11.0+ ARM64

ferogram-0.1.2-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.2-cp312-abi3-android_24_x86_64.whl (4.7 MB view details)

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

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

Uploaded Android API level 24+ x86CPython 3.12+

ferogram-0.1.2-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.2-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.2.tar.gz.

File metadata

  • Download URL: ferogram-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 e3d4bbebb69dc2626ac9e9fe72e783b81e2dfd59311a211430c4ecbd89097124
MD5 af559e10ad2525966e92d657eddb7616
BLAKE2b-256 72feda45de35dac31b9457ab347169c8d16dbb973360ad1dc85a0aaf1513cc1a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ferogram-0.1.2-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 5.2 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.2-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 967838a13bfa27fc125f5809c95cb3f55781f81213f91f7b03a3d21ed8615d4b
MD5 a0440ccd88ab84bebdcf5d27e6f8e458
BLAKE2b-256 7ad999d5c038779b5e2522077dc9946df3144912f648521a5721d32c5c3260a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.2-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1c729ebb3aca63a6fc0f117964d44ddaa110bfb8e6c01383bad7ba2374b3732b
MD5 765128f61b358725df977da682c931a6
BLAKE2b-256 63456595416c4cc88500d6ac0c980d42eb70d5337d70ea996919cea32f712be8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.2-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 08969db3a06f20f4e369e386c2b27025b1b0305f68cf1fb2238287c94f83bad4
MD5 55290c20fba871fd7358d3ea0efd1003
BLAKE2b-256 79ad290971a82d135448a35cb8ae11db9f0dea686b45492cd1cb318c77ee3797

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.2-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 66ce14f0b566e0ef49e73467fe28111f697c1852ce8b4cd3ffd305e5a9202c83
MD5 60c9ee9cdf33f7e85ba6627a43a531e2
BLAKE2b-256 f6b8086b4fe486a199e183af2b2a7ce39a4d6a8f65881c8de1186ecafff51992

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.2-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a5d09da01e2322b4a4999e5589e6cfb3701814d4472f3e7e1198eceae8f92498
MD5 1d444fe07e50c68a9c2b0fb3c29ed245
BLAKE2b-256 caf45384fa3b5794251de11ee60407e987e18d4f69b984aaa9846b213ff3135e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.2-cp312-abi3-android_24_x86_64.whl
Algorithm Hash digest
SHA256 da5f5e97849a4c9933dc3eb10e365e43641de8fb725b999623604a27c07a7001
MD5 51e3eba5f1cd18a103006acec6e38ab0
BLAKE2b-256 b8f4742865d7f5f7ed341e173a2e3ff7b8655a227bfa8ffd6fa775fd2a6e2483

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.2-cp312-abi3-android_24_x86.whl
Algorithm Hash digest
SHA256 fa5a471cb64dd15d289b8906948662e0cd250ec8d16d00d969d0f22bfab9d2d5
MD5 62e4c016fc60f437cb27e7f9573a38eb
BLAKE2b-256 6dabe918368c701801fda5e403b765ec799e64c4a5acf2e7c9e8fd3ed8aef2c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.2-cp312-abi3-android_24_armeabi_v7a.whl
Algorithm Hash digest
SHA256 f1ca82ec40baead3005ba8893fcb654bfb3ed3b77c9a889fea638c5117bdac5e
MD5 9338ad12bbd1f534e700d75dd972e112
BLAKE2b-256 34aa7502edad1e40675ca65e4020188803d1b0731b98dfa4893e720cfebb7d4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ferogram-0.1.2-cp312-abi3-android_24_arm64_v8a.whl
Algorithm Hash digest
SHA256 6182aff7f4a4a8bc2aa8adacdecd6cabee9ec15d57c40408bc13d27d3cf744db
MD5 519fc55a116d2a568cf99d4fba6f1217
BLAKE2b-256 781201dee5c9b33be39c3f73354c9c0eefe4e75637b0c7af2429051296a36d11

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