Build your own random Twitter Bot by Azure Logic Apps and Azure Table Storage

I’m Noriyuki TAKEI. In this post, I’ll show you how to build your own random Twitter Bot by Azure Logic Apps and Azure Table Storage.

What’s Azure Logic Apps?

Azure Logic Apps is a cloud service that allows you to build your own automatic tasks without coding.You can define automatic tasks like “Service B is executed after Service A is executed”. In this case, A is called “Trigger” and B is Called “Action”. Trigger and Action has many built-in services. For example, if you would like to build a task “Tweet a specific message on Twitter every AM 7:00”, “every AM 7:00” is a Trigger and “Tweet a specific message on Twitter” is an Action. Azure Logic Apps has many other Actions and Trigger, so you can do many simple tasks without coding!

What’s Azure Table Storage?

Azure Table Storage is a managed Key-Value Store provided by Azure. It’s simple and fast to create. Azure Table Storage dose not contain many convenient features like RDB(joining tables). However, it’s a very suitable service that meet the need I describe in this post.

System Architecture

This system I’ll describe in this post allows Twitter Bot to tweet a message everyday extracted randomly from a database. It’s very useful for posting regular information on Twitter.

In this post, I use Azure Table Storage to store messages for tweeting every day. When using Azure Table Storage, it does not need to define a RDB schema, so it’s suitable in this case.

I use Azure Logic Apps to tweet a message on Twitter. Azure Logic Apps has a built-in Action for tweeting a message on Twitter, so it’s fast to start!!

Twitter Bot generated by Azure Logic Apps is able to make a scheduled tweet stored in Azure Table Storage. I guess that many engineers build a Twitter Bot by Logic Apps, but using Azure Table Storage is rare.

Let’s try this!!

In this chapter, I’ll describe the detail how you build your own Twitter Bot by Logic Apps and Azure Table Storage.

Procedure to create Twitter Bot

The procedure is as follows.

  1. Add all tweets stored in Azure Table to an array
  2. Count all elements in the array and assign it to an variable.
  3. Generate random number from 0 to the size of the array – 1
  4. Get the element at the index(the random number generated above). This element means a message to tweet.
  5. Tweet the message

Create a Storage Account

To create a storage account in the Azure portal, follow these steps.

On the Azure portal menu, select All services. In the list of resources, type Storage Accounts. As you begin typing, the list filters based on your input. Select Storage Accounts.

 

Click “+ Create”.

 

On the Basics tab, select the subscription in which to create the storage account. Under the Resource group field, select your desired resource group, or create a new resource group. Next, enter a name for your storage account. Set the Account kind field to Storage V2 (general-purpose v2). Click “Review + create”.

 

Click “Create”.

 

Access the storage account you created above, and click “Tables”.

 

Click “+ Table”. Type “tweets” for your table in the Table name box, then click OK. Click “Storage Explore(preview)” in the left pain.

 

Click “Tables”, and then Table “tweets” you created above appears. Click “+ Add”.

 

You will see the window below. Type “tweet” in the Value Box of Partition Key, and “0001” in the Value Box of Row Key.It requires that Partition Key and Row Key are used to specify the primary key of a table. Add Property “tweet”, and type a message you would like to tweet by Twitter Bot. In the same way, add many records you would like to tweet randomly.

 

Create a Logic App

To create a Logic App in the Azure portal, follow these steps.

On the Azure portal menu, select All services. In the list of resources, type logic apps. As you begin typing, the list filters based on your input. Select Logic apps.

 

Click “+ Create”.

 

On the Basics tab, select the subscription in which to create Logic Apps. Under the Resource group field, select your desired resource group, or create a new resource group. Next, enter a name for your Logic Apps. Click “Review + create”.

 

Click “Create”.

 

Access the Logic Apps you created above, and then click “Blank Logic App”.

 

Click “Built-in” and then click “Schedule”.

 

Click “Recurrence”.

 

Configure the settings for tweeting at AM 7:30 every day.

  • Type “1” in the Interval box
  • Select “Day” in the Frequency box
  • Click “Add new parameter” and check “at these houes” and “at these minutes”

 

Select “7” in the “At these hours” box and type “30” in the “At these minutes” box. This setting will invoke Logic Apps at AM 7:00 every day. Click “+ New step”.

 

