Create your bot account
First, you will need to create a new account for your bot. The account for your bot should be a unique handle that describes your bot’s purpose. You will also want to set your bot’s profile picture and background image. Additionally, you will want to set up the bio of your bot to say it’s a bot and who built it.
For @FactualCat, the bio is as follows:
A #TwitterBot that Tweets cat facts by @JessicaGarson
Authenticate on behalf of your bot
Before you can use the Twitter API v2, you will need a developer account. Once you have a developer account, you will need to create a Project in the developer portal. This tutorial uses v1.1 endpoints. To use v1.1 endpoints, you will need elevated access, which you can apply for from the developer portal.
Each Project contains an App, with which you can generate the credentials required to use the Twitter API. You can learn more about getting started with the Twitter API in the getting started section of our documentation. You can apply for access from your main handle and authenticate on behalf of your account.
To authenticate a new account, you can use a pin-based OAuth flow. A helpful forum post also explains how to authenticate on behalf of a bot account.
Step 1 - Get your OAuth token
You can use a REST Client such as Insomnia or Postman to make a POST request to the request token endpoint. For this endpoint, you will need to use OAuth 1.0a.
After you make the request you will get back a response that looks similar to the one below:
Step 2 - Create and go to the authenticate URL
Using the OAuth token you generated in the last step, you can create a URL that you can go to authorize your application. You should be signed in as your bot account when you visit this URL. You will start with the prefix of https://api.twitter.com/oauth/authenticate?oauth_token= and append it with the OAuth token from the previous step.
For this example, the URL you’d visit in your web browser would be similar to the one below but replaced with the credentials generated from the previous step. Authorizing on a browser will return a seven-digit pin as an OAuth verifier in the next step.
You can learn more about creating an authenticate URL in our documentation on the subject.
Step 3 - Getting your Access Token and Access Token Secret
You are now ready to get the Access Token and Access Token Secret for your bot account. Next, you will use the POST oauth/access_token endpoint. For this endpoint, you will need to use OAuth 1.0a.
You can now make a POST request to the following endpoint using the OAuth verifier you generated in the previous step and the OAuth token you used in the last step.
You should get back to a response that is similar to the following:
oauth_token you get back is the Access Token for your bot, and the
oauth_token_secret is the Access Token Secret for your bot.
Posting your first Tweet on behalf of your bot
Now that you’ve created your Access Token and Access Token Secret, you are now ready to start Tweeting on behalf of your bot. Like how you used OAuth 1.0 in a REST Client such as Insomnia or Postman in the previous steps, you can now do the same but be sure to replace the Access Token and Access Token Secret with the ones you just created. The Consumer Key and Consumer Secret should be the same as they were previously.
You can now make a POST request to the manage Tweets endpoint by entering the following URL into Insomnia or Postman:
In the body tab of your request, be sure to select the format as JSON and enter in the following payload:
The response should look similar to the following:
If you check your bot account, you will see that you have posted your first Tweet on behalf of your bot.
Setting up Google Cloud Functions
Now that you’ve posted your first Tweet on behalf of your bot, you are ready to start configuring your bot to run regularly. First, you will deploy and write your code in Google Cloud Functions and run it at a scheduled time that you determine with Google Cloud Scheduler.
You will first need to set up an environment for the Google Cloud Platform. After you have your environment set up, you can set up a Cloud Function. If you are using Google Cloud Platform for the first time, you will need to set up a credit card before creating an environment. To set up a Cloud Function, you can follow a similar process outlined in the Python quickstart for Cloud Functions. You can first set your cloud region based on your location and select your trigger as “Cloud Pub/Sub” with a new topic.
Creating your environment variables
To avoid directly adding your keys and tokens to your Cloud Function, you can create environment variables. For example, under the “Runtime, build, connections and security settings“ header, you can set up environment variables for your Consumer Key, Consumer Secret, Access Token, and Access Token Secret. Of course, you will want to set these values the same as what you used while posting the Tweet in the previous step.
Configuring your code
After configuring your Cloud Function, you will be taken to a code page to set your runtime, the programming language, and the version you are using. This example uses Python 3.9 as the runtime environment. You will also want to select the entry point as
Editing your main.py file
main.py file is where the main logic of the code lives. In this file, you will be parsing a cat fact for this bot from catfact.ninja and Tweeting it from the Twitter API.
First, you will need to import the following packages.
After you’ve imported your packages, you will want to write a few lines of code to get the environment variables you set while configuring your Cloud Function.
After you have the keys and tokens set for your Cloud Function, you can now call the catfact.ninja endpoint to grab a random cat fact.
You will also need to place the cat fact into a dictionary so that you can pass it as a JSON payload.
To make the request to the Twitter API, you can use the following function:
Now, you are ready to edit the
hello_pubsub function to have variables for your fact, payload, url, auth, and request.
You can check out the full version of the code. There is also a version available for testing locally on your machine. You will need to set up the environment variables for the local version to work.
Editing your requirements.txt file
requirements.txt file determines what packages you need to install and installs them in your environment for you.
If you go to the
requirements.txt file, you will want to edit it to match the following:
Deploying your function
Once you have your code set, you are now ready to press the button that says “Deploy” to deploy your function. You may want to check out the pricing structure for Cloud Functions.
Scheduling your Tweets with Google Cloud Scheduler
After setting up your Cloud Function, you can use the Cloud Scheduler to determine how often your bot will Tweet. You need to set up a job and how often it will run.
For example, @FactualCat is currently Tweeting every 12 hours using the following notation: