Async, OpenAI-style SDK for building bots for the Luffa messaging platform.
- Fully async (based on
httpxandasyncio) - OpenAI-style interface (
luffa_bot.robot_key = "...") - Simple APIs:
receive(),send_to_user(),send_to_group(), andrun() - Advanced group messages with buttons, confirms, and @mentions
- Built-in deduplication, concurrency control, and middleware support
- CLI for quick bot setup
pip install luffa-bot-python-sdkRequires Python 3.9+.
import asyncio
import luffa_bot
# Set the robot secret key (or use LUFFA_ROBOT_SECRET env var)
luffa_bot.robot_key = "YOUR_ROBOT_SECRET"
async def main():
# Poll once for messages
envelopes = await luffa_bot.receive()
for env in envelopes:
for msg in env.messages:
if env.type == 0:
await luffa_bot.send_to_user(env.uid, f"You said: {msg.text}")
else:
await luffa_bot.send_to_group(env.uid, f"[group] {msg.text}")
asyncio.run(main())Use the built-in run() method to continuously poll and handle messages.
import asyncio
import luffa_bot
luffa_bot.robot_key = "YOUR_ROBOT_SECRET"
async def handler(msg, env, client):
if "help" in msg.text.lower():
await client.send_to_user(env.uid, "How can I help?")
else:
await client.send_to_user(env.uid, f"Echo: {msg.text}")
asyncio.run(luffa_bot.run(handler, interval=1.0, concurrency=5))- Automatic deduplication by
msgId(FIFO-capped memory) - Configurable polling interval
- Concurrency limit (process multiple messages at once)
- Middleware and error hook support
from luffa_bot.models import GroupMessagePayload, SimpleButton
payload = GroupMessagePayload(
text="Pick an option:",
button=[SimpleButton(name="OK", selector="ok")]
)
await luffa_bot.send_to_group("GROUP_ID", payload, message_type=2)message_type=1→ Text onlymessage_type=2→ Buttons/advanced messages
LUFFA_ROBOT_SECRET: Default robot key if not set vialuffa_bot.robot_key.
A CLI is included for quick bot testing:
export LUFFA_ROBOT_SECRET="..."
# Run an echo bot
luffa-bot run --interval 1.0
# Send DM to a user
luffa-bot send --uid <USER_ID> --text "Hello"
# Send message to a group with buttons
luffa-bot send-group --uid <GROUP_ID> --text "Hi group" --with-buttonspip install -e ".[dev]"- Tests:
pytest - Lint:
ruff check . - Type-check:
mypy .
- Fix:
_ensure_client()was recreating thehttpx.AsyncClient(and leaking the old connection pool) on every API call. Connection pooling now works correctly across calls. - Fix: Dedupe eviction used
set.pop()(arbitrary removal). Now uses FIFO eviction viadequeso oldest IDs are dropped first. - Fix: CLI
sendandsend-groupcommands now properly close the HTTP client on exit. - Fix: GitHub Actions workflow directory renamed from
workflow/toworkflows/— CI/CD was silently not running. - Security: Removed unused dependencies
anyioandpydanticfrominstall_requires(unnecessary install footprint). - Compat: Replaced deprecated
asyncio.get_event_loop()withasyncio.get_running_loop()in example bot. - Added
__version__export (luffa_bot.__version__).
- Minor fixes and improvements.
- Initial async SDK with
receive(),send_to_user(),send_to_group(), andrun(). - Middleware pipeline, deduplication, concurrency control.
- CLI entry point (
luffa-bot).
Niraj Kulkarni (Sabma Labs, University of Surrey)
Apache License 2.0