@FactualCat is a Twitter Bot that Tweets cat facts daily. Built using the manage Tweets endpoint in the Twitter API v2 and deployed with Google Cloud Functions and Cloud Scheduler. This bot parses cat facts from an API that provides cat facts, catfact.ninja, and Tweets them out.
There are two versions of this code sample in this repository:
- cat_fact_bot.py - The Python code sample for testing locally
- gcp_function.py - The version of the code currently deployed to Cloud Functions.
First, you will need to create a new account for your bot.
Before you can use the Twitter API v2, you will need a developer account. Once you have an developer account, you will need to create a Project in 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. There also is a helpful forum post explaining how to authenticate on behalf of a bot account.
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.
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. You can obtain these credentials in the developer portal, located in the “Keys and Tokens” page of the App inside your Project.
After configuring your Cloud Function, you will see a 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 can edit the main.py
file that appears in the code editor to match the code found in gcp_function.py. You also do the same for your requirements.txt file.
After setting up your Cloud Function, you can use the Cloud Scheduler to determine how often your bot will Tweet. For example, @FactualCat is currently Tweeting every 12 hours using the following notation:
0 */12 * * *
-
For general questions related to the API and features, please use the v2 section of our developer community forums.
-
If there's an bug or issue with the sample code itself, please create a new issue here on GitHub.
-
If you have questions about the future, check out these resources:
- Guide to the Future of the Twitter API
- Twitter API Roadmap
- Twitter Developer Feedback, where you can post and vote on ideas and feature requests
We welcome pull requests that add meaningful additions to these code samples, particularly for languages that are not yet represented here.
We feel that a welcoming community is important and we ask that you follow Twitter's Open Source Code of Conduct in all interactions with the community.
Copyright 2021 Twitter, Inc.
Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0