Update main.py
Browse files
main.py
CHANGED
|
@@ -1,29 +1,126 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import openai
|
| 3 |
import tweepy
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
_tweet = "I'm a pretty average middle aged guy, got a solid job and had a good family but things got messy and took " \
|
| 11 |
-
"a turn for the worst. Everyone says you need to hit rock bottom to go back up so here I am!"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def prompt(tweet, roast=True):
|
| 15 |
-
roast_or_toast = "snarky joke" if roast else "encouragement"
|
| 16 |
-
return f"A user tweeted this tweet:\n\n{tweet}\\n\nThe following {roast_or_toast} was given as an answer:"
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
response = openai.Completion.create(
|
| 21 |
engine="text-davinci-002",
|
| 22 |
-
prompt=
|
| 23 |
temperature=0.7,
|
| 24 |
max_tokens=60,
|
| 25 |
top_p=1,
|
| 26 |
frequency_penalty=0,
|
| 27 |
-
presence_penalty=0
|
| 28 |
)
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
import os
|
| 3 |
+
import time
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
import openai
|
| 7 |
import tweepy
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger()
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger.setLevel(logging.INFO)
|
| 12 |
+
|
| 13 |
+
openai_key = os.environ.get("openai_api_key")
|
| 14 |
+
consumer_key = os.environ.get("consumer_key")
|
| 15 |
+
consumer_secret_key = os.environ.get("consumer_secret_key")
|
| 16 |
+
access_token = os.environ.get("access_token")
|
| 17 |
+
access_token_secret = os.environ.get("access_token_secret")
|
| 18 |
+
bearer_token = os.environ.get("bearer_token")
|
| 19 |
+
|
| 20 |
+
client = tweepy.Client(
|
| 21 |
+
bearer_token=bearer_token,
|
| 22 |
+
consumer_key=consumer_key,
|
| 23 |
+
consumer_secret=consumer_secret_key,
|
| 24 |
+
access_token=access_token,
|
| 25 |
+
access_token_secret=access_token_secret,
|
| 26 |
+
wait_on_rate_limit=True,
|
| 27 |
+
)
|
| 28 |
|
| 29 |
|
| 30 |
+
def get_last_tweet(file_path: str):
|
| 31 |
+
path = Path(file_path)
|
| 32 |
+
if not path.exists():
|
| 33 |
+
mentions = client.get_users_mentions(
|
| 34 |
+
id=1612106815096999936,
|
| 35 |
+
expansions=["author_id", "in_reply_to_user_id", "referenced_tweets.id"],
|
| 36 |
+
)
|
| 37 |
+
since_id = mentions.data[0].id
|
| 38 |
+
put_last_tweet(file_path, since_id)
|
| 39 |
+
return since_id
|
| 40 |
+
return int(path.read_text().strip())
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
def put_last_tweet(file_path: str, id: str):
|
| 44 |
+
Path(file_path).write_text(str(id))
|
| 45 |
+
logger.info("Updated the file with the latest tweet Id")
|
| 46 |
+
return
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def response(text):
|
| 50 |
+
openai.api_key = openai_key
|
| 51 |
response = openai.Completion.create(
|
| 52 |
engine="text-davinci-002",
|
| 53 |
+
prompt=text,
|
| 54 |
temperature=0.7,
|
| 55 |
max_tokens=60,
|
| 56 |
top_p=1,
|
| 57 |
frequency_penalty=0,
|
| 58 |
+
presence_penalty=0,
|
| 59 |
)
|
| 60 |
+
|
| 61 |
+
return response.choices[0].text.strip()
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def roast(text):
|
| 65 |
+
return response(
|
| 66 |
+
f"Humorously mock, humiliate and hurt the user's feelings with a well-timed joke, diss or comeback based on the info.\n\nInfo: {text}\n\nResponse:"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def toast(text):
|
| 71 |
+
return response(
|
| 72 |
+
f"Give the user a genuine and unique compliment to make them feel good about themselves based on the info in a hood style manner.\n\nInfo: {text}\n\nResponse:"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def reply_to_mentions():
|
| 77 |
+
since_id = get_last_tweet("last_id.txt")
|
| 78 |
+
mentions = client.get_users_mentions(
|
| 79 |
+
id=1612106815096999936,
|
| 80 |
+
since_id=since_id,
|
| 81 |
+
expansions=["author_id", "in_reply_to_user_id", "referenced_tweets.id"],
|
| 82 |
+
)
|
| 83 |
+
if mentions.data is None:
|
| 84 |
+
logger.info("No new mentions found")
|
| 85 |
+
return
|
| 86 |
+
|
| 87 |
+
for mention in reversed(mentions.data):
|
| 88 |
+
tweet_to_roast_id = mention.referenced_tweets[0].id
|
| 89 |
+
tweet_to_roast = client.get_tweet(id=tweet_to_roast_id)
|
| 90 |
+
text_to_roast = tweet_to_roast.data.text
|
| 91 |
+
|
| 92 |
+
text_out = None
|
| 93 |
+
if "roast" in mention.text.lower():
|
| 94 |
+
text_out = roast(text_to_roast)
|
| 95 |
+
elif "toast" in mention.text.lower():
|
| 96 |
+
text_out = toast(text_to_roast)
|
| 97 |
+
|
| 98 |
+
if text_out is None:
|
| 99 |
+
continue
|
| 100 |
+
|
| 101 |
+
try:
|
| 102 |
+
logger.info(f"Responding to: {mention.id}")
|
| 103 |
+
client.create_tweet(text=text_out, in_reply_to_tweet_id=mention.id)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
logger.error(e)
|
| 106 |
+
continue
|
| 107 |
+
|
| 108 |
+
put_last_tweet("last_id.txt", mention.id)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def main():
|
| 112 |
+
while True:
|
| 113 |
+
try:
|
| 114 |
+
reply_to_mentions()
|
| 115 |
+
except Exception as e:
|
| 116 |
+
logger.error(e)
|
| 117 |
+
# Print more helpful error messages with line number
|
| 118 |
+
import traceback
|
| 119 |
+
|
| 120 |
+
traceback.print_exc()
|
| 121 |
+
|
| 122 |
+
time.sleep(60)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
main()
|