Fun with Twitter Bots

Please be sure to answer the question.

Provide details and share…stackoverflow.

comPlease make sure to not make your keys and tokens public, and also not make them part of your git repository if you intend to make the repo public.

Setting up Python scripts for automated retweetingHere I will walk through the steps for writing a simple script, that retweets (from my personal account) one recent public tweet based on keyword search every few minutes.

Install TweepyI used the easy to use tweepy library functions to get the public tweets, search these tweets, and then to retweet.

To install tweepy in a conda environment follow these steps:conda create -n twitterbotconda install -c conda-forge tweepy# To activate this environmentconda activate twitterbot# To deactivate this environmentconda deactivatePython script for running the bot on your local environmentNext, create a simple script toPrint the 20 recent tweets from my timeline, andSearch for tweets with a specific keyboard and then retweet one of these tweets from my timeline.

Here is the code I used to do this.

Please fill in your own app’s keys and tokens to run this, and remember not to publish your keys and tokens ????????????# Other Libsimport tweepy# Get the access keys and tokens from the Heroku environmentAPI_KEY = 'your_app's_consumer_api_key'API_SECRET_KEY = 'your_app's_consumer_api_secret_key'ACCESS_TOKEN = 'your_app's_access_token'ACCESS_TOKEN_SECRET = 'your_app's_access_token_secret'# This is the meat of the script that drives the twitterbotauth = tweepy.

OAuthHandler(API_KEY, API_SECRET_KEY)auth.

set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)api = tweepy.

API(auth)public_tweets = api.

home_timeline()for tweet in public_tweets: print(tweet.

text)search = "crispr"numberOfTweets = 1for tweet in tweepy.


search, search).

items(numberOfTweets): try: tweet.

retweet() print('Retweeted the tweet') except tweepy.

TweepError as e: print(e.

reason) except StopIteration: breakIf you are curious to know what “crispr” is, check this video by Jennifer Doudna, one of the inventors of this awesome technology:Now, you can run the above script locally on your computer or you can run it on a remote server.

There are several options to run your apps remotely, and these solutions are generally referred to as Platform as a Service (PaaS) solutions.

Good examples of this category are Heroku, Google app engine, and AWS Elastic Bean Stalk.

They provide a simplified framework to get your app up and running without you having to worry about setting up the infrastructure, operating system, and other middleware.

Running your app on HerokuI used Heroku here since I haven’t used it before, and so far has been quite an easy experience.

Heroku set upCreate a Heroku account and choose a free account.

Next, create an app and name it whatever you want.

Now you have a basic app ready to be deployed.

Heroku also comes with a command line interface (CLI) that you can use to connect to and control your Heroku app from your local computer.

The first thing you would want to do once you install the Heroku CLI is to log in using heroku login to log in using a browser or heroku login -i to log in using the command lineDeploy your app on HerokuNow that we have a new Heroku app, go to your Heroku app’s deployment menu.

Should look something like this below for my app which Heroku decided to name warm-atoll-45971 ????.????.????You will notice that there are several deployment methods for you to choose from.

I decided to go with Github instead of using Heroku’s integrated remote git option.

Note, the very last line in the above screenshot which indicates that the Heroku app automatically deploys from the master branch of my repo.

So, it is constantly monitoring my GitHub repo and making changes to the Heroku app as soon as it detects changes in my GitHub repo’s master branch.

Next, push the simple script we created before to GitHub, and also make sure to have a requirements doc (pip freeze > requirements.

txt) in your repo to let Heroku know what libraries to install to run your app remotely.

 ,Also, create another file named Procfile in your repo that lists commands to be executed by your Heroku app on startup.

You can find all the files I used in my repo.

From the Heroku article on Procfiles: “A Procfile declares its process types on individual lines, each with the following format:<process type>: <command><process type> is an alphanumeric name for your command, such as,webworker, urgentworker, clock, and so on.

<command> indicates the command that every dyno of the process type should execute on startup, such as rake jobs:work.

In my Procfile I used: worker: python bot.

py I tried using web: python bot.

py"I tried both worker: python bot.

py and web: python bot.


I only could get the worker option to work and not the web option in my first try.

Set up Heroku‘s environmentOne last thing needs to be done before you have your app running.

You need to let your Heroku app know about the API keys and access tokens for accessing your twitter app.

The best way to do this is to use Heroku’s environment variables.

Do not include your keys in your python script which is open to anyone who has access to your GitHub repo.

To add environment variables to your Heroku app select the settings app and choose the Config Vars option as shown below for my app:Now that we have all the pieces in place, start the Heroku app by runningheroku ps:scale worker=1 -a ‘your-heroku-app-name’.

This will start running your python script within your GitHub repo and start retweeting based on the keyword searches you specified within that script.

To stop the Heroku app anytime, run heroku ps:stop worker -a ‘warm-atoll-45971’.

That’s it!.I hope to add more to this simple app in the coming weeks.

Thanks for reading!https://www.



. More details

Leave a Reply