Skip to content

Commit 1418a1c

Browse files
committed
remove game_twitter_plugin,
redirect all too twitter_plugin adjust read me
1 parent a3594bb commit 1418a1c

File tree

5 files changed

+154
-605
lines changed

5 files changed

+154
-605
lines changed

plugins/twitter/README.md

Lines changed: 88 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,97 @@
33
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.
44

55
## Installation
6+
67
From this directory (`twitter`), run the installation:
8+
79
```bash
810
poetry install
911
```
1012

1113
## Usage
12-
1. Choose one of the 2 options to initialise the Twitter plugin
13-
- `GameTwitterPlugin`
14-
- This allows one to leverage GAME's X enterprise API credentials (i.e. higher rate limits)
15-
- `TwitterPlugin`
16-
- This allows one to use their own X API credentials
17-
2. Initialise plugin objects, run set-up scripts and use plugin functions
18-
- `GameTwitterPlugin`
19-
- To get the access token to us this plugin, run the following command
20-
```bash
21-
poetry run twitter-plugin-gamesdk auth -k <GAME_API_KEY>
22-
```
23-
You will see the following output:
24-
```bash
25-
Waiting for authentication...
26-
27-
Visit the following URL to authenticate:
28-
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
29-
```
30-
After authenticating, you will receive the following message:
31-
```bash
32-
Authenticated! Here's your access token:
33-
apx-<xxx>
34-
```
35-
- Set the access token as an environment variable called `GAME_TWITTER_ACCESS_TOKEN` (e.g. using a `.bashrc` or a `.zshrc` file):
36-
- Import and initialize the plugin to use in your worker:
37-
```python
38-
import os
39-
from twitter_plugin_gamesdk.game_twitter_plugin import GameTwitterPlugin
40-
41-
# Define your options with the necessary credentials
42-
options = {
43-
"id": "test_game_twitter_plugin",
44-
"name": "Test GAME Twitter Plugin",
45-
"description": "An example GAME Twitter Plugin for testing.",
46-
"credentials": {
47-
"gameTwitterAccessToken": os.environ.get("GAME_TWITTER_ACCESS_TOKEN")
48-
},
49-
}
50-
# Initialize the TwitterPlugin with your options
51-
game_twitter_plugin = GameTwitterPlugin(options)
52-
53-
# Post a tweet
54-
post_tweet_fn = game_twitter_plugin.get_function('post_tweet')
55-
post_tweet_fn("Hello world!")
56-
```
57-
You can refer to `examples/test_game_twitter.py` for more examples on how to call the twitter functions. Note that there is a limited number of functions available. If you require more, please submit a feature requests via Github Issues.
58-
59-
- `TwitterPlugin`
60-
- 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).
61-
- Create a project app, generate the following credentials and set the following environment variables (e.g. using a `.bashrc` or a `.zshrc` file):
62-
- `TWITTER_API_KEY`
63-
- `TWITTER_API_SECRET_KEY`
64-
- `TWITTER_ACCESS_TOKEN`
65-
- `TWITTER_ACCESS_TOKEN_SECRET`
66-
- Import and initialize the plugin to use in your worker:
67-
```python
68-
import os
69-
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin
70-
71-
# Define your options with the necessary credentials
72-
options = {
73-
"id": "test_twitter_worker",
74-
"name": "Test Twitter Worker",
75-
"description": "An example Twitter Plugin for testing.",
76-
"credentials": {
77-
"apiKey": os.environ.get("TWITTER_API_KEY"),
78-
"apiSecretKey": os.environ.get("TWITTER_API_SECRET_KEY"),
79-
"accessToken": os.environ.get("TWITTER_ACCESS_TOKEN"),
80-
"accessTokenSecret": os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"),
81-
},
82-
}
83-
# Initialize the TwitterPlugin with your options
84-
twitter_plugin = TwitterPlugin(options)
85-
86-
# Post a tweet
87-
post_tweet_fn = twitter_plugin.get_function('post_tweet')
88-
post_tweet_fn("Hello world!")
89-
```
90-
You can refer to `examples/test_twitter.py` for more examples on how to call the twitter functions.
14+
15+
The Twitter plugin can be initialized in one of two ways:
16+
17+
1. Using GAME's X enterprise API credentials (higher rate limits)
18+
19+
- To get the access token for this option, run the following command:
20+
21+
```bash
22+
poetry run twitter-plugin-gamesdk auth -k <GAME_API_KEY>
23+
```
24+
25+
You will see the following output:
26+
27+
```bash
28+
Waiting for authentication...
29+
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+
```
33+
34+
After authenticating, you will receive the following message:
35+
36+
```bash
37+
Authenticated! Here's your access token:
38+
apx-<xxx>
39+
```
40+
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:
43+
44+
```python
45+
import os
46+
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin
47+
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)
56+
57+
# Post a tweet
58+
post_tweet_fn = twitter_plugin.get_function('post_tweet')
59+
post_tweet_fn("Hello world!")
60+
```
61+
62+
2. Using your own X API credentials
63+
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:
72+
73+
```python
74+
import os
75+
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin
76+
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)
89+
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+
```
94+
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).
96+
97+
Example usage:
98+
99+
You can refer to the example files in the `examples` directory for more examples on how to call the twitter functions.

