bot: sending out messages works, images have problems still

pull/13/head
missytake 2023-10-09 11:03:59 +02:00
parent b50f6c9cbf
commit 5b00fd92a2
2 changed files with 66 additions and 1 deletions

View File

@ -5,8 +5,9 @@ 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
from .commands import help_message, set_display_name, set_avatar, start_chat
class SetupPlugin:
@ -73,6 +74,24 @@ class RelayPlugin:
if arguments[0] == "/set_avatar":
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),
)
if "success" in result:
for msg in outside_chat.get_messages():
self.forward_to_relay_group(msg)
self.reply(message.chat, result, quote=message)
else:
logging.debug("Ignoring message, just the crew chatting")
@ -181,3 +200,9 @@ 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

@ -1,6 +1,7 @@
import logging
import deltachat
from deltachat.capi import lib as dclib
def help_message() -> str:
@ -9,6 +10,7 @@ def help_message() -> str:
:return: the help message
"""
help_text = """
Start a chat:\t/start_chat alice@example.org,bob@example.org Chat_Title Hello friends!
Change the bot's name:\t/set_name <name>
Change the bot's avatar:\t/set_avatar (attach image)
Show this help text:\t\t/help
@ -38,3 +40,41 @@ def set_avatar(
account.set_avatar(message.filename)
crew.set_profile_image(message.filename)
return "Avatar changed to this image."
def start_chat(
ac: deltachat.Account,
recipients: [],
title: str,
text: str,
attachment: str,
view_type: str,
) -> (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
:return: the outside chat and a success/failure message
"""
logging.info(
"Sending message to %s with subject '%s': %s",
", ".join(recipients),
title,
text,
)
chat = ac.create_group_chat(title, recipients)
msg = deltachat.Message.new_empty(ac, view_type=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)
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()