Skip to main content
This tutorial guides you through creating a Twitter agent that automatically posts tweets at regular intervals using the Galadriel framework.

Prerequisites

  • Python 3.10 or higher
  • A Twitter developer account with API keys
  • An OpenAI API key

Step 1: Set Up the Development Environment

  1. Create a virtual environment:
  2. Install Galadriel:

Step 2: Obtain Twitter API Keys

  1. Create a Twitter developer account at https://developer.x.com/.
  2. Create a new app and obtain the following credentials:
    • Consumer API Key
    • Consumer API Secret
    • Access Token
    • Access Token Secret Ensure that the user authentication is set up with write access.

Step 3: Set Up Environment Variables

  1. Create a file named .env in your project directory.
  2. Add the following environment variables to the .env file:
    Replace the placeholder values with your actual API keys and tokens. Optionally, to skip the actual Twitter posting and just print the results, add:

Step 4: Create the Twitter Agent

  1. Create a file named twitter_agent.py with the following code:
    Key components:
    • LiteLLMModel: Loads the OpenAI GPT-4o model.
    • TwitterPostTool: Enables posting tweets to Twitter.
    • CodeAgent: Creates the agent with the specified prompt and tools.
    • Cron: Triggers the agent to run every 3 hours.
    • AgentRuntime: Manages the execution of the agent.

Step 5: Run the Agent

Execute the script: The agent will now run automatically, generating and posting tweets every 3 hours. If DRY_RUN=true is set, it will print the generated tweets
to the console instead of posting them to Twitter.

Example using tweet from output client

Create a file named twitter_agent.py with the following code:

Conclusion

You have now created a fully autonomous Twitter agent using the Galadriel framework. This agent runs continuously, generating and posting
tweets without any manual intervention. You can further customize the agent by modifying the prompt, adding more tools, and adjusting the
posting interval.