Skip to content

A simple bot - example

For starting develop bots with mxbt you can use this example.

bot.py structure

from mxbt import Bot, Context, Creds, Listener
import asyncio

bot = Bot(
    prefix="!",                 # Standart command prefix, commands can setup it own prefix
    creds=Creds.from_json_file(
        "credits.json"
    )
)
lr = Listener(bot)

@lr.on_command(prefix="?", aliases=["echo", "e"])  # Every command can setup it own prefix, it is optionaly
async def ctx_echo(ctx: Context) -> None:           # Context object contains main info about event
    await ctx.reply(ctx.body)                       # Reply message to event room

if __name__ == "__main__":
    asyncio.run(lr.start_polling())                 # Run bot event loop

credits.json structure

{
    "homeserver" : "https://matrix.org",
    "user_id" : "myusername",
    "password" : "mypassword"
}

bot.py structure

from mxbt import Bot, Context, Creds, Listener
import asyncio

bot = Bot(
    prefix="!",                 # Standart command prefix, commands can setup it own prefix
    creds=Creds(
        homeserver="https://matrix.org",
        username="mybot",
        password="mypassword"
    )
)
lr = Listener(bot)

@lr.on_command(prefix="?", aliases=["echo", "e"])   # Every command can setup it own prefix, it is optionaly
async def ctx_echo(ctx: Context) -> None:           # Context object contains main info about event
    await ctx.reply(ctx.body)                       # Reply message to event room

if __name__ == "__main__":
    asyncio.run(lr.start_polling())                 # Run bot event loop