Code Monkey home page Code Monkey logo

factualcat-twitter-bot's Introduction

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

factualcat-twitter-bot's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.