diff --git a/lxmf_message_fields.py b/lxmf_message_fields.py new file mode 100644 index 0000000..b05db56 --- /dev/null +++ b/lxmf_message_fields.py @@ -0,0 +1,25 @@ +from typing import List + + +# helper class for passing around an lxmf image field +class LxmfImageField: + + def __init__(self, image_type: str, image_bytes: bytes): + self.image_type = image_type + self.image_bytes = image_bytes + + +# helper class for passing around an lxmf file attachment +class LxmfFileAttachment: + + def __init__(self, file_name: str, file_bytes: bytes): + self.file_name = file_name + self.file_bytes = file_bytes + + +# helper class for passing around an lxmf file attachments field +class LxmfFileAttachmentsField: + + def __init__(self, file_attachments: List[LxmfFileAttachment]): + self.file_attachments = file_attachments + diff --git a/web.py b/web.py index d4b5eba..88f4f40 100644 --- a/web.py +++ b/web.py @@ -17,6 +17,7 @@ import base64 from peewee import SqliteDatabase import database +from lxmf_message_fields import LxmfImageField, LxmfFileAttachmentsField, LxmfFileAttachment class ReticulumWebChat: @@ -523,7 +524,9 @@ class ReticulumWebChat: query.execute() # handle sending an lxmf message to reticulum - async def send_message(self, destination_hash, message_content): + async def send_message(self, destination_hash, content: str, + image_field: LxmfImageField = None, + file_attachments_field: LxmfFileAttachmentsField = None): try: @@ -545,8 +548,30 @@ class ReticulumWebChat: lxmf_destination = RNS.Destination(destination_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, "lxmf", "delivery") # create lxmf message - lxmf_message = LXMF.LXMessage(lxmf_destination, self.local_lxmf_destination, message_content, desired_method=LXMF.LXMessage.DIRECT) + lxmf_message = LXMF.LXMessage(lxmf_destination, self.local_lxmf_destination, content, desired_method=LXMF.LXMessage.DIRECT) lxmf_message.try_propagation_on_fail = True + + lxmf_message.fields = {} + + # add file attachments field + if file_attachments_field is not None: + + # create array of [[file_name, file_bytes], [file_name, file_bytes], ...] + file_attachments = [] + for file_attachment in file_attachments_field.file_attachments: + file_attachments.append([file_attachment.file_name, file_attachment.file_bytes]) + + # set field attachments field + lxmf_message.fields[LXMF.FIELD_FILE_ATTACHMENTS] = file_attachments + + # add image field + if image_field is not None: + lxmf_message.fields[LXMF.FIELD_IMAGE] = [ + image_field.image_type, + image_field.image_bytes, + ] + + # register delivery callbacks lxmf_message.register_delivery_callback(self.on_lxmf_sending_state_updated) lxmf_message.register_failed_callback(self.on_lxmf_sending_failed)