Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 88 additions & 79 deletions plugins/twitter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,97 @@
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.

## Installation

From this directory (`twitter`), run the installation:

```bash
poetry install
```

## Usage
1. Choose one of the 2 options to initialise the Twitter plugin
- `GameTwitterPlugin`
- This allows one to leverage GAME's X enterprise API credentials (i.e. higher rate limits)
- `TwitterPlugin`
- This allows one to use their own X API credentials
2. Initialise plugin objects, run set-up scripts and use plugin functions
- `GameTwitterPlugin`
- To get the access token to us this plugin, run the following command
```bash
poetry run twitter-plugin-gamesdk auth -k <GAME_API_KEY>
```
You will see the following output:
```bash
Waiting for authentication...

Visit the following URL to authenticate:
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
```
After authenticating, you will receive the following message:
```bash
Authenticated! Here's your access token:
apx-<xxx>
```
- Set the access token as an environment variable called `GAME_TWITTER_ACCESS_TOKEN` (e.g. using a `.bashrc` or a `.zshrc` file):
- Import and initialize the plugin to use in your worker:
```python
import os
from twitter_plugin_gamesdk.game_twitter_plugin import GameTwitterPlugin

# Define your options with the necessary credentials
options = {
"id": "test_game_twitter_plugin",
"name": "Test GAME Twitter Plugin",
"description": "An example GAME Twitter Plugin for testing.",
"credentials": {
"gameTwitterAccessToken": os.environ.get("GAME_TWITTER_ACCESS_TOKEN")
},
}
# Initialize the TwitterPlugin with your options
game_twitter_plugin = GameTwitterPlugin(options)

# Post a tweet
post_tweet_fn = game_twitter_plugin.get_function('post_tweet')
post_tweet_fn("Hello world!")
```
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.

- `TwitterPlugin`
- 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).
- Create a project app, generate the following credentials and set the following environment variables (e.g. using a `.bashrc` or a `.zshrc` file):
- `TWITTER_API_KEY`
- `TWITTER_API_SECRET_KEY`
- `TWITTER_ACCESS_TOKEN`
- `TWITTER_ACCESS_TOKEN_SECRET`
- Import and initialize the plugin to use in your worker:
```python
import os
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin

# Define your options with the necessary credentials
options = {
"id": "test_twitter_worker",
"name": "Test Twitter Worker",
"description": "An example Twitter Plugin for testing.",
"credentials": {
"apiKey": os.environ.get("TWITTER_API_KEY"),
"apiSecretKey": os.environ.get("TWITTER_API_SECRET_KEY"),
"accessToken": os.environ.get("TWITTER_ACCESS_TOKEN"),
"accessTokenSecret": os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"),
},
}
# Initialize the TwitterPlugin with your options
twitter_plugin = TwitterPlugin(options)

# Post a tweet
post_tweet_fn = twitter_plugin.get_function('post_tweet')
post_tweet_fn("Hello world!")
```
You can refer to `examples/test_twitter.py` for more examples on how to call the twitter functions.

The Twitter plugin can be initialized in one of two ways:

1. Using GAME's X enterprise API credentials (higher rate limits)

- To get the access token for this option, run the following command:

```bash
poetry run twitter-plugin-gamesdk auth -k <GAME_API_KEY>
```

You will see the following output:

```bash
Waiting for authentication...

Visit the following URL to authenticate:
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
```

After authenticating, you will receive the following message:

```bash
Authenticated! Here's your access token:
apx-<xxx>
```

- Set the access token as an environment variable called `GAME_TWITTER_ACCESS_TOKEN` (e.g. using a `.bashrc` or a `.zshrc` file).
- Import and initialize the plugin to use in your worker:

```python
import os
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin

# Define your options with the necessary credentials
options = {
"credentials": {
"gameTwitterAccessToken": os.environ.get("GAME_TWITTER_ACCESS_TOKEN")
},
}
# Initialize the TwitterPlugin with your options
twitter_plugin = TwitterPlugin(options)

# Post a tweet
post_tweet_fn = twitter_plugin.get_function('post_tweet')
post_tweet_fn("Hello world!")
```

2. Using your own X API credentials

- 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).
- Create a project app, generate the following credentials and set them as environment variables (e.g. using a `.bashrc` or a `.zshrc` file):
- `TWITTER_BEARER_TOKEN`
- `TWITTER_API_KEY`
- `TWITTER_API_SECRET_KEY`
- `TWITTER_ACCESS_TOKEN`
- `TWITTER_ACCESS_TOKEN_SECRET`
- Import and initialize the plugin to use in your worker:

```python
import os
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin

# Define your options with the necessary credentials
options = {
"credentials": {
"bearerToken": os.environ.get("TWITTER_BEARER_TOKEN"),
"apiKey": os.environ.get("TWITTER_API_KEY"),
"apiSecretKey": os.environ.get("TWITTER_API_SECRET_KEY"),
"accessToken": os.environ.get("TWITTER_ACCESS_TOKEN"),
"accessTokenSecret": os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"),
},
}
# Initialize the TwitterPlugin with your options
twitter_plugin = TwitterPlugin(options)

# Post a tweet
post_tweet_fn = twitter_plugin.twitter_client.create_tweet
post_tweet_fn(text="Hello world! This is a test tweet from the Twitter Plugin!")
```

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).

Example usage:

You can refer to the example files in the `examples` directory for more examples on how to call the twitter functions.
69 changes: 0 additions & 69 deletions plugins/twitter/examples/test_game_twitter.py

This file was deleted.

92 changes: 56 additions & 36 deletions plugins/twitter/examples/test_twitter.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,83 @@
import os
from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin

# Define your options with the necessary credentials
options = {
"id": "test_twitter_plugin",
"name": "Test Twitter Plugin",
"description": "An example Twitter Plugin for testing.",
# Using your own X API credentials
""" options = {
"credentials": {
"bearerToken": os.environ.get("TWITTER_BEARER_TOKEN"),
"apiKey": os.environ.get("TWITTER_API_KEY"),
"apiSecretKey": os.environ.get("TWITTER_API_SECRET_KEY"),
"accessToken": os.environ.get("TWITTER_ACCESS_TOKEN"),
"accessTokenSecret": os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"),
},
} """

# Using GAME Twitter API credentials
options = {
"credentials": {
"gameTwitterAccessToken": "apx-xxx",
},
}

# Initialize the TwitterPlugin with your options
twitter_plugin = TwitterPlugin(options)

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

# Test case 2: Reply to a Tweet
print("\nRunning Test Case 2: Reply to a Tweet")
reply_tweet_fn = twitter_plugin.get_function('reply_tweet')
reply_tweet_fn(tweet_id=1879472470362816626, reply="Hey! This is a test reply!", media_ids=[])
# Test case 2: Post a Tweet with Media
print("\nRunning Test Case 2: Post a Tweet with Media")
print("\nUpload media")
with open("sample_media/media_file.png", "rb") as f:
media_id = twitter_plugin.twitter_client.upload_media(f)
print(f"Uploaded media_id: {media_id}")
post_tweet_fn = twitter_plugin.twitter_client.create_tweet
post_tweet_fn(text="Hello world! This is a test tweet with media from the Twitter Plugin!", media_ids=[media_id])
print("Posted tweet with media!")


# Test case 3: Reply to a Tweet
print("\nRunning Test Case 3: Reply to a Tweet")
reply_tweet_fn = twitter_plugin.twitter_client.create_tweet
reply_tweet_fn(in_reply_to_tweet_id=1915274034100809968, text="Hey! This is a test reply!")
print("Replied to tweet!")

# Test case 3: Like a Tweet
print("\nRunning Test Case 3: Like a Tweet")
like_tweet_fn = twitter_plugin.get_function('like_tweet')
like_tweet_fn(tweet_id=1879472470362816626)
# Test case 4: Like a Tweet
print("\nRunning Test Case 4: Like a Tweet")
like_tweet_fn = twitter_plugin.twitter_client.like
like_tweet_fn(tweet_id=1915274034100809968)
print("Liked tweet!")

# Test case 4: Quote a Tweet
print("\nRunning Test Case 4: Quote a Tweet")
quote_tweet_fn = twitter_plugin.get_function('quote_tweet')
quote_tweet_fn(tweet_id=1879472470362816626, quote="Hey! This is a test quote tweet!", media_ids=[])
# Test case 5: Quote a Tweet
print("\nRunning Test Case 5: Quote a Tweet")
quote_tweet_fn = twitter_plugin.twitter_client.create_tweet
quote_tweet_fn(quote_tweet_id=1915274034100809968, text="Hey! This is a test quote tweet!")
print("Quoted tweet!")

# Test case 5: Get Metrics
print("\nRunning Test Case 5: Get Metrics")
get_metrics_fn = twitter_plugin.get_function('get_metrics')
metrics = get_metrics_fn()
# Test case 6: Get Metrics
print("\nRunning Test Case 6: Get Metrics")
get_metrics_fn = twitter_plugin.twitter_client.get_me
metrics = get_metrics_fn(user_fields=["public_metrics"])
print("Metrics:", metrics)

# Test case 6: Get User From Handle
print("\nRunning Test Case 6: Get User From Handle")
get_user_fn = twitter_plugin.get_function('get_user_from_handle')
user_id = get_user_fn('celesteanglm')
print("user_id:", user_id)

# Test case 7: Get User Mentions
print("\nRunning Test Case 7: Get User Mentions")
get_user_fn = twitter_plugin.get_function("get_user_from_handle")
user_id = get_user_fn("GAME_Virtuals")
get_user_mentions_fn = twitter_plugin.get_function("get_user_mentions")
user_mentions = get_user_mentions_fn(user_id, max_results=100)
print("user_mentions:", user_mentions)
# Test case 7: Get User From Handle
print("\nRunning Test Case 7: Get User From Handle")
get_user_fn = twitter_plugin.twitter_client.get_user
user = get_user_fn(username='celesteanglm', user_fields=["public_metrics"])
print("user:", user)

# Test case 8: Get User Mentions
print("\nRunning Test Case 8: Get User Mentions")
get_user_fn = twitter_plugin.twitter_client.get_user
user = get_user_fn(username="GAME_Virtuals")
get_user_mentions_fn = twitter_plugin.twitter_client.get_users_mentions
user_mentions = get_user_mentions_fn( id = user['data']['id'],
max_results = 10,
tweet_fields = ["id", "created_at", "text"],
expansions = ["attachments.media_keys"],
media_fields = ["url"])
print("user_mentions:", user_mentions)

Loading