mirror of
https://github.com/Faridik/Carolyn.git
synced 2026-07-08 13:21:53 +00:00
Добавлен /broadcast в API сервиса таблиц. Добавлен декоратор проверки доступа/ Добавлена команда /broadcast в телеграмбота. Захардкодена клавиатура
This commit is contained in:
parent
f285fb1350
commit
95a985d848
@ -10,6 +10,7 @@ import time
|
||||
logging.basicConfig()
|
||||
LOG = logging.getLogger(__name__)
|
||||
LOG.setLevel(logging.DEBUG)
|
||||
SUPER_USER = (420, 228)
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.db = None
|
||||
@ -30,13 +31,26 @@ def availability(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
if app.db is None:
|
||||
return flask.jsonify(dict(error=True, message=app.db_error)), 500
|
||||
raise Exception("cannot init manager")
|
||||
result = func(*args, **kwargs)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def superuser(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
tg_id = flask.request.args.get("tg_id", -1)
|
||||
student: Student = app.db.get_student_by_tg_id(tg_id)
|
||||
if student.number not in SUPER_USER:
|
||||
raise Forbidden(f"Доступ запрещен для студента {student.name}")
|
||||
result = func(student, *args, **kwargs)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def hello_world():
|
||||
return "Hello world!\n"
|
||||
@ -69,6 +83,14 @@ def grades():
|
||||
)
|
||||
|
||||
|
||||
@app.route("/broadcast")
|
||||
@availability
|
||||
@superuser
|
||||
def broadcast(student):
|
||||
"""Выполняет рассылку студентам."""
|
||||
return flask.jsonify(student)
|
||||
|
||||
|
||||
@app.errorhandler(StudentAlreadyAuthed)
|
||||
def show_error(err):
|
||||
return flask.jsonify({"error": type(err).__name__, "message": err.message}), 409
|
||||
|
||||
@ -6,6 +6,7 @@ from pathlib import Path
|
||||
|
||||
|
||||
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
|
||||
import telegram
|
||||
from telegram.ext import (
|
||||
Updater,
|
||||
CommandHandler,
|
||||
@ -163,6 +164,29 @@ def grades(update: Update, context: CallbackContext):
|
||||
look_msg.edit_text(MESSAGES.Score.timeit(diff))
|
||||
|
||||
|
||||
def broadcast(update: Update, context: CallbackContext):
|
||||
"""Команда: разослать студентам."""
|
||||
access_message = update.message.reply_text("🔐 Проверка доступа...")
|
||||
user_id = update.message.from_user.id
|
||||
try:
|
||||
r = requests.get(f"{HOST}/broadcast", params={"tg_id": user_id})
|
||||
assert r.status_code == 200, "Доступ запрещен к /broadcast"
|
||||
data = r.json()
|
||||
access_message.edit_text(f"👨💻 {data['name']}")
|
||||
|
||||
kb = telegram.ReplyKeyboardMarkup(
|
||||
[
|
||||
[telegram.KeyboardButton("5374"), telegram.KeyboardButton("5371")],
|
||||
[telegram.KeyboardButton("1337")],
|
||||
],
|
||||
one_time_keyboard=True,
|
||||
)
|
||||
update.message.reply_text("Выбери группу", reply_markup=kb)
|
||||
except:
|
||||
access_message.edit_text("🤚 Команда не может быть выполнена")
|
||||
LOG.exception("Не удалось отправить сообщение в бродкаст.")
|
||||
|
||||
|
||||
def echo(update: Update, context: CallbackContext):
|
||||
"""Ответ на не командное сообщение (отвечает тем же сообщением)"""
|
||||
context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
|
||||
@ -172,12 +196,14 @@ def echo(update: Update, context: CallbackContext):
|
||||
|
||||
start_handler = CommandHandler("start", start)
|
||||
grades_handler = CommandHandler("grades", grades)
|
||||
broadcast_handler = CommandHandler("broadcast", broadcast)
|
||||
|
||||
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
|
||||
|
||||
dispatcher.add_handler(start_handler)
|
||||
dispatcher.add_handler(grades_handler)
|
||||
dispatcher.add_handler(echo_handler)
|
||||
dispatcher.add_handler(broadcast_handler)
|
||||
|
||||
# Начало работы бота
|
||||
updater.start_polling()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user