|
| 1 | +import os |
| 2 | +import requests |
| 3 | +from dotenv import load_dotenv |
1 | 4 | from twitter_plugin_gamesdk.twitter_plugin import TwitterPlugin |
2 | 5 |
|
3 | | -# Define your options with the necessary credentials |
4 | | -# Using your own X API credentials |
5 | | -""" options = { |
6 | | - "credentials": { |
7 | | - "bearerToken": os.environ.get("TWITTER_BEARER_TOKEN"), |
8 | | - "apiKey": os.environ.get("TWITTER_API_KEY"), |
9 | | - "apiSecretKey": os.environ.get("TWITTER_API_SECRET_KEY"), |
10 | | - "accessToken": os.environ.get("TWITTER_ACCESS_TOKEN"), |
11 | | - "accessTokenSecret": os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"), |
12 | | - }, |
13 | | -} """ |
14 | | - |
15 | | -# Using GAME Twitter API credentials |
16 | | -options = { |
17 | | - "credentials": { |
18 | | - "gameTwitterAccessToken": "apx-xxx", |
19 | | - }, |
20 | | -} |
21 | | - |
22 | | -# Initialize the TwitterPlugin with your options |
23 | | -twitter_plugin = TwitterPlugin(options) |
24 | | - |
25 | | -# Test case 1: Post a Tweet |
26 | | -print("\nRunning Test Case 1: Post a Tweet") |
27 | | -post_tweet_fn = twitter_plugin.twitter_client.create_tweet |
28 | | -post_tweet_fn(text="Hello world! This is a test tweet from the Twitter Plugin!") |
29 | | -print("Posted tweet!") |
30 | | - |
31 | | -# Test case 2: Post a Tweet with Media |
32 | | -print("\nRunning Test Case 2: Post a Tweet with Media") |
33 | | -print("\nUpload media") |
34 | | -with open("sample_media/media_file.png", "rb") as f: |
35 | | - media_id = twitter_plugin.twitter_client.upload_media(f) |
36 | | - print(f"Uploaded media_id: {media_id}") |
37 | | -post_tweet_fn = twitter_plugin.twitter_client.create_tweet |
38 | | -post_tweet_fn(text="Hello world! This is a test tweet with media from the Twitter Plugin!", media_ids=[media_id]) |
39 | | -print("Posted tweet with media!") |
40 | | - |
41 | | - |
42 | | -# Test case 3: Reply to a Tweet |
43 | | -print("\nRunning Test Case 3: Reply to a Tweet") |
44 | | -reply_tweet_fn = twitter_plugin.twitter_client.create_tweet |
45 | | -reply_tweet_fn(in_reply_to_tweet_id=1915274034100809968, text="Hey! This is a test reply!") |
46 | | -print("Replied to tweet!") |
47 | | - |
48 | | -# Test case 4: Like a Tweet |
49 | | -print("\nRunning Test Case 4: Like a Tweet") |
50 | | -like_tweet_fn = twitter_plugin.twitter_client.like |
51 | | -like_tweet_fn(tweet_id=1915274034100809968) |
52 | | -print("Liked tweet!") |
53 | | - |
54 | | -# Test case 5: Quote a Tweet |
55 | | -print("\nRunning Test Case 5: Quote a Tweet") |
56 | | -quote_tweet_fn = twitter_plugin.twitter_client.create_tweet |
57 | | -quote_tweet_fn(quote_tweet_id=1915274034100809968, text="Hey! This is a test quote tweet!") |
58 | | -print("Quoted tweet!") |
59 | | - |
60 | | -# Test case 6: Get Metrics |
61 | | -print("\nRunning Test Case 6: Get Metrics") |
62 | | -get_metrics_fn = twitter_plugin.twitter_client.get_me |
63 | | -metrics = get_metrics_fn(user_fields=["public_metrics"]) |
64 | | -print("Metrics:", metrics) |
65 | | - |
66 | | -# Test case 7: Get User From Handle |
67 | | -print("\nRunning Test Case 7: Get User From Handle") |
68 | | -get_user_fn = twitter_plugin.twitter_client.get_user |
69 | | -user = get_user_fn(username='celesteanglm', user_fields=["public_metrics"]) |
70 | | -print("user:", user) |
71 | | - |
72 | | -# Test case 8: Get User Mentions |
73 | | -print("\nRunning Test Case 8: Get User Mentions") |
74 | | -get_user_fn = twitter_plugin.twitter_client.get_user |
75 | | -user = get_user_fn(username="GAME_Virtuals") |
76 | | -get_user_mentions_fn = twitter_plugin.twitter_client.get_users_mentions |
77 | | -user_mentions = get_user_mentions_fn( id = user['data']['id'], |
78 | | - max_results = 10, |
79 | | - tweet_fields = ["id", "created_at", "text"], |
80 | | - expansions = ["attachments.media_keys"], |
81 | | - media_fields = ["url"]) |
82 | | -print("user_mentions:", user_mentions) |
| 6 | +def run_twitter_actions(): |
| 7 | + load_dotenv() |
| 8 | + token = os.getenv("GAME_TWITTER_ACCESS_TOKEN") |
| 9 | + if not token: |
| 10 | + raise RuntimeError("Please set GAME_TWITTER_ACCESS_TOKEN in your .env") |
83 | 11 |
|
| 12 | + # ——— Initialization options ——— |
| 13 | + # # Option A: Using your own X API credentials (uncomment to use) |
| 14 | + # options = { |
| 15 | + # "credentials": { |
| 16 | + # "api_key": os.environ.get("TWITTER_API_KEY"), |
| 17 | + # "api_key_secret": os.environ.get("TWITTER_API_KEY_SECRET"), |
| 18 | + # "access_token": os.environ.get("TWITTER_ACCESS_TOKEN"), |
| 19 | + # "access_token_secret": os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"), |
| 20 | + # }, |
| 21 | + # } |
| 22 | + |
| 23 | + # Option B: Using GAME Twitter API credentials |
| 24 | + options = { |
| 25 | + "credentials": { |
| 26 | + "game_twitter_access_token": token |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + twitter_plugin = TwitterPlugin(options) |
| 31 | + client = twitter_plugin.twitter_client |
| 32 | + |
| 33 | + try: |
| 34 | + # 1. Who am I? |
| 35 | + me = client.get_me() |
| 36 | + me_data = me["data"] |
| 37 | + user_id = me_data["id"] |
| 38 | + print(f"🙋 Logged in as: @{me_data['username']} ({me_data['name']})") |
| 39 | + |
| 40 | + # 2. Post a tweet |
| 41 | + tweet = client.create_tweet(text="Hello Web3 🧵 #GameByVirtuals") |
| 42 | + tweet_id = tweet["data"]["id"] |
| 43 | + print(f"✅ Tweet posted: https://x.com/i/web/status/{tweet_id}") |
| 44 | + |
| 45 | + # 3. Like it |
| 46 | + client.like(tweet_id=tweet_id) |
| 47 | + print("❤️ Tweet liked!") |
| 48 | + |
| 49 | + # 4. Reply to it |
| 50 | + reply = client.create_tweet( |
| 51 | + text="Replying to my own tweet 😎", |
| 52 | + in_reply_to_tweet_id=tweet_id |
| 53 | + ) |
| 54 | + print(f"💬 Replied: https://x.com/i/web/status/{reply['data']['id']}") |
| 55 | + |
| 56 | + # 5. Quote it |
| 57 | + quote = client.create_tweet( |
| 58 | + text="Excited to be testing the new Game Twitter Plugin!", |
| 59 | + quote_tweet_id=tweet_id |
| 60 | + ) |
| 61 | + print(f"🔁 Quoted: https://x.com/i/web/status/{quote['data']['id']}") |
| 62 | + |
| 63 | + # 5. Upload local media and tweet |
| 64 | + with open("sample_media/virtuals-logo.png", "rb") as img: |
| 65 | + media_id = client.upload_media(media=img) |
| 66 | + local = client.create_tweet( |
| 67 | + text="Check this out! Uploaded with local media!", |
| 68 | + media_ids=[media_id] |
| 69 | + ) |
| 70 | + print(f"🖼️ Local media tweet: https://x.com/i/web/status/{local['data']['id']}") |
| 71 | + |
| 72 | + # 7. Upload URL media and tweet |
| 73 | + url = "https://assets.coingecko.com/coins/images/51063/large/Gaming_Agent_1fe70d54ba.jpg" |
| 74 | + resp = requests.get(url) |
| 75 | + resp.raise_for_status() |
| 76 | + media_id_url = client.upload_media(media=resp.content) |
| 77 | + url_tweet = client.create_tweet( |
| 78 | + text="Check this out! Uploaded with URL media!", |
| 79 | + media_ids=[media_id_url] |
| 80 | + ) |
| 81 | + print(f"🖼️ URL media tweet: https://x.com/i/web/status/{url_tweet['data']['id']}") |
| 82 | + |
| 83 | + # 8. Search tweets |
| 84 | + search = client.search_recent_tweets(query="#GameByVirtuals", max_results=10) |
| 85 | + hits = search.get("data", []) |
| 86 | + print(f"🔍 Found {len(hits)} tweets for #GameByVirtuals:") |
| 87 | + for i, t in enumerate(hits, 1): |
| 88 | + print(f" {i}. https://x.com/i/web/status/{t['id']}") |
| 89 | + |
| 90 | + # 9. Mentions timeline |
| 91 | + mentions = client.get_users_mentions(id=user_id, max_results=20) |
| 92 | + mdata = mentions.get("data", []) |
| 93 | + print(f"🔔 You have {len(mdata)} recent mentions:") |
| 94 | + for i, t in enumerate(mdata, 1): |
| 95 | + print(f" {i}. https://x.com/i/web/status/{t['id']}") |
| 96 | + |
| 97 | + # 10. Followers |
| 98 | + followers = client.get_users_followers(id=user_id, max_results=20) |
| 99 | + fdata = followers.get("data", []) |
| 100 | + print(f"👥 You have {len(fdata)} followers:") |
| 101 | + for i, u in enumerate(fdata, 1): |
| 102 | + print(f" {i}. @{u['username']} ({u['name']})") |
| 103 | + |
| 104 | + # 11. Following |
| 105 | + following = client.get_users_following(id=user_id, max_results=20) |
| 106 | + gdata = following.get("data", []) |
| 107 | + print(f"➡️ You are following {len(gdata)} users:") |
| 108 | + for i, u in enumerate(gdata, 1): |
| 109 | + print(f" {i}. @{u['username']} ({u['name']})") |
| 110 | + |
| 111 | + # 12. Get my public metrics |
| 112 | + metrics = client.get_me(user_fields=["public_metrics"]) |
| 113 | + print("📊 My metrics:", metrics["data"]["public_metrics"]) |
| 114 | + |
| 115 | + # 13. Read-only lookup of another user |
| 116 | + other = client.get_user(username="GAME_Virtuals") |
| 117 | + print("🔎 Lookup @GAME_Virtuals:", other["data"]) |
| 118 | + |
| 119 | + # 14. Get metrics for another handle |
| 120 | + game_virtuals = client.get_user(username="GAME_Virtuals", user_fields=["public_metrics"]) |
| 121 | + print("📊 @GAME_Virtuals metrics:", game_virtuals["data"]["public_metrics"]) |
| 122 | + |
| 123 | + # 15. Advanced mentions for GAME_Virtuals |
| 124 | + adv_user = client.get_user(username="GAME_Virtuals") |
| 125 | + adv_mentions = client.get_users_mentions( |
| 126 | + id=adv_user["data"]["id"], |
| 127 | + max_results=10, |
| 128 | + tweet_fields=["id", "created_at", "text"], |
| 129 | + expansions=["attachments.media_keys"], |
| 130 | + media_fields=["url"] |
| 131 | + ) |
| 132 | + print("🔔 Advanced mentions for @GAME_Virtuals:", adv_mentions) |
| 133 | + |
| 134 | + except Exception as e: |
| 135 | + print("❌ Error during Twitter actions:", e) |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + run_twitter_actions() |
0 commit comments