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

Provide shortcuts for direct messages #233

Merged
merged 2 commits into from
Jun 21, 2021
Merged
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
72 changes: 50 additions & 22 deletions mmpy_bot/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,24 @@ def create_post(
file_ids = (
self.upload_files(file_paths, channel_id) if len(file_paths) > 0 else []
)

post = dict(
channel_id=channel_id,
message=message,
file_ids=file_ids,
root_id=root_id,
props=props,
)

if ephemeral_user_id:
return self.posts.create_ephemeral_post(
{
"user_id": ephemeral_user_id,
"post": {
"channel_id": channel_id,
"message": message,
"file_ids": file_ids,
"root_id": root_id,
"props": props,
},
"post": post,
}
)

return self.posts.create_post(
{
"channel_id": channel_id,
"message": message,
"file_ids": file_ids,
"root_id": root_id,
"props": props,
}
)
return self.posts.create_post(post)

def get_thread(self, post_id: str):
"""Wrapper around driver.posts.get_thread, which for some reason returns
Expand Down Expand Up @@ -116,31 +111,64 @@ def reply_to(
file_paths: Optional[Sequence[str]] = None,
props: Dict = {},
ephemeral: bool = False,
direct: bool = False,
):
"""Reply to the given message.

Supports sending ephemeral messages if the bot permissions allow it. If the
message is part of a thread, the reply will be added to that thread.

Also supports replying privately by setting direct=True.
"""
if file_paths is None:
file_paths = []

if ephemeral:
return self.create_post(
channel_id=message.channel_id,
if direct and not message.is_direct_message:
# NOTE we explicitly don't pass root_id as it would refer to a
# post in a channel not a private session
direct_args = dict(
receiver_id=message.user_id,
message=response,
root_id=message.reply_id,
file_paths=file_paths,
props=props,
ephemeral_user_id=message.user_id,
ephemeral_user_id=message.user_id if ephemeral else None,
)

return self.create_post(
return self.direct_message(**direct_args)

reply_args = dict(
channel_id=message.channel_id,
message=response,
root_id=message.reply_id,
file_paths=file_paths,
props=props,
ephemeral_user_id=message.user_id if ephemeral else None,
)

return self.create_post(**reply_args)

def direct_message(
self,
receiver_id: str,
message: str,
file_paths: Optional[Sequence[str]] = None,
root_id: str = "",
props: Dict = {},
ephemeral_user_id: Optional[str] = None,
):
# Private/direct messages are sent to a special channel that
# includes the bot and the recipient
direct_id = self.channels.create_direct_message_channel(
[self.user_id, receiver_id]
)["id"]

return self.create_post(
channel_id=direct_id,
message=message,
root_id=root_id,
file_paths=file_paths,
props=props,
ephemeral_user_id=ephemeral_user_id,
)

def respond_to_web(self, event: WebHookEvent, response):
Expand Down