Getting Started

Get up and running with kurigram-addons in under 5 minutes.

Installation

pip
pip install kurigram-addons
Poetry
poetry add kurigram-addons

Requirements: Python ≥ 3.10, Kurigram ≥ 2.1.35. Redis ≥ 6.0.0 is only needed if you use RedisStorage for FSM.

Minimal Bot with Keyboards

Create a bot that responds with an inline keyboard when the user types /start.

main.py
from kurigram_addons import KurigramClient, Router, InlineKeyboard, MemoryStorage

router = Router()

@router.on_command("start")
async def start(client, message):
    kb = InlineKeyboard(row_width=2)
    kb.button("🔍 Search", callback="search")
    kb.button("⚙️ Settings", callback="settings")
    kb.button("📖 Help", callback="help")
    await message.reply("Welcome! Choose an option:", reply_markup=kb)

app = KurigramClient(
    "my_bot",
    bot_token="YOUR_TOKEN",
    storage=MemoryStorage(),
    auto_flood_wait=True,
)
app.include_router(router)
app.run()

Lifecycle Hooks

Run async setup/teardown around the client lifecycle — connect databases, warm caches, etc.

lifecycle.py
@app.on_startup
async def init():
    await database.connect()
    print("✅ Database ready")

@app.on_shutdown
async def cleanup():
    await database.disconnect()
    print("🛑 Disconnected")

First Conversation Flow

Collect user input across multiple steps with a declarative class-based conversation.

registration.py
from kurigram_addons import Conversation, ConversationState

class Registration(Conversation):
    name = ConversationState(initial=True)
    email = ConversationState()

    @name.on_enter
    async def ask_name(self, ctx):
        await ctx.message.reply("What's your name?")

    @name.on_message
    async def save_name(self, ctx):
        await ctx.helper.update_data(name=ctx.message.text)
        await self.goto(ctx, self.email)

    @email.on_enter
    async def ask_email(self, ctx):
        await ctx.message.reply("Great! What's your email?")

    @email.on_message
    async def save_email(self, ctx):
        data = await ctx.helper.get_data()
        name = data["name"]
        await ctx.helper.finish()
        await ctx.message.reply(
            f"Done! Name: {name}, Email: {ctx.message.text}"
        )

# Register with the client
app.include_conversation(Registration)

What's Next?