Twitter bot in python — How to retweet from multiple #hashtags

Piotr Krosniak
2 min readMar 21, 2018

A Twitterbot is a code that integrates with the Twitter API platform. You can automatically post, retweeting, like, or following other users. Just remember no Spamming, Twitter set daily limits also on API calls for free users so don’t be too aggressive with your script. This tutorial will walk you through how to create twitter bot that will find a specific hashtag, retweet the tweets.

Prerequisites

Although you can use a local computer to set up and run your BOT, if you would like it to be continuously running you can upload to the web server and run day and night.

You will need a twitter account of course and python installed on your computer. I recommend PyCharm as nice IDE for python with many plugins and add-ons. Also, you’ll need to create a Twitter app and install the Python Tweepy library. You should have your Consumer Key, Consumer Secret, Access Token, and Access Token Secret in hand before beginning this tutorial.

First created credentials.py file and paste your credentials from Twitter API site Next, you can start writing your code.

You can add multiple hashtags in one bot just ad OR in between the tags and single parenthesis around this query in line 11 below. Also you can change tweet.retweet() print(‘Retweeted the tweet’) to tweet.favorite() print(‘Like the tweet’) so your bot will like selected tweets instead of retweeting.

Just be aware of Twitter rules for this operations and read Twitter Automation Rules.

2. Posting automated mentions and replies
The reply and mention functions are intended to make communication between Twitter users easier. Automating these actions to reach many users on an unsolicited basis is an abuse of the feature, and is not permitted. For example, sending automated replies to Tweets based on keyword searches alone is not permitted. Spammy or duplicative use of mentions and replies may result in enforcement action, such as the removal of your Tweets from Search or the suspension of your app or account.

And finally combined code with both like and retweet options.

Originally published at piotrgisworks.blogspot.com.

--

--

Piotr Krosniak

DataScience and #GIS specialist. Love #triathlon #dataviz and #opensource tech. Dad of two work in UNICEF