Skip to content

Commit 7a75a23

Browse files
committed
feat: allow upload of media to X
1 parent 6e66d87 commit 7a75a23

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed
14.6 KB
Loading

plugins/twitter/examples/test_game_twitter.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from twitter_plugin_gamesdk.game_twitter_plugin import GameTwitterPlugin
3+
import base64
34

45
# Define your options with the necessary credentials
56
options = {
@@ -26,20 +27,44 @@
2627
reply_tweet_fn(tweet_id=1879472470362816626, reply="Hey! This is a test reply!", media_ids=[])
2728
print("Replied to tweet!")
2829

29-
# # Test case 3: Quote a Tweet
30-
# print("\nRunning Test Case 3: Quote a Tweet")
31-
# quote_tweet_fn = game_twitter_plugin.get_function('quote_tweet')
32-
# quote_tweet_fn(tweet_id=1879472470362816626, quote="Hey! This is a test quote tweet!", media_ids=[])
33-
# print("Quoted tweet!")
34-
35-
# # Test case 4: Search Tweets
36-
# print("\nRunning Test Case 4: Search Tweets")
37-
# search_tweets_fn = game_twitter_plugin.get_function('search_tweets')
38-
# response = search_tweets_fn(query="Python")
39-
# print(f"Searched tweets: {response}")
40-
41-
# # Test case 5: Get authenticated user
42-
# print("\nRunning Test Case 5: Get details of authenticated user")
43-
# get_authenticated_user_fn = game_twitter_plugin.get_function('get_authenticated_user')
44-
# response = get_authenticated_user_fn()
45-
# print(f"Got details of authenticated user: {response}")
30+
# Test case 3: Quote a Tweet
31+
print("\nRunning Test Case 3: Quote a Tweet")
32+
quote_tweet_fn = game_twitter_plugin.get_function('quote_tweet')
33+
quote_tweet_fn(tweet_id=1879472470362816626, quote="Hey! This is a test quote tweet!", media_ids=[])
34+
print("Quoted tweet!")
35+
36+
# Test case 4: Search Tweets
37+
print("\nRunning Test Case 4: Search Tweets")
38+
search_tweets_fn = game_twitter_plugin.get_function('search_tweets')
39+
response = search_tweets_fn(query="Python")
40+
print(f"Searched tweets: {response}")
41+
42+
# Test case 5: Get authenticated user
43+
print("\nRunning Test Case 5: Get details of authenticated user")
44+
get_authenticated_user_fn = game_twitter_plugin.get_function('get_authenticated_user')
45+
response = get_authenticated_user_fn()
46+
print(f"Got details of authenticated user: {response}")
47+
48+
# Test case 6: Get my mentions
49+
print("\nRunning Test Case 6: Get my mentions")
50+
mentions_fn = game_twitter_plugin.get_function('mentions')
51+
response = mentions_fn()
52+
print(f"My mentions: {response}")
53+
54+
# Test case 7: Get my followers
55+
print("\nRunning Test Case 7: Get list of users who are followers of me")
56+
followers_fn = game_twitter_plugin.get_function('followers')
57+
response = followers_fn()
58+
print(f"My followers: {response}")
59+
60+
# Test case 8: Get ppl following me
61+
print("\nRunning Test Case 8: Get list of users I am following")
62+
following_fn = game_twitter_plugin.get_function('following')
63+
response = following_fn()
64+
print(f"Users I am following: {response}")
65+
66+
# Test case 9: Upload media
67+
print("\nRunning Test Case 9: Upload media")
68+
with open("sample_media/media_file.png", "rb") as f:
69+
media_id = game_twitter_plugin.upload_media(f)
70+
print(f"Uploaded media_id: {media_id}")

plugins/twitter/twitter_plugin_gamesdk/game_twitter_plugin.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import requests
2626
from typing import Optional, Dict, Any, Callable, List
27-
import os
27+
import os,io
2828
import logging
2929

3030

@@ -60,6 +60,9 @@ def __init__(self, options: Dict[str, Any]) -> None:
6060
"quote_tweet": self._quote_tweet,
6161
"search_tweets": self._search_tweets,
6262
"get_authenticated_user": self._get_authenticated_user,
63+
"mentions": self._mentions,
64+
"followers": self._followers,
65+
"following": self._following
6366
}
6467
# Configure logging
6568
logging.basicConfig(level=logging.INFO)
@@ -161,4 +164,42 @@ def _get_authenticated_user(self) -> Dict[str, Any]:
161164
"""
162165
Get details of the authenticated user.
163166
"""
164-
return self._fetch_api("/me", "GET")
167+
return self._fetch_api("/me", "GET")
168+
169+
def _mentions(self, pagination_token: Optional[str] = None) -> Dict[str, Any]:
170+
"""
171+
Get mentions of the authenticated user.
172+
"""
173+
endpoint = "/mentions"
174+
if pagination_token:
175+
endpoint += f"?paginationToken={paginationToken}"
176+
return self._fetch_api(endpoint, "GET")
177+
178+
def _followers(self, pagination_token: Optional[str] = None) -> Dict[str, Any]:
179+
"""
180+
Get followers of the authenticated user.
181+
"""
182+
endpoint = "/followers"
183+
if pagination_token:
184+
endpoint += f"?paginationToken={paginationToken}"
185+
return self._fetch_api(endpoint, "GET")
186+
187+
def _following(self, pagination_token: Optional[str] = None) -> Dict[str, Any]:
188+
"""
189+
Get list of users whom the authenticated user is following.
190+
"""
191+
endpoint = "/following"
192+
if pagination_token:
193+
endpoint += f"?paginationToken={paginationToken}"
194+
return self._fetch_api(endpoint, "GET")
195+
196+
def upload_media(self, media: bytes) -> str:
197+
"""
198+
Uploads media (e.g. image, video) to X and returns the media ID.
199+
"""
200+
response = requests.post(
201+
url = f"{self.base_url}/media",
202+
headers = {k: v for k, v in self.headers.items() if k != "Content-Type"},
203+
files = {"file": media}
204+
)
205+
return response.json().get("mediaId")

0 commit comments

Comments
 (0)