reactbot icon indicating copy to clipboard operation
reactbot copied to clipboard

Use backreferences in variables

Open HarHarLinks opened this issue 1 year ago • 2 comments

Building on the nitter sample, it would be pretty neat if we could do this:

templates:
    issue:
        type: m.room.message
        content:
            msgtype: m.text
            body: ${{URL}}

default_flags:
- ignorecase

rules:
    github:
        matches:
        - \#(\d+)
        template: issue
        variables:
            URL: https://github.com/my/project/issue/${{1}}
    jira:
        matches:
        - [^#](\d+)
        template: issue
        variables:
            URL: https://jira.atlassian.com/browse/ABC-${{1}}

HarHarLinks avatar Feb 23 '24 22:02 HarHarLinks

Rule variables are meant to be static. Just rename the rule variable to url_prefix and move the reference from the variable to the template. You could even have the variable itself be a primitive template which is evaluated inside the template (you can have jinja2 in template variables or as the template content, e.g. https://github.com/maubot/reactbot/blob/master/samples/random-reaction.yaml#L6)

tulir avatar Feb 23 '24 23:02 tulir

Just rename the rule variable to url_prefix

right, that works for the given case. my example does not cover this, but as you have more complex cases (e.g. URLs aren't just prefixes anymore) or are made up of multiple parts, I'm back to code/yaml duplication, no?

HarHarLinks avatar Feb 23 '24 23:02 HarHarLinks