In the following steps, I will show you how to initialize the required variables. At first, initialize the variable to which you assign the entities extracted from Azure Table Storage. Click “Variables” in the Logic App designer.

 

Click “Initialize variable”.

 

Type “tweets” in the “Name” box. Select “Array” in the “Type” box.

 

Rename this step’s name to ‘Initialize the variable “tweets”‘. Click “…” at the upper-right corner of the step window, click “Rename” and type the name you would like to modify.

 

In the same way, initialize the following variables.

Step’s name
Initialize the variable “count”
Variable name
count
Variable type
Integer
Variable value
empty

 

Step’s name
Initialize the variable “randvalue”
Variable name
randvalue
Variable type
Integer
Variable value
empty

 

Step’s name
Initialize the variable “tweet”
Variable name
tweet
Variable type
String
Variable value
empty

 

To get the entities from Azure Table Storage, follow these steps.

Type “azure table storage” in the text box at the top of the step window. As you begin typing, the list filters based on your input. Click “Get entities”.

 

Enter an arbitrary name in “Connection name”, select the storage account you created above in “Storage Account” and click “Create”.

 

Select the table you created above in “Table”.

 

Convert the entities you got in the previous step to JSON. Click “+ New step”, type “json” in the text box at the top of the step window. As you begin typing, the list filters based on your input. Click “Parse JSON”.

 

Click “Content” > “Dynamic content” > “Get entities result”.

 

Type the following content in “Schema”.

{
    "type": "object",
    "properties": {
        "odata.metadata": {
            "type": "string"
        },
        "value": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "odata.etag": {
                        "type": "string"
                    },
                    "PartitionKey": {
                        "type": "string"
                    },
                    "RowKey": {
                        "type": "string"
                    },
                    "Timestamp": {
                        "type": "string"
                    },
                    "tweet": {
                        "type": "string"
                    }
                },
                "required": [
                    "odata.etag",
                    "PartitionKey",
                    "RowKey",
                    "Timestamp",
                    "tweet"
                ]
            }
        }
    }
}

 

Rename the step’s name to “parsejson”.

 

Assign the entities you got from Azure Table Storage to the array “tweets” initialized above.Click “Set variable”.

 

Select “tweets” in “Name”. Type the following expression in “Value” and click “OK”.

@body(‘parsejson’)[‘value’]

 

Rename the step’s name easily identifiable.

 

Get the number of tweets you got from Azure Table Storage and assign it to the variable “count”. Follow the next steps.

At first, click “Set variable”, select “count” in “Name” and type the following expression in “Expression”.

 

Rename the step’s name easily identifiable.

 

The array “tweets” defined above contains the entities you got from Azure Table Storage.Generate the index of this array randomly to tweet a message randomly.

For example, if 3 messages are stored in the Azure Table Storage, the array “tweets” has the indexes whose range is from 0 to 2.So the range of index is from 0 to the number of the entities stored in Azure Table Storage minus 1. However, when using rand function of Logic Apps, rand(0,3) generate random values whose range is 0 to 2. Therefor, select “randvalue” in “Name” and type the following expression in “Value”.

 

Rename the step’s name easily identifiable.

 

Assign a message for tweeting to the variable “tweet”. Use “Set Variable”. Select “tweet” in “Name” and type the following expression. Set the random value generated above to the index of the array “tweets” and get the associative arrays whose key is “tweet”. Thus, you can get the message you would like to tweet.

variables(‘tweets’)[variables(‘randvalue’)][‘tweet’]

 

Rename the step’s name easily identifiable.

 

Click “+ New step”, type “twitter” in the text box at the top of the step window. As you begin typing, the list filters based on your input. Click “Post a tweet”.

 

Type your connection name easily identifiable in “Connection name”. Select “Use default shred application” in “Authentication Type”. Click “Sign in”.

 

You will see the OAuth Login Screen. Type your Username and Password to login Twitter.

※ Sorry for in Japanese. I can’t get the screen capture in English.

 

 

Click “Add new parameter” and check “Tweet text”.

 

Click “Dynamic content” > “tweet”.

 

It’s ready to invoke Logic Apps!! Click “Run” to invoke Logic Apps.

 

The all steps are as follows.

ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

0人がこの投稿は役に立ったと言っています。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です