Monitoring and sending notifications using Logic Apps
One of my colleagues, who works for a social grievance management project, is responsible for monitoring the problems that users post on social media platforms, including Facebook and Twitter. He was facing the problem of continuously monitoring the tweets posted on his customer's Twitter handle with specific hashtags. His main job was to respond quickly to the tweets by users with a huge follower count, say, users with more than 50,000 followers. Hence, he was looking for a solution that kept monitoring a particular hashtag and alerted him whenever a user with more than 50,000 followers tweets so that he can quickly have his team respond to that user.
Note
For the sake of simplicity, in this recipe, we will have the condition to check for 200 followers instead of 50,000 followers.
Before I knew about Azure Logic Apps, I thought it would take a few weeks to learn about, develop, test, and deploy such a solution. Obviously, it would take a good amount of time to learn, understand, and consume the Twitter (or any other social channel) API to get the required information and build an end-to-end solution that solves the problem.
Fortunately, after exploring Logic Apps and its out-of-the-box connectors, it hardly takes 10 minutes to design a solution for the problem that my friend had.
In this recipe, you'll learn how to design a logic app that integrates with Twitter (for monitoring tweets) and Gmail (for sending emails).
Getting ready
You need to have the following to work with this recipe:
- A valid Twitter account
- A valid Gmail account
When working with the recipe, you'll need to authorize Azure Logic Apps to access both Twitter and Gmail accounts.
The following are the logic app concepts that we'll be using in this recipe. They are the building blocks for developing logic apps:
- Connectors: Connectors are the wrappers around APIs that any system would provide to expose its features.
- Actions: Actions are steps in the Logic App workflow.
- Trigger: A trigger is the first step in any logic app, usually specifying the event that fires the trigger and starts running your logic app.
Learn more about them by referring to the following link: https://docs.microsoft.com/azure/connectors/apis-list
How to do it...
We'll go through the following steps:
- Creating a new logic app.
- Designing the logic app with Twitter and Gmail connectors.
- Testing the logic app by tweeting the tweets with the specific hashtag.
Creating a new logic app
Perform the following steps:
- Log in to the Azure portal, search for logic app, and select Logic App.
- In the Create logic app blade, once you have provided the Name, Resource group, Subscription, and Location information, click on the Create button to create the logic app:
Figure 3.8: Creating a new logic app
In this section, we have created a logic app. Let's now move on to the next section.
Designing the logic app with Twitter and Gmail connectors
In this section, we will design the logic app by adding the required connectors and trigger, and by performing the following steps:
- After the logic app has been created, navigate to Logic app designer and choose Blank logic app.
- Next, you will be prompted to choose connectors. In the connectors list, search for Twitter and click on the Twitter connecter. This will show you the list of triggers associated with the Twitter connector, as shown in Figure 3.9. It will prompt you to connect to Twitter by asking for Twitter account credentials:
Figure 3.9: Logic app—selecting the trigger
- Once you have clicked on the Twitter trigger, you will be prompted to provide Search text (for example, hashtags and keywords) and the Frequency at which you would like the logic app to poll the tweets. This is how it appears after you provide the details:
Figure 3.10: Logic app—providing the Twitter search text
- Let's now add a new condition by clicking on New step, searching for control, and clicking on it, as shown in Figure 3.11:
Figure 3.11: Logic app—searching for the control connector
- It will now open Actions. Select Condition, as shown in Figure 3.12:
Figure 3.12: Logic app—selecting a condition
- From the previous instruction, the following screen will be displayed, where you can choose the values for the condition and choose what you would like to add when the condition evaluates to true or false:
Figure 3.13: Logic app—selecting a condition—choosing a value
- When you click on the Choose a value input field, you will get all the parameters on which you could add a condition; in this case, you need to choose Followers count, as shown in Figure 3.14:
Figure 3.14: Logic app—selecting a condition—Followers count
- Once you have chosen the Followers count parameter, you need to create a condition (followers count is greater than or equal to 200), as shown in Figure 3.15:
Figure 3.15: Logic app—selecting a condition—completed condition
- In the If true section of the preceding Condition, click on the Add an Action button, search for the Gmail connector, and select Gmail | Send email, as shown in Figure 3.16:
Figure 3.16: Logic app—If true action—sending an email using Gmail
- It will ask you to log in if you haven't already done so. Provide your credentials and authorize Azure Logic Apps to access your Gmail account.
- Once you authorize, you can add parameters by clicking on the arrow, as shown in Figure 3.17, and selecting the Subject and Body checkboxes:
Figure 3.17: Logic app—If true action—configuring Gmail options
- Once you have selected the fields, you can frame your email with Add dynamic content with the Twitter parameters, as shown in Figure 3.18:
Figure 3.18: Logic app—If true action—configuring the subject and body
- Once you are done, click on the Save button.
In this section, you have learned how to create a trigger that is triggered whenever a tweet is posted and you have also created a condition that sends emails using the Gmail connector if the followers count exceeds a specific value. Let's now move on to the next section to learn how to test the functionality.
Testing the logic app by tweeting the tweets with the specific hashtag
In this section, you will learn how to test the logic app by performing the following steps:
- Post a tweet on Twitter with the hashtag #AzureFunctions, as shown in Figure 3.19:
Figure 3.19: A tweet regarding the #AzureFunctions hashtag in Twitter
- After three minutes, the logic app should have been triggered. Navigate to the Overview blade of the logic app and view Runs history:
Figure 3.20: Logic app—Runs history
- It triggered for me and I received the emails. One of them is shown in Figure 3.21:
Figure 3.21: Logic app—email received following a tweet
How it works...
We have created a new logic app and have chosen the Twitter connector to monitor the tweets posted with the hashtag #AzureFunctions at three-minute intervals. If there are any tweets with that hashtag, it checks whether the follower count is greater than or equal to 200. If the follower count meets the condition, then a new action is created with a new Gmail connector that is capable of sending an email with the dynamic content being framed using the Twitter connector parameters.
In this recipe, we have designed a logic app that gets triggered whenever a tweet is posted on Twitter. You have also learned that Logic Apps allows you to add basic logic (in this recipe, a condition) without writing code. If you have some complex functionality, then it would not be possible to implement using Logic Apps. In such cases, Azure Functions would come in handy. Let's now move on to the next recipe to learn how to integrate Logic Apps with Azure Functions.