Top.Mail.Ru
Ответы
Аватар пользователя
Аватар пользователя
Аватар пользователя
Аватар пользователя
Программирование
+2

Как сделать пароль на телеграмм бота. Python

А то каждый будет использовать моего бота

По дате
По рейтингу
Аватар пользователя
Оракул
10мес
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
 import bcrypt 
import sqlite3 
from telegram import Update 
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext 
 
TOKEN = 'YOUR_TOKEN_HERE' 
RAW_PASSWORD = 'your_secure_password' 
 
# Инициализация базы данных 
conn = sqlite3.connect('users.db', check_same_thread=False) 
cursor = conn.cursor() 
cursor.execute(''' 
    CREATE TABLE IF NOT EXISTS users ( 
        user_id INTEGER PRIMARY KEY, 
        authenticated BOOLEAN 
    ) 
''') 
conn.commit() 
 
# Хэширование пароля 
hashed_password = bcrypt.hashpw(RAW_PASSWORD.encode('utf-8'), bcrypt.gensalt()) 
 
def start(update: Update, context: CallbackContext): 
    user = update.effective_user 
    update.message.reply_text( 
        f"Привет, {user.first_name}! Пожалуйста, введите пароль для доступа к функциям бота." 
    ) 
 
def check_password(update: Update, context: CallbackContext): 
    user = update.effective_user 
    entered_password = update.message.text 
 
    if bcrypt.checkpw(entered_password.encode('utf-8'), hashed_password): 
        cursor.execute('REPLACE INTO users (user_id, authenticated) VALUES (?, ?)', (user.id, True)) 
        conn.commit() 
        update.message.reply_text("Пароль верный! Доступ открыт.") 
    else: 
        update.message.reply_text("Неверный пароль. Попробуйте снова.") 
 
def is_authenticated(user_id): 
    cursor.execute('SELECT authenticated FROM users WHERE user_id = ?', (user_id,)) 
    result = cursor.fetchone() 
    return result and result[0] 
 
def protected_command(update: Update, context: CallbackContext): 
    user = update.effective_user 
    if is_authenticated(user.id): 
        update.message.reply_text("Вы успешно выполнили защищённую команду!") 
    else: 
        update.message.reply_text("У вас нет доступа. Пожалуйста, введите пароль с помощью команды /start.") 
 
def main(): 
    updater = Updater(TOKEN, use_context=True) 
    dispatcher = updater.dispatcher 
 
    # Обработчики команд 
    dispatcher.add_handler(CommandHandler("start", start)) 
    dispatcher.add_handler(CommandHandler("protected", protected_command)) 
 
    # Обработчик сообщений для проверки пароля 
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, check_password)) 
 
    updater.start_polling() 
    updater.idle() 
 
    # Закрытие соединения с базой данных при завершении 
    conn.close() 
 
if __name__ == '__main__': 
    main() 
Аватар пользователя
10мес

никак