From 88024f4356f5bb288a154ee89c832b6e5defe325 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 21 May 2024 21:13:08 +0300 Subject: [PATCH] adding reply to messages --- main.py | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index a3a5b1b..53c93e1 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import telebot import datetime import sqlite3 - +import re class Database: def __init__(self,db_name) -> None: self.db_name = db_name @@ -42,7 +42,7 @@ class Database: info_users = sql["cursor"].fetchone() self.close(sql['connect'],sql['cursor']) if info_users is None: - return {"status":False} + return {"status":False,"info_users":info_users} return {"status":True,"info_user":info_users} def create_user(self, message: telebot.types.Message): 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)) sql["connect"].commit() 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() date = datetime.datetime.now().strftime("%Y-%m-%d") sql["cursor"].execute(""" 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() + self.close(sql['connect'],sql['cursor']) + return sql["cursor"].lastrowid def close(self,connect,cursor): cursor.close() connect.close() @@ -69,11 +71,12 @@ class TelegramBot(Database): def __init__(self,db_name,token): super().__init__(db_name) self.bot = telebot.TeleBot(token) + self.admin_chat_id = -4235588549 self.router() def router(self): @self.bot.message_handler(commands=['start']) - def start(message): + def start(message: telebot.types.Message): text = "" if self.check_user(message.from_user.id)["status"]: text += f"С возвращением, {message.from_user.first_name}" @@ -85,15 +88,36 @@ class TelegramBot(Database): text ) @self.bot.message_handler(func=lambda message: True) - def echo_all(message): - check = self.check_user(message.from_user.id) - if not check["status"]: return - self.create_message(message) - self.bot.reply_to(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) + if not check["status"]: + 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,"Сообщение отправлено админу") + text = f''' +Номер заявки: {id_message} +ID пользователя: {message.from_user.id} +Сообщение: {message.text} +''' + self.bot.send_message(self.admin_chat_id, text) self.bot.polling() TelegramBot( db_name="tg.db", - token="7037219892:AAFbu4QNhX6ffS0X1XoFiNTkpTa80yousC4" + token="" ) \ No newline at end of file