Build a Twitter bot with Runnerty in 5 min

Runnerty is an open source process orchestrator which allows us to build workflows establishing real dependencies between the processes.

In this tutorial we are going to build a twitter bot using Runnerty in less than 5 minutes. This bot is going to listen to the new followers of a twitter account and send a direct message to them.

I am assuming that you know how Runnerty works basically. If you do not know yet, I really encourage you to read its basics: https://medium.com/runnerty/introducing-runnerty-1284c3e5d1b1 .

Installing Runnerty and cloning the quick start repo.

First thing we have to do is to install runnerty.

npm i -g runnerty

Next step is to download the Runnerty quick start repo. This repo contains a sample runnerty chain with just one proccess:

# Clone this repository
git clone https://github.com/runnerty/runnerty-quick-start

# Go into the repository
cd runnerty-quick-start

# Install dependencies
npm install

Now we are going to work in this project adapting it to our purpose.

Installing and configuring the runnerty twitter trigger:

To start working with the trigger we are going to install it:

npm i @runnerty/trigger-twitter

Now, we only have to configure the trigger in our config.json. If we look at the sample configuration instructions, we only have to write our API key data. This will be our config.json:

"triggers": [
{
"id":"twitter_default",
"type":"@runnerty-trigger-twitter",
"consumerKey":"MyTwitterConsumerKey",
"consumerSecret":"MyTwitterconsumerSecret",
"accessToken":"MyTwitteraccessToken",
"accessTokenSecret":"MyTwitteraccessTokenSecret"
}
]

To get a twitter API key go to the website: https://apps.twitter.com and log-in with the account and click on “create new app”

Then, we have to fill the data and click on “create your Twitter application”

Finally, we can go to the “Keys and Access Tokens” of our app Settings and we will find there our API key access data:

Once we have our twitter credentials in the runnerty twitter trigger confurarion, we are ready to use it in our chain. We move to the plan.json file and we will see that it is currently using the scheduler trigger. We have to chage it by our twitter trigger. The trigger section of the plan have to look like this:

{
"chains":[
{
"id":"STREAM_NEW_FOLLOWERS",
"name":"Stream for new followers and send a DM to them",
"triggers":[
{
"id":"twitter_dafault",
"command":"follow"
}
],
"processes":[
]
}
]
}

As we can see, I have added the command property, this property tell the triggers to fire the chain when someone start to follow the twitter account. I also have changed the chain id and name for a logic name.

Installing and configuring the twitter executor:

Once we have our trigger configured we can start to plan the process. This process is going to send direct message to the new follower. So, we are going to need the Runnerty twitter executor:

npm i -g @runnerty/executor-twitter

As we did with the trigger, we have to configure it in our config.json file adding the configuration to our executors section and changing the credentials for ours:

"executors": [
{
"id":"twitter_default",
"type":"@runnerty-executor-twitter",
"consumerKey":"MyTwitterConsumerKey",
"consumerSecret":"MyTwitterconsumerSecret",
"accessToken":"MyTwitteraccessToken",
"accessTokenSecret":"MyTwitteraccessTokenSecret"
}
]

Now we are ready to cconfigure the process, going bat to the plan.json in the processes section, we are going to modify the existing process:

"processes": [
{
"id": "SEND_DM_TO_NEW_FOLLOER",
"name": "It sends a direct message to the new follower",
"exec": {
"id": "twitter_default",
"command":"direct",
"screen_name":"@GV(user_screen_name)",
"textToSend": "Hi there, thanx for following me."
},
"chain_action_on_fail": {
"action": "end"
}
}
]
  • I have removed the output section (This writes the output of the process in a file) we are not going to need it.
  • I have given a logic id and name to the process.
  • I have change the exec section adding the twitter executor and configuring the params to indicates the executor to send a DM, user id who may receive it and the message text.

As you can see, I am using an runnerty function to get the user_screen_name. It is good to claryfy how it works.

The “user_screem_name” came from the twitter trigger which shares the information of the new follower with the rest of the chain once it is fired. If you look at the npm package documentation you can see all the data that is shared with the chain:

user_id: number
user_id_str: number
user_name: string
user_screen_name: string
user_location: string
user_description: string
user_followers_count: number
user_friends_count: number
user_listed_count: number
user_favourites_count: number
user_statuses_count: number
user_lang: string
user_profile_background_image_url: string
user_profile_image_url: string
user_following: boolean
user_follow_request_sent: boolean
tweet_created_at: date
tweet_id: number
tweet_id_str: number
tweet_text: string
tweet_retweet_count: number
tweet_favorite_count: number
tweet_hashtags: object
tweet_data: object with all the data retreived by the Twitter API

You have probably noticed that I am using a @GV() function in the “screen_name” property. This is a native function from Runnerty that gets the value from a variable. Runnerty also provides several functions for transforming data, you can have a look at the official documentantion http://docs.runnerty.io/functions/

Now we have our chain ready for lunching. All that we have to do is run:

runnerty

Every time we hav a new follower in twitter the chain is fired and sends an email to the new follower. You can download the entire demo in this repo: https://github.com/Jhonsensf/runnerty-simple-twitter-bot