fix: start_chat can contain attachments now

This commit is contained in:
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 import deltachat
from deltachat import account_hookimpl from deltachat import account_hookimpl
from deltachat.capi import lib as dclib 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 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) result = set_avatar(self.account, message, self.crew)
self.reply(message.chat, result, quote=message) self.reply(message.chat, result, quote=message)
if arguments[0] == "/start_chat": 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( outside_chat, result = start_chat(
self.account, self.account,
recipients, message,
title,
" ".join(words),
message.filename if message.filename else "",
self.get_message_view_type(message),
) )
if "success" in result: if "success" in result:
for msg in outside_chat.get_messages(): 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) self.reply(message.chat, result, quote=message)
else: else:
logging.debug("Ignoring message, just the crew chatting") logging.debug("Ignoring message, just the crew chatting")
@ -131,7 +121,7 @@ class RelayPlugin:
return return
outside_chat.send_msg(message) 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.""" """forward a request to a relay group; create one if it doesn't exist yet."""
outsider = message.get_sender_contact().addr outsider = message.get_sender_contact().addr
crew_members = self.crew.get_contacts() crew_members = self.crew.get_contacts()
@ -148,10 +138,14 @@ class RelayPlugin:
group_name, crew_members, verified=False group_name, crew_members, verified=False
) )
# relay_group.set_profile_image("assets/avatar.jpg") # relay_group.set_profile_image("assets/avatar.jpg")
relay_group.send_text( if started_by_crew:
"This is the relay group for %s; I'll only forward 'direct replies' to the outside." explanation = f"We started a chat with {message.chat.get_name()}. This was our first message:"
% (message.chat.get_name()) 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 = self.kvstore.get("relays")
relay_mappings.append(tuple([message.chat.id, relay_group.id])) relay_mappings.append(tuple([message.chat.id, relay_group.id]))
self.kvstore.set("relays", relay_mappings) self.kvstore.set("relays", relay_mappings)
@ -200,9 +194,3 @@ class RelayPlugin:
if mapping[0] == outside_id: if mapping[0] == outside_id:
return self.account.get_chat_by_id(mapping[1]) return self.account.get_chat_by_id(mapping[1])
return None 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 import deltachat
from deltachat.capi import lib as dclib from deltachat.capi import lib as dclib
from deltachat.message import _view_type_mapping
def help_message() -> str: def help_message() -> str:
@ -44,37 +45,47 @@ def set_avatar(
def start_chat( def start_chat(
ac: deltachat.Account, ac: deltachat.Account,
recipients: [], command: deltachat.Message,
title: str,
text: str,
attachment: str,
view_type: str,
) -> (deltachat.Chat, str): ) -> (deltachat.Chat, str):
"""Start a chat with one or more outsiders. """Start a chat with one or more outsiders.
:param ac: the account object of the bot :param ac: the account object of the bot
:param recipients: A list with email addresses to be added to the group :param command: the message with the command
: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
:return: the outside chat and a success/failure message :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( logging.info(
"Sending message to %s with subject '%s': %s", "Sending %s message to %s with subject '%s': %s",
view_type,
", ".join(recipients), ", ".join(recipients),
title, title,
text, text,
) )
chat = ac.create_group_chat(title, recipients) 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) msg.set_text(text)
if attachment: if attachment:
logging.info("Message has a %s attachment with path %s", view_type, 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) sent_id = dclib.dc_send_msg(ac._dc_context, chat.id, msg._dc_msg)
if sent_id == msg.id: if sent_id == msg.id:
return chat, "Chat successfully created." return chat, "Chat successfully created."
else: else:
logging.error("Can't send message. sent_id: %s, msg.id: %s", sent_id, msg.id) 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() 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