File size: 4,238 Bytes
7a7b4e7
805c3e0
7a7b4e7
 
 
36ad45e
68773aa
7a7b4e7
 
 
 
 
 
 
 
 
 
 
 
0a19f94
 
 
 
7a7b4e7
 
 
 
 
 
 
 
1ef8a31
 
7a7b4e7
 
 
 
 
 
 
 
 
 
 
1ef8a31
36ad45e
7a7b4e7
 
 
 
36ad45e
 
7a7b4e7
 
0f33e7d
 
7a7b4e7
0f33e7d
 
 
 
7a7b4e7
0f33e7d
7a7b4e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a19f94
7a7b4e7
 
 
 
 
 
 
 
 
 
d512690
 
 
 
 
 
 
 
 
 
 
 
167ee92
 
 
6f36c51
d512690
167ee92
d512690
 
167ee92
d512690
 
 
 
167ee92
 
d512690
 
7a7b4e7
 
 
 
 
 
 
 
 
0a19f94
7a7b4e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import logging
import os
import time
from pathlib import Path

import openai
import tweepy

logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)
logger.setLevel(logging.INFO)

openai_key = os.environ.get("openai_api_key")
consumer_key = os.environ.get("consumer_key")
consumer_secret_key = os.environ.get("consumer_secret_key")
access_token = os.environ.get("access_token")
access_token_secret = os.environ.get("access_token_secret")
bearer_token = os.environ.get("bearer_token")

home_path = os.environ.get("HOME")
last_id_filepath = home_path + "/app/last_id.txt"


client = tweepy.Client(
    bearer_token=bearer_token,
    consumer_key=consumer_key,
    consumer_secret=consumer_secret_key,
    access_token=access_token,
    access_token_secret=access_token_secret,
    wait_on_rate_limit=True,
)


def get_last_tweet(file_path: str):
    path = Path(file_path)
    if not path.exists():
        mentions = client.get_users_mentions(
            id=1612106815096999936,
            expansions=["author_id", "in_reply_to_user_id", "referenced_tweets.id"],
        )
        since_id = mentions.data[0].id
        put_last_tweet(file_path, since_id)
        return since_id
    return int(path.read_text().strip())


def put_last_tweet(file_path: str, id: str):
    Path(file_path).write_text(str(id))
    logger.info("Updated the file with the latest tweet Id")
    return


def response(text):
    openai.api_key = openai_key
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=text,
        temperature=0.7,
        max_tokens=60,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
    )

    return response.choices[0].text.strip()


def roast(text):
    return response(
        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:"
    )


def toast(text):
    return response(
        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:"
    )


def reply_to_mentions():
    since_id = get_last_tweet(last_id_filepath)
    mentions = client.get_users_mentions(
        id=1612106815096999936,
        since_id=since_id,
        expansions=["author_id", "in_reply_to_user_id", "referenced_tweets.id"],
    )
    if mentions.data is None:
        logger.info("No new mentions found")
        return

    for mention in reversed(mentions.data):
        try:
            if mention.author_id == 1612106815096999936:
                continue
            
            if mention.referenced_tweets is None:
                logger.info(f"Skipping {mention.id} as it is not a reply")
            tweet_to_roast_id = mention.referenced_tweets[0].id
            tweet_to_roast = client.get_tweet(id=tweet_to_roast_id)
            text_to_roast = tweet_to_roast.data.text

            text_out = None
            text_in = mention.text.lower()
            text_in = text_in.replace('@roastortoastgpt', '')
            text_in = text_in.replace('roastortoastgpt','')

            logger.info(f"In Text: {text_in}")
            if "roast" in text_in:
                logger.info(f"Roasting!")
                text_out = roast(text_to_roast)
            elif "toast" in text_in:
                logger.info("Toasting!")
                text_out = toast(text_to_roast)

            if text_out is None:
                continue

            logger.info(f"Out Text: {text_out}")
        except Exception as e:
            logger.error(e)
            continue

        try:
            logger.info(f"Responding to: {mention.id}")
            client.create_tweet(text=text_out, in_reply_to_tweet_id=mention.id)
        except Exception as e:
            logger.error(e)
            continue

    put_last_tweet(last_id_filepath, mention.id)


def main():
    while True:
        try:
            reply_to_mentions()
        except Exception as e:
            logger.error(e)
            # Print more helpful error messages with line number
            import traceback

            traceback.print_exc()

        time.sleep(60)


if __name__ == "__main__":
    main()