Skip to content

xdevplatform/FactualCat-Twitter-Bot

Repository files navigation

FactualCat Twitter Bot

@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:

Steps to running this code sample

Step 1: Create your bot account

First, you will need to create a new account for your bot.

Step 2: Authenticate on behalf of 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.

Step 3: Setting up a Cloud Function

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.

Step 4: Scheduling your Tweets with Cloud Scheduler

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 * * *

Support

Contributing

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.

License

Copyright 2021 Twitter, Inc.

Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0

About

A Twitter bot example using the v2 manage Tweets functionality

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Languages