There are many ways to implement Twitter Bot out there. The popular ones seem to be using Twython or Tweepy libraries with Python. However, these may not work out for you depending on the level of developer access Twitter grants you. Since I chose the free developer access, I had to find a way to work with Twitter API v2. After extensively googling for ideas, I found this site helpful and it turned out Twitter has a good tutorial.
Step 1. Create a Twitter "developer" account.
We need the twitter API to interact with twitter from Raspberry Pi, outside the twitter domain.
Sign in with your twitter account; I created a new google account, EcoPiBot
Go to twitter's developer platform and sign up for a free account.
You are asked to agree with twitter terms and provide the description for your purpose for the developer account.
Step 2. Create a Twitter APP.
We need the twitter API to interact with twitter from Raspberry Pi, outside the twitter domain.
Go to twitter's developer portal.
Create an app for twitter API; I named mine EcoPiBot.
Consumer Keys section provides API Key and Secret.
API Key is a jumbled user name that represents the app and API Secret is its password.
Save it in a secure location
Treat it like a password or a set of keys
If security has been compromised, regenerate it
DO NOT store it in public places or shared docs
Go to Projects & Apps menu, set up User Authentication Settings.
Authentication allows users to log in to your App with Twitter and your App to make requests for authenticated users.
Make the app permission to be "read and write"; the default is "read."
Step 3. Set up Raspberry Pi for Twitter Bot
Make a directory where you will keep your code and go to the directory
> mkdir EcoPiBot
> cd EcoPiBot
Test you are able to make connection to your twitter APP.
> curl --request GET 'https://api.twitter.com/2/tweets/search/recent?query=from:twitterdev' --header 'Authorization: Bearer REPLACE_THIS_WITH_YOUR_BEARER_TOKEN'
Twitter returned the following result:
{"client_id":"27103191","detail":"When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.","registration_url":"https://developer.twitter.com/en/docs/projects/overview","title":"Client Forbidden","required_enrollment":"Appropriate Level of API Access","reason":"client-not-enrolled","type":"https://api.twitter.com/2/problems/client-forbidden"}
My EcoPiBot APP ID is in fact 27103191, client_id, so I know authentication worked.
I decided to use create_tweet.py, one of the code samples from the GitHub from Twitter tutorial.
Open an nano editor to write your python script by giving any name you'd like.
> sudo nano create_tweet.py
Save the file and exit.
Ctrl + X, then Y, and then Enter.
I made a minor change to include my consumer_key and consumer_secret, which are the sequences of jumbled letters of API_Key and Secret.
Make the script executable and run!
> sudo chmod +x create_tweet.py
> sudo python create_tweet.py
Go to your tweeter APP page and check out the tweet!
Well, it turned out this code should be modified to skip the authentication prompt every single time since I want to automate the tweeting.