M
M
madreyk2021-03-26 06:06:03
Python
madreyk, 2021-03-26 06:06:03

How to link if and specific array in telegram bot?

Python 3.9.2
PyTelegramBotAPI
I have to create a private telegram bot for a specific group of people. In the "users" array, I plan to add 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 is 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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cheabatto, 2021-03-26
@Cheabatto

Try 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, 'Permission denied', parse_mode='HTML')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question