DEV Community

Cover image for Sorcerer’s Code: Spellbinding Regex match in Power Automate’s C# Plugin
Bala Madhusoodhanan
Bala Madhusoodhanan

Posted on

Sorcerer’s Code: Spellbinding Regex match in Power Automate’s C# Plugin

Intro:

Power Automate is a tool from Microsoft that helps people do their work automatically, like magic. It can do a lot of things without needing extra help. But when it comes to checking data to make sure it’s right, Power Automate doesn’t have a built-in way to do that using something called regex, which is like a secret code for finding patterns in text. That’s where the C# plugin, a kind of add-on, becomes very useful. We found a smart way to use this add-on to make Power Automate do regex checks. It’s like finding a hidden path in a maze that leads you to the prize. This blog will show you how we did this, making it possible to check data in a new way with Power Automate.

Setup:

Imagine you’re a sorcerer in training, and your first task is to create a magic spell that can tell if a secret code is correct or not. In our world, this is like setting up a special tool in Power Automate that doesn’t need a key to work. Using the custom connector framework we would create a connector "Spoof". It’s like a magic wand that uses a C# script, which is a set of instructions for the computer, to check if the words we use fit a certain pattern, known as regex. We prepare a magic scroll, or a JSON body, with two important parts: the regex pattern and the text we want to check. When we use this magic wand in our workflow, it will ask us for the secret code and the words. If they match, the spell works and it says ‘true’, like a green light. If not, it says ‘false’, like a red light. This way, we can easily check if our words are following the secret code without any hassle.

So for this I have used the below API as it doesnt need any API keys

https://cat-fact.herokuapp.com/facts/facts/random
Enter fullscreen mode Exit fullscreen mode

The operation ID would be leveraged in the cutome code

Image description

Add a body to the API post with 2 string. the first string is the content and the second is the regex pattern.

Image description

Enable the custom code and in my example the API request operation ID was "RegexIsMatch"

Image description

public class Script : ScriptBase
{
   public override async Task<HttpResponseMessage> ExecuteAsync()
{
    // Check if the operation ID matches what is specified in the OpenAPI definition of the connector
    if (this.Context.OperationId == "RegexIsMatch")
    {
        return await this.HandleRegexIsMatchOperation().ConfigureAwait(false);
    }

    // Handle an invalid operation ID
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
    response.Content = CreateJsonContent($"Unknown operation ID '{this.Context.OperationId}'");
    return response;
}

private async Task<HttpResponseMessage> HandleRegexIsMatchOperation()
{
    HttpResponseMessage response;

    // We assume the body of the incoming request looks like this:
    // {
    //   "textToCheck": "<some text>",
    //   "regex": "<some regex pattern>"
    // }
    var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);

    // Parse as JSON object
    var contentAsJson = JObject.Parse(contentAsString);

    // Get the value of text to check
    var textToCheck = (string)contentAsJson["textToCheck"];

    // Create a regex based on the request content
    var regexInput = (string)contentAsJson["regex"];
    var rx = new Regex(regexInput);

    JObject output = new JObject
    {
        ["textToCheck"] = textToCheck,
        ["isMatch"] = rx.IsMatch(textToCheck),
    };

    response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = CreateJsonContent(output.ToString());
    return response;
}
}

Enter fullscreen mode Exit fullscreen mode

Next, we set up a straightforward flow, like laying out the steps of a spell. It starts with an instant trigger, a bit like saying “Ready, set, go!” This trigger asks for two things: the regex pattern, which is like the secret code, and the text we want to check. We send this information to our custom connector, which acts like our magical assistant, and it carefully places the data where it needs to go. The connector then works its magic and gives us back an answer, which we keep safe in a compose variable, like storing the result in a potion bottle. With this flow ready, all that’s left is to test our magic and see it work!

Magic show

Magic

Image description

It’s amazing what you can achieve with a little creativity and the right tools. So go ahead, give it a try and share the magic you have created with the community

Reference:

Custom Connectors - Custom code

Top comments (0)