-
Notifications
You must be signed in to change notification settings - Fork 4
/
twitter_GPT_bot.py
55 lines (42 loc) · 1.44 KB
/
twitter_GPT_bot.py
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
47
48
49
50
51
52
53
54
55
import tweepy
import openai
import time
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
# Insert your OpenAI API key here
openai_key = ""
auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret, access_token, access_token_secret)
api = tweepy.API(auth)
# Authenticate with OpenAI API
openai.api_key = openai_key
# Set the starting ID for mentions
replied_tweets = []
while True:
# Search for tweets that mention the account
mentions = api.mentions_timeline(count=100)
if len(mentions) == 0:
time.sleep(20)
continue
# Iterate over the mentions
for mention in mentions:
# Check if the tweet has already been replied to
if mention.id not in replied_tweets:
# Use GPT-3 to generate a reply
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"@{mention.user.screen_name} {mention.text[:280]}",
max_tokens=250,
temperature=0.5,
)
print(response.choices[0].text)
# Post the reply
api.update_status(
f"@{mention.user.screen_name} {response.choices[0].text[:280]}",
in_reply_to_status_id=mention.id,
)
print(f"{mention.id} reply tweet done")
# Add the tweet to the list of replied tweets
replied_tweets.append(mention.id)
time.sleep(20)