Bot for Twitter.

Let’s consider use case creating Twitter bot using tweepy library which will detect tweets containing certain keywords and reply users with text of your choice.

The main problem will be not implementing the code itself, but creating Twitter developers' account.

Twitter bot with Python.

The funniest thing that Twitter allows bots creation, has API with rich possibilities, but does not allow to state during account creation that you gonna use most of them. During developers' account setup you will be asked several questions about your application use. If you will state that your bot will read tweets and like, repost or reply to them you will get the verdict - It looks like your proposed use case may be in violation of one or more sections of the Developer Agreement and Policy, Automation Rules, and/or the Twitter Rules.

This is really strange as we all know Twitter full of bots doing this stuff. And what is really funny, Developer Policy starts with statement - Twitter loves developers. But I cannot confirm that !!! I was really shocked to know that only liking some tweets is considered as a spam by Twitter. Now it's really up to you if you wish to provide correct information during developers' account setup.

The code utself is pretty simple. First we import tweepy and proceed with authorisation procedure. Then we use the search method from Twitter API and run a query for the string “Data Science” and store it in the variable var. Next we create a list of specific strings we want to make our reply. Now using nested loops we iterate over list of Tweets, then over our list of strings and check if the Tweet corresponds to the string. And finally we send reply .

import tweepy

# it is recommended to keep keys and tokens in separate python file and then import them

CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)

var ="Data Science")

#list of specific strings we check for in Tweets
list_reply = ['Data Science',
    'DataScience!!!', ]

for s in var:
    for i in list_reply:
        if i == s.text:
            sn = s.user.screen_name
            m = "@%s Hi,Data Science bro!" % (sn)
            s = api.update_status(m,

See also related topics: