pmauduit / slack-bot

a slack bot based on simple-slack-api
2 stars 0 forks source link

using picocli to parse commands sent to the bot ? #33

Open pmauduit opened 2 years ago

pmauduit commented 2 years ago

I'm not very comfortable with parsing the commands sent to the bot with regexps, maybe using a lib like picocli could be a better option for this: https://picocli.info/#_example_application

pmauduit commented 2 years ago
@Grab('info.picocli:picocli-groovy:4.6.3')
import picocli.CommandLine
import static picocli.CommandLine.*

class JiraIssueCmd {

    @Parameters(paramLabel = "command")
    def command

    @Parameters(paramLabel = "issue")
    def issueId
}

def jira = new JiraIssueCmd()
def parsed = new CommandLine(jira).parseArgs("!jira GEO-1234".split(" "))

println jira.issueId
println jira.command

returns:

GEO-1234
!jira

This looks promising.

pmauduit commented 2 years ago

Actually, it won't spend me doing a little bit of parsing:

  1. I have to determine which kind of listener is concerned ("!jira", "!odoo", ...)
  2. I need to figure out the subcommand ("issue", "worklog", ...) to give the expected class representing the command being sent by the user:
[...]
def jira = new JiraIssueCmd()
def msg = "!jira issue GEO-1234".split(" ")

if (msg[0] == "!jira") {
    def parsed = new CommandLine(jira).parseArgs(msg[1..-1] as String[])
}
// then if (msg[1] == "... whatever subcommand") ?

println msg[1..-1]

println jira.issueId
println jira.command

Then maybe having to fiddle with try/catches to properly handle errors.

although picocli is interesting, the formalism is IMHO too strict: I want to chat with my bot, if I would have wanted a "over slack shell interface", it would have sounded relevant.