Automating Your Kentico Cloud Content with Azure Logic Apps
The world is becoming more automated. From cars to coffee makers, systems are becoming more dynamic as developers leverage integrations between applications and platforms. When it comes to publishing content, any improvements that save an editor time is a huge step in the right direction. Luckily, teaming Kentico Cloud up with Azure Logic Apps can bring you a ton of automation, with only a little custom code.
Bryan SoltisPublished on Dec 7, 2017
Just about everyone can benefit from a little automation in their lives. Whether it’s keeping multiple systems in sync, or alerting you to a sale on that trip to Siberia (hey, maybe somebody actually is looking for that), producing and delivering content without human interaction can bring some great benefits.
As more companies adopt this machine-driven approach to their content, the integrations can get quite complex as developers try to understand how the puzzle pieces fit together.
When it comes to Kentico Cloud and Azure, that challenge gets a lot easier. Thanks to the new webhook support, Kentico Cloud content updates can be monitored by a number of systems. Combine that with Microsoft Azure Logic Apps, and you get a powerful automation solution.
In this blog, I’ll show you how you can leverage Kentico Cloud and Azure to automate your Twitter feeds. You can find out more about Azure Logic apps here.
Define the Process
Before you start any automation project, you need to define the process. Once you understand WHAT you want to do, you can start deciding HOW you want it to happen. In my example, I wanted to automate my Twitter posts, based on my Kentico Cloud content updates. Here’s a basic chart of the steps.
As you can see, I plan on using Azure Logic Apps and Azure Functions as part of my process.
Creating the Kentico Cloud Content
The next step of the process is to have some content to automate. In my example, I’m going to create a basic Article content model in Kentico Cloud. My demo model has a title and some customized Tweet text. In reality, the model would have many more elements for the body, header images, and other related properties.
Creating an Azure Function
With the content in place, I was ready to add my automation functionality. While the Azure Logic App would handle most of the process, I knew I would need to leverage the Kentico Cloud API to retrieve information. I chose an Azure Function for the job, mainly because I already had most of the code written from a previous blog. And the best part? This is the only custom code I would need!
In the function, I queried the Kentico Cloud API for the specified content item and returned the tweet text value. For the function itself, a simple HTTP Request would do, as it would be called directly from my Azure Logic App.
Here is the full code of the function.
#r "Newtonsoft.Json"
using System;
using System.Net;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using KenticoCloud.Delivery;
using System.Configuration;
using System.Security.Cryptography;
private static string strTweet = "";
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
// Get request body
var codename = req.Content.ReadAsStringAsync().Result;
DeliveryClient client = new DeliveryClient("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
// Get the details from Kentico Cloud
DeliveryItemResponse response = await client.GetItemAsync(codename);
if(response != null)
{
var item = response.Item;
strTweet = item.GetString("tweet_text");
log.Info(strTweet);
}
return strTweet == ""
? req.CreateResponse(HttpStatusCode.BadRequest, "No body posted")
: req.CreateResponse(HttpStatusCode.OK, strTweet);
}
Creating an Azure Logic App
At this point, I was ready to create my Azure Logic App. In the Azure portal, I selected Logic App from the Marketplace to create the new app.
Defining the Workflow
Once the app was created, I selected the Logic App Designer to create the process. This tool allows you to quickly add actions and conditions, defining parameters as you go. I architected the process to match my design, selecting the values and systems as I went.
I started with an initial action of an HTTP Request. This will be initiated when Kentico Cloud fires the webhook on a content update. The HTTP Request accepts JSON being posted in, in this case the Kentico Cloud Webhook data.
You can use the Use sample payload to generate schema option to generate the correct structure. I used the Kentico Cloud Webhook documentation for a sample notification payload. This allowed me to quickly generate the schema the Logic App would be receiving.
Once the Logic App understood the schema of the data, I could pull dynamic values as part of my workflow. Because the webhook notification contained a collection of items updated, I was able to loop through each item to process them individually.
For each content item, I wanted to evaluate some values before posting. Specifically, I wanted to check if the content was being published. I selected the operation property from the collection and added a basic condition.
If the content was Published, I then wanted to check if it was an Article. I added another condition to check the type value.
NOTE
You may need to do some trial and error with the properties to make sure the value you are evaluating is the correct one.
After evaluating the operation and type, I was finally ready to add some integration. I added a new action to call my Azure Function. I selected my function from the list, and set the Request Body to the codename value. This would pass the content item’s codename to my Azure Function. This is the value I used to query the Kentico Cloud API to get the tweet text.
The last step of the process was to post the text to Twitter. Azure Logic Apps have a lot of built-in integrations, so I selected Post a tweet from the list and entered my credentials. Because my Azure Function returned the tweet text in the response body, I selected the Body parameter for the Tweet text.
Adding the Webhook
The last step of the process was to add the Kentico Cloud Webhook. In my Azure Logic App, I copied the HTTP POST URL from the HTTP Request. In Kentico Cloud, I created a new webhook and added the URL
Testing the Process
To test the process, I published an article.
In the Azure Portal, I selected Logic App / Overview to view the recent executions. I confirmed the Run history and Trigger History displayed the execution.
Selecting the record showed the details of the exaction. I confirmed all the steps completed.
I viewed the actual data passed to the Logic App by expanding the Http Request action step.
I then confirmed the tweet was successfully posted to Twitter.
The last test was to confirm the Logic App handled an unpublish event properly. In Kentico Cloud, I unpublished the article.
In the Logic App / Overview tab, I selected the new notification and verified the execution details. In this case, the Type Check step failed because the article did not have a publish operation.
Moving Forward
Azure Logic Apps are an extremely powerful tool you can use to automate processes and systems. There are built-in integrations with several external applications and platforms, which allow you to connect your applications with little to no code. Combined with Kentico Cloud’s webhook support, Azure Logic Apps can help you create dynamic applications and implement some awesome automation processes. Good luck!