【Mac/python3.8/English】Twitter Bot that Tweets Every Hour!

# Objective 

To create a twitter bot that tweets every certain period of time (In this project, every one hour) with <a href="https://docs.tweepy.org/en/stable/index.html">tweepy<a/>

# Version

    - python 3.8 
    - tweepy 4.4.0


# Get Developer Account for Twitter
To create developer account for twitter, go to <a href="https://developer.twitter.com/en">here<a/> 
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

![Screen Shot 2022-01-08 at 16.31.29.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1044781/09bffdf2-c428-1075-6623-88f2be494cac.png)

``` python
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 

```python
api.update_status("Hello, Twitter! This is my first tweet from tweepy!")
```

And check it out! 
![Screen Shot 2022-01-09 at 13.48.55.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1044781/57fd3e4c-d78c-cd6d-3ca0-31ab3de44581.png)

You can do pretty much everything with tweepy. For more information, visit <a href="https://docs.tweepy.org/en/stable/api.html">tweepy<a/>. 

# 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. <a href="https://www.taniarascia.com/setting-up-a-basic-cron-job-in-linux/">Crontab<a/> is a command-line utility that build-in to your Mac. First, you need to log in to your sudo.


```{terminal}
sudo su
```

Then, you can open crontab by typing following. 

```{terminal}
crontab -e
```
Once you open it, you see the crontab editor window that looks like this 
![Screen Shot 2022-01-09 at 12.11.14.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1044781/49c2828a-af05-55e9-7b47-3fe549ac177d.png)
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 <a href= "https://support.acquia.com/hc/en-us/articles/360004224494-Cron-time-string-format">acquia<a/> or <a href="https://atsashimipy.hatenablog.com/entry/2019/06/10/095048">Hatena Blog<a/> (Japanese)

```{termial}
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! 

:::note alert
[Errno 1] Operation not permitted 
=> Go <a href="https://yuki.world/mac-python-cron-operation-not-permitted/">YUKI.WORLD <a/>
:::
:::note alert
operation not permitted
=> Go <a href="https://osxdaily.com/2020/04/27/fix-cron-permissions-macos-full-disk-access/">OSXDaialy <a/>
:::
:::note alert
"/tmp/crontab.Tn1dwTH5oj":5: bad day-of-week
  crontab: errors in crontab file, can't install
=> Your formatting may not be right
:::
:::note alert
unauthorized 401 error.
=> You may need to set up your developer account properly. <a href="https://stackoverflow.com/questions/69563386/tweepy-unauthorized-401-error-when-retweeting">Stack overflow <a/>StackOverFlow
:::



You can still run the script, but only while your PC is awake, so set wake up program using <a href="https://www.dssw.co.uk/reference/pmset.html">pmset</a>. 

```{termial}
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 <a href="https://apple.stackexchange.com/questions/56635/schedule-multiple-wake-up-times-for-mac">page <a/> was helpful or <a href="https://qiita.com/sanmo3/items/b21942f58430310482e6">this <a/> (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

[^1]: On my mac, I did not need to put ".py" after the python file