breqdev/flask-discord-interactions

Sending PNG image attachments causes utf-8 error

EndenDragon opened this issue · 3 comments

Describe the bug
When responding with a PNG file attachment, I am getting a UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte error.

@discord.command(name="test", description="Test command!")
def command_test(ctx):
    return Message(
        embed = Embed(
            title = "Embed Title",
            description = "Description",
            color = 2728830,
            image = {"url": "attachment://google.png"},
        ),
        file = ("google.png", open("google.png", "r"), "image/png")
    )

Stack trace:

10.0.2.2 - - [12/Nov/2022 18:42:19] "POST /discord HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/endendragon/.local/lib/python3.7/site-packages/flask/app.py", line 2548, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/endendragon/.local/lib/python3.7/site-packages/flask/app.py", line 2528, in wsgi_app
    response = self.handle_exception(e)
  File "/home/endendragon/.local/lib/python3.7/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/endendragon/.local/lib/python3.7/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/endendragon/.local/lib/python3.7/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/endendragon/.local/lib/python3.7/site-packages/flask/app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/endendragon/.local/lib/python3.7/site-packages/flask_discord_interactions/discord.py", line 851, in interactions
    response, mimetype = result.encode()
  File "/home/endendragon/.local/lib/python3.7/site-packages/flask_discord_interactions/models/message.py", line 244, in encode
    return (multipart.to_string().decode(), multipart.content_type)
  File "/home/endendragon/.local/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 297, in to_string
    return self.read()
  File "/home/endendragon/.local/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 314, in read
    self._load(bytes_to_load)
  File "/home/endendragon/.local/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 203, in _load
    written += part.write_to(self._buffer, amount)
  File "/home/endendragon/.local/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 528, in write_to
    written += buffer.append(self.body.read(amount_to_read))
  File "/home/endendragon/.local/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 576, in read
    return self.fd.read(length)
  File "/usr/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

Categorization
This issue impacts...

  • The library itself
  • Documentation
  • Examples

To Reproduce
A mockup of the source is provided. To test, here is a sample image you may download and put aside next to the script: https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png

Expected behavior
Image to be sent along with the rich embed.

Environment
I am using... (check any that apply, leave unchecked if you don't know)

  • Quart and/or Asyncio
  • A "Bot User"
  • Additional Flask extensions (SQLAlchemy, Flask-CORS, etc.)
  • Additional backend services (Redis, RQ, Celery)

Additional context
I understand there is a 3 second limit in Discord. We'll look into message deferrals later. However the utf8 error is the first barrier in sending image attachments.

This looks like the same problem I'm trying to help fix with #121

Should be fixed in #121 and v2.1.2. Try pip install flask-discord-interactions --upgrade and let me know if that solves your problem!

I'm going to close this for now since it seems like the issue is fixed, feel free to reopen if you're still having trouble!