A Go library and HTTP server for interacting with Twitter/X using the github.com/imperatrona/twitter-scraper package. The service also includes a PostgreSQL database for storing and searching tweets.
The library supports managing multiple Twitter accounts with cookie persistence. Here's how it works:
-
Create an
accounts.jsonfile in the XGO path (default:$HOME/x-go) with your Twitter accounts:[ { "username": "your_twitter_username", "password": "your_twitter_password" } ] -
When the server starts:
- It checks for existing cookies in the
cookiesdirectory - If cookies exist, it tries to use them for authentication
- If cookies are invalid or don't exist, it logs in using the credentials from
accounts.json - After successful login, it saves the cookies to
cookies/{username}.json
- It checks for existing cookies in the
XGO_PATH: Path to the X-Go directory (default:$HOME/x-go)
Create a config.yaml file in the root directory with the following structure:
usernames:
- username1
- username2
# Add more usernames to track
postgres_url: "postgres://username:password@localhost:5432/dbname"
getmoni_api_key: "your_getmoni_api_key" # Required for GetMoni API integrationBefore running the server for the first time or after making changes to the database schema, run the migration command:
go run cmd/migrate/main.goThis will:
- Create the necessary database tables if they don't exist
- Insert usernames from config.yaml into the users table
- Set up indexes and constraints
The service integrates with the GetMoni API for additional functionality. To use this feature:
- Obtain an API key from GetMoni
- Add the
getmoni_api_keyto yourconfig.yaml
GET /api/user/{username}/tweets- Get user tweetsGET /api/user/{username}/profile- Get user profileGET /api/tweet/{id}- Get tweet by IDGET /api/search/tweets- Search tweets in database- Query parameters:
q(required) - Search querysort_by(optional) - Sort by "timestamp", "likes", or "views"limit(optional) - Number of tweets to return (default: 50)
- Query parameters:
GET /api/search?q={query}- Search tweetsPOST /api/follow/{id}- Follow userPOST /api/unfollow/{id}- Unfollow userPOST /api/tweet- Create tweetPOST /api/tweet/{id}/like- Like tweetPOST /api/tweet/{id}/unlike- Unlike tweetPOST /api/tweet/{id}/retweet- Retweet
The service runs two background tasks:
- Profile Updates: Updates user profiles every 10 seconds
- Tweet Updates: Fetches 20 tweets per user every 6 hours
The project implements a Multi-Agent Communication Protocol (MCP) server that provides programmatic access to Twitter functionality through standardized agent communication.
- Manages multiple Twitter agents with session persistence
- Provides tool-based interaction with Twitter API
- Supports middleware for request handling
- Includes logging and recovery capabilities
XGO_PATH: Path to the X-Go directory (default:$HOME/x-go) - Required for agent management and cookie storage
- Ensure
XGO_PATHenvironment variable is set - Configure your Twitter accounts in
accounts.json - Run the MCP server:
go run main.go
The server will start and handle MCP protocol communication through stdin/stdout.
This project supports two server modes: HTTP API server and MCP server.
-
Build the HTTP server:
go build -o x-go-http cmd/httpserver/main.go
-
Build the MCP server:
go build -o x-go-mcp main.go
-
Build the migration tool:
go build -o x-go-migrate cmd/migrate/main.go
- Copy
accounts.json.exampletoaccounts.jsonand add your Twitter accounts - Create
config.yamlwith your database configuration and usernames to track - Run database migrations:
./x-go-migrate
- Set environment variables:
export XGO_PATH=$HOME/x-go
- Run the server:
./x-go-http
The HTTP server will start on port 8080.
- Ensure
accounts.jsonis configured with your Twitter accounts - Set environment variables:
export XGO_PATH=$HOME/x-go
- Run the server:
./x-go-mcp
The MCP server will handle communication through stdin/stdout using the MCP protocol.
You can also run the servers using Docker. Both servers use a Docker volume to persist data and configurations.
-
Build HTTP server image:
docker build -f Dockerfile.http -t x-go-http . -
Build MCP server image:
docker build -f Dockerfile.mcp -t x-go-mcp . -
Create a Docker volume for data persistence (optional):
docker volume create x-go-data
-
Run database migrations:
docker run --rm \ -v x-go-data:/x-go \ x-go-http ./x-go-migrate
-
Run HTTP server container:
# Using a named volume docker run -p 8080:8080 \ -v x-go-data:/x-go \ x-go-http # OR using a local directory docker run -p 8080:8080 \ -v $HOME/x-go:/x-go \ x-go-http
-
Run MCP server container:
# Using a named volume docker run -i \ -v x-go-data:/x-go \ x-go-mcp # OR using a local directory docker run -i \ -v $HOME/x-go:/x-go \ x-go-mcp
The volume at /x-go contains:
accounts.json: Twitter account credentialscookies/: Directory storing authentication cookiesconfig.yaml: Database configuration and usernames to track- Other persistent data generated by the application
Note:
- The
-vflag mounts the volume to persist data between container restarts - Using a named volume (
x-go-data) is recommended for production - Using a local directory mount is useful for development
- The same volume can be shared between HTTP and MCP servers if needed