Objective
To create a twitter bot that tweets every certain period of time (In this project, every one hour) with tweepy
Version
- python 3.8
- tweepy 4.4.0
Get Developer Account for Twitter
To create developer account for twitter, go to here
You may need to enable your e-mail and phone number for the Elevated account. You need to update your account from Basic plan to Elevated plan to manage your tweets. They ask to fill the application that you need to write 100 to 200 words sentence about the purpose of the account. But it's free!
Get API Keys and Connect
On the developer portal of the twitter, create new project and get
- consumer key
- consumer secret
- access token
- access token secret
You can get them by clicking the bottom shown below
import tweepy as tw
# save keys
consumer_key= 'CONSUMER_KEY'
consumer_secret= 'CONSUMER_SECRET'
access_token= 'ACCESS_TOKEN'
access_token_secret= 'ACCESS_TOKEN_SECRET'
# set authorization info
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)
"Hello, Twitter"
After you connect to Twitter API, it's ready to tweet!
Test your tweet by typing
api.update_status("Hello, Twitter! This is my first tweet from tweepy!")
You can do pretty much everything with tweepy. For more information, visit tweepy.
Scheduling
Now, your python script is ready to tweet whenever you run it. The next step is to schedule your Mac to run your script automatically. I used cronrab. Crontab is a command-line utility that build-in to your Mac. First, you need to log in to your sudo.
sudo su
Then, you can open crontab by typing following.
crontab -e
Once you open it, you see the crontab editor window that looks like this
By typing i, you can start editing.
I want to make bot that tweet every hour. So I type 1
For more crontab scheduling format, visit acquia or Hatena Blog (Japanese)
0 * * * * /opt/anaconda3/bin/python3 /<PATH_TO_SCRIPT>/twitter.py
Exit by hitting esc, and type :wq
Then, your twitter bot is ready to tweet every hour!
[Errno 1] Operation not permitted
=> Go YUKI.WORLD
operation not permitted
=> Go OSXDaialy
"/tmp/crontab.Tn1dwTH5oj":5: bad day-of-week
crontab: errors in crontab file, can't install
=> Your formatting may not be right
unauthorized 401 error.
=> You may need to set up your developer account properly. Stack overflow StackOverFlow
You can still run the script, but only while your PC is awake, so set wake up program using pmset.
0 10 * * * /opt/anaconda3/bin/python3 /<PATH_TO_SCRIPT>/twitter.py 1> /dev/null
1 10 * * * pmset repeat wakeorpoweron MTWRFSU 10:58:00
0 11 * * * /opt/anaconda3/bin/python3 /<PATH_TO_SCRIPT>/twitter.py 1> /dev/null
1 11 * * * pmset repeat wakeorpoweron MTWRFSU 12:58:00
0 12 * * * /opt/anaconda3/bin/python3 /<PATH_TO_SCRIPT>/twitter.py 1> /dev/null
1 12 * * * pmset repeat wakeorpoweron MTWRFSU 09:58:00
In the code above,
10:00 Run script
10:01 Schedule power on at 10:58
11:00 Run script
11:01 Schedule power on at 11:58
12:00 Run script
12:01 Schedule power on at 9:58 (Next day)
etc.
It's bit tricky until you get used to it, but it's not as complicated as it looks!
This page was helpful or this (Japanese)
Conclusion
Congratulations! You made your first Twitter bot!
References
- https://www.taniarascia.com/setting-up-a-basic-cron-job-in-linux/
- https://support.acquia.com/hc/en-us/articles/360004224494-Cron-time-string-format
- https://atsashimipy.hatenablog.com/entry/2019/06/10/095048
- https://yuki.world/mac-python-cron-operation-not-permitted/
- https://osxdaily.com/2020/04/27/fix-cron-permissions-macos-full-disk-access/
- https://stackoverflow.com/questions/69563386/tweepy-unauthorized-401-error-when-retweeting
-
On my mac, I did not need to put ".py" after the python file ↩