Search code examples
pythonencodingtelegram-botpython-telegram-bot

UnicodeDecodeError when trying to sendPhoto via python-telegram-bot


I'm using python-telegram-bot with Python 2.7. Right as the documentation suggests, this is the method I use to send the photo from disk:

bot.sendPhoto(update.message.chat_id, photo=open(card.image.path, 'rb'))

Where card.image.path is a full path to JPG file. When executing this method I get UnicodeDecode error. Please see the full traceback below.

Traceback (most recent call last):
  File "/projects/gcards/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/projects/gcards/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/projects/gcards/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/projects/gcards/venv/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/projects/gcards/venv/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "./frontend/telegram/views/callback.py", line 31, in post
    bot_commands.send_card(bot, update, card=card)
  File "./frontend/telegram/views/commands.py", line 22, in send_card
    post_message = bot.sendPhoto(update.message.chat_id, photo=open(card.image.path, 'rb'))
  File "/projects/gcards/venv/lib/python2.7/site-packages/telegram/bot.py", line 128, in decorator
    result = func(self, *args, **kwargs)
  File "/projects/gcards/venv/lib/python2.7/site-packages/telegram/bot.py", line 145, in decorator
    return Bot._post_message(url, data, kwargs)
  File "/projects/gcards/venv/lib/python2.7/site-packages/telegram/bot.py", line 175, in _post_message
    network_delay=network_delay)
  File "/projects/gcards/venv/lib/python2.7/site-packages/telegram/utils/request.py", line 77, in decorator
    return func(*args, **kwargs)
  File "/projects/gcards/venv/lib/python2.7/site-packages/telegram/utils/request.py", line 165, in post
    data=data.to_form(),
  File "/projects/gcards/venv/lib/python2.7/site-packages/telegram/inputfile.py", line 147, in to_form
    return InputFile._parse(form)
  File "/projects/gcards/venv/lib/python2.7/site-packages/telegram/inputfile.py", line 165, in _parse
    return '\r\n'.join(form)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

I investigated the library and as far I as understood, it fails to decode the uploaded file. I tried things like:

with io.open(card.image.path, 'r', encoding='utf8') as photo:
            bot.sendPhoto(update.message.chat_id, photo=photo.read())

But that resulted in another encoding error of different kind. Any ideas on how do I get the photo uploaded?

UPDATE

As far as I sorted out - sendPhoto() puts chat_id and photo into data dict and here are library methods from the end of the traceback:

The one used in data.to_form()

def to_form(self):
        """
        Returns:
            str:
        """
        form = []
        form_boundary = '--' + self.boundary

        # Add data fields
        for name, value in self.data.items():
            form.extend([
                form_boundary,
                'Content-Disposition: form-data; name="%s"' % name,
                '',
                str(value)
            ])

        # Add input_file to upload
        form.extend([
            form_boundary,
            'Content-Disposition: form-data; name="%s"; filename="%s"' % (
                self.input_name, self.filename
            ),
            'Content-Type: %s' % self.mimetype,
            '',
            self.input_file_content
        ])

        form.append('--' + self.boundary + '--')
        form.append('')

        return InputFile._parse(form)

And the InputFile._parse(form)

def _parse(form):
    """
    Returns:
        str:
    """
    if sys.version_info > (3,):
        # on Python 3 form needs to be byte encoded
        encoded_form = []
        for item in form:
            try:
                encoded_form.append(item.encode())
            except AttributeError:
                encoded_form.append(item)

        return b'\r\n'.join(encoded_form)
    return '\r\n'.join(form)

Solution

  • Check if in your card.image.path there are no Unicode characters, in case of any, I'd recommend to encode to UTF-8. Check this link for further information.