我将python-telegram-bot与 Python 2.7 一起使用。正如文档所暗示的那样,这是我用来从磁盘发送照片的方法:
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())
但这导致了另一个不同类型的编码错误。关于如何上传照片的任何想法?
更新
据我整理 - sendPhoto() 将 chat_id 和 photo 放入数据字典中,这里是回溯末尾的库方法:
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(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)