-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
119 lines (90 loc) · 5.13 KB
/
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
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
# Happy New Year! 2025
import praw
import os
from openai import OpenAI
from dotenv import load_dotenv
#praw is a Python wrapper for the Reddit API
username = "" # your Reddit username
password = "" # password you use to login to reddit
client_id = "" # under personal token
client_secret = "" # next to the word secret
load_dotenv()
api_key = os.getenv('OPENAI_API_KEY') # fill in your key in .env
''' Remember to replace the above values with your values'''
reddit = praw.Reddit( #creating a reddit instance with the above credentials, user_agent is a string that describes the bot
client_id=client_id,
client_secret=client_secret,
user_agent="test_bot",
username=username,
password=password
)
print(reddit.user.me()) #prints the username of the bot
subreddit = reddit.subreddit("learnpython") #specifying the subreddit to monitor, use your own subreddit of your choice
print(subreddit)
hot_25_submissions = subreddit.hot(limit=25) #the top 25 hot submissions in the subreddit of learnpython
for submission in hot_25_submissions:
print("Title: ", submission.title) # prints the title of the submission
print("Text: ", submission.selftext) # prints the text of the submission
print("Score: ", submission.score) # prints the score of the submission
# The code above will read what is on the front page of the subreddit learnpython
# now let's do the same for the other categories of the subreddit
print('\033[1m' + 'new' + '\033[0m') #special print statement to make the text bold
new_25_submissions = subreddit.new(limit=25) #the top 25 new submissions in the subreddit of learnpython
for submission in new_25_submissions:
print("Title: ", submission.title) # prints the title of the submission
print("Text: ", submission.selftext) # prints the text of the submission
print("Score: ", submission.score) # prints the score of the submission
print('\033[1m' + 'top' + '\033[0m')
top_25_submissions = subreddit.top(limit=25, time_filter='all') #the top 25 top submissions in the subreddit of learnpython
for submission in top_25_submissions:
print("Title: ", submission.title) # prints the title of the submission
print("Text: ", submission.selftext) # prints the text of the submission
print("Score: ", submission.score) # prints the score of the submission
print('\033[1m' + 'rising' + '\033[0m')
rising_25_submissions = subreddit.rising(limit=25) #the top 25 rising submissions in the subreddit of learnpython
for submission in rising_25_submissions:
print("Title: ", submission.title) # prints the title of the submission
print("Text: ", submission.selftext) # prints the text of the submission
print("Score: ", submission.score) # prints the score of the submission
print('\033[1m' + 'END' + '\033[0m')
# Now we submit our post to the subreddit
subreddit = reddit.subreddit("testingground4bots") #specifying the subreddit to monitor, no ban on this subreddit
print(subreddit)
subreddit.submit("This is a test post", selftext="This is a test post by a bot, How are you?")
subreddit.submit("This is a test post 2", selftext="Happy New Year! 2025")
#submitting a post to the subreddit
# submit(title, description) is a function that submits a post to the subreddit
# now we will comment on a post, or also fetch comments
submission = reddit.submission("1hq2eye") # sample submission id, use whatever you want
comments = submission.comments # url, find the mixed letter number value between comments and post title
print(submission.title) #prints the title of the submission
print(submission.comments) #prints the comments of the submission, all of em
# commentforrest is a function that prints the comments of the submission, read more about it in the documentation
print(len(submission.comments)) #prints the number of comments in the submission
comments.replace_more(limit=None) #replaces the more comments with the actual comments, increase comments
print(len(submission.comments)) #prints the number of comments in the submission
# ai response to comments using chatgpt and your API KEY
for comment in comments:
if 'GitHub' in comment.body:
ai_input = comment.body
response = OpenAI().chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": ai_input}
],
max_tokens=100
)
comment.reply(response.choices[0].message.content)
for comment in comments:
if 'GitHub' in comment.body:
comment.reply("I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.") # standard without AI
'''
In summary, this program does the following:
1. Log in to Reddit using the credentials provided
2. Print the top 25 hot submissions in the subreddit learnpython, as well as similar information for the top, new, and rising submissions
3. Submit two posts to the subreddit testingground4bots
4. Comments on a post in the subreddit learnpython
5. Uses the OpenAI API to respond to comments that contain the word "GitHub"
Check my README for any additional information
'''