2016-04-19 122 views
3

我使用python-telegram-bot與Python 2.7。右爲文檔建議,這是我用它來從磁盤發送照片的方法:UnicodeDecodeError當試圖通過python-telegram-bot發送照片

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

哪裏card.image.path是JPG文件完整路徑。當執行這個方法時,我得到UnicodeDecode錯誤。請參閱下面的完整回溯。

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) 

我調查了這個庫,據我瞭解,它無法解碼上傳的文件。我試過這樣的東西:

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

但是,這導致了另一種編碼錯誤。關於如何上傳照片的任何想法?

UPDATE

至於我整理出來 - sendPhoto()把chat_id和照片到數據字典,這裏是從回溯結束庫方法:

的一箇中使用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) 

而InputFile._parse(形式)

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) 
+1

使用Python 2.x或3.x?編碼在Python 2.x是一個痛苦... – linusg

+0

這是Python 2.7 –

+0

啊謝謝,我已經從錯誤追溯已經讀過。讓我們找到這個解決方案... – linusg

回答

1

檢查你的card.image.path是否沒有Unicode字符,如果有的話,我建議編碼爲UTF-8。請查閱link瞭解更多信息。

+0

是的,這是問題。 –