How do I use capture groups in Regular Expressions in Bot Composer

778 views Asked by At

I am attempting to build a Microsoft Teams bot using the Bot Framework Composer. What I would like to do is create an integration with ServiceNow. The goal would be that if anyone posts a record number (ex. REQ0123456, INC0123456, KB0123456) into the group or direct chat (with the bot), the bot would look up that record and provide a card or a short summary of the record to the chat.

To avoid creating a completely separate intent for each record type, I was hoping to use RegEx to gather the match into 2 capture groups; one for the tbl_code and one for the number.

Here is the entry for the user input:

> add some example phrases to trigger this intent:
- look up {conversation.sn_record.tbl_code=REQ}{conversation.sn_record.number=0123456}
- lookup {conversation.sn_record.tbl_code=REQ}{conversation.sn_record.number=0123456}
- {conversation.sn_record.tbl_code=REQ}{conversation.sn_record.number=0123456}
- lu {conversation.sn_record.tbl_code=REQ}{conversation.sn_record.number=0123456}

> entity definitions:
@ regex  sn_record tbl_code, number = /([a-z]{2,4})([0-9]{7})/mi

The Issue I'm Having I don't know how to get the values back from the individual capture groups. I would like to have them separate so that I can determine which table needs to be queried. I could probably just use the entire match and the search API in ServiceNow for the whole record string, but I would still like to know how to use capture group values.

I'm currently using turn.recognized.text, but I don't think this is the best method for what I'm looking to do. This returns the entire regex match.

I'm very new to this framework, so please be gentle. :) Let me know if there is more information I can provide.

Thanks all.

Best Regards, Josh

1

There are 1 answers

1
JMorrisett On BEST ANSWER

I was able to figure this one out using the examples in the ToDosSample bot.

The answer was to use named capture groups and then add them to a dialog property to use in the corresponding dialog.

For reference here are the changes I had to make:

  • New Regex

    • (?<sn_record>(?<tbl_code>[a-z]{2,4})(?<numbers>[0-9]{7}))
  • New Dialog Properties

    • dialog.sn_record = @sn_record
    • dialog.sn_tbl_code = @tbl_code
    • dialog.sn_numbers = @numbers
  • New response

    • - Okay, looking up ${dialog.sn_tbl_code}${dialog.sn_numbers}