adding reply to messages

This commit is contained in:
admin 2024-05-21 21:13:08 +03:00
parent 02c26474e4
commit 88024f4356

42
main.py
View file

@ -1,7 +1,7 @@
import telebot import telebot
import datetime import datetime
import sqlite3 import sqlite3
import re
class Database: class Database:
def __init__(self,db_name) -> None: def __init__(self,db_name) -> None:
self.db_name = db_name self.db_name = db_name
@ -42,7 +42,7 @@ class Database:
info_users = sql["cursor"].fetchone() info_users = sql["cursor"].fetchone()
self.close(sql['connect'],sql['cursor']) self.close(sql['connect'],sql['cursor'])
if info_users is None: if info_users is None:
return {"status":False} return {"status":False,"info_users":info_users}
return {"status":True,"info_user":info_users} return {"status":True,"info_user":info_users}
def create_user(self, message: telebot.types.Message): def create_user(self, message: telebot.types.Message):
sql = self.connect_db() sql = self.connect_db()
@ -52,14 +52,16 @@ class Database:
""", (message.from_user.id, message.from_user.username,message.from_user.first_name,message.from_user.last_name,date)) """, (message.from_user.id, message.from_user.username,message.from_user.first_name,message.from_user.last_name,date))
sql["connect"].commit() sql["connect"].commit()
self.close(sql['connect'],sql['cursor']) self.close(sql['connect'],sql['cursor'])
def create_message(self,message: telebot.types.Message): def create_message(self,message: telebot.types.Message,info_user):
sql = self.connect_db() sql = self.connect_db()
date = datetime.datetime.now().strftime("%Y-%m-%d") date = datetime.datetime.now().strftime("%Y-%m-%d")
sql["cursor"].execute(""" sql["cursor"].execute("""
insert into messages (id_user, message_id, message_text, date_send) values (?,?,?,?) insert into messages (id_user, message_id, message_text, date_send) values (?,?,?,?)
""", (message.from_user.id,message.message_id,message.text,date)) """, (info_user[0],message.message_id,message.text,date))
sql["connect"].commit() sql["connect"].commit()
self.close(sql['connect'],sql['cursor']) self.close(sql['connect'],sql['cursor'])
return sql["cursor"].lastrowid
def close(self,connect,cursor): def close(self,connect,cursor):
cursor.close() cursor.close()
connect.close() connect.close()
@ -69,11 +71,12 @@ class TelegramBot(Database):
def __init__(self,db_name,token): def __init__(self,db_name,token):
super().__init__(db_name) super().__init__(db_name)
self.bot = telebot.TeleBot(token) self.bot = telebot.TeleBot(token)
self.admin_chat_id = -4235588549
self.router() self.router()
def router(self): def router(self):
@self.bot.message_handler(commands=['start']) @self.bot.message_handler(commands=['start'])
def start(message): def start(message: telebot.types.Message):
text = "" text = ""
if self.check_user(message.from_user.id)["status"]: if self.check_user(message.from_user.id)["status"]:
text += f"С возвращением, {message.from_user.first_name}" text += f"С возвращением, {message.from_user.first_name}"
@ -85,15 +88,36 @@ class TelegramBot(Database):
text text
) )
@self.bot.message_handler(func=lambda message: True) @self.bot.message_handler(func=lambda message: True)
def echo_all(message): def echo_all(message: telebot.types.Message):
if message.chat.id == self.admin_chat_id:
pass
if message.chat.id == self.admin_chat_id and message.reply_to_message != None:
reply_message = message.reply_to_message.text
id_application = re.search(r"Номер заявки: (\d+)",reply_message).group(1)
id_user = re.search(r"ID пользователя: (\d+)",reply_message).group(1)
message_text = reply_message.split("\n")
print(id_application,id_user)
self.bot.send_message(
id_user,
f"Ответ от админа: {message.text}"
)
else:
check = self.check_user(message.from_user.id) check = self.check_user(message.from_user.id)
if not check["status"]: return if not check["status"]:
self.create_message(message) self.create_user(message)
check = self.check_user(message.from_user.id)
id_message = self.create_message(message,check["info_user"])
self.bot.reply_to(message,"Сообщение отправлено админу") self.bot.reply_to(message,"Сообщение отправлено админу")
text = f'''
Номер заявки: {id_message}
ID пользователя: {message.from_user.id}
Сообщение: {message.text}
'''
self.bot.send_message(self.admin_chat_id, text)
self.bot.polling() self.bot.polling()
TelegramBot( TelegramBot(
db_name="tg.db", db_name="tg.db",
token="7037219892:AAFbu4QNhX6ffS0X1XoFiNTkpTa80yousC4" token=""
) )