encoding - UnicodeDecodeError when trying to sendPhoto via python-telegram-bot -


i'm using python-telegram-bot python 2.7. right documentation suggests, method use send photo disk:

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

where card.image.path full path jpg file. when executing method unicodedecode error. please see 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 library , far understood, fails decode uploaded file. tried things like:

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

but resulted in encoding error of different kind. ideas on how photo uploaded?

update

as far sorted out - sendphoto() puts chat_id , photo data dict , here library methods end of traceback:

the 1 used in data.to_form()

def to_form(self):         """         returns:             str:         """         form = []         form_boundary = '--' + self.boundary          # add data fields         name, value in self.data.items():             form.extend([                 form_boundary,                 'content-disposition: form-data; name="%s"' % name,                 '',                 str(value)             ])          # add input_file 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 inputfile._parse(form)

def _parse(form):     """     returns:         str:     """     if sys.version_info > (3,):         # on python 3 form needs byte encoded         encoded_form = []         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) 

check if in card.image.path there no unicode characters, in case of any, i'd recommend encode utf-8. check link further information.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -