mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-06-06 05:42:33 +00:00
simplex-chat-python: add BotCommand.params field (#7034)
This commit is contained in:
@@ -26,7 +26,15 @@ bot = Bot(
|
||||
profile=BotProfile(display_name="Squaring bot"),
|
||||
db=SqliteDb(file_prefix="./squaring_bot"),
|
||||
welcome="Send me a number, I'll square it.",
|
||||
commands=[BotCommand(keyword="help", label="Show help")],
|
||||
commands=[
|
||||
# `params=None` (default): the client SENDS `/help` immediately
|
||||
# when the user taps it in the commands menu.
|
||||
BotCommand(keyword="help", label="Show help"),
|
||||
# `params="<number>"`: the client PASTES `/square <number>`
|
||||
# into the input box and positions the cursor at the end. The
|
||||
# user replaces `<number>` with the actual number and sends.
|
||||
BotCommand(keyword="square", label="Square a number", params="<number>"),
|
||||
],
|
||||
)
|
||||
|
||||
NUMBER_RE = re.compile(r"^-?\d+(\.\d+)?$")
|
||||
@@ -48,5 +56,19 @@ async def help_cmd(msg: Message, _cmd: ParsedCommand) -> None:
|
||||
await msg.reply("Send a number, I'll square it.")
|
||||
|
||||
|
||||
@bot.on_command("square")
|
||||
async def square_cmd(msg: Message, cmd: ParsedCommand) -> None:
|
||||
"""Demonstrates the `params` flow: `cmd.args` is the trimmed text
|
||||
AFTER `/square`. When the user tapped the menu entry above, the
|
||||
client pasted `/square <number>` and the user replaced
|
||||
`<number>` with the actual value before sending."""
|
||||
try:
|
||||
n = float(cmd.args)
|
||||
except ValueError:
|
||||
await msg.reply(f"Usage: /square <number> (got {cmd.args!r})")
|
||||
return
|
||||
await msg.reply(f"{n} * {n} = {n * n}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
bot.run()
|
||||
|
||||
Reference in New Issue
Block a user