Ignorar Navegação

Subscribing to Tweets using Azure Logic Apps

por Gage Ames
Última atualização: 10/09/2018
Editar no GitHub

A tutorial for using Azure Logic Apps to monitor recent tweets on Twitter. The app sends an email notification to subscribers when new tweets are posted matching a search term.

Technologies Used

Prerequisites

  • An Azure subscription
  • An email account (Office 365 or Outlook for all features, Gmail if approval emails are not required)
  • A Twitter account
  • A web browser
  • Azure Storage Explorer (optional, but recommended for working with Azure Table Storage)

Create Azure resources

Logics Apps allow you to build and test your application entirely from the Azure Portal using the Logic Apps Designer. Let's get started by creating a few Logic Apps and a storage account.

Sign in to the Azure portal

  1. Go to the Azure portal and sign in with your Azure account credentials. > See Prerequisites for sign up links if you don't have an account

Create a Logic App to manage subscribers

  1. From the main Azure menu, choose Create a resource > Web > Logic App.
  2. Provide details about your Logic App as shown here. After you're done, choose Create.

    | Property | Value | Description | |--------------------- | Name | TwitterNotifier | The name for your Logic App | | Subscription | <your-Azure-subscription-name> | The name for your Azure subscription | | Resource group | Use existing > TwitterLogicAppSample | Use the same resource group as the Logic App | | Location | East US | The region where your Logic App is hosted | | Log Analytics | Off | Keep the Off setting for diagnostic logging. |

Create a Storage Account

  1. From the main Azure menu, choose Create a resource > Storage > Storage account - blob, file, table, queue.
  2. Provide details about your Storage Account as shown here. After you're done, choose Create.

    | Property | Value | Description | |--------------------- | Email Encoded | Get entities result An entity Partition Key | Choose this value from the Dynamic content tab | | Search Term Encoded | Get entities result An entity Row Key | Choose this value from the Dynamic content tab | | Email | uriComponentToString(item()['PartitionKey']) | Enter this value into the Expression tab of the Dynamic content dialog | | Search Term | uriComponentToString(item()['RowKey']) | Enter this value into the Expression tab of the Dynamic content dialog | | Last Tweet ID | item()['LastTweetId'] | Enter this value into the Expression tab of the Dynamic content dialog |

    > The `item()` portion of the expression is a function that returns the item that is in the current iteration of a repeating action. It may be helpful to think of this as the element at the current index of a loop over an array.
    

  3. Save the Logic App.

Create a loop to iterate through the subscription objects

  1. Choose + New step in the Logic Apps Designer.
  2. Under Choose an action, enter "for each" as your filter. From the actions list, choose the For each Control action.
  3. In the For each action, specify the output of the Select action as the value for the Select an output from previous steps box. Click within the box and choose Output under the Select step header of the Dynamic content tab.

  4. Save the Logic App.

Get recent tweets from the Twitter "Search tweets" API

Twitter offers an API that provides the most recent tweets matching a search term (word, phrase, or hashtag). We'll query that API for each subscribed search term within our Logic App. The API is available as a Microsoft Connector. A connector is a proxy or a wrapper around an API that allows the underlying service to talk to Microsoft Flow, PowerApps, and Logic Apps. You can read more about the Twitter Connector here. 1. Choose Add an action within the For each loop in the Logic Apps Designer. 1. Under Choose an action, enter "search tweets" as your filter. From the actions list, choose the Search tweets Twitter action. 1. If asked for credentials, sign in to your Twitter account so that Logic Apps can create a connection and call the Twitter API.

![](doc/images/twitter-notifier-add-twitter-connection-screenshot.png)
  1. In the Search tweets action, specify the properties of the API request.

    1. Click within the Search text box. The dynamic content dialog will appear. Switch to the Expression tab and enter item()['Search Term'] into the expression box. Click OK to save the expression.
    2. Click the Show advanced options link within the Search tweets action.
    3. If you wish, increase the Maximum results to 100.
    4. Click within the sinceId box. The dynamic content dialog will appear. Switch to the Expression tab and enter item()['Last Tweet ID'] into the expression box. Click OK to save the expression. > This will ask the Twitter API to only return tweets that were created after the specified tweet ID.

  2. Save the Logic App.

Check if there are new tweets matching the search term

We want to check if any new tweets matching the search term have been posted since the last time our Logic App executed. Since we asked the Twitter API to only return tweets since the last tweet ID our Logic App encountered, we can check the number of tweets returned by the API to determine if we should notify the subscriber of new tweets. 1. Choose Add an action within the For each loop in the Logic Apps Designer. 1. Under Choose an action, enter "condition" as your filter. From the actions list, choose the Condition Control action. 1. In the Condition action, populate the tree of conditions to evaluate. 1. Leave the And drop down as is. 1. Click inside the first Choose a value box. The dynamic content dialog will appear. Switch to the Expression tab and enter length(body('Search_tweets')) into the expression box. Click OK to save the expression. > This expression will return the number of tweets in the body of the Twitter API response. 1. Change the value of the is equal to condition to is greater than. 1. In the Choose a value box, enter 0.

