我在Python 2.7中使用python-telegram-bot。正如文档所示,这是我用来从磁盘发送照片的方法:
this.yourcomboBoxname.DropDownStyle = ComboBoxStyle.DropDownList;
其中card.image.path是JPG文件的完整路径。执行此方法时,我得到UnicodeDecode错误。请参阅下面的完整追溯。
bot.sendPhoto(update.message.chat_id, photo=open(card.image.path, 'rb'))
我调查了这个库,据我所知,它无法解码上传的文件。我尝试过这样的事情:
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)
但是这导致了另一种编码错误。关于如何上传照片的任何想法?
更新
据我整理出来 - sendPhoto()将chat_id和照片放入数据 dict,这里是跟踪结束时的库方法:
data.to_form()
中使用的那个with io.open(card.image.path, 'r', encoding='utf8') as photo:
bot.sendPhoto(update.message.chat_id, photo=photo.read())
和InputFile._parse(表单)
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)