AI agent on X that will serve as the avatar of Lil Green for updating posts.
This AI agent will randomly generate scenes of UFOs appearing around the world, interact with players through responses, and embark on Earth trips from an alien’s perspective.
Moreover, the AI agent will randomly participate in players' PVP Space, creating an immersive AI player experience.
Stack
Programming : Python
APIs: X API, OpenAI API
Libraries: tweepy (for interacting with X), openai (for interacting with OpenAI), schedule (for scheduling tasks)
The code here only shows the architecture design, the actual code will be refined and optimized.
import tweepy
import openai
import schedule
import time
import random
import requests
from io import BytesIO
from PIL import Image
# Configure API keys
TWITTER_API_KEY = 'YOUR_TWITTER_API_KEY'
TWITTER_API_SECRET = 'YOUR_TWITTER_API_SECRET'
TWITTER_ACCESS_TOKEN = 'YOUR_TWITTER_ACCESS_TOKEN'
TWITTER_ACCESS_SECRET = 'YOUR_TWITTER_ACCESS_SECRET'
OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY'
# Set up OpenAI
openai.api_key = OPENAI_API_KEY
# Set up Twitter client
auth = tweepy.OAuth1UserHandler(
TWITTER_API_KEY, TWITTER_API_SECRET,
TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_SECRET
)
twitter_api = tweepy.API(auth)
# List of random locations (can be expanded)
locations = [
"New York City", "Paris", "Tokyo", "Sydney", "Cairo",
"London", "Los Angeles", "Beijing", "Moscow", "Rio de Janeiro"
]
def generate_post():
location = random.choice(locations)
# Use OpenAI to generate text
prompt = f"Describe a scenario where an alien named Lil Green descends in {location}."
response = openai.Completion.create(
engine="text-davinci-4",
prompt=prompt,
max_tokens=150
)
text = response.choices[0].text.strip()
# Use OpenAI to generate image
image_prompt = f"A green alien named Lil Green descending from a UFO in {location}, cartoon style"
image_response = openai.Image.create(
prompt=image_prompt,
n=1,
size="1024x1024"
)
image_url = image_response['data'][0]['url']
# Download the image
image_data = requests.get(image_url).content
image = Image.open(BytesIO(image_data))
image_path = "ufo_image.png"
image.save(image_path)
# Post the tweet
twitter_api.update_with_media(image_path, status=text)
print(f"Posted tweet: {text}")
def reply_to_comments():
# Get the latest tweets
tweets = twitter_api.user_timeline(count=5)
for tweet in tweets:
# Get mentions
mentions = twitter_api.mentions_timeline(since_id=tweet.id)
for mention in mentions:
if mention.in_reply_to_status_id == tweet.id:
# Use OpenAI to generate a response
prompt = f"Lil Green the alien responds to the following comment: {mention.text}"
response = openai.Completion.create(
engine="text-davinci-4",
prompt=prompt,
max_tokens=100
)
reply_text = response.choices[0].text.strip()
# Reply to the comment
twitter_api.update_status(
status=f"@{mention.user.screen_name} {reply_text}",
in_reply_to_status_id=mention.id
)
print(f"Replied to @{mention.user.screen_name}: {reply_text}")
# Schedule tasks
schedule.every().day.at("10:00").do(generate_post)
schedule.every(10).minutes.do(reply_to_comments)
print("Lil Green is running...")
while True:
schedule.run_pending()
time.sleep(1)