Logic apps provide a way to allow seamless integration of various SaaS services in your application. You can use Logic Apps to design workflows in the cloud which can automate business processes.
An example of automating business processes can be seen in social media management where you can monitor tweets and create alerts for them, like sending notification via email etc.
To integrate a SaaS service, we need a template. This can be a built-in template or you can design your own custom templates. There are several built-in connectors provided by Azure portal such as Twitter, Facebook, Office 365, DocumentDB etc.
Why do I need Azure Logic Apps?
Logic Apps provide speed and scalability for integration of various services. In addition to it, various triggering options and the ease of designing them makes the API simpler.
Logic Apps also allows you to connect legacy and cutting-edge systems together.
Here are some advantages of using Logic Apps.
1) Leveraging underlying azure capabilities: One of the underlying capability is that Logic Apps has auditing. You can track who has modified or deleted a resource.
It also includes a deployment lifecycle with resource manager. You don’t need to write custom scripts to deliver or host an app in azure. You can use the built-in capabilities in Visual Studio to deploy it as one unit.
2) Easy to Use: Logic apps have a no-code designer tool for rapid application creation. It provides a simple user interface , to do integration in the cloud and automate it, without writing code.
Logic Apps also allows you to consume custom and reusable APIs more easily using its built-in templates.
In addition, you have the option to use your own custom APIs. With this, you can build your own custom APIs and integrate them with Logic Apps which we will see shortly in our sample application.
3) Scalability: Developers need not worry about hosting, scalability, availability and management as Logic Apps will scale up automatically to meet demands.
You will be able to deliver apps at massive scale from having tens to millions of concurrent users at the same time.
4) Fast and agile development
5) Simplified Workflow definition: Logic Apps have only four main components:
- Parameters are objects that you want to reuse across workflows. Re-using values or even complex objects throughout the definition makes it easier to comprehend. Separating out configuration from the definition itself makes sharing easy as well as across different environments.
- Triggers are what start the Logic App. Triggers can be evaluated at recurring intervals or have its state maintained across executions.
- Actions can depend on other actions. The dependencies are what will determine the order of the Actions executing. State is also maintained across executions.
- Outputs are what gets sent from calls into workflow.
Building a sample application for logic apps and twitter integration using custom API
We will be building a sample application for logic apps which will have custom APIs to receive the tweets from twitter and then deliver the tweets via an email.
Following is the workflow which we will be using:
This workflow does the following:
1) We will be using a custom API which will get the latest tweets for a particular search field from Twitter.
2) Since the data received will be in form of a list, we need to parse the list using a built-in JSON parser. It will have tweetedBy and tweetedText as properties.
3) Once we get the above properties, we will use an email Action to send emails to configured email address.
Prerequisites
You need to have the following prerequisites before proceeding.
1. Visual Studio - If you don't have Visual Studio installed on your computer, you can download it from this URL. I am using VS 2015.
2. Windows Azure Subscription - you can also subscribe for a free trial of Windows Azure from this URL.
3. Twitter Account with Application Management configured.
Twitter Application Management provides Application registration & configuration for developers using the Twitter REST & Streaming APIs.
The reason behind configuring twitter Application management is we need to register our webapp to get twitter feeds. For that we need Consumer Key, Consumer Secret, Access Token, Access Token Secret etc.
Configuring Twitter Application Management:
1) You need to sign up for a twitter Account (URL) if you don’t have one.
2) Once you are done with the signup process, you need to configure Twitter Application Management account.
3) Sign in to Twitter Application Management using your twitter account credentials. You will see the following screen.
Click on the Create New App Button. The following screen will be displayed.
4) Enter all the application details as shown in the following image and click on the Create Button.
Once you are done with this, Keys and Access Token tabs will be visible as shown below where you will have access to Consumer Key, Consumer Secret, Access Token and Access Token Secret which are required in your API code to get the latest tweets.
Also Permissions Tab will be visible to make sure you update the Level of Access to Read, Write and Access direct Messages.
With this, we now have configured our Twitter Application Management stuff.
Next step is to create an API which will get the latest twitter feeds using the Consumer Key, Consumer Secret, Access Token and Access Token Secret we just generated.
Creating a custom API to get feeds from twitter
Create a simple ASP.Net MVC application using Visual Studio. Choose the template as WebAPI as shown in the following image.
Now create a new Get Method in ValuesController.cs file as shown below:
[HttpGet]
public IHttpActionResult GetData ()
{
var temp = objDetail.GetData();
var jsonSerialiser = new JavaScriptSerializer ();
var json = jsonSerialiser.Serialize(temp);
return Ok(temp);
}
The objDetail object in the above code has been instantiated using Dependency Injection.
The statement objDetail.GetData() calls GetData method which is defined in another Service project which contains the logic to get the twitter feeds as shown in below image.
To get the twitter feeds for a particular text, here is the code:
public class Detail: IDetail
{
public static string _consumerKey = "*******************";
public static string _consumerSecret = "*******************";
public static string _accessToken = "*******************";
public static string _accessTokenSecret = "*******************";
private SingleUserAuthorizer authorizer = new SingleUserAuthorizer
{
CredentialStore =
new SingleUserInMemoryCredentialStore
{
ConsumerKey = _consumerKey,
ConsumerSecret = _consumerSecret,
AccessToken = _accessToken,
AccessTokenSecret = _accessTokenSecret
}
};
public List<TwitterData> GetData ()
{
var searchTerm = "azureweekly";
var results = SearchTwitter(searchTerm);
return results;
}
public List<TwitterData> SearchTwitter (string searchTerm)
{
List<Status> statuslst = new List<Status> ();
List<TwitterData> lst = new List<Services.TwitterData> ();
var twitterContext = new TwitterContext(authorizer);
var srch =
Enumerable.SingleOrDefault((from search in
twitterContext.Search
where search.Type == SearchType.Search &&
search.Query == searchTerm &&
search.Count == 5
select search));
if (srch != null && srch.Statuses.Count > 0)
{
statuslst= srch.Statuses.ToList();
}
for (int i=0;i<statuslst.Count;i++)
{
TwitterData objData = new Services.TwitterData();
objData.Text = statuslst[i].Text;
objData.userName = statuslst[i].User.Name;
objData.LineBreak = Environment.NewLine;
lst.Add(objData);
}
return lst;
}
}
Once you are done with the code, you can publish your API to Azure as a webapp.
For publishing your API to Azure webapp you can refer to the following link.
Creating Logic App
Please follow the steps below to create a Logic App:
1) Go to Azure Management Portal and sign in with your Azure credentials.
2) You will see the home screen of Azure Portal.
3) Go to New > Web + Mobile > Logic App
4) Enter App Name, Subscription, select an existing resource group or create a new one, Location, App Service Plan and click create.
5) Your app will undergo deployment and once the deployment is completed, you will receive a notification.
6) Once the logic app is created, we need to design a workflow for it to get our result.
Click on the logic app you created, now click on templates and the following screen will be displayed.
7) Search for triggers. Since we want to use a custom API, search for HTTP trigger and select it from the list.
8) Once you select HTTP trigger action, enter the URI of Azure Hosted API as mentioned in Creating a custom API section. In our case, it is http://helloworldcustomapi.azurewebsites.net/api/Values/Getdata
Note: We have hosted the above url only for this demo, and it might not be available later. Please create your own.
As shown in the image below you need to set parameters mentioned in the HTTP Template.
- Type of Method: GET
- We will not be passing any parameters to the HEADER section since we don’t have an authenticated WebAPI.
- Also in the BODY field Request Content is empty since we are not passing any parameters to the webAPI.
- Frequency with which WebAPI will be triggered. We have set it to three minutes.
The next step is to add a JSON parser which will parse the entire output received from WebAPI since we need to extract two fields - TweetedBy and TweetedText to send an email.
9) To add a JSON parser, click on Add Action at the bottom of the HTTP template as shown below, search for the parser and select it.
10) Once you selected the parse JSON action, you will be seeing the following screen where you need to construct the schema based on the output received from your API.
Since the data I am getting is an array and it has properties like userName, Text and LineBreak (print on newline), my schema will be as shown below.
The Content field in Parse JSON template will be BODY i.e. Response Body received from the WebAPI.
11) The next step is to add an Email Template since our end goal is to trigger an email for whatever tweets are generated.
To add an Email Template, click on Add Action at bottom of the Parse JSON template, search for send Email and select it.
12) Once you selected the above template, the following screen will be displayed.
Note: The Foreach condition gets appended automatically at the top of the send email templates, since our output from JSON Parser will be an iterative one.
It will iterate each row of the JSON array, extract the properties needed and then will trigger an email.
Now the BODY part in the send email template will be comprising of fields generated from the JSON Parser.
userName, LineBreak and Text are the fields which we are using in BODY part of send email.
In the TO Section of Send Email, enter the email address of the person to whom you want to send these automated emails.
With this, we have completed our Logic App workflow which will be displayed as shown below:
Now close the workflow designer and it will navigate you to your logic app screen.
Click on the Run Trigger button app as shown in the following image to start receiving tweets in your mailbox!
If you want to stop your logic app, just click on the Disable button at the top as highlighted in above image.
Now check your emails to see the tweets.
Pricing of Logic Apps:
For pricing of Azure Logic App, please refer to the following link.
Conclusion
In this article, we saw why we need logic apps and some of its uses. We also learned how to create a logic app in the azure portal and create a workflow using templates, as well as to configure the custom api in logic apps.
The above logic apps tutorial can be useful in scenarios like Error Handling, Logging where an email will be triggered on occurrence of any exception or there can be a scenario where we have one SharePoint list and whenever a new list item is added, we need to insert a record in a storage and then send an email to the admin.
This article was technically reviewed by Vikram Pendse.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Pushkar is working as a Senior Software Engineer in a leading software company in (Pune) India. He is passionate about Microsoft Technologies especially Microsoft Azure and is also a passionate blogger.