Skip to content

Commit b8b851e

Browse files
authored
Merge pull request #160 from game-by-virtuals/twitter-plugin/yang-update-docs
[Twitter Plugin] Improve Docs and Examples
2 parents 4a49630 + 5976435 commit b8b851e

File tree

6 files changed

+271
-161
lines changed

6 files changed

+271
-161
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ dist/
1111

1212
# JetBrains IDEs
1313
.idea/
14+
15+
.venv/

plugins/twitter/README.md

Lines changed: 122 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,153 @@
11
# Twitter Plugin for GAME SDK
22

3-
The Twitter plugin is a lightweight wrapper over commonly-used twitter API calls. It can be used as a executable on its own or by combining multiple of these into an executable.
3+
The **Twitter Plugin** provides a lightweight interface for integrating Twitter (X) functionality into your GAME SDK agents. Built on top of [`virtuals_tweepy`](https://pypi.org/project/virtuals-tweepy/) by the Virtuals team — a maintained fork of [`Tweepy`](https://pypi.org/project/tweepy/)) — this plugin lets you easily post tweets, fetch data, and execute workflows through agent logic.
4+
5+
Use it standalone or compose multiple Twitter actions as part of a larger agent job.
6+
7+
---
48

59
## Installation
610

7-
From this directory (`twitter`), run the installation:
11+
You can install the plugin using either `poetry` or `pip`:
812

913
```bash
14+
# Using Poetry (from the plugin directory)
1015
poetry install
1116
```
17+
or
18+
```bash
19+
# Using pip (recommended for integration projects)
20+
pip install twitter_plugin_gamesdk
21+
```
22+
23+
---
1224

13-
## Usage
25+
## Authentication Methods
1426

15-
The Twitter plugin can be initialized in one of two ways:
27+
We support two primary ways to authenticate:
1628

17-
1. Using GAME's X enterprise API credentials (higher rate limits)
29+
### 1. GAME's Sponsored X Enterprise Access Token (Recommended)
1830

19-
- To get the access token for this option, run the following command:
31+
Virtuals sponsors the community with a **Twitter Enterprise API access plan**, using OAuth 2.0 with PKCE. This provides:
2032

21-
```bash
22-
poetry run twitter-plugin-gamesdk auth -k <GAME_API_KEY>
23-
```
33+
- Higher rate limits: **35 calls / 5 minutes**
34+
- Smoother onboarding
35+
- Free usage via your `GAME_API_KEY`
2436

25-
You will see the following output:
37+
#### a. Get Your Access Token
2638

27-
```bash
28-
Waiting for authentication...
39+
Run the following command to authenticate using your `GAME_API_KEY`:
2940

30-
Visit the following URL to authenticate:
31-
https://x.com/i/oauth2/authorize?response_type=code&client_id=VVdyZ0t4WFFRMjBlMzVaczZyMzU6MTpjaQ&redirect_uri=http%3A%2F%2Flocalhost%3A8714%2Fcallback&state=866c82c0-e3f6-444e-a2de-e58bcc95f08b&code_challenge=K47t-0Mcl8B99ufyqmwJYZFB56fiXiZf7f3euQ4H2_0&code_challenge_method=s256&scope=tweet.read%20tweet.write%20users.read%20offline.access
32-
```
41+
```bash
42+
poetry run twitter-plugin-gamesdk auth -k <GAME_API_KEY>
43+
```
44+
45+
This will prompt:
46+
47+
```bash
48+
Waiting for authentication...
49+
50+
Visit the following URL to authenticate:
51+
https://x.com/i/oauth2/authorize?...
52+
53+
Authenticated! Here's your access token:
54+
apx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
55+
```
56+
57+
#### b. Store Your Access Token
58+
59+
We recommend storing environment variables in a `.env` file:
60+
61+
```
62+
# .env
63+
64+
GAME_TWITTER_ACCESS_TOKEN=apx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
65+
```
3366
34-
After authenticating, you will receive the following message:
67+
Then, use `load_dotenv()` to load them:
3568
36-
```bash
37-
Authenticated! Here's your access token:
38-
apx-<xxx>
39-
```
69+
```python
70+
import os
71+
from dotenv import load_dotenv
72+
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin
4073
41-
- Set the access token as an environment variable called `GAME_TWITTER_ACCESS_TOKEN` (e.g. using a `.bashrc` or a `.zshrc` file).
42-
- Import and initialize the plugin to use in your worker:
74+
load_dotenv()
4375
44-
```python
45-
import os
46-
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin
76+
options = {
77+
"credentials": {
78+
"game_twitter_access_token": os.environ.get("GAME_TWITTER_ACCESS_TOKEN")
79+
}
80+
}
4781
48-
# Define your options with the necessary credentials
49-
options = {
50-
"credentials": {
51-
"gameTwitterAccessToken": os.environ.get("GAME_TWITTER_ACCESS_TOKEN")
52-
},
53-
}
54-
# Initialize the TwitterPlugin with your options
55-
twitter_plugin = TwitterPlugin(options)
82+
twitter_plugin = TwitterPlugin(options)
83+
client = twitter_plugin.twitter_client
5684
57-
# Post a tweet
58-
post_tweet_fn = twitter_plugin.get_function('post_tweet')
59-
post_tweet_fn("Hello world!")
60-
```
85+
client.create_tweet(text="Tweeting with GAME Access Token!")
86+
```
87+
88+
---
89+
90+
### 2. Use Your Own Twitter Developer Credentials
91+
92+
Use this option if you need access to Twitter endpoints requiring a different auth level (e.g., **OAuth 1.0a User Context** or **OAuth 2.0 App Only**).
93+
94+
> See [X API Auth Mapping](https://docs.x.com/resources/fundamentals/authentication/guides/v2-authentication-mapping) to determine which auth level is required for specific endpoints.
95+
96+
#### a. Get Your Developer Credentials
97+
98+
1. Sign in to the [Twitter Developer Portal](https://developer.x.com/en/portal/dashboard).
99+
2. Create a project and app.
100+
3. Generate the following keys and store them in your `.env` file:
101+
102+
```
103+
# .env
104+
105+
TWITTER_API_KEY=...
106+
TWITTER_API_SECRET_KEY=...
107+
TWITTER_ACCESS_TOKEN=...
108+
TWITTER_ACCESS_TOKEN_SECRET=...
109+
```
110+
111+
#### b. Initialize the Plugin
112+
113+
```python
114+
import os
115+
from dotenv import load_dotenv
116+
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin
117+
118+
load_dotenv()
119+
120+
options = {
121+
"credentials": {
122+
"api_key": os.environ.get("TWITTER_API_KEY"),
123+
"api_key_secret": os.environ.get("TWITTER_API_SECRET_KEY"),
124+
"access_token": os.environ.get("TWITTER_ACCESS_TOKEN"),
125+
"access_token_secret": os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"),
126+
}
127+
}
128+
129+
twitter_plugin = TwitterPlugin(options)
130+
client = twitter_plugin.twitter_client
131+
132+
client.create_tweet(text="Tweeting with personal developer credentials!")
133+
```
61134
62-
2. Using your own X API credentials
135+
---
63136
64-
- If you don't already have one, create a X (twitter) account and navigate to the [developer portal](https://developer.x.com/en/portal/dashboard).
65-
- Create a project app, generate the following credentials and set them as environment variables (e.g. using a `.bashrc` or a `.zshrc` file):
66-
- `TWITTER_BEARER_TOKEN`
67-
- `TWITTER_API_KEY`
68-
- `TWITTER_API_SECRET_KEY`
69-
- `TWITTER_ACCESS_TOKEN`
70-
- `TWITTER_ACCESS_TOKEN_SECRET`
71-
- Import and initialize the plugin to use in your worker:
137+
## Examples
72138
73-
```python
74-
import os
75-
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin
139+
Explore the [`examples/`](./examples) directory for sample scripts demonstrating how to:
76140
77-
# Define your options with the necessary credentials
78-
options = {
79-
"credentials": {
80-
"bearerToken": os.environ.get("TWITTER_BEARER_TOKEN"),
81-
"apiKey": os.environ.get("TWITTER_API_KEY"),
82-
"apiSecretKey": os.environ.get("TWITTER_API_SECRET_KEY"),
83-
"accessToken": os.environ.get("TWITTER_ACCESS_TOKEN"),
84-
"accessTokenSecret": os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"),
85-
},
86-
}
87-
# Initialize the TwitterPlugin with your options
88-
twitter_plugin = TwitterPlugin(options)
141+
- Post tweets
142+
- Reply to mentions
143+
- Quote tweets
144+
- Fetch user timelines
145+
- And more!
89146
90-
# Post a tweet
91-
post_tweet_fn = twitter_plugin.twitter_client.create_tweet
92-
post_tweet_fn(text="Hello world! This is a test tweet from the Twitter Plugin!")
93-
```
147+
---
94148
95-
For detailed documentation on each function's parameters and usage, please refer to the [Tweepy Client Documentation](https://docs.tweepy.org/en/stable/client.html).
149+
## API Reference
96150
97-
Example usage:
151+
This plugin wraps [`virtuals_tweepy`](https://pypi.org/project/virtuals-tweepy/), which is API-compatible with [Tweepy’s client interface](https://docs.tweepy.org/en/stable/client.html). Refer to their docs for supported methods and parameters.
98152
99-
You can refer to the example files in the `examples` directory for more examples on how to call the twitter functions.
153+
---
-14.6 KB
Binary file not shown.
95.4 KB
Loading

0 commit comments

Comments
 (0)