plugins/twitter/examples/test_game_twitter.py

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,86 @@
1+
import sys
12
import os
2-
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin
3+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../")))
4+
from plugins.twitter.twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin
35

46
# Define your options with the necessary credentials
5-
options = {
6-
"id": "test_twitter_plugin",
7-
"name": "Test Twitter Plugin",
8-
"description": "An example Twitter Plugin for testing.",
7+
# Using your own X API credentials
8+
""" options = {
99
"credentials": {
1010
"bearerToken": os.environ.get("TWITTER_BEARER_TOKEN"),
1111
"apiKey": os.environ.get("TWITTER_API_KEY"),
1212
"apiSecretKey": os.environ.get("TWITTER_API_SECRET_KEY"),
1313
"accessToken": os.environ.get("TWITTER_ACCESS_TOKEN"),
1414
"accessTokenSecret": os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"),
1515
},
16+
} """
17+
18+
# Using GAME Twitter API credentials
19+
options = {
20+
"credentials": {
21+
"gameTwitterAccessToken": "apx-a3a69f5bbb7aba5b595c3f14457ce4d1",
22+
},
1623
}
1724

1825
# Initialize the TwitterPlugin with your options
1926
twitter_plugin = TwitterPlugin(options)
2027

2128
# Test case 1: Post a Tweet
2229
print("\nRunning Test Case 1: Post a Tweet")
23-
post_tweet_fn = twitter_plugin.get_function('post_tweet')
24-
post_tweet_fn("Hello world! This is a test tweet from the Twitter Plugin!", media_ids=[])
30+
post_tweet_fn = twitter_plugin.twitter_client.create_tweet
31+
post_tweet_fn(text="Hello world! This is a test tweet from the Twitter Plugin!", media_ids=[])
2532
print("Posted tweet!")
2633

27-
# Test case 2: Reply to a Tweet
28-
print("\nRunning Test Case 2: Reply to a Tweet")
29-
reply_tweet_fn = twitter_plugin.get_function('reply_tweet')
30-
reply_tweet_fn(tweet_id=1879472470362816626, reply="Hey! This is a test reply!", media_ids=[])
34+
# Test case 2: Post a Tweet with Media
35+
print("\nRunning Test Case 2: Post a Tweet with Media")
36+
print("\nUpload media")
37+
with open("sample_media/media_file.png", "rb") as f:
38+
media_id = twitter_plugin.twitter_client.upload_media(f)
39+
print(f"Uploaded media_id: {media_id}")
40+
post_tweet_fn = twitter_plugin.twitter_client.create_tweet
41+
post_tweet_fn(text="Hello world! This is a test tweet with media from the Twitter Plugin!", media_ids=[media_id])
42+
print("Posted tweet with media!")
43+
44+
45+
# Test case 3: Reply to a Tweet
46+
print("\nRunning Test Case 3: Reply to a Tweet")
47+
reply_tweet_fn = twitter_plugin.twitter_client.create_tweet
48+
reply_tweet_fn(in_reply_to_tweet_id=1914249509963808976, text="Hey! This is a test reply!", media_ids=[])
3149
print("Replied to tweet!")
3250

33-
# Test case 3: Like a Tweet
34-
print("\nRunning Test Case 3: Like a Tweet")
35-
like_tweet_fn = twitter_plugin.get_function('like_tweet')
36-
like_tweet_fn(tweet_id=1879472470362816626)
51+
# Test case 4: Like a Tweet
52+
print("\nRunning Test Case 4: Like a Tweet")
53+
like_tweet_fn = twitter_plugin.twitter_client.like
54+
like_tweet_fn(tweet_id=1914249509963808976)
3755
print("Liked tweet!")
3856

39-
# Test case 4: Quote a Tweet
40-
print("\nRunning Test Case 4: Quote a Tweet")
41-
quote_tweet_fn = twitter_plugin.get_function('quote_tweet')
42-
quote_tweet_fn(tweet_id=1879472470362816626, quote="Hey! This is a test quote tweet!", media_ids=[])
57+
# Test case 5: Quote a Tweet
58+
print("\nRunning Test Case 5: Quote a Tweet")
59+
quote_tweet_fn = twitter_plugin.twitter_client.create_tweet
60+
quote_tweet_fn(tweet_id=1914249509963808976, quote="Hey! This is a test quote tweet!", media_ids=[])
4361
print("Quoted tweet!")
4462

45-
# Test case 5: Get Metrics
46-
print("\nRunning Test Case 5: Get Metrics")
47-
get_metrics_fn = twitter_plugin.get_function('get_metrics')
48-
metrics = get_metrics_fn()
63+
# Test case 6: Get Metrics
64+
print("\nRunning Test Case 6: Get Metrics")
65+
get_metrics_fn = twitter_plugin.twitter_client.get_me
66+
metrics = get_metrics_fn(user_fields=["public_metrics"])
4967
print("Metrics:", metrics)
5068

51-
# Test case 6: Get User From Handle
52-
print("\nRunning Test Case 6: Get User From Handle")
53-
get_user_fn = twitter_plugin.get_function('get_user_from_handle')
54-
user_id = get_user_fn('celesteanglm')
55-
print("user_id:", user_id)
56-
57-
# Test case 7: Get User Mentions
58-
print("\nRunning Test Case 7: Get User Mentions")
59-
get_user_fn = twitter_plugin.get_function("get_user_from_handle")
60-
user_id = get_user_fn("GAME_Virtuals")
61-
get_user_mentions_fn = twitter_plugin.get_function("get_user_mentions")
62-
user_mentions = get_user_mentions_fn(user_id, max_results=100)
63-
print("user_mentions:", user_mentions)
69+
# Test case 7: Get User From Handle
70+
print("\nRunning Test Case 7: Get User From Handle")
71+
get_user_fn = twitter_plugin.twitter_client.get_user
72+
user = get_user_fn(username='celesteanglm', user_fields=["public_metrics"])
73+
print("user:", user)
74+
75+
# Test case 8: Get User Mentions
76+
print("\nRunning Test Case 8: Get User Mentions")
77+
get_user_fn = twitter_plugin.twitter_client.get_user
78+
user = get_user_fn(username="GAME_Virtuals")
79+
get_user_mentions_fn = twitter_plugin.twitter_client.get_users_mentions
80+
user_mentions = get_user_mentions_fn( id = user['data']['id'],
81+
max_results = 10,
82+
tweet_fields = ["id", "created_at", "text"],
83+
expansions = ["attachments.media_keys"],
84+
media_fields = ["url"])
85+
print("user_mentions:", user_mentions)
86+

0 commit comments

Comments
 (0)