luckydonald / pytgbot

python module to access the telegram bot api.
MIT License
66 stars 16 forks source link

Python 2: "UnicodeEncodeError: 'ascii' codec can't encode character u'\U0001f602' (😂) in position" #5

Closed erivan-as closed 7 years ago

erivan-as commented 7 years ago

Hi @luckydonald , I am studing this package and while testing the examples I was catched by the error mencioned above. How can I overcome this error?

Error message displayed

Traceback (most recent call last):
  File "inline.py", line 28, in <module>
    for update in bot.get_updates(limit=100, offset=last_update_id+1, error_as_empty=True):
  File "build/bdist.linux-x86_64/egg/pytgbot/bot.py", line 133, in get_updates
  File "build/bdist.linux-x86_64/egg/pytgbot/api_types/__init__.py", line 50, in from_array_list
  File "build/bdist.linux-x86_64/egg/pytgbot/api_types/__init__.py", line 105, in from_array_list
  File "build/bdist.linux-x86_64/egg/pytgbot/api_types/__init__.py", line 117, in from_array_list
  File "build/bdist.linux-x86_64/egg/pytgbot/api_types/receivable/updates.py", line 192, in from_array
  File "build/bdist.linux-x86_64/egg/pytgbot/api_types/receivable/updates.py", line 903, in from_array
UnicodeEncodeError: 'ascii' codec can't encode character u'\U0001f602' in position 0: ordinal not in range(128)

P.S: I am using Ubuntu 16.04

Erivan

luckydonald commented 7 years ago
Python 3.6.0 (default, Jan  1 2017, 18:45:22)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\U0001f602')
😂

Image, in case the browser messed up:

image

You got an emoji here. Emojis aren't ASCII.

Python 2 doesn't use unicode for strings. That's why the line in updates.py:903 blows up:

 data['text'] = str(array.get('text')) if array.get('text') is not None else None

The best solution is probably to upgrade to python 3, because you will probably also run into errors using unicode on your program, python 2 just isn't made for unicode. In the end using a chat messanger without emojis isn't fun.

Nevertheless, I will also work on supporting python 2 for the next release.

erivan-as commented 7 years ago

Ok,

Thanks for attention. Good job, congratulations for pytgbot

Erivan