Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modmail! #50

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions bubbles/commands/periodic/modmail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from bubbles.config import app, reddit, rooms_list

from slack_sdk.models import blocks


sub = reddit.subreddit("transcribersofreddit")


def process_modmail(message_state: str) -> None:
for convo in sub.modmail.conversations(state=message_state):
if not convo.last_unread:
# this attribute will have a timestamp if there's an unread message and
# will be empty if we've been here before.
continue
# The other end of the message is either in .participant (a Redditor instance)
# or .participant_subreddit (a dict).
if convo.participant != {}:
recipient = f"u/{convo.participant.name}"
elif convo.participant_subreddit != {}:
recipient = f"r/{convo.participant_subreddit['name']}"
else:
recipient = "unknown participant"

latest_message = convo.messages[-1]
convo.read()

app.client.chat_postMessage(
channel=rooms_list["mod_messages"],
as_user=True,
icon_emoji="modmail",
username="Modmail!",
blocks=[
blocks.SectionBlock(text=f"*u/{latest_message.author.name}* :arrow_right: *{recipient}*"),
blocks.DividerBlock(),
blocks.SectionBlock(text=latest_message.body_markdown),
blocks.DividerBlock(),
blocks.ContextBlock(elements=[
blocks.MarkdownTextObject(text=f"Conversation ID: {convo.id}"),
blocks.MarkdownTextObject(text=f"Message ID: {latest_message.id}")
])
]
)


def modmail_callback() -> None:
unread_counts: dict[str, int] = sub.modmail.unread_count()

for message_state, count in unread_counts:
if count > 0:
process_modmail(message_state)
14 changes: 14 additions & 0 deletions bubbles/tl_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from bubbles.commands.periodic.banbot_check import banbot_check_callback
from bubbles.commands.periodic.etsy_sale_check import etsy_recent_sale_callback
from bubbles.commands.periodic.get_in_progress_posts import get_in_progress_callback
from bubbles.commands.periodic.modmail import modmail_callback
from bubbles.commands.periodic.rule_monitoring import rule_monitoring_callback
from bubbles.commands.periodic.welcome_ping import (
welcome_ping_callback,
Expand Down Expand Up @@ -90,6 +91,19 @@ class Meta:
regular_interval = timedelta(hours=12)


class CheckModmail(TLJob):
def job(self):
try:
modmail_callback()
except Exception as e:
tb_str = "".join(traceback.format_exception(None, e, e.__traceback__))
logging.error(f"Failed to check for rule changes: {e}\n{tb_str}")

class Meta:
start_interval = timedelta(seconds=0) # start now
regular_interval = timedelta(seconds=30)


class RuleMonitoring(TLJob):
def job(self):
try:
Expand Down