r/ModCoord • • 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)
152 Upvotes

96 comments sorted by

View all comments

Show parent comment

2

u/enn_nafnlaus Jun 12 '23

Okay, so I don't use a password, I just connect with my gmail account. What should I do then?

Yes, I did include my sub. I also picked a random user agent.

3

u/BuckRowdy Jun 12 '23

To be honest I think you would have to convert your account to a reddit account. The way these reddit apps work is with a username, and password. I don't think you can run one any other way. I've never run across this specific issue though.

2

u/enn_nafnlaus Jun 12 '23

ED: I just created a password (wasn't hard, just use the forgot password link) and the bot works, BUT, since it has no time limit, it's going and re-replying to all the people I already replied to! :( Need to have a time limit in it.

2

u/enn_nafnlaus Jun 12 '23

ED2: Modifications to the script to make it check:

#!/usr/bin/python3
import praw
import time
import datetime
import pytz

# User-configurable date and time

since = "2023-06-12 11:30:00"
timezone = pytz.utc

processed_mail = []
since_datetime = datetime.datetime.strptime(since, "%Y-%m-%d %H:%M:%S")
since_datetime = timezone.localize(since_datetime)
while True:
try:
print("Fetching modmail conversations...")
conversations = reddit.subreddit(sub_name).mod.stream.modmail_conversations()
for conv in conversations:
if conv.id not in processed_mail:
for message in conv.messages:
date = datetime.datetime.strptime(message.date, "%Y-%m-%dT%H:%M:%S.%f%z")
body = message.body_markdown.lower()
if any(keyword in body for keyword in keywords):
print(f"Found keyword in message with ID {conv.id} from user {conv.user.name}")
if date > since_datetime:
conv.reply(response_message, author_hidden=True)
conv.archive()
print(f"Replied to message ID {conv.id} from user {conv.user.name} with the preset response\n")
else:
print("Too old; skipping:", date)
processed_mail.append(conv.id)
except Exception as e:
print(f"An error occurred: {e}")
print("Sleeping for 60 seconds before retrying...")
time.sleep(60)

2

u/BuckRowdy Jun 12 '23

This was a quick hatchet job just to get something up. If you pass skip_existing=True to the function it will do the same thing I think. Either way I’ll check and update tonight.

1

u/enn_nafnlaus Jun 12 '23

I guess I'll just stop replying to the automated-feeling requests, and only (manually) reply to the ones that put in a custom message for why they want to join.

2

u/enn_nafnlaus Jun 12 '23

My config looks like:

# Create a Reddit instance
reddit = praw.Reddit(
client_id="XXXXXXXXXXXXXX-X_XXXXX",
client_secret="XXXXX-XXXXXXXXXXXXXXXXXXXXXXXX",
#password="",
user_agent="Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumi...."
username="enn_nafnlaus"
)
sub_name = "Oobabooga"
keywords = ['private', 'blackout', 'dark', 'closed', 'join', 'shut down']
response_message = """Autoreply due to high volumes!) Hi - if this is about joining...

"""