finos / symphony-bdk-python

Symphony Python Bot Development Kit (BDK)
https://symphony-bdk-python.finos.org/
Apache License 2.0
31 stars 34 forks source link

Slash command args optional params and params with more than one string? #303

Closed dky closed 2 years ago

dky commented 2 years ago

Support Question

Hello Symphony Developers! Thanks for the work on: https://github.com/finos/symphony-bdk-python/pull/284/files

It was helpful for us to grab @mention users. However we ran into a few issues below.

Borrowing off:

 @activities.slash("/echo {@mention_argument}")
        async def on_echo_mention(context: CommandContext):
            mentioned_user = context.arguments.get_mention("mention_argument")
            message = f"Mentioned user: {mentioned_user.user_display_name}, id: {mentioned_user.user_id}"

We tried:

@activities.slash("/echo {@mention_argument} {a_second_arg")
        async def on_echo_mention(context: CommandContext):
            mentioned_user = context.arguments.get_mention("mention_argument")
            message = f"Mentioned user: {mentioned_user.user_display_name}, id: {mentioned_user.user_id}"

But it appears that if {a_second_arg} is not passed the message won't expand. That's fine and we can get around it by declaring two decorators I guess one with the arg and one without but is there a way to make {a_second_arg} here optional if not passed the message will still trigger?

The second issue we faced was passing an arg/string with spaces in it. That appears to not work at all. In the example above if I were to:

@bot /echo @someone How are you doing? 

This would completely fail, I'm guessing it's thinking that any string with a space is considered another arg it can't process but any way to allow args with spaces? If there's a cleaner workaround to this I'd love to hear it as well!

We need this because we typically take the full string "How are you doing?" and do something with that value and it would be annoying to tell users to have to quote the string.

Thank you for all the awesome work on this, it's made our Symphony experiences much more fun and pleasurable to use.

symphony-soufiane commented 2 years ago

Hello @dky, thank you for raising this issue. Regarding the first point, the parameters cannot be optional. A workaround would be to use different decorators with the same function. Here is an example:

@activities.slash("/echo {string_arg}")
@activities.slash("/echo")
async def on_echo_mention(context: CommandContext):
       argument = context.arguments.get_string("string_arg") if context.arguments else None
       message = f"Argument received: {argument}" if argument else "No Argument received"
       logging.info(message)

Regarding the second one, the white space is used by the input tokenizer to tokenise the message parts. Therefore "How are you doing" is split to 4 tokens. I don't see an efficient workaround but I encourage you to open a feature request issue and we can work on it.

For further documentation: https://github.com/finos/symphony-bdk-python/blob/main/docsrc/markdown/activity-api.md#slash-command-pattern-format