caronc / apprise

Apprise - Push Notifications that work with just about every platform!
https://hub.docker.com/r/caronc/apprise
BSD 2-Clause "Simplified" License
10.9k stars 384 forks source link

Attachments: support for byte buffer? #1112

Closed joergschultzelutter closed 2 months ago

joergschultzelutter commented 2 months ago

:question: Support for byte buffer attachments?

I am in the process of migrating several of my programs' native messaging capabilities to Apprise; this will remove the burden of constantly updating these programs whenever there is a change in a messenger's native API.

Several of these programs generate map images (format = png). Rather than writing these images to disk, I write them to a byte buffer and then use the byte buffer as the actual image reference. This approach works with e.g. the native Telegram API.

Code excerpt from where the image gets generated:

` iobuffer = io.BytesIO()

    if staticmaps.cairo_is_supported():
        image = context.render_cairo(800, 500)
        image.write_to_png(iobuffer)
    else:
        image = context.render_pillow(800, 500)
        image.save(iobuffer, format="png")

    # reset the buffer position
    iobuffer.seek(0)

    # get the buffer value and return it. This is our actual image
    view = iobuffer.getvalue()

`

With the Telegram Python API, I am able to send that picture 'as is' mybot.send_photo(chat_id=user_id, photo=view)

The obvious benefit is that I do not need to create temporary files on my hard drive and store this image data e.g. in dict/list structures which then can be accessed directly without e.g. reading external files from disk

Now that the program's messaging code has been migrated to Apprise, I wanted use the same approach and pass that byte buffer to Apprise as attachment. However, Apprise obviously expects a file name and chokes on the input.

apobj.notify(body=apprise_message, title=apprise_header, tag="all",attach=view) #does not work

2024-04-19 19:35:44,120 AppriseAttachment -ERROR- An invalid attachment url (type=<class 'bytes'>) was specified.

I am aware that my approach is probably an edge case. Nevertheless, I was wondering if there is any option that I missed in the documentation which would allow me to work with an internal byte buffer object rather than having to write the associated file to disk (and then use it in Apprise as file reference)

Thank you Chris!

caronc commented 2 months ago

It looks like you're trying to leverage libraries from the pillow library. Apprise doesn't have that built into it.

Adding an AttachIOStream to work along AttachFile, and AttachHTTP isn't out of the question. It's like you said though... very edge case like though. For now you would actually have to write to disk and pass it directly.

joergschultzelutter commented 2 months ago

Thought so :-) Thanks for your swift response; I will refactor the code and use temporary files.