python-telegram-bot / python-telegram-bot

We have made you a wrapper you can't refuse
https://python-telegram-bot.org
GNU General Public License v3.0
25.91k stars 5.25k forks source link

Issue on sendPhoto #239

Closed gibosler closed 8 years ago

gibosler commented 8 years ago

Hi, i've an issue trying to sendPhoto with bot. I've tryped many combinations of configuration without results. I'm using your libs downloaded and imported in my script. I'm using virtualenv with python 2.7.9 When i'll try to sendPhoto with code: bot.sendPhoto(chat_id=update.message.chat_id, photo=open('/tmp/phchart.png', 'rb'))

i receive the error:

[...]/inputfile.py", line 165, in _parse return '\r\n'.join(form) UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)

I'm using in venv because i'm planning to use with django as a backend to configure/store/monitor my bot. Thank's in advance for your help.

jh0ker commented 8 years ago

Photos should be in JPEG format, it might be related to that. If you can, please give it a try.

leandrotoledo commented 8 years ago

@gibosler Could you post the image somewhere so we can try to reproduce?

gibosler commented 8 years ago

I've tryed with jpeg format with no results. image

leandrotoledo commented 8 years ago

@gibosler Cool, I'll look into that, meanwhile could you check if you are using the latest stable version?

>>> import telegram
>>> print telegram.__version__
3.4
gibosler commented 8 years ago

VERSION: 3.4

rahiel commented 8 years ago

I cannot reproduce this issue on python 2.7 and version 3.4 of the library, your graph is sent properly using bot.sendPhoto(chat_id=chat_id, photo=open('./bla.jpg', 'rb')), could you try bot.sendPhoto(chat_id=chat_id, photo=open('./bla.jpg', 'r'))?

vadimdne commented 8 years ago

Hi, I'm getting exactly the same issue with sendPhoto. Full details are in this SO question:

I'm uploading JPG file, both open(file, 'rb') and open(file, 'r') result in same encoding error.

Python 2.7, Telegram 3.4

vadimdne commented 8 years ago

By the way, the self implemented send_photo method mentioned in #110 works just fine for me.

gibosler commented 8 years ago

I've made some tests. In many case it works, maybe when i start a python shell, starting bot and sendPhoto. The problem exists when the library is used with others extensions (updater in my case) and/or importing django models. Last test i've removed import of os and sys lib in my script and it works. Re-adding this it works too, but when i import logging library and log config it fails. Error is always on unicodedecode error on post data of image. The same error using sendDocument.. It seems a randomized error.. i'm investigating on it..

leandrotoledo commented 8 years ago

Hi @gibosler,

Could you mock up some code so we can also reproduce the problem?

Thanks!

slonvpalto commented 8 years ago

I can clearly reproduce the issue with a jpeg file. What should I supply to assist you fixing the problem?

leandrotoledo commented 8 years ago

Hi @slonvpalto could you provide more details about the issue, like:

Thanks!

juanbzpy commented 8 years ago

FYI, I am able to send pictures with the direct path like this

bot.sendPhoto(update.message.chat_id, photo='./image_file.jpg')
leandrotoledo commented 8 years ago

Friendly ping @slonvpalto @gibosler, any luck on how to reproduce this?

grumbler commented 8 years ago

The reason for this problem is unicode somewhere. (At least it was in my case) It could be the leading part of the path.

Nanoseb commented 8 years ago

I had the same issue and it was solved by changing the encoding of the path to utf-8

leandrotoledo commented 8 years ago

I'm resolving this issue, please feel free to reopen if further attention here is needed. Also check @Nanoseb's comment, seems like most of time the problem is the path to file needs to be utf-8 encoded.