S
S
StenMarsh13372020-10-15 17:54:41
Python
StenMarsh1337, 2020-10-15 17:54:41

Authorization in telegram bot?

Tell me how to make an authorization check by chat_id and priority on id-groups, for example 1-Admin (the Admin menu pops up)
2-User (a greeting pops up).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-10-15
@StenMarsh1337

Here is the simplest code. Write the /admin command to the bot - and it will respond depending on the situation. For example, if user_id is in the database, and user_group_id = '1' - then the bot welcomes the admin, if user_group_id is not equal to '1', then the bot welcomes the user. If the user_id is not in the database at all, then it says that the user is not registered in the database.

import sqlite3
import telebot

token = 'ТОКЕН'

bot = telebot.TeleBot(token)

def getAccess(user_id):
  with sqlite3.connect('users.db') as conn:
    cursor = conn.cursor()
    cursor.execute('SELECT user_group_id FROM users WHERE user_id=?',(user_id,))
    result = cursor.fetchone()
    return result

@bot.message_handler(commands=['admin'])
def repeat_all_message(message):
  print(message.chat.id)
  bot.send_message(message.chat.id,message.text)

  access = getAccess(message.chat.id)

  if access:
    if access[0] == '1':
      bot.send_message(message.chat.id,'Привет Admin!')
    else:
      bot.send_message(message.chat.id,'Привет User!')
  else:
    bot.send_message(message.chat.id,'Вы не зарегистрированны в системе!')


if __name__ == '__main__':
  bot.polling(none_stop=True)

PS: My user_group_id is a string, but you can also make it an int;
PS: Most likely, no one will write code for you, because then you will not understand anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question