forked from PrakharDoneria/Sarcastic-Twitter-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
46 lines (40 loc) · 1.53 KB
/
main.py
File metadata and controls
46 lines (40 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import tweepy
import google.generativeai as genai
# Twitter API Authentication
client = tweepy.Client(
bearer_token=os.getenv("TWITTER_BEARER_TOKEN"),
consumer_key=os.getenv("TWITTER_API_KEY"),
consumer_secret=os.getenv("TWITTER_API_SECRET"),
access_token=os.getenv("TWITTER_ACCESS_TOKEN"),
access_token_secret=os.getenv("TWITTER_ACCESS_SECRET")
)
# Gemini API Configuration
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
model = genai.GenerativeModel(
model_name="gemini-2.0-flash",
generation_config={
"temperature": 1,
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": 280,
"response_mime_type": "text/plain",
},
system_instruction="You are a Twitter bot. Your job is to pos tweets about random web development, html, css, or js facts that makes user intrested in coding and engage with the post make sure to use relevant tags and be in tweet character limit",
)
def generate_tweet():
chat_session = model.start_chat(history=[])
response = chat_session.send_message("Generate a tweet.")
return {response.text.strip()}
def post_tweet():
"""Posts a tweet using the generated content."""
tweet_text = generate_tweet()
try:
client.create_tweet(text=tweet_text)
print(f"Tweeted: {tweet_text}")
return {"status": "success", "tweet": tweet_text}
except tweepy.TweepyException as e:
print(f"Error posting tweet: {e}")
return {"status": "error", "message": str(e)}
if __name__ == "__main__":
post_tweet()