+1 vote
ago by
Python 3.9.2
PyTelegramBotAPI
I need to create a private telegram bot, for a specific group of people. In the "users" array I plan to include users who have access

users=['<usernames>']
@bot.message_handler(commands='Начать')
def handle_start(message):
if message.from_user.username==(users):
bot.send_message(message.from_user.id, 'Вот', reply_markup=user_markup, parse_mode='HTML')
if message.from_user.username!=(users):
bot.send_message(message.from_user.id, 'В доступе отказано', reply_markup=user_markup, parse_mode='HTML')
Or I think
users=['<usernames>']
@bot.message_handler(commands='Начать')
def handle_start(message):
if message.from_user.username==(users):
bot.send_message(message.from_user.id, 'Вот', reply_markup=user_markup, parse_mode='HTML')
else:
bot.send_message(message.from_user.id, 'В доступе отказано', reply_markup=user_markup, parse_mode='HTML')
But here's the debug:
Traceback (most recent call last):
File "start.py", line 25, in <module>
bot.polling(none_stop=True, interval=0)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 485, in polling
self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 544, in __threaded_polling
raise e
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 506, in __threaded_polling
polling_thread.raise_exceptions()
File "/home/mem/.local/lib/python3.8/site-packages/telebot/util.py", line 87, in raise_exceptions
raise self.exception_info
File "/home/mem/.local/lib/python3.8/site-packages/telebot/util.py", line 69, in run
task(*args, **kwargs)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 323, in __retrieve_updates
self.process_new_updates(updates)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 384, in process_new_updates
self.process_new_messages(new_messages)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 410, in process_new_messages
self._notify_command_handlers(self.message_handlers, new_messages)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 2183, in _notify_command_handlers
if self._test_message_handler(message_handler, message):
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 2149, in _test_message_handler
if not self._test_filter(message_filter, filter_value, message):
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 2170, in _test_filter
return test_cases.get(message_filter, lambda msg: False)(message)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 2166, in <lambda>
'commands': lambda msg: msg.content_type == 'text' and util.extract_command(msg.text) in filter_value,
TypeError: 'in <string>' requires string as left operand, not NoneType
ago by
First, your error is not related to the question. Second, it is obviously useless to compare a string with a list of strings.
ago by
Alan Gibizov , in does not work:

@bot.message_handler(commands='Начать')
def handle_start(message):
if message.from_user.username in users:
bot.send_message(message.from_user.id, 'Вот', reply_markup=user_markup, parse_mode='HTML')
else:
bot.send_message(message.from_user.id, 'В доступе отказано', reply_markup=user_markup, parse_mode='HTML')
mistake:
Traceback (most recent call last):
File "start.py", line 25, in <module>
bot.polling(none_stop=True, interval=0)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 485, in polling
self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 544, in __threaded_polling
raise e
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 506, in __threaded_polling
polling_thread.raise_exceptions()
File "/home/mem/.local/lib/python3.8/site-packages/telebot/util.py", line 87, in raise_exceptions
raise self.exception_info
File "/home/mem/.local/lib/python3.8/site-packages/telebot/util.py", line 69, in run
task(*args, **kwargs)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 323, in __retrieve_updates
self.process_new_updates(updates)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 384, in process_new_updates
self.process_new_messages(new_messages)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 410, in process_new_messages
self._notify_command_handlers(self.message_handlers, new_messages)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 2183, in _notify_command_handlers
if self._test_message_handler(message_handler, message):
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 2149, in _test_message_handler
if not self._test_filter(message_filter, filter_value, message):
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 2170, in _test_filter
return test_cases.get(message_filter, lambda msg: False)(message)
File "/home/mem/.local/lib/python3.8/site-packages/telebot/__init__.py", line 2166, in <lambda>
'commands': lambda msg: msg.content_type == 'text' and util.extract_command(msg.text) in filter_value,
TypeError: 'in <string>' requires string as left operand, not NoneType
I will try to read this book in my spare time
ago by
madreyk , several people have already told you that the error is not related to the comparison code. It occurs in the library.
ago by
Sergey Gornostaev Yes, I understand that you can't compare different types of data, but when I put a bracket and specify str, nothing changes
ago by
Where do you all go without even basic knowledge? And then there are thousands of the same-type, frankly idiotic, questions like yours on the net.
ago by
Indeed, the error
TypeError: 'in ' requires string as left operand, not NoneType
says that there is an error in the operands of the in operation - and you do not see such an operation in your code.

In general, it should look something like this:
if “a” in “abc”:
print(“a present”)
else:
print(“a absent”)
Or
if “a” in [“a”, “b”, “c”]:
print(“a present”)
else:
print(“a absent”)
As for these code snippets, there are several problems. First, the attempt to compare the value of the variable message.from_user.username with the list mentioned by Sergei.
You should use in instead of == as in the examples above.
Second, you can see right away that there is absolutely no understanding that brackets in python are not just brackets. When I started out, I didn't understand the meaning of parentheses for a long time, either. Personally, the book "Learning Python" by M. Lutz helped me, the first two hundred pages will give a huge leap in understanding.
And, for a snack, there is a feeling that not everything is clear with the lists, and with the PEP 8 not familiar...

Either spend the time to learn Python, or the money to hire a programmer. Otherwise, it looks like you're trying to redesign a radio transmitter for a different band, without any understanding of Ohm's law or how to connect radio components. Don't be like that.
ago by
Try replacing
message.from_user.username==(users)
At
message.from_user.username in users
ago by
madreyk , the error says: the left operand of the in operation should be a string, not None. And you have this:
message.from_user.username
returns None.
Why? It's not easy to figure out. Maybe you're using it wrong; maybe it's crooked in and of itself; but that's where you have to dig. It's not your fault, of course.

1 Answer

0 votes
ago by
Try it like this:
users=['users']
@bot.message_handler(commands='start')
def handle_start(message):
print(message.from_user.username)
if message.from_user.username in users:
bot.send_message(message.from_user.id, 'Here', parse_mode='HTML')
if message.from_user.username not in users:
bot.send_message(message.from_user.id, 'Access denied', parse_mode='HTML')
...