Us­ing Python to Au­to­mate Retweets

“By 2022, most Plat­form-as-a-Ser­vice (PaaS) of­fer­ings will evolve to a fun­da­men­tally server­less model, ren­der­ing the cloud plat­form ar­chi­tec­tures that are dom­i­nant in 2017 as legacy ar­chi­tec­tures.” - Gart­ner

OpenSource For You - - Developers - By: Ganesh Sa­marthyam and Srushith Repakula The au­thors work at CodeOps Tech­nolo­gies, which is a soft­ware tech­nol­ogy, con­sult­ing and train­ing com­pany based in Ben­galuru.

In the Septem­ber 2017 is­sue of OSFY, we de­mys­ti­fied server­less com­put­ing. We also dis­cussed its pros and cons, the tech­nolo­gies in­volved, use cases, the chal­lenges, etc. So how about us­ing server­less com­put­ing to sim­plify or au­to­mate your tasks? For ex­am­ple, wouldn’t it be nice to au­to­mat­i­cally retweet the tweets that have your favourite hash­tags (such as #server­less or #open­source). That’s the topic we’ll ex­plore in this ar­ti­cle. We are go­ing to im­ple­ment a bot (let’s call it Tweet­Bot), in which you can spec­ify the hash­tags to retweet at spec­i­fied time in­ter­vals.

We will start with writ­ing an auto-retweet­ing code in Python and get it work­ing on our ma­chine. Later, we will de­ploy it in OpenWhisk on IBM Bluemix. For this ar­ti­cle, we as­sume you al­ready know the fun­da­men­tals of Python pro­gram­ming. You need not know any­thing about OpenWhisk or Bluemix — we’ll in­tro­duce these to you in this ar­ti­cle.

De­vel­op­ing the Tweet­Bot

In or­der to write the Python code for auto-retweet­ing, the pre­req­ui­site is that Python 3 must be in­stalled in your ma­chine.

The Twit­ter API pro­vides pro­gram­matic ac­cess to read and write Twit­ter data, cre­ate a new tweet, read user pro­file and fol­lower data, retweet, and more. For this, you must first cre­ate a Twit­ter ap­pli­ca­tion (see https://apps.twit­ and note down the OAuth cre­den­tials for the bot con­fig­u­ra­tion.

The Twit­ter API in Python can be ac­cessed us­ing the Twit­terFol­lowBot Python mod­ule. Us­ing this mod­ule, you can do much more than just retweet – you can auto-fol­low and auto-like. For de­vel­op­ing the Twit­terBot, you must in­stall the mod­ule into a folder rather than the de­fault bin di­rec­tory. For that, use the fol­low­ing com­mand:

pip in­stall --tar­get <tar­get_­folder> Twit­terFol­lowBot

Let us now cre­ate a pro­gram that uses the Twit­terFol­lowBot Python mod­ule to retweet the latest ‘count’ num­ber of tweets with a spe­cific phrase (‘#Server­less’ in our case). For that, we need to cre­ate a Twit­terFol­lowBot in­stance. The Twit­terFol­lowBot uses the con­fig.txt file in the cur­rent di­rec­tory to con­fig­ure the bot.

So, let us first con­fig­ure the bot. To do so, you should cre­ate a con­fig.txt file and fill in the fol­low­ing in­for­ma­tion so that the bot can con­nect to the Twit­ter API. Here are the en­tries for the con­fig.txt file:



CONSUMER_KEY: the twit­ter app


TWITTER_HANDLE: <your twit­ter han­dle> ALREADY_FOLLOWED_FILE:al­ready-fol­lowed.txt


The files al­ready­fol­lowed.txt, fol­low­ers.txt and fol­low­ing. txt con­tain the re­spec­tive twit­ter IDs. You must cre­ate these files — you can leave them empty, though. The rest of the fields may also be left empty.

# Pro­gram to retweet five latest tweets based on a hashtag (#Server­less) from Twit­terFol­lowBot im­port Twit­terBot

def retweet():

# cre­ate an in­stance of the Twit­terFol­lowBot

# by de­fault, the bot will look for a con­fig­u­ra­tion

file called con­fig.txt

# in your cur­rent di­rec­tory my_bot = Twit­terBot()

# au­toretweets the 5(count) latest tweets that matches the hashtag­to_rt(“#Server­less”, count = 5) re­turn {‘mes­sage’ : ‘retweeted suc­cess­fully’}


That’s the short and sweet code! Let’s save it as and run it. The out­put is shown in Fig­ure 1.

When you ex­e­cute the Python script, it con­nects to the Twit­ter API and retweets. The re­sult throws up a warn­ing in­di­cat­ing that the fol­low­ers.txt isn’t up­dated; you can just ig­nore that warn­ing or up­date the files to get rid of the warn­ing. The pro­gram dis­plays the tweets that are retweeted, so we know it is work­ing.

After we get the code work­ing in our ma­chine, it is time to de­ploy and ex­e­cute it in the cloud us­ing the server­less ap­proach. We are go­ing to use Apache OpenWhisk as the server­less plat­form. We will use IBM Bluemix as the cloud plat­form to de­ploy the OpenWhisk ac­tion(s).

Apache OpenWhisk

Apache OpenWhisk is an open source server­less cloud plat­form that ex­e­cutes func­tions in re­sponse to events at any scale.

OpenWhisk was started by IBM and is now in­cu­bated by Apache. Adobe is an im­por­tant con­trib­u­tor to the project and has con­trib­uted the API Gate­way. OpenWhisk ex­e­cutes func­tions (called ac­tions) in re­sponse to events (called trig­gers). Since it is server­less, you just need to pro­vide your code to be ex­e­cuted; specif­i­cally, you don’t need to con­cern your­self with how to man­age the life cy­cle or op­er­a­tions of the un­der­ly­ing con­tain­ers that ex­e­cute the code.

You can run OpenWhisk on your lap­top, on-premise, or in the cloud. When run­ning in the cloud, you could use a Plat­form-as-a-Ser­vice (PaaS) ver­sion of the OpenWhisk pro­vided by IBM Bluemix or you can pro­vi­sion it your­self into In­fras­truc­ture-as-a-Ser­vice (IaaS) clouds, such as Bluemix, Ama­zon EC2, Mi­crosoft Azure and Google Cloud Plat­form. Here are some key as­pects of OpenWhisk:

It’s open source. If you want, you can ex­plore the code and tinker with it, and change it ac­cord­ing to your re­quire­ments. Sup­port for a wide range of pro­gram­ming lan­guages in­clud­ing Node.js 6, Python 3, PHP 7.1 and Swift 3.1.1 is avail­able.

Ac­tions (server­less func­tions) can also be cus­tom ex­e­cutable pro­grams pack­aged in a Docker con­tainer, i.e., you can run Docker images in OpenWhisk.

You can run it lo­cally! None of the ma­jor server­less plat­forms pro­vide this fea­ture. You can build and run ac­tions lo­cally on your own hard­ware be­hind your own fire­wall, and then de­ploy it to ex­e­cute in the cloud.

Tweet­Bot us­ing OpenWhisk

In or­der to use Tweet­Bot in a server­less ap­proach, you need to in­stall OpenWhisk in your ma­chine and have a BlueMix ac­count. For in­stalling OpenWhisk, re­fer to https://con­sole. for the set-up and con­fig­u­ra­tion in­for­ma­tion. The beauty of server­less tech­nol­ogy is that you don’t have to re­write your en­tire ap­pli­ca­tion; just tweak the plain code that runs in your ma­chine and you’ll be fine! Sur­pris­ingly, our Tweet­Bot re­quires only one change — it should have a main func­tion with an in­put pa­ram­e­ter (dic­tio­nary type) and is to be saved as a __­ file.

Here is the mod­i­fied Python code:

# Pro­gram to retweet five latest tweets based on a hashtag (#Server­less) from Twit­terFol­lowBot im­port Twit­terBot

def retweet(dict):

# cre­ate an in­stance of the Twit­terFol­lowBot

# by de­fault, the bot will look for a con­fig­u­ra­tion file called con­fig.txt

# in your cur­rent di­rec­tory my_bot = Twit­terBot()

# au­toretweets the 5(count) latest tweets that matches the hashtag­to_rt(“#Server­less”, count = 5)

def main(dict): retweet(dict) re­turn {‘mes­sage’ : ‘retweeted suc­cess­fully’}

Now, save this Python code in a file named __­ Cre­ate the con­fig.txt, al­ready­fol­lowed.txt, fol­low­ers.txt and fol­low­ing.txt (as ear­lier), and zip them all with the __­ file and the Twit­terFol­lowBot de­pen­dency mod­ule files.

In­vo­ca­tion process

Once the wsk CLI (com­mand line in­ter­face) is in­stalled and the zip file is ready, fol­low the steps given be­low.

Step 1. Cre­ate and up­date an ac­tion: Log in to the IBM Bluemix ac­count (https://con­ and cre­ate a new ac­tion. You can up­load the zip files with de­pen­den­cies only via the CLI. The syn­tax is:

wsk ac­tion cre­ate <ac­tion-name> --kind <lan­guage:ver­sion> <file_­name>

Sam­ple Com­mand: wsk ac­tion cre­ate tweet­Bot --kind python:3

Step 2. In­voke the func­tion (non-block­ing mode): The syn­tax is:

wsk ac­tion in­voke <ac­tion-name>

Sam­ple Com­mand: wsk ac­tion in­voke tweet­Bot

Step 3. Check for the re­sult: Since we in­voked the func­tion in a non-block­ing mode (be­cause we haven’t added the ‘--block­ing’ pa­ram­e­ter), the com­mand re­turned im­me­di­ately, but it is ex­e­cut­ing in the back­ground. The syn­tax is:

wsk ac­ti­va­tion re­sult <ac­tion ID>

Sam­ple Com­mand: wsk ac­ti­va­tion re­sult f4d­f0d1d­cb12488396978d2c­da832b09

Step 4. Check out the logs: The syn­tax is:

wsk ac­ti­va­tion logs <ac­tion ID>

Sam­ple Com­mand: wsk ac­ti­va­tion logs f4d­f0d1d­cb12488396978d2c­da832b09

Au­to­mate the in­vo­ca­tion

The mo­ment the ac­tion was in­voked, your Twit­ter ac­count would have retweeted the five latest tweets that have the hashtag ‘#Server­less’ in it. How­ever, this is still a man­ual in­vo­ca­tion. For max­i­mum im­pact, it would be bet­ter to

au­to­mate the in­vo­ca­tion process as well, so that you can con­fig­ure the ac­tion and for­get it once and for all.

A pe­ri­odic trig­ger would be the best op­tion. It trig­gers the ac­tion based on a spe­cific time, and will retweet the latest tweets with ‘#Server­less’ in it. One can ei­ther choose a pat­tern or write a cron ex­pres­sion.

The pat­tern shown in Fig­ure 7 will in­voke the Tweet­Bot ac­tion ev­ery week day (Monday to Friday) ev­ery six hours, keep­ing your Twit­ter ac­count live with­out your in­ter­ven­tion!

Words of cau­tion

Be­fore we end this ar­ti­cle, here are a cou­ple of things you should be care­ful about:

1. OpenWhisk is open source and free, but to de­ploy it, we use IBM Bluemix which isn’t free. The Tweet­Bot ac­tion will seam­lessly run in the free tier, but for any other ap­pli­ca­tion, please es­ti­mate the monthly costs. The pric­ing cal­cu­la­tor (https://con­ pric­ing) could be handy.

2. This ac­tion uses the Twit­ter API in the back­ground via the Twit­terFol­lowBot mod­ule. Mis­us­ing this may lead to the ban­ning of your Twit­ter app, so please make sure you clearly read the Twit­ter au­to­ma­tion rules (https://sup­port. twit­­ti­cles/76915).

In this ar­ti­cle, we dis­cussed a Tweet­Bot that can be writ­ten to au­to­mat­i­cally retweet the latest tweets with cer­tain given hash­tags. We wrote the ac­tions (server­less func­tions) in Python, and de­ployed them in OpenWhisk on IBM Bluemix. We also used trig­gers to au­to­mat­i­cally in­voke the ac­tions at spec­i­fied time in­ter­vals. With this in­tro­duc­tion, we in­vite you to fur­ther ex­plore OpenWhisk as well as other ex­cit­ing server­less tech­nolo­gies.


[1] Apache OpenWhisk:­cu­ba­tor. [2] IBM Bluemix Func­tions: https://con­ openwhisk/ https://dev.twit­­lic [3] Twit­ter API:

Fig­ure 3: Types of ac­tion in­vo­ca­tions

Fig­ure 1: Out­put for

Fig­ure 5: Re­sult of the com­mand

Fig­ure 4: In­vo­ca­tion process

Fig­ure 6: Types of trig­gers

Fig­ure 7: Pe­ri­odic trig­ger time set­tings

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.