![](doc/images/twitter-notifier-add-condition-screenshot.png)
  1. Save the Logic App.

Update the Last Tweet ID for the subscription in Azure Table Storage

  1. Choose Add an action within the If true block below the Condition in the Logic Apps Designer.
  2. Under Choose an action, enter "merge entity" as your filter. From the actions list, choose the Merge Entity Azure Table Storage action.
  3. In the Merge Entity action, specify the table entity to update.

    1. In the Table box, choose subscriptions from the drop down.
    2. Click inside the Partition Key box. The dynamic content dialog will appear. Switch to the Expression tab and enter item()['Email Encoded'] into the expression box. Click OK to save the expression.
    3. Click inside the Row Key box. The dynamic content dialog will appear. Switch to the Expression tab and enter item()['Search Term Encoded'] into the expression box. Click OK to save the expression.
    4. In the Entity box, enter the JSON template below. json { "LastTweetId": <id> }
      1. After populating the field, highlight the <id> portion of the field's value and delete it.
      2. With your cursor at the position where <id> used to be, click the Expression tab in the Dynamic content dialog.
      3. In the expression box, enter first(body('Search_tweets'))['TweetId'] and choose OK. This will update the ID of the most recent tweet in the table entity.
    5. In the ETag box, enter *. This will update the table entity regardless of whether it has changed since we queried it.

  4. Save the Logic App.

Send a notification email if there are new tweets matching the subscribed term

  1. Choose Add an action within the If true block below the Merge Entity action in the Logic Apps Designer.
  2. Under Choose an action, enter "send an email" as your filter. From the actions list, select the Send an email action for the email provider that you want.
    1. To filter the actions list to a specific app or service, you can select that app or service first:
      • For personal Microsoft accounts, select Outlook.com.
      • For Office 365 work or school accounts, select Office 365 Outlook.
      • For personal Google accounts, select Gmail.
  3. If asked for credentials, sign in to your email account so that Logic Apps can create a connection to your email account.
  4. In the Send an email action, specify the data that you want the email to include.

    1. Click inside the To box. The dynamic content dialog will appear. Switch to the Expression tab and enter item()['Email'] into the expression box. Click OK to save the expression.
    2. In the Subject box, enter the value <number> new tweets matching <term>.
      1. After populating the field, highlight the <number> portion of the field's value and delete it.
      2. With your cursor at the position where <number> used to be, click the Expression tab in the Dynamic content dialog.
      3. In the expression box, enter length(body('Search_tweets')) and choose OK. This will insert the number of new tweets into the subject at the correct position.
      4. Now highlight the <term> portion of the field's value and delete it.
      5. With your cursor at the position where <term> used to be, click the Expression tab in the Dynamic content dialog.
      6. In the expression box, enter item()['Search Term'] and choose OK. This will insert the subscribed search term into the subject at the correct position. > OPTIONAL To be grammatically correct, we should make the word "tweets" singular if the number of new tweets is 1. If you like, replace the word "tweets" with this expression: if(equals(length(body('Search_tweets')), 1), 'Tweet', 'Tweets').
    3. In the Body box, enter any value you like. If you don't want to include anything, just enter a space.
      1. If you would like to include the content of the most recent tweet, click within the Body box and enter any explanatory text along with the expression first(body('Search_tweets'))['TweetText'] via the dynamic content dialog. > If you would like to include other properties of the most recent tweet in the email body, refer to the list of property paths available in the TweetModel. Simply replace TweetText in the expression with one of the other property paths, such as TweetedBy.

  5. Save the Logic App.

Test out the TwitterNotifier Logic App

Congratulations! The Logic App should now be ready for use. Let's test it out! 1. Save any outstanding changes made to the Logic App using the Save button at the top of the Logic Apps Designer. 1. Close the Logic Apps Designer using the X in the top right corner of the blade. 1. If you previously disabled the Logic App while creating it, click the Enable button at the top of the Logic App overview blade. 1. Once the Logic App is enabled, choose Run Trigger > Recurrence at the top of the Logic App overview blade. > Alternatively, wait for the recurrence trigger to fire as scheduled. 1. Choose Refresh at the top of the Logic App overview blade. 1. Under the Run history section of the Logic App overview blade, find the most recent run of the Logic App. Click its row to view a visualization of the executed workflow.

![](doc/images/twitter-notifier-run-history-screenshot.png)