Skip to main content

A Telegram-style Python SDK for the Luffa Bot API

Project description

Luffa Python SDK

A Telegram-style Python SDK for the Luffa Bot API. Build bots in minutes with a clean decorator-based interface.

Install

pip install luffa-sdk

With .env support:

pip install luffa-sdk[dotenv]

Quickstart

import os
from dotenv import load_dotenv
from luffa import LuffaBot

load_dotenv()
bot = LuffaBot(secret=os.getenv("BOT_SECRET"))


@bot.command("ping")
async def ping(msg):
    await msg.reply("Pong!")


@bot.on_message()
async def echo(msg):
    await msg.reply(f"You said: {msg.text}")


bot.run()

That's it. Save as bot.py, add your BOT_SECRET to .env, and run it.

Getting Your Bot Secret

  1. Open robot.luffa.im and scan the QR code with Luffa
  2. Click New Bot to create a bot
  3. Copy the SecretKey — that's your BOT_SECRET
  4. Add the bot as a friend in Luffa to start chatting

Features

Commands

Register command handlers with the @bot.command() decorator. The leading / is handled automatically. Arguments are parsed into msg.args.

@bot.command("greet")
async def greet(msg):
    name = msg.args[0] if msg.args else "stranger"
    await msg.reply(f"Hello, {name}!")

User sends /greet World → bot replies Hello, World!

Fallback Handler

Catch all non-command messages (or unregistered commands) with @bot.on_message():

@bot.on_message()
async def fallback(msg):
    await msg.reply("I don't understand that. Try /help")

Interactive Buttons

Send menu-style buttons in group chats. Private chats automatically fall back to a text list.

from luffa import button

@bot.command("menu")
async def menu(msg):
    await msg.reply_buttons(
        "What would you like to do?",
        buttons=[
            button("📋 Help", "/help"),
            button("🏓 Ping", "/ping"),
            button("ℹ️ About", "/about"),
        ],
    )

Confirm Dialogs

Two side-by-side buttons for yes/no style interactions:

from luffa import confirm_button

@bot.command("delete")
async def delete(msg):
    await msg.reply_confirm(
        "Are you sure you want to delete?",
        confirm=[
            confirm_button("Yes", "/confirm_delete", style="destructive"),
            confirm_button("Cancel", "/cancel"),
        ],
    )

@Mentions

Mention users in group messages:

from luffa import mention

await bot.send_group_text(
    group_id,
    "@abc123 you've been invited!",
    at_list=[mention("abc123", location=0, length=8)],
)

Hidden Buttons

Buttons can be hidden so the tapped message isn't visible to others:

button("Secret vote", "/vote yes", hidden=True)

API Reference

LuffaBot(secret, poll_interval=1)

The main bot class.

Method Description
bot.command(name) Decorator — register a /command handler
bot.on_message() Decorator — register a fallback handler
bot.send_text(uid, text) Send a private message
bot.send_group_text(uid, text, at_list?) Send a group text message
bot.send_group_buttons(uid, text, buttons, dismiss?, at_list?) Send group message with buttons
bot.send_group_confirm(uid, text, confirm, dismiss?, at_list?) Send group message with confirm buttons
bot.run() Start polling (blocking)
bot.start() Start polling (async)

Message

Passed to every handler.

Property Type Description
msg.text str Message text
msg.args list[str] Command arguments
msg.uid str Chat ID (user or group)
msg.sender_uid str ID of the user who sent it
msg.is_group bool Whether it's a group message
msg.msg_id str Unique message ID
msg.at_list list @mentions in the message
msg.url_link str | None Attached URL
Method Description
await msg.reply(text) Reply with text (auto-routes private/group)
await msg.reply_buttons(text, buttons, dismiss?) Reply with menu buttons
await msg.reply_confirm(text, confirm, dismiss?) Reply with confirm buttons

Components

Function Description
button(name, selector, hidden?) Create a menu button
confirm_button(name, selector, style?, hidden?) Create a confirm button ("default" or "destructive")
mention(uid, location?, length?) Create an @mention

Limitations

The Luffa Bot API currently supports:

  • ✅ Text messages (private & group)
  • ✅ Interactive buttons (group only)
  • ✅ @mentions (group only)
  • ❌ Voice / audio messages
  • ❌ Images / files
  • ❌ Message deletion
  • ❌ Admin actions (kick, mute, pin)

License

MIT

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

luffa_sdk-0.1.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

luffa_sdk-0.1.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: luffa_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for luffa_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0c802342347ce7a5204eceda528c3cb9318c1452725c832120f0381b0211f37e
MD5 890d3278a38d9fc42f988b4571d26592
BLAKE2b-256 b94d170907316829ee326e61742e66c60b7bc57cf8f327c03aa8e7b0d04c7558

See more details on using hashes here.

File details

Details for the file luffa_sdk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: luffa_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for luffa_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0702a6658ce7e0ccfa62b74374e0b55c9e356ce37678e0a449c07d7526f231d8
MD5 6372b65f98c218297fa64328696b0adf
BLAKE2b-256 bc7022a78ab045af15452ce8e6756e3df41b65cd576dacb02b979ca0a39322ea

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