Skip to content

Unable to upload attachment as bytes - Solution #601

@dvrhax

Description

@dvrhax

To start I'll say I'm ignorant of the pull request processor else I would have done this there.

My issue was is that I have a mmpy_bot that listens on MQTT and post messages. Recently I needed to upload an image instead of just text and as that image comes over as bytes already, I didn't want to save it and reupload it.

So I added this function to driver.py:

def upload_file_bytes(self, name: str, file_bytes: bytes, channel_id: str):
        file_list = [
            (
                "files",
                (
                    name,
                    file_bytes,
                ),
            )
        ]
        result = self.files.upload_file(
            files=file_list, data={"channel_id": channel_id}
        )
        print(result)
        return [info["id"] for info in result["file_infos"]][0]

*Without the print this doesn't return, so not sure what race condition is going on

I then also modified create_post with this parameter declaration

file_ids: Optional[Sequence[str]] = None

and then added a conditional around the file_ids creation in the body

if file_paths:
            file_ids = (
                self.upload_files(file_paths, channel_id) if len(file_paths) > 0 else []
            )

Let me know if there is something I'm missing and there's a better way to do this or if I can help in some other way. Would appreciate this getting merged so that I can continue to stay up to date, or I can always fork.

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions