ebellocchia / telegram_crypto_price_bot

Telegram bot for displaying cryptocurrencies prices and charts
MIT License
53 stars 21 forks source link

Error UTF-8 on new installation on Debian 11 #4

Closed fouille closed 1 year ago

fouille commented 2 years ago

Hello,

I Have this error just after have installed, when I run a single request :

2022-10-27 18:38:32,529 ERROR - 'utf-8' codec can't encode characters in position 0-1: surrogates not allowed                                                                         [0/878]
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/dispatcher.py", line 242, in handler_worker
    await self.loop.run_in_executor(
  File "/usr/lib/python3.9/concurrent/futures/thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs) 
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/bot/bot_base.py", line 107, in <lambda>
    curr_type(lambda client, message: curr_cfg["callback"](self, client, message),
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/bot/bot_handlers_cfg.py", line 87, in <lambda>
    "callback": lambda self, client, message: self.DispatchCommand(client,
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/bot/bot_base.py", line 120, in DispatchCommand
    self.cmd_dispatcher.Dispatch(client, message, cmd_type, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/command/command_dispatcher.py", line 124, in Dispatch
    cmd_class.Execute(message, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/command/command_base.py", line 96, in Execute
    self._ExecuteCommand(**kwargs)
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/command/commands.py", line 150, in _ExecuteCommand
    coin_info_sender.SendMessage(self.cmd_data.Chat(), coin_id, coin_vs, last_days)
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/info_message_sender/coin_info_message_sender.py", line 94, in SendMessage
    self.chart_price_info_msg_sender.SendMessage(chat, coin_id, coin_vs, last_days)
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/info_message_sender/info_message_sender_base.py", line 61, in SendMessage
    self.last_sent_msg = self._SendMessage(chat, *args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/info_message_sender/chart_price_info_message_sender.py", line 80, in _SendMessage
    return self._MessageSender().SendPhoto(chat,
  File "/usr/local/lib/python3.9/dist-packages/telegram_crypto_price_bot/message/message_sender.py", line 72, in SendPhoto
    return self.client.send_photo(receiver.id, photo, **kwargs)     # type: ignore
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/sync.py", line 78, in async_to_sync_wrap
    return asyncio.run_coroutine_threadsafe(coroutine, main_loop).result()
  File "/usr/lib/python3.9/concurrent/futures/_base.py", line 440, in result
    return self.__get_result()
  File "/usr/lib/python3.9/concurrent/futures/_base.py", line 389, in __get_result
    raise self._exception
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/methods/messages/send_photo.py", line 170, in send_photo
    r = await self.invoke(
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/methods/advanced/invoke.py", line 79, in invoke
    r = await self.session.invoke(
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/session/session.py", line 361, in invoke
    return await self.send(query, timeout=timeout)
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/session/session.py", line 290, in send 
    message = self.msg_factory(data)
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/session/internals/msg_factory.py", line 37, in __call__
    len(body)
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/raw/core/tl_object.py", line 79, in __len__
    return len(self.write())
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/raw/functions/messages/send_media.py", line 168, in write
    b.write(Vector(self.entities))
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/raw/core/primitives/vector.py", line 58, in __new__
    + [cast(bytes, t(i)) if t else i.write() for i in value]
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/raw/core/primitives/vector.py", line 58, in <listcomp>
    + [cast(bytes, t(i)) if t else i.write() for i in value]
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/raw/types/message_entity_pre.py", line 86, in write
    b.write(String(self.language))
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/raw/core/primitives/string.py", line 31, in __new__
    return super().__new__(cls, value.encode()) 
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 0-1: surrogates not allowed

Can you help me ?

fouille commented 2 years ago

For workaround.

I've added in /usr/local/lib/python3.9/dist-packages/pyrogram/raw/core/primitives/string.py In last line i've replaced encode() by encode('utf-8', 'replace')

ebellocchia commented 1 year ago

Hello, It wasn't happening with previous versions of pyrogram, probably something changed in pyrogram code. I'll investigate.

Thanks

ebellocchia commented 1 year ago

Update: this seems to be related to issue #6, probably the missing new line for the markdown delimiter triggers an error in the last versions of pyrogram. I'll fix it.