|
3 | 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. |
4 | 4 |
|
5 | 5 | ## Installation |
| 6 | + |
6 | 7 | From this directory (`twitter`), run the installation: |
| 8 | + |
7 | 9 | ```bash |
8 | 10 | poetry install |
9 | 11 | ``` |
10 | 12 |
|
11 | 13 | ## 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. |
0 commit comments