add support in send_message function for adding images and file attachments

This commit is contained in:
liamcottle 2024-05-04 22:45:16 +12:00
commit dd42da3bd9
2 changed files with 52 additions and 2 deletions

25
lxmf_message_fields.py Normal file
View file

@ -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

29
web.py
View file

@ -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)