This commit is contained in:
84
handlers/add_button.py
Normal file
84
handlers/add_button.py
Normal file
@@ -0,0 +1,84 @@
|
||||
from telegram import Update
|
||||
from telegram.ext import ContextTypes, ConversationHandler, CommandHandler, CallbackQueryHandler, MessageHandler, filters
|
||||
from db import SessionLocal
|
||||
from models import Channel, Group, Button
|
||||
|
||||
SELECT_TARGET, INPUT_NAME, INPUT_URL = range(3)
|
||||
|
||||
async def add_button_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
# Если выбран канал или группа уже сохранены — сразу переход к названию
|
||||
if context.user_data.get('channel_id'):
|
||||
context.user_data['target'] = f"channel_{context.user_data['channel_id']}"
|
||||
await update.message.reply_text('Введите название кнопки:')
|
||||
return INPUT_NAME
|
||||
elif context.user_data.get('group_id'):
|
||||
context.user_data['target'] = f"group_{context.user_data['group_id']}"
|
||||
await update.message.reply_text('Введите название кнопки:')
|
||||
return INPUT_NAME
|
||||
# Если нет — стандартный выбор
|
||||
session = SessionLocal()
|
||||
channels = session.query(Channel).all()
|
||||
groups = session.query(Group).all()
|
||||
session.close()
|
||||
keyboard = []
|
||||
for c in channels:
|
||||
keyboard.append([{'text': f'Канал: {c.name}', 'callback_data': f'channel_{c.id}'}])
|
||||
for g in groups:
|
||||
keyboard.append([{'text': f'Группа: {g.name}', 'callback_data': f'group_{g.id}'}])
|
||||
if not keyboard:
|
||||
await update.message.reply_text('Нет каналов или групп для добавления кнопки.')
|
||||
return ConversationHandler.END
|
||||
await update.message.reply_text('Выберите канал или группу:', reply_markup=None)
|
||||
context.user_data['keyboard'] = keyboard
|
||||
return SELECT_TARGET
|
||||
|
||||
async def select_target(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
data = query.data
|
||||
context.user_data['target'] = data
|
||||
await query.edit_message_text('Введите название кнопки:')
|
||||
return INPUT_NAME
|
||||
|
||||
async def input_name(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
context.user_data['button_name'] = update.message.text
|
||||
await update.message.reply_text('Введите ссылку для кнопки:')
|
||||
return INPUT_URL
|
||||
|
||||
async def input_url(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
url = update.message.text
|
||||
name = context.user_data.get('button_name')
|
||||
target = context.user_data.get('target')
|
||||
if not target or ('_' not in target):
|
||||
await update.message.reply_text('Ошибка: не выбран канал или группа. Попробуйте снова.')
|
||||
return ConversationHandler.END
|
||||
session = SessionLocal()
|
||||
try:
|
||||
type_, obj_id = target.split('_', 1)
|
||||
obj_id = int(obj_id)
|
||||
if type_ == 'channel':
|
||||
button = Button(name=name, url=url, channel_id=obj_id)
|
||||
elif type_ == 'group':
|
||||
button = Button(name=name, url=url, group_id=obj_id)
|
||||
else:
|
||||
await update.message.reply_text('Ошибка: неверный тип объекта.')
|
||||
session.close()
|
||||
return ConversationHandler.END
|
||||
session.add(button)
|
||||
session.commit()
|
||||
await update.message.reply_text('Кнопка добавлена.')
|
||||
except Exception as e:
|
||||
await update.message.reply_text(f'Ошибка при добавлении кнопки: {e}')
|
||||
finally:
|
||||
session.close()
|
||||
return ConversationHandler.END
|
||||
|
||||
add_button_conv = ConversationHandler(
|
||||
entry_points=[CommandHandler('add_button', add_button_start)],
|
||||
states={
|
||||
SELECT_TARGET: [CallbackQueryHandler(select_target)],
|
||||
INPUT_NAME: [MessageHandler(filters.TEXT & ~filters.COMMAND, input_name)],
|
||||
INPUT_URL: [MessageHandler(filters.TEXT & ~filters.COMMAND, input_url)],
|
||||
},
|
||||
fallbacks=[]
|
||||
)
|
||||
17
handlers/add_channel.py
Normal file
17
handlers/add_channel.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from telegram import Update
|
||||
from telegram.ext import ContextTypes
|
||||
from db import SessionLocal
|
||||
from models import Channel
|
||||
|
||||
async def add_channel(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
args = context.args
|
||||
if len(args) < 2:
|
||||
await update.message.reply_text('Используйте: /add_channel <название> <ссылка>')
|
||||
return
|
||||
name, link = args[0], args[1]
|
||||
session = SessionLocal()
|
||||
channel = Channel(name=name, link=link)
|
||||
session.add(channel)
|
||||
session.commit()
|
||||
session.close()
|
||||
await update.message.reply_text(f'Канал "{name}" добавлен.')
|
||||
17
handlers/add_group.py
Normal file
17
handlers/add_group.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from telegram import Update
|
||||
from telegram.ext import ContextTypes
|
||||
from db import SessionLocal
|
||||
from models import Group
|
||||
|
||||
async def add_group(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
args = context.args
|
||||
if len(args) < 2:
|
||||
await update.message.reply_text('Используйте: /add_group <название> <ссылка>')
|
||||
return
|
||||
name, link = args[0], args[1]
|
||||
session = SessionLocal()
|
||||
group = Group(name=name, link=link)
|
||||
session.add(group)
|
||||
session.commit()
|
||||
session.close()
|
||||
await update.message.reply_text(f'Группа "{name}" добавлена.')
|
||||
40
handlers/channel_buttons.py
Normal file
40
handlers/channel_buttons.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from telegram.ext import CommandHandler, CallbackQueryHandler, ConversationHandler, ContextTypes
|
||||
from db import SessionLocal
|
||||
from models import Channel, Button
|
||||
|
||||
SELECT_CHANNEL, MANAGE_BUTTONS = range(2)
|
||||
|
||||
async def channel_buttons_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
session = SessionLocal()
|
||||
channels = session.query(Channel).all()
|
||||
session.close()
|
||||
keyboard = [[InlineKeyboardButton(c.name, callback_data=str(c.id))] for c in channels]
|
||||
await update.message.reply_text(
|
||||
"Выберите канал для настройки клавиатуры:",
|
||||
reply_markup=InlineKeyboardMarkup(keyboard)
|
||||
)
|
||||
return SELECT_CHANNEL
|
||||
|
||||
async def select_channel(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
channel_id = int(query.data)
|
||||
context.user_data['channel_id'] = channel_id
|
||||
session = SessionLocal()
|
||||
buttons = session.query(Button).filter_by(channel_id=channel_id).all()
|
||||
session.close()
|
||||
text = "Кнопки этого канала:\n"
|
||||
for b in buttons:
|
||||
text += f"- {b.name}: {b.url}\n"
|
||||
text += "\nДобавить новую кнопку: /add_button\nУдалить: /del_button <название>"
|
||||
await query.edit_message_text(text)
|
||||
return ConversationHandler.END
|
||||
|
||||
channel_buttons_conv = ConversationHandler(
|
||||
entry_points=[CommandHandler('channel_buttons', channel_buttons_start)],
|
||||
states={
|
||||
SELECT_CHANNEL: [CallbackQueryHandler(select_channel)],
|
||||
},
|
||||
fallbacks=[]
|
||||
)
|
||||
21
handlers/del_button.py
Normal file
21
handlers/del_button.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from telegram import Update
|
||||
from telegram.ext import CommandHandler, ContextTypes
|
||||
from db import SessionLocal
|
||||
from models import Button
|
||||
|
||||
async def del_button(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
args = context.args
|
||||
if not args:
|
||||
await update.message.reply_text('Используйте: /del_button <название>')
|
||||
return
|
||||
name = args[0]
|
||||
session = SessionLocal()
|
||||
button = session.query(Button).filter_by(name=name).first()
|
||||
if not button:
|
||||
await update.message.reply_text('Кнопка не найдена.')
|
||||
session.close()
|
||||
return
|
||||
session.delete(button)
|
||||
session.commit()
|
||||
session.close()
|
||||
await update.message.reply_text(f'Кнопка "{name}" удалена.')
|
||||
22
handlers/edit_button.py
Normal file
22
handlers/edit_button.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from telegram import Update
|
||||
from telegram.ext import CommandHandler, ContextTypes
|
||||
from db import SessionLocal
|
||||
from models import Button
|
||||
|
||||
async def edit_button(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
args = context.args
|
||||
if len(args) < 3:
|
||||
await update.message.reply_text('Используйте: /edit_button <название> <новое_название> <новая_ссылка>')
|
||||
return
|
||||
name, new_name, new_url = args[0], args[1], args[2]
|
||||
session = SessionLocal()
|
||||
button = session.query(Button).filter_by(name=name).first()
|
||||
if not button:
|
||||
await update.message.reply_text('Кнопка не найдена.')
|
||||
session.close()
|
||||
return
|
||||
button.name = new_name
|
||||
button.url = new_url
|
||||
session.commit()
|
||||
session.close()
|
||||
await update.message.reply_text(f'Кнопка "{name}" изменена.')
|
||||
2
handlers/edit_post.py
Normal file
2
handlers/edit_post.py
Normal file
@@ -0,0 +1,2 @@
|
||||
# Заглушка для будущей реализации редактирования поста
|
||||
# Можно реализовать хранение черновиков и их редактирование
|
||||
40
handlers/group_buttons.py
Normal file
40
handlers/group_buttons.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from telegram.ext import CommandHandler, CallbackQueryHandler, ConversationHandler, ContextTypes
|
||||
from db import SessionLocal
|
||||
from models import Group, Button
|
||||
|
||||
SELECT_GROUP, MANAGE_BUTTONS = range(2)
|
||||
|
||||
async def group_buttons_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
session = SessionLocal()
|
||||
groups = session.query(Group).all()
|
||||
session.close()
|
||||
keyboard = [[InlineKeyboardButton(g.name, callback_data=str(g.id))] for g in groups]
|
||||
await update.message.reply_text(
|
||||
"Выберите группу для настройки клавиатуры:",
|
||||
reply_markup=InlineKeyboardMarkup(keyboard)
|
||||
)
|
||||
return SELECT_GROUP
|
||||
|
||||
async def select_group(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
group_id = int(query.data)
|
||||
context.user_data['group_id'] = group_id
|
||||
session = SessionLocal()
|
||||
buttons = session.query(Button).filter_by(group_id=group_id).all()
|
||||
session.close()
|
||||
text = "Кнопки этой группы:\n"
|
||||
for b in buttons:
|
||||
text += f"- {b.name}: {b.url}\n"
|
||||
text += "\nДобавить новую кнопку: /add_button\nУдалить: /del_button <название>"
|
||||
await query.edit_message_text(text)
|
||||
return ConversationHandler.END
|
||||
|
||||
group_buttons_conv = ConversationHandler(
|
||||
entry_points=[CommandHandler('group_buttons', group_buttons_start)],
|
||||
states={
|
||||
SELECT_GROUP: [CallbackQueryHandler(select_group)],
|
||||
},
|
||||
fallbacks=[]
|
||||
)
|
||||
73
handlers/new_post.py
Normal file
73
handlers/new_post.py
Normal file
@@ -0,0 +1,73 @@
|
||||
from telegram import Update, InputMediaPhoto, InlineKeyboardMarkup, InlineKeyboardButton
|
||||
from telegram.ext import ContextTypes, ConversationHandler, MessageHandler, CommandHandler, filters, CallbackQueryHandler, ContextTypes
|
||||
|
||||
from db import SessionLocal
|
||||
from models import Channel, Group, Button
|
||||
|
||||
SELECT_MEDIA, SELECT_TEXT, SELECT_TARGET = range(3)
|
||||
|
||||
async def new_post_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
await update.message.reply_text('Отправьте картинку для поста или /skip:')
|
||||
return SELECT_MEDIA
|
||||
|
||||
async def select_media(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
if update.message.photo:
|
||||
context.user_data['photo'] = update.message.photo[-1].file_id
|
||||
await update.message.reply_text('Введите текст поста или пересланное сообщение:')
|
||||
return SELECT_TEXT
|
||||
|
||||
async def select_text(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
context.user_data['text'] = update.message.text or update.message.caption
|
||||
session = SessionLocal()
|
||||
channels = session.query(Channel).all()
|
||||
groups = session.query(Group).all()
|
||||
session.close()
|
||||
keyboard = []
|
||||
for c in channels:
|
||||
keyboard.append([InlineKeyboardButton(f'Канал: {c.name}', callback_data=f'channel_{c.id}')])
|
||||
for g in groups:
|
||||
keyboard.append([InlineKeyboardButton(f'Группа: {g.name}', callback_data=f'group_{g.id}')])
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
await update.message.reply_text('Выберите, куда отправить пост:', reply_markup=reply_markup)
|
||||
return SELECT_TARGET
|
||||
|
||||
async def select_target(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
data = query.data
|
||||
session = SessionLocal()
|
||||
try:
|
||||
if data.startswith('channel_'):
|
||||
channel_id = int(data.split('_')[1])
|
||||
channel = session.query(Channel).get(channel_id)
|
||||
buttons = session.query(Button).filter_by(channel_id=channel_id).all()
|
||||
markup = InlineKeyboardMarkup([[InlineKeyboardButton(b.name, url=b.url)] for b in buttons]) if buttons else None
|
||||
chat_id = channel.link.strip()
|
||||
else:
|
||||
group_id = int(data.split('_')[1])
|
||||
group = session.query(Group).get(group_id)
|
||||
buttons = session.query(Button).filter_by(group_id=group_id).all()
|
||||
markup = InlineKeyboardMarkup([[InlineKeyboardButton(b.name, url=b.url)] for b in buttons]) if buttons else None
|
||||
chat_id = group.link.strip()
|
||||
# Проверка chat_id
|
||||
if not (chat_id.startswith('@') or chat_id.startswith('-')):
|
||||
await query.edit_message_text('Ошибка: ссылка должна быть username (@channel) или числовой ID (-100...)')
|
||||
return ConversationHandler.END
|
||||
try:
|
||||
await context.bot.send_photo(chat_id=chat_id, photo=context.user_data.get('photo'), caption=context.user_data.get('text'), reply_markup=markup)
|
||||
await query.edit_message_text('Пост отправлен!')
|
||||
except Exception as e:
|
||||
await query.edit_message_text(f'Ошибка отправки поста: {e}')
|
||||
finally:
|
||||
session.close()
|
||||
return ConversationHandler.END
|
||||
|
||||
new_post_conv = ConversationHandler(
|
||||
entry_points=[CommandHandler('new_post', new_post_start)],
|
||||
states={
|
||||
SELECT_MEDIA: [MessageHandler(filters.PHOTO | filters.Document.IMAGE | filters.COMMAND, select_media)],
|
||||
SELECT_TEXT: [MessageHandler(filters.TEXT | filters.FORWARDED, select_text)],
|
||||
SELECT_TARGET: [CallbackQueryHandler(select_target)],
|
||||
},
|
||||
fallbacks=[]
|
||||
)
|
||||
2
handlers/send_post.py
Normal file
2
handlers/send_post.py
Normal file
@@ -0,0 +1,2 @@
|
||||
# Заглушка для будущей реализации отправки поста вручную
|
||||
# Можно реализовать выбор поста и канал/группу для отправки
|
||||
Reference in New Issue
Block a user