init commit
This commit is contained in:
79
app/bot/handlers/drafts.py
Normal file
79
app/bot/handlers/drafts.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from datetime import datetime
|
||||
from telegram import Update
|
||||
from telegram.constants import ChatType, ParseMode
|
||||
from telegram.ext import ContextTypes
|
||||
|
||||
from app.db.session import get_session
|
||||
from app.db.models import User, Draft
|
||||
from app.bot.messages import (
|
||||
ASK_MEDIA, ASK_TEXT, TEXT_ADDED, CONFIRM, ALREADY_AT_TEXT, ALREADY_READY, NEED_START_NEW
|
||||
)
|
||||
from app.bot.keyboards.common import kb_next_text, kb_confirm
|
||||
from .add_group import add_group_capture, STATE_KEY # /add_group ожидание
|
||||
|
||||
# Состояния пошагового редактора
|
||||
STATE_DRAFT = "draft_state"
|
||||
KEY_DRAFT_ID = "draft_id"
|
||||
STATE_AWAIT_MEDIA = "await_media"
|
||||
STATE_AWAIT_TEXT = "await_text"
|
||||
STATE_CONFIRM = "confirm"
|
||||
|
||||
|
||||
def _start_new_draft(tg_id: int) -> Draft:
|
||||
with get_session() as s:
|
||||
u = s.query(User).filter_by(tg_id=tg_id).first()
|
||||
if not u:
|
||||
u = User(tg_id=tg_id, name="")
|
||||
s.add(u); s.commit(); s.refresh(u)
|
||||
# Закрываем предыдущие "editing" как cancelled (по желанию)
|
||||
s.query(Draft).filter(Draft.user_id == u.id, Draft.status == "editing").update({"status": "cancelled"})
|
||||
d = Draft(user_id=u.id, status="editing")
|
||||
s.add(d); s.commit(); s.refresh(d)
|
||||
return d
|
||||
|
||||
|
||||
def _get_draft(draft_id: int) -> Draft | None:
|
||||
with get_session() as s:
|
||||
return s.get(Draft, draft_id)
|
||||
|
||||
|
||||
async def new_cmd(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
||||
d = _start_new_draft(update.effective_user.id)
|
||||
ctx.user_data[KEY_DRAFT_ID] = d.id
|
||||
ctx.user_data[STATE_DRAFT] = STATE_AWAIT_MEDIA
|
||||
await update.effective_message.reply_text(ASK_MEDIA, reply_markup=kb_next_text(d.id))
|
||||
|
||||
|
||||
async def on_text(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
||||
# Если ждём chat_id для /add_group, отдаём управление туда
|
||||
if ctx.user_data.get(STATE_KEY):
|
||||
return await add_group_capture(update, ctx)
|
||||
|
||||
if update.effective_chat.type != ChatType.PRIVATE:
|
||||
return
|
||||
|
||||
draft_id = ctx.user_data.get(KEY_DRAFT_ID)
|
||||
state = ctx.user_data.get(STATE_DRAFT)
|
||||
|
||||
if not draft_id or not state:
|
||||
await update.effective_message.reply_text(NEED_START_NEW)
|
||||
return
|
||||
|
||||
if state == STATE_AWAIT_MEDIA:
|
||||
# Сначала медиа
|
||||
await update.effective_message.reply_text("Сначала пришлите медиа и нажмите «Дальше — текст».")
|
||||
return
|
||||
|
||||
if state == STATE_CONFIRM:
|
||||
await update.effective_message.reply_text(ALREADY_READY)
|
||||
return
|
||||
|
||||
if state == STATE_AWAIT_TEXT:
|
||||
with get_session() as s:
|
||||
d = s.get(Draft, draft_id)
|
||||
d.text = update.effective_message.text_html_urled
|
||||
d.updated_at = datetime.utcnow()
|
||||
s.commit()
|
||||
ctx.user_data[STATE_DRAFT] = STATE_CONFIRM
|
||||
await update.effective_message.reply_text(TEXT_ADDED, parse_mode=ParseMode.HTML)
|
||||
await update.effective_message.reply_text(CONFIRM, reply_markup=kb_confirm(draft_id))
|
||||
Reference in New Issue
Block a user