Linking to an Azure Function

The output from a task run can be posted to an Azure function so that custom code can be run on the data. To create an Azure function and link your WebGet task to it, please follow the steps below.
  1. Create a free Azure account and log in.
  2. Click on Create a resource:
  3. Type "Function App" into the search, then click on Function App and then Create:
  4. Enter an App name and a Storage account name and click Create. The default values for the other settings will work fine. The creation process will take a few minutes.
  5. In the All resources panel, find the new function and click on it. You may need to click Refresh for it to show up in this list:
  6. Switch to version 1 of the functions (version 2 isn't working very well at the moment) by clicking on Program features and then Function app settings: Then click on ~1 in the Runtime version:
  7. Click on the Overview tab Then the New Function button.
  8. Click on In-portal and then the Continue button:
  9. Click on Webhook + API and then the Create button:
  10. Copy the code for "run.csx" below into the code editor and click Save.
  11. Click on View files: Then click on function.json:
  12. Copy the code for "function.json" below into the code editor and click Save.
  13. Test your code by clicking on the Test tab in the Azure portal. You will need to add the "webget-status" parameter and an example Request body. Click Save and run.
  14. When you are satisfied that the code is working correctly, click on the "Get function URL" link in Azure. Click Copy and then Paste this link into the Web Integration Url in WebGet. See Posting output to a web service for more information.

You can also write your Azure function inside the Visual Studio (free) environment. This requires a download however it can make writing code much easier. Instead of selecting In-Portal above you will need to select Visual Studio Then select Direct Publish Then follow the on-screen instructions. When you are finished you will need to "Publish" your function to Azure.

Code Samples


Below is a code sample of a WebGet task that gathers weather information and sends a warning email if there will be a frost in the morning. It sends email using the SendGrid app. You will need to sign up for a free SendGrid account and create an API key in order to use it. Alternatively you can remove the line of code that sends the email.

Code from the run.csx file
// Add these reference if you want to save data to Azure storage:
#r "Microsoft.WindowsAzure.Storage"
#r "Newtonsoft.Json"

// Add this reference if you want to send emails using the SendGrid app:
#r "SendGrid"

using System.Net;

// Add these statements if you want to save data to Azure storage:
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;

// Add these statements if you want to send emails using the SendGrid app.
using SendGrid;
using SendGrid.Helpers.Mail;

// Add the ", CloudBlockBlob data" parameter below if you want to save your Json data to Azure storage
public static async Task Run(HttpRequestMessage req, TraceWriter log, CloudBlockBlob dataFile)
{
    log.Info("C# HTTP trigger function processed a request.");

    // Exit if this run contains warning or errors
    string status = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "webget-status", true) == 0)
        .Value;
    if (status != "Successful") {
        log.Info("The run contains warnings or errors!");
        return req.CreateResponse(HttpStatusCode.BadRequest, "The run contains warnings or errors!");
    }

    // Read the json data from the body of the request
    JsonData data = await req.Content.ReadAsAsync();
    
    // Save the data to Azure storage
    dataFile.UploadText(JsonConvert.SerializeObject(data));

    // Send an email to me if there is a high frost risk for tomorrow morning
    if (data.FrostRisk == "High" || data.FrostRisk == "Extreme") {
       SendEmail("Frost Alert", "There is a high risk of a frost occurring tomorrow morning");
       log.Info("Frost alert email sent.");
    }

    // Return a successful response
    return req.CreateResponse(HttpStatusCode.OK, "Successful");
}

// This structure must match the structure in the resulting Json data from your task run. Please 
// note that you will need to remove the spaces from your Extract step names for this code to work.
public class JsonData {
    public string Forecast;
    public int Min;
    public int Max;
    public string FrostRisk;
}

public static void SendEmail(string subject, string body) {
    string apiKey = "Your SendGrid api key";  // Put your SendGrid api key in here
    dynamic sg = new SendGridAPIClient(apiKey);
    Email from = new Email("from@example.com"); // Replace this address
    Email to = new Email("to@gmail.com"); // Replace this address
    Content content = new Content("text/plain", body);
    Mail mail = new Mail(from, subject, to, content);
    sg.client.mail.send.post(requestBody: mail.Get());
}

Code from the function.json file

Please note you will need to replace "webgetteststorage" with the name of the storage account you used when you created your function.

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "blob",
      "name": "dataFile",
      "path": "webgetteststorage/data.json",
      "connection": "AzureWebJobsStorage",
      "direction": "inout"
    }
  ]
}