Making Twitter Bot with raspberry Pi 3

In this post , I am making a Bot with Raspberry Pi . which can follow, tweet and you can make any other application with that bot.

for making this bot, I am using Twython application , the language I used is Python.


step 1  Create a twitter handle for bot using your email id. if you have already one go to step 2.



step 2.   Go to app.twitter.com and click on Create new app.

   

fill the details and submit.

step 3. after Creating App change the Access Level Read, write, and direct messages (modify app permissions)

step 4. Generate API key+Secret

step 5. generate consumer key+secret

the twitter part is over now we have to work on Raspberry pi to install Twython.

Open terminal on RPi and run the command

sudo apt-get update
sudo apt-get install python-setuptools
sudo easy_install pip
sudo pip install twython

after successfully running these commands now we have to make make a python script for a tweet.

Python code for Tweeting with bot
-------------------------------------------------------------------------
#!/usr/bin/env python
import sys
from twython import Twython

tweetStr = "hi friends i am bot "

# your twitter consumer and access information goes here
# note: these are garbage strings and won't work
apiKey = '********************************'
apiSecret = '*******************************************************'
accessToken = '**************************************************'
accessTokenSecret = '***************************************************'

twitter = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)

twitter.update_status(status=tweetStr)

print "Tweeted: " + tweetStr
--------------------------------------------------------------------------

API key,API secret,acess token, access token secret * should be replaced by your twitter keys.

to save this code use any name with .py extension(test.py)
now run this code using the command

python test.py                 // in my case use yours script name.


Python code for following users with bot
---------------------------------------------------------------------------
#!/usr/bin/env python
import sys
from twython import Twython,TwythonError



# your twitter consumer and access information goes here
# note: these are garbage strings and won't work

if len(sys.argv) >= 2:
    target = sys.argv[1]
else:
    target = raw_input("User to follow: ")
    # For Python 3.x use: target = input("User to follow: ")

apiKey = '********************************'
apiSecret = '*******************************************************'
accessToken = '**************************************************'
accessTokenSecret = '***************************************************'


twitter = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)

try:
    twitter.create_friendship(screen_name=target, follow="true")
except TwythonError as e:
    print(e)

---------------------------------------------------------------------------
API key,API secret,acess token, access token secret * should be replaced by your twitter keys.

to save this code use any name with .py extension(userfollow.py)
now run this code using the command

python test.py                 // in my case use yours script name.

I will add more scripts later.


Thank you.