pyTelegramBotAPI icon indicating copy to clipboard operation
pyTelegramBotAPI copied to clipboard

TypeError: 'in <string>' requires string as left operand, not NoneType

Open pendurov opened this issue 5 years ago • 7 comments

Please answer these questions before submitting your issue. Thanks!

  1. What version of pyTelegramBotAPI are you using? 4.0.0
  2. What OS are you using? MacOS/Debian 20
  3. What version of python are you using? 3.9

The code worked stably. After a while, an error began to appear. It comes out when processing text and, sometimes, commands. Attempts to change the version of Python and pyTelegramAPI did not lead to anything. The error occurs both on the server (Debian) and on the local machine (Mac OS)

Full error: 2021-09-05 11:35:41,901 (init.py:585 MainThread) ERROR - TeleBot: "Exception traceback: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 580, in infinity_polling allowed_updates=allowed_updates, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 617, in polling self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 676, in __threaded_polling raise e File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 638, in __threaded_polling polling_thread.raise_exceptions() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/util.py", line 100, in raise_exceptions raise self.exception_info File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/util.py", line 82, in run task(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 391, in __retrieve_updates self.process_new_updates(updates) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 467, in process_new_updates self.process_new_messages(new_messages) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 497, in process_new_messages self._notify_command_handlers(self.message_handlers, new_messages) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 2876, in _notify_command_handlers if self._test_message_handler(message_handler, message): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 2842, in _test_message_handler if not self._test_filter(message_filter, filter_value, message): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 2863, in _test_filter return test_cases.get(message_filter, lambda msg: False)(message) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telebot/init.py", line 2859, in 'commands': lambda msg: msg.content_type == 'text' and util.extract_command(msg.text) in filter_value, TypeError: 'in ' requires string as left operand, not NoneType

pendurov avatar Sep 05 '21 06:09 pendurov

Somewhere you passed STRING as command filter, not LIST. Check commands parameters in your message_handler handlers

Badiboy avatar Sep 05 '21 08:09 Badiboy

This is my content_type handler:

@bot.message_handler(content_types=['text']) def handle_messages(message):

and command handler:

@bot.message_handler(commands=["start"]) def cmd_start(message):

pendurov avatar Sep 05 '21 09:09 pendurov

Your bot have only two handlers at all? No error can happen here.

Badiboy avatar Sep 05 '21 17:09 Badiboy

i think this is not your full code. Also note that command handler should be first, then content types handler should go

coder2020official avatar Sep 05 '21 18:09 coder2020official

i think this is not your full code. Also note that command handler should be first, then content types handler should go

Of course, this is not all my code. With this code, I showed that I use a LIST, not a STRING. My content types handler this is the last procedure in the code before if name == 'main' Now the error only goes to content type handler, everything is ok for the commands. When I look in the debugger, the error appears immediately as soon as I write the text to the bot and press ENTER, my code does not have time to execute

pendurov avatar Sep 06 '21 08:09 pendurov

With this code, I showed that I use a LIST, not a STRING.

If you used STRING - your bot would never work. My concern is that SOMEWHERE in your handlers you used string instead of list. By mistake.

Badiboy avatar Sep 06 '21 08:09 Badiboy

My concern is that SOMEWHERE in your handlers you used string instead of list. By mistake.

You're right! I carefully looked at the entire code and found an error:

@bot. message_handler(commands= 'stop') def self_delete(message):

After I fixed it, everything worked. Thank you very much!

pendurov avatar Sep 06 '21 08:09 pendurov