Files
mab/examples/command_bot.py

145 lines
4.5 KiB
Python

"""
This example implements Matrix bot that can execute some commands.
It uses environment variables to specify authorization data. Use Ctrl+C to stop
the bot.
"""
import asyncio
import time
import html
import os
import traceback
import logging
from mab import *
from _environment import check_environment
async def on_help_command(ctx: EventContext, vars: ContextVars) -> None:
"""!help"""
HELP_MESSAGE = (
"<strong>Here is the list of the commands:</strong><br>"
"<ul>"
"<li><code>!help</code> - this help message</li>"
"<li><code>!time</code> - get UNIX timestamp</li>"
"<li><code>!raise</code> - raise <code>RuntimeError()</code></li>"
"<li><code>!assert</code> - perform <code>assert</code> that will fail</li>"
"<li><code>!mul A B [C] [D]...</code> - multiply A, B... and so on</li>"
"<li><code>!args arg1 [arg2] ... [arg5]</code> - command that takes 1..5 arguments</li>"
"</ul>"
)
await ctx.bot.send.text(
ctx.room,
HELP_MESSAGE,
is_html=True
)
async def on_time_command(ctx: EventContext, vars: ContextVars) -> None:
"""!time"""
await ctx.bot.send.text(
ctx.room,
f"Current UNIX timestamp is <strong>{int(time.time())}</strong>",
is_html=True
)
async def on_raise_command(ctx: EventContext, vars: ContextVars) -> None:
"""!raise"""
await ctx.bot.send.text(
ctx.room,
"<strong>Executing <code>raise RuntimeError()</code>...</strong>",
is_html=True
)
raise RuntimeError()
async def on_assert_command(ctx: EventContext, vars: ContextVars) -> None:
"""!assert"""
await ctx.bot.send.text(
ctx.room,
"<strong>Executing <code>assert False</code>...</strong>",
is_html=True
)
assert False
async def on_mul_command(ctx: EventContext, vars: ContextVars) -> None:
"""!mul"""
try:
numbers = [float(v) for v in vars[CTX_COMMAND].args]
v = numbers[0]
for n in numbers[1:]:
v *= n
response = " * ".join(html.escape("%.2f" % n) for n in numbers)
response += f" = <strong>{html.escape(str(v))}<strong>"
await ctx.bot.send.text(
ctx.room,
response,
is_html=True
)
except Exception as e:
await ctx.bot.send.text(ctx.room, f"Could not process the command: {e}")
async def on_args_command(ctx: EventContext, vars: ContextVars) -> None:
"""!args"""
try:
response = (
f"Prefix: <code>{vars[CTX_COMMAND].prefix}</code><br>"
f"Verb: <code>{vars[CTX_COMMAND].verb}</code><br>"
f"Arguments: <code>{len(vars[CTX_COMMAND].args)}</code><br>"
f"Arguments are:<br><ol>"
)
for arg in vars[CTX_COMMAND].args:
response += f"<li><code>{html.escape(arg)}</code></li>"
response += "</ol>"
await ctx.bot.send.text(
ctx.room,
response,
is_html=True
)
except:
await ctx.bot.send.text(
ctx.room,
f"Could not process the command: {traceback.format_exc()}"
)
async def invalid_usage(ctx: EventContext, vars: ContextVars) -> None:
"""This callback is called when the bot used incorrectly."""
await ctx.bot.send.text(
ctx.room,
"Use <code>!help</code>",
is_html=True)
async def main() -> None:
"""Application entry point"""
logging.basicConfig(level=logging.INFO)
logging.getLogger("nio").setLevel(logging.CRITICAL+1)
check_environment()
config = MatrixBotConfig(
matrix_homeserver_url=os.environ["MAB_SERVER"],
matrix_username_localpart=os.environ["MAB_USER"],
storage_directory="session_storage"
)
bot = MatrixBot(config)
COMMANDS = {
on_help_command: BodyCommandFilter(["help", "?"]),
on_time_command: BodyCommandFilter("time"),
on_raise_command: BodyCommandFilter("raise"),
on_assert_command: BodyCommandFilter("assert"),
on_mul_command: BodyCommandFilter("mul", min_args=2),
on_args_command: BodyCommandFilter("args", min_args=1, max_args=5),
}
for callback, filter in COMMANDS.items():
f = ~SenderIsBotFilter() & filter
bot.add_callback(f, callback)
bot.add_callback(~SenderIsBotFilter() & NewMessageFilter(), invalid_usage)
# run until Ctrl+C
try:
await bot.run()
except asyncio.CancelledError:
pass
if __name__ == "__main__":
asyncio.run(main())