DEV Community

Cover image for Unraveling the URL Enigma with Power Automate’s C# Plugin
Bala Madhusoodhanan
Bala Madhusoodhanan

Posted on

Unraveling the URL Enigma with Power Automate’s C# Plugin

Intro:

Emails often contain links that are valuable for various reasons. Power Automate by Microsoft is a tool that can automate many tasks, but it doesn’t have a built-in feature to Find all (as in excel) instance of a specific string. Felling in love with bring in your own c# code as a plugin helped to solve a small problem of extracting URL from an email body.

C# Code:

Piggy backing on the previous blog on the framework the only change is



using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

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 == "URLextract")
{
// Corrected method name
return await this.HandleURLextractOperation().ConfigureAwait(false);
}

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

}

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

// The structure of the message body of the incoming request looks like this:
// {
//   "urltext": "&lt;some text&gt;"
// }
    var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
    var contentAsJson = JObject.Parse(contentAsString);

    // Extract the input text from the request content
var inputText = (string)contentAsJson["urltext"];

// Call the ExtractUrls method to get the list of URLs
    var urls = await ExtractUrls(inputText);

    // Create a JSON object to hold the response content
    JObject output = new JObject
    {
        ["urls"] = JArray.FromObject(urls)
    };

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

// The ExtractUrls method provided by the user
public async Task<List<string>> ExtractUrls(string inputText)
{
// Define a regular expression to match URLs
var urlRegex = new Regex(@"\b(?:https?://|www.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);

    // Find matches in the input text
    var matches = urlRegex.Matches(inputText);

    // Create a list to hold the URLs
    var urls = new List&lt;string&gt;();

    foreach (Match match in matches)
    {
        // Add each URL to the list
        urls.Add(match.Value);
    }

    // Return the list of URLs
    return urls;
}
// Helper method to create JSON content
private StringContent CreateJsonContent(string jsonString)
{
    return new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json");
}
Enter fullscreen mode Exit fullscreen mode

}

Enter fullscreen mode Exit fullscreen mode




Magic:

demo

This JSON output organizes the URLs neatly, making it easy to process and utilize them in various applications.

Top comments (0)