add_group, add_Channel process modification
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -1,18 +1,62 @@
|
|||||||
|
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
from telegram.ext import ContextTypes
|
from telegram.ext import ContextTypes, ConversationHandler, CommandHandler, MessageHandler, filters
|
||||||
from db import AsyncSessionLocal
|
from db import AsyncSessionLocal
|
||||||
from models import Channel
|
from models import Channel
|
||||||
|
|
||||||
async def add_channel(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
INPUT_NAME, INPUT_LINK = range(2)
|
||||||
args = context.args or []
|
|
||||||
if update.message is None:
|
async def add_channel_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
return
|
if context.user_data is None:
|
||||||
if len(args) < 2:
|
context.user_data = {}
|
||||||
await update.message.reply_text('Используйте: /add_channel <название> <ссылка>')
|
if update.message:
|
||||||
return
|
await update.message.reply_text('Введите название канала (или "название ссылка" через пробел):')
|
||||||
name, link = args[0], args[1]
|
return INPUT_NAME
|
||||||
|
|
||||||
|
async def input_channel_name(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
if context.user_data is None:
|
||||||
|
context.user_data = {}
|
||||||
|
text = update.message.text.strip() if update.message and update.message.text else ''
|
||||||
|
if ' ' in text:
|
||||||
|
name, link = text.split(' ', 1)
|
||||||
|
context.user_data['channel_name'] = name
|
||||||
|
context.user_data['channel_link'] = link
|
||||||
|
return await save_channel(update, context)
|
||||||
|
else:
|
||||||
|
context.user_data['channel_name'] = text
|
||||||
|
if update.message:
|
||||||
|
await update.message.reply_text('Теперь отправьте ссылку на канал:')
|
||||||
|
return INPUT_LINK
|
||||||
|
|
||||||
|
async def input_channel_link(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
if context.user_data is None:
|
||||||
|
context.user_data = {}
|
||||||
|
link = update.message.text.strip() if update.message and update.message.text else ''
|
||||||
|
context.user_data['channel_link'] = link
|
||||||
|
return await save_channel(update, context)
|
||||||
|
|
||||||
|
async def save_channel(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
if context.user_data is None:
|
||||||
|
context.user_data = {}
|
||||||
|
name = context.user_data.get('channel_name')
|
||||||
|
link = context.user_data.get('channel_link')
|
||||||
|
if not name or not link:
|
||||||
|
if update.message:
|
||||||
|
await update.message.reply_text('Ошибка: не указано название или ссылка.')
|
||||||
|
return ConversationHandler.END
|
||||||
async with AsyncSessionLocal() as session:
|
async with AsyncSessionLocal() as session:
|
||||||
channel = Channel(name=name, link=link)
|
channel = Channel(name=name, link=link)
|
||||||
session.add(channel)
|
session.add(channel)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
await update.message.reply_text(f'Канал "{name}" добавлен.')
|
if update.message:
|
||||||
|
await update.message.reply_text(f'Канал "{name}" добавлен.')
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
|
add_channel_conv = ConversationHandler(
|
||||||
|
entry_points=[CommandHandler('add_channel', add_channel_start)],
|
||||||
|
states={
|
||||||
|
INPUT_NAME: [MessageHandler(filters.TEXT & ~filters.COMMAND, input_channel_name)],
|
||||||
|
INPUT_LINK: [MessageHandler(filters.TEXT & ~filters.COMMAND, input_channel_link)],
|
||||||
|
},
|
||||||
|
fallbacks=[]
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,19 +1,62 @@
|
|||||||
|
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
from telegram.ext import ContextTypes
|
from telegram.ext import ContextTypes, ConversationHandler, CommandHandler, MessageHandler, filters
|
||||||
from db import AsyncSessionLocal
|
from db import AsyncSessionLocal
|
||||||
from models import Group
|
from models import Group
|
||||||
|
|
||||||
async def add_group(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
INPUT_NAME, INPUT_LINK = range(2)
|
||||||
args = context.args or []
|
|
||||||
if update.message is None:
|
async def add_group_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
return
|
if context.user_data is None:
|
||||||
if len(args) < 2:
|
context.user_data = {}
|
||||||
await update.message.reply_text('Используйте: /add_group <название> <ссылка>')
|
if update.message:
|
||||||
return
|
await update.message.reply_text('Введите название группы (или "название ссылка" через пробел):')
|
||||||
name, link = args[0], args[1]
|
return INPUT_NAME
|
||||||
session = AsyncSessionLocal()
|
|
||||||
group = Group(name=name, link=link)
|
async def input_group_name(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
session.add(group)
|
if context.user_data is None:
|
||||||
session.commit()
|
context.user_data = {}
|
||||||
session.close()
|
text = update.message.text.strip() if update.message and update.message.text else ''
|
||||||
await update.message.reply_text(f'Группа "{name}" добавлена.')
|
if ' ' in text:
|
||||||
|
name, link = text.split(' ', 1)
|
||||||
|
context.user_data['group_name'] = name
|
||||||
|
context.user_data['group_link'] = link
|
||||||
|
return await save_group(update, context)
|
||||||
|
else:
|
||||||
|
context.user_data['group_name'] = text
|
||||||
|
if update.message:
|
||||||
|
await update.message.reply_text('Теперь отправьте ссылку на группу:')
|
||||||
|
return INPUT_LINK
|
||||||
|
|
||||||
|
async def input_group_link(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
if context.user_data is None:
|
||||||
|
context.user_data = {}
|
||||||
|
link = update.message.text.strip() if update.message and update.message.text else ''
|
||||||
|
context.user_data['group_link'] = link
|
||||||
|
return await save_group(update, context)
|
||||||
|
|
||||||
|
async def save_group(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
if context.user_data is None:
|
||||||
|
context.user_data = {}
|
||||||
|
name = context.user_data.get('group_name')
|
||||||
|
link = context.user_data.get('group_link')
|
||||||
|
if not name or not link:
|
||||||
|
if update.message:
|
||||||
|
await update.message.reply_text('Ошибка: не указано название или ссылка.')
|
||||||
|
return ConversationHandler.END
|
||||||
|
async with AsyncSessionLocal() as session:
|
||||||
|
group = Group(name=name, link=link)
|
||||||
|
session.add(group)
|
||||||
|
await session.commit()
|
||||||
|
if update.message:
|
||||||
|
await update.message.reply_text(f'Группа "{name}" добавлена.')
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
|
add_group_conv = ConversationHandler(
|
||||||
|
entry_points=[CommandHandler('add_group', add_group_start)],
|
||||||
|
states={
|
||||||
|
INPUT_NAME: [MessageHandler(filters.TEXT & ~filters.COMMAND, input_group_name)],
|
||||||
|
INPUT_LINK: [MessageHandler(filters.TEXT & ~filters.COMMAND, input_group_link)],
|
||||||
|
},
|
||||||
|
fallbacks=[]
|
||||||
|
)
|
||||||
|
|||||||
11
init_db.py
11
init_db.py
@@ -1,5 +1,16 @@
|
|||||||
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
from db import init_db
|
from db import init_db
|
||||||
|
|
||||||
|
# Проверка bot.db перед инициализацией
|
||||||
|
if os.path.exists("bot.db") and os.path.isdir("bot.db"):
|
||||||
|
print("Удаляю папку bot.db...")
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
shutil.rmtree("bot.db")
|
||||||
|
if not os.path.exists("bot.db"):
|
||||||
|
print("Создаю пустой файл bot.db...")
|
||||||
|
open("bot.db", "a").close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(init_db())
|
asyncio.run(init_db())
|
||||||
|
|||||||
8
main.py
8
main.py
@@ -65,8 +65,8 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||||||
await update.message.reply_text(help_text, parse_mode='HTML')
|
await update.message.reply_text(help_text, parse_mode='HTML')
|
||||||
|
|
||||||
# Импорт обработчиков
|
# Импорт обработчиков
|
||||||
from handlers.add_channel import add_channel
|
from handlers.add_channel import add_channel_conv
|
||||||
from handlers.add_group import add_group
|
from handlers.add_group import add_group_conv
|
||||||
from handlers.add_button import add_button_conv
|
from handlers.add_button import add_button_conv
|
||||||
from handlers.new_post import new_post_conv
|
from handlers.new_post import new_post_conv
|
||||||
from handlers.group_buttons import group_buttons_conv
|
from handlers.group_buttons import group_buttons_conv
|
||||||
@@ -84,8 +84,8 @@ def main():
|
|||||||
application = Application.builder().token(TELEGRAM_TOKEN).build()
|
application = Application.builder().token(TELEGRAM_TOKEN).build()
|
||||||
application.add_handler(CommandHandler('start', start))
|
application.add_handler(CommandHandler('start', start))
|
||||||
application.add_handler(CommandHandler('help', help_command))
|
application.add_handler(CommandHandler('help', help_command))
|
||||||
application.add_handler(CommandHandler('add_channel', add_channel))
|
application.add_handler(add_channel_conv)
|
||||||
application.add_handler(CommandHandler('add_group', add_group))
|
application.add_handler(add_group_conv)
|
||||||
application.add_handler(add_button_conv)
|
application.add_handler(add_button_conv)
|
||||||
application.add_handler(new_post_conv)
|
application.add_handler(new_post_conv)
|
||||||
application.add_handler(group_buttons_conv)
|
application.add_handler(group_buttons_conv)
|
||||||
|
|||||||
Reference in New Issue
Block a user