Проблема бота в телеге
Выдаёт такую ошибку
Traceback (most recent call last):
File "C:\Users\Recs\PycharmProjects\bot\bot.py", line 71, in <module>
bot.polling(none_stop=True)
File "C:\Users\Recs\PycharmProjects\bot\venv\lib\site-packages\telebot\__init__.py", line 621, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "C:\Users\Recs\PycharmProjects\bot\venv\lib\site-packages\telebot\__init__.py", line 695, in __threaded_polling
raise e
File "C:\Users\Recs\PycharmProjects\bot\venv\lib\site-packages\telebot\__init__.py", line 651, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\Recs\PycharmProjects\bot\venv\lib\site-packages\telebot\util.py", line 147, in raise_exceptions
raise self.exception_info
File "C:\Users\Recs\PycharmProjects\bot\venv\lib\site-packages\telebot\util.py", line 93, in run
task(*args, **kwargs)
File "C:\Users\Recs\PycharmProjects\bot\bot.py", line 68, in bot_message
bot.send_message(chat_info[1], message.text)
TypeError: 'bool' object is not subscriptable
Уже перепровел все что можно и bool тоже проверял но может из-за рукожопости что-то упустил
Сам бот
import telebot
from telebot import types
from database import Database
db = Database('db.db')
bot = telebot.TeleBot(config.TOKEN)
@bot.message_handler(commands = ['start'])
def start(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton('?Поиск собеседника')
markup.add(item1)
bot.send_message(message.chat.id, 'Привет, {0.first_name}! Добро пожаловать в анонимный чат! Нажми на поиск собеседника.'.format(message.from_user),reply_markup=markup)
@bot.message_handler(commands = ['menu'])
def menu(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
item1 = types.KeyboardButton('?Поиск собеседника')
markup.add(item1)
bot.send_message(message.chat.id, 'Меню'.format(message.from_user), reply_markup=markup)
@bot.message_handler(commands = ['stop'])
def stop(message):
chat_info = db.get_active_chat(message.chat.id)
if chat_info != False:
db.delete_chat(chat_info[0])
markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
item1 = types.KeyboardButton('?Поиск собеседника')
markup.add(item1)
bot.send_message(chat_info[1], 'Собеседник покинул чат', reply_markup = markup)
bot.send_message(message.chat.id, 'Вы вышли из чата', reply_markup = markup)
else:
bot.send_message(message.chat.id, 'Вы не начали чат!', reply_markup = markup)
@bot.message_handler(content_types = ['text'])
def bot_message(message):
if message.chat.type == 'private':
if message.text == '? Поиск собеседника':
markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
item1 = types.KeyboardButton('Остановить поиск')
markup.add(item1)
chat_two = db.get_chat() #Берем собеседника ктороые стоит в очереди первый
if db.create_chat(message.chat.id, chat_two) == False:
db.add_queue(message.chat.id)
bot.send_message(message.chat.id, 'Поиск собеседника', reply_markup=markup)
else:
mess = 'Собеседник найден! Чтобы остановить диалог, нажмите /stop'
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton('/stop')
markup.add(item1)
bot.send_message(message.chat.id, mess, reply_markup=markup)
bot.send_message(chat_two, mess, reply_markup=markup)
elif message.text == 'Остановить поиск':
db.delete_queue(message.chat.id)
bot.send_message(message.chat.id, 'Поиск остановлен, напишите /menu')
else:
chat_info = db.get_active_chat(message.chat.id)
bot.send_message(chat_info[1], message.text)
bot.polling(none_stop=True)```
**База данных**
```import sqlite3
class Database:
def __init__(self, database_file):
self.connection = sqlite3.connect(database_file, check_same_thread=False)
self.cursor = self.connection.cursor()
def add_queue(self, chat_id):
with self.connection:
return self.cursor.execute("INSERT INTO 'queue' ('chat_id') VALUES (?)", (chat_id,))
def delete_queue(self, chat_id):
with self.connection:
return self.cursor.execute("DELETE FROM 'queue' WHERE 'chat_id' = ?", (chat_id,))
def delete_chat(self, id_chat):
with self.connection:
return self.cursor.execute("DELETE FROM 'chats' WHERE 'id' = ?", (id_chat,))
def get_chat(self):
with self.connection:
chat = self.cursor.execute("SELECT FROM 'queue'", ()).fetchmany(1)
if(bool(len(chat))):
for row in chat:
return row[1]
else:
return False
def create_chat(self, chat_one, chat_two):
with self.connection:
if chat_two != 0:
#создание чата
self.cursor.execute("DELETE FROM 'queue WHERE' 'chat_id' = ?", (chat_two,))
self.cursor.execute("INSERT INTO 'chats'('chat_one', 'chat_two') VALUES (?,?)", (chat_one, chat_two,))
return True
else:
return False
def get_active_chat(self, chat_id):
with self.connection:
chat = self.cursor.execute("SELECT * FROM 'chats' WHERE 'chat_one' = ?", (chat_id,))
id_chat = 0
for row in chat:
id_chat = row[0]
chat_info = [row[0], row[2]]
if id_chat == 0:
chat = self.cursor.execute("SELECT * FROM 'chats' WHERE 'chat_two' = ?", (chat_id,))
for row in chat:
id_chat = row[0]
chat_info = [row[0], row[1]]
if id_chat == 0:
return False
else:
return chat_info
else:
return chat_info```