telebot проблемаc чатом
не работает функционал полностью помогите найти ошибки вот код:
main.py:
import telebot
from telebot import types
from database import DataBase
import confi
db = DataBase('db.db')
bot = telebot.TeleBot(confi.token)
@bot.message_handler(commands=['start'])
def starting(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=['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 stoping(message):
chat_info = db.get_acvtive_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:
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton('👥 Поиск собеседника')
markup.add(item1)
bot.send_message(message.chat.id, '❌ Вы не начили чат', reply_markup=markup)
@bot.message_handler(content_types=['text'])
def text(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:
mes = 'Собеседник найден! Чтобы остановить диалог напишите /stop'
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton('/stop')
markup.add(item1)
bot.send_message(message.chat.id, mes, reply_markup=markup)
bot.send_message(chat_two, mes, reply_markup=markup)
elif message.text == '❌ Остановить поиск':
db.delete_queue(message.chat.id)
bot.send_message(message.chat.id, '❌ Поиск остановлен, напишите /menu')
else:
chat_info = db.get_acvtive_chat(message.chat.id)
bot.send_message(chat_info[1], message.text)
bot.polling(non_stop=True)
database.py:
import sqlite3
class DataBase():
def __init__(self, database_file):
self.connect = sqlite3.connect(database_file, check_same_thread=False)
self.cursor = self.connect.cursor()
def add_queue(self, chat_id):
with self.connect:
return self.cursor.execute('INSERT INTO "queue" ("chat_id") VALUES(?)', (chat_id,))
def delete_queue(self, chat_id):
with self.connect:
return self.cursor.execute('DELETE FROM "queue" WHERE "chat_id" = ?', (chat_id,))
def delete_chat(self, id_chat):
with self.connect:
return self.cursor.execute('DELETE FROM "chats" WHERE "id" = ?', (id_chat,))
def get_chat(self):
with self.connect:
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.connect:
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_acvtive_chat(self, chat_id):
with self.connect:
chat = self.cursor.execute("SELECT * FROM 'chats' WHERE 'chat_one' = ?", (chat_id,))
id_chat = 0
for row in chat:
id_chat = row[0]
jls_extract_var = [row[0], row[2]]
chat_info = jls_extract_var
if id_chat == 0:
chat = self.cursor.execute("SELECT * FROM 'chats' WHERE 'chat_two' = ?", (chat_id,))
for row in chat:
id_chat == [row[0], row[1]]
if id_chat == 0:
return False
else:
return chat_info
else:
return chat_info
Источник: Stack Overflow на русском