fix: start_chat can contain attachments now

pull/13/head
missytake 2023-10-09 15:03:26 +02:00
parent 5b00fd92a2
commit ffa7fa4563
2 changed files with 35 additions and 36 deletions

View File

@ -5,7 +5,6 @@ import pickledb
import deltachat
from deltachat import account_hookimpl
from deltachat.capi import lib as dclib
from deltachat.message import _view_type_mapping
from .commands import help_message, set_display_name, set_avatar, start_chat
@ -75,22 +74,13 @@ class RelayPlugin:
result = set_avatar(self.account, message, self.crew)
self.reply(message.chat, result, quote=message)
if arguments[0] == "/start_chat":
recipients = arguments[1].split(",")
title = arguments[2].replace('_', ' ')
words = []
for i in range(3, len(arguments)):
words.append(arguments[i])
outside_chat, result = start_chat(
self.account,
recipients,
title,
" ".join(words),
message.filename if message.filename else "",
self.get_message_view_type(message),
message,
)
if "success" in result:
for msg in outside_chat.get_messages():
self.forward_to_relay_group(msg)
self.forward_to_relay_group(msg, started_by_crew=True)
self.reply(message.chat, result, quote=message)
else:
logging.debug("Ignoring message, just the crew chatting")
@ -131,7 +121,7 @@ class RelayPlugin:
return
outside_chat.send_msg(message)
def forward_to_relay_group(self, message: deltachat.Message):
def forward_to_relay_group(self, message: deltachat.Message, started_by_crew=False):
"""forward a request to a relay group; create one if it doesn't exist yet."""
outsider = message.get_sender_contact().addr
crew_members = self.crew.get_contacts()
@ -148,10 +138,14 @@ class RelayPlugin:
group_name, crew_members, verified=False
)
# relay_group.set_profile_image("assets/avatar.jpg")
relay_group.send_text(
"This is the relay group for %s; I'll only forward 'direct replies' to the outside."
% (message.chat.get_name())
)
if started_by_crew:
explanation = f"We started a chat with {message.chat.get_name()}. This was our first message:"
else:
explanation = (
f"This is the relay group for {message.chat.get_name()}; "
"I'll only forward 'direct replies' to the outside."
)
relay_group.send_text(explanation)
relay_mappings = self.kvstore.get("relays")
relay_mappings.append(tuple([message.chat.id, relay_group.id]))
self.kvstore.set("relays", relay_mappings)
@ -200,9 +194,3 @@ class RelayPlugin:
if mapping[0] == outside_id:
return self.account.get_chat_by_id(mapping[1])
return None
def get_message_view_type(self, message: deltachat.Message) -> str:
"""Get the view_type of a Message."""
for view_name, view_code in _view_type_mapping.items():
if view_code == message._view_type:
return view_name

View File

@ -2,6 +2,7 @@ import logging
import deltachat
from deltachat.capi import lib as dclib
from deltachat.message import _view_type_mapping
def help_message() -> str:
@ -44,37 +45,47 @@ def set_avatar(
def start_chat(
ac: deltachat.Account,
recipients: [],
title: str,
text: str,
attachment: str,
view_type: str,
command: deltachat.Message,
) -> (deltachat.Chat, str):
"""Start a chat with one or more outsiders.
:param ac: the account object of the bot
:param recipients: A list with email addresses to be added to the group
:param title: The title of the group
:param text: The test of the first message
:param attachment: (optional) an attachment, can be empty string
:param view_type: the view_type of the message
:param command: the message with the command
:return: the outside chat and a success/failure message
"""
arguments = command.text.split(" ")
recipients = arguments[1].split(",")
title = arguments[2].replace("_", " ")
words = []
for i in range(3, len(arguments)):
words.append(arguments[i])
text = " ".join(words)
attachment = command.filename if command.filename else ""
view_type = get_message_view_type(command)
logging.info(
"Sending message to %s with subject '%s': %s",
"Sending %s message to %s with subject '%s': %s",
view_type,
", ".join(recipients),
title,
text,
)
chat = ac.create_group_chat(title, recipients)
msg = deltachat.Message.new_empty(ac, view_type=view_type)
msg = deltachat.Message.new_empty(ac, view_type)
msg.set_text(text)
if attachment:
logging.info("Message has a %s attachment with path %s", view_type, attachment)
msg.set_file(attachment, view_type)
msg.set_file(attachment)
sent_id = dclib.dc_send_msg(ac._dc_context, chat.id, msg._dc_msg)
if sent_id == msg.id:
return chat, "Chat successfully created."
else:
logging.error("Can't send message. sent_id: %s, msg.id: %s", sent_id, msg.id)
return chat, "Something went wrong...\n\n" + help_message()
def get_message_view_type(message: deltachat.Message) -> str:
"""Get the view_type of a Message."""
for view_name, view_code in _view_type_mapping.items():
if view_code == message._view_type:
return view_name