File size: 1,559 Bytes
851f3c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import sqlite3
from datetime import datetime
from app.config import FEEDBACK_DB_PATH
from app.integrations.telegram_bot import send_telegram_message

def _init_db():
    os.makedirs(os.path.dirname(FEEDBACK_DB_PATH), exist_ok=True)
    with sqlite3.connect(FEEDBACK_DB_PATH) as conn:
        conn.execute("""
        CREATE TABLE IF NOT EXISTS feedback (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            created_at TEXT,
            author TEXT,
            channel TEXT,
            topic TEXT,
            rating INTEGER,
            message TEXT
        )
        """)
        conn.commit()

def save_feedback(author: str, channel: str, topic: str, rating: int, message: str) -> str:
    _init_db()
    ts = datetime.utcnow().isoformat(timespec="seconds") + "Z"
    with sqlite3.connect(FEEDBACK_DB_PATH) as conn:
        conn.execute(
            "INSERT INTO feedback(created_at, author, channel, topic, rating, message) VALUES(?,?,?,?,?,?)",
            (ts, author.strip(), channel.strip(), topic.strip(), int(rating), message.strip())
        )
        conn.commit()
    return f"✅ Сохранено (UTC {ts})"

def send_feedback_to_telegram(author: str, channel: str, topic: str, rating: int, message: str) -> str:
    text = (
        "🧾 2MOOD Feedback\n"
        f"Автор: {author}\n"
        f"Канал: {channel}\n"
        f"Тема: {topic}\n"
        f"Оценка: {rating}/5\n\n"
        f"{message}"
    )
    ok, info = send_telegram_message(text)
    return "✅ " + info if ok else "⚠️ " + info