r/ModCoord • u/BuckRowdy • Jun 12 '23
Here's a Python Modmail Auto Responder to declutter your modmail.
Edit: This bot is now on github so it can be improved by the community. https://github.com/notesbot/auto_respond
import praw
import time
# Create a Reddit instance
reddit = praw.Reddit(
client_id="",
client_secret="",
password="",
user_agent="",
username=""
)
sub_name = "YOUR_SUBREDDIT"
keywords = ['private', 'blackout', 'dark', 'closed', 'join', 'shut down']
response_message = "Hello and thank you for your message. It appears that you are writing in about the Reddit wide blackout to protest API changes. We would like to direct you to [this post](https://www.reddit.com/r/Save3rdPartyApps/comments/1476ioa/reddit_blackout_2023_save_3rd_party_apps/) where you can find, among other information, a list of participating subreddits. While we appreciate your interest in this topic, at this time we are not commenting via modmail on this. Please help us keep our modmail clear for urgent information. Thank you for your cooperation.\n\nIf your issue was resolved, please just ignore this message.\n\n- If your issue was not resolved, we apologize. Please respond to this message to send it back to the top of our queue. Neither Reddit's modmail system nor Reddit's moderators are perfect, so sometimes we overlook modmail tickets.\n\n Thank you"
processed_mail = []
while True:
try:
print("Fetching modmail conversations...")
conversations = reddit.subreddit(sub_name).mod.stream.modmail_conversations(skip_existing=True)
for conv in conversations:
if len([author for author in conv.authors if author.is_admin]) > 0:
reddit.redditor("mod_mailer").message(subject=f"{conv.owner}", message =f"New Admin modmail in r/{conv.owner}\n\n---\n\nNew modmail message from admins https://mod.reddit.com/mail/all/{conv.id}\n\nSubject: {conv.subject}")
conv.archive()
if conv.id not in processed_mail:
for message in conv.messages:
body = message.body_markdown.lower()
if any(keyword in body for keyword in keywords):
print(f"Found modmail in r/{conv.owner} - keyword in message with ID {conv.id} from user {conv.user.name}")
conv.reply(body=response_message, author_hidden=True)
conv.archive()
processed_mail.append(conv.id)
print(f"Replied to message ID {conv.id} from user {conv.user.name} with the preset response\n")
#print(processed_mail)
except Exception as e:
print(f"An error occurred: {e}")
print("Sleeping for 60 seconds before retrying...")
time.sleep(60)
151
Upvotes
•
u/BuckRowdy Jun 12 '23 edited Jun 12 '23
I updated the script to notify your entire mod team if an admin modmail is sent.
Edit: some of you are asking how to run this script. Here's a guide I wrote about how to run one. https://www.reddit.com/r/modguide/comments/s3xwbu/how_to_run_a_basic_python_script_for_reddit_from/
if you've never done this before, it is a little bit involved.
13
u/tweedge Jun 12 '23 edited Jun 12 '23
Ah lol I should have read this sub first! r/cybersecurity just made our own, same issue as you, even though we put up a website ahead of time explaining what's going on :/
Our code is here: https://github.com/r-cybersecurity/modmail-autoreply
Edit: our code has been updated this morning to gracefully handle Reddit outages, like the one experienced today (eyeroll)
5
u/chaseoes Jun 12 '23 edited Jun 12 '23
Yet another example of how Reddit's mobile app is inadequate. We shouldn't need to do this.
Suggest throwing it on github so the community can help improve it.
Edit: Got this error:
3
u/[deleted] Jun 12 '23
Why all the modmail request that I’m Getting is users with old accounts and years of not no posts/comments, spammers?
2
u/troop98 Jun 12 '23 edited Jun 12 '23
dumb question, but all instances of "subreddit name" or "your subreddit" are to be replaced with the subreddit name right? or does it not matter
1
u/snipeftw Jun 12 '23
Can someone put together an in depth guide on how to set this up?
3
u/CapriGuitar Jun 12 '23
Thank you so much. I've also had a modmail deluge.
1
u/AutoModerator Jun 12 '23
If you are a moderator and you are interested in automating taking your subreddit private, a bot has been created for this purpose.
Please see here: https://www.reddit.com/r/ModCoord/comments/142rzna/a_bot_to_make_your_subreddit_private/
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/More-Athlete1175 Jun 12 '23
I really wish I could understand the language of which you're referring too 😕
1
u/MangyCanine Jun 12 '23
Can this be modified to catch keywords in the Subject, too?
[join]is common, yet the keywords are only matched in the body.Also, I had to indent the whole
try ... exceptblock:1
u/Minute_Helicopter_97 Jun 12 '23
Convenience as a post.
1
u/ANakedRobot Jun 12 '23
Getting this:
If I add '#!/usr/bin/env python3' to the top of the file, I get this:
I have created my OAuth app, have my client id, secret, username, and pw. No MFA in use.
1
u/PantslessDan Jun 12 '23
Thank you so much for this!
1
u/somepianoplayer Jun 12 '23
Thank you, this helps a lot for my other account and this will help a lot of other mods, I would give you an award if I had one
0
u/[deleted] Jun 12 '23
[deleted] — view deleted comment