From 24af11f88a0358e657178ed9d66ab8d66183e534 Mon Sep 17 00:00:00 2001 From: Maxim Romanko Date: Wed, 31 Jul 2024 16:07:06 +0300 Subject: [PATCH] [Add] Added main func of container --- Dockerfile | 3 +- docker-compose | 17 +++++++++ main.py | 102 ++++++++++++++++++++++++++++++++++++++++++++++++- req.txt | 26 +++++++++++++ 4 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 docker-compose diff --git a/Dockerfile b/Dockerfile index 905fad6..3866d41 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,8 @@ MAINTAINER Tan Jin (tjtanjin) WORKDIR /usr/src/app # copy all the files to the container -COPY . . +COPY main.py . +COPY req.txt . RUN mkdir ./images # install app-specific dependencies diff --git a/docker-compose b/docker-compose new file mode 100644 index 0000000..693d7f6 --- /dev/null +++ b/docker-compose @@ -0,0 +1,17 @@ +version: '3.9' + +services: + + telegram_bot_docker_tmpl: + container_name: MireaCheckBot + image: mirea_check_bot + + build: . + + environment: + - BOT_TOKEN=${BOT_TOKEN} + + volumes: + - ./logs:/~/TelegramBotDockerTmpl/logs + + restart: on-failure \ No newline at end of file diff --git a/main.py b/main.py index bd1d0e2..efa925e 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,105 @@ +import os +from dotenv import load_dotenv +from health_ping import HealthPing + +from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, constants +from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes + +import requests + +dotenv_path = os.path.join(os.path.dirname(__file__), ".env") +load_dotenv(dotenv_path) + +TOKEN = os.getenv("BOT_TOKEN") +# CHAT = os.getenv("CHAT_ID") + +api_1 = "https://priem.mirea.ru/competitions_api/entrants?competitions[]=1793877758295678262" +api_2 = "https://priem.mirea.ru/competitions_api/entrants?competitions[]=1793877924783332662" + + +if os.getenv("HEALTHCHECKS_ENDPOINT"): + HealthPing( + url=os.getenv("HEALTHCHECKS_ENDPOINT"), + schedule="1 * * * *", + retries=[60, 300, 720], + ).start() + +hpo = 0 + +snils = "143-471-137-40" +place = 0 + +# Define a few command handlers. These usually take the two arguments update and +# context. +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Sends a message with three inline buttons attached.""" + await update.message.reply_text("Вы хуй, можете проверить списки!") + + +async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Send a message when the command /help is issued.""" + await update.message.reply_text("Help!") + + +async def check_lists(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Parses the CallbackQuery and updates the message text.""" + query = update.callback_query + + # CallbackQueries need to be answered, even if no notification to the user is needed + # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery + await query.answer() + + await query.edit_message_text(text=f"Selected option: {query.data}") + + +async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Echo the user message.""" + await update.message.reply_text(update.message.text) + + +async def check_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + global hpo, snils, place + + response = requests.get(api_1) + + if response.status_code == 200: + # Получение данных в формате JSON + + data = response.json() + + data = data["data"][0]["entrants"] + if len(data) != 0: + for entrant in data: + if entrant["spn"] != snils: + if "iHPO" in entrant.keys(): + hpo += 1 + if entrant["s"] == "Активный" or entrant["s"] == "Сданы ВИ": + place += 1 + else: + await update.message.reply_text(f"*Конкурс: Проектирование и обслуживание высоконагруженных информационных систем*\nВы на {place+1} месте по общему конкурсу, на {hpo+1} месте по высшему приоритеу, колво баллов {entrant["fm"]}", parse_mode=constants.ParseMode.MARKDOWN_V2) + else: + await update.message.reply_text(f"Проверьте позже, в текущий момент сервис недоступен!") + + else: + print(f'Ошибка: {response.status_code}') def main(): - pass + """ + Handles the initial launch of the program (entry point). + """ + print(TOKEN[:10]) + application = Application.builder().token(TOKEN).build() + + # on different commands - answer in Telegram + application.add_handler(CommandHandler("start", start)) + application.add_handler(CallbackQueryHandler(check_lists)) + application.add_handler(CommandHandler("help", help_command)) + application.add_handler(CommandHandler("check", check_command)) + + # Run the bot until the user presses Ctrl-C + application.run_polling(allowed_updates=Update.ALL_TYPES) + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/req.txt b/req.txt index e69de29..ae3a0b1 100644 --- a/req.txt +++ b/req.txt @@ -0,0 +1,26 @@ +aiofiles==23.2.1 +aiohttp==3.9.5 +aiosignal==1.3.1 +annotated-types==0.7.0 +anyio==4.4.0 +attrs==23.2.0 +certifi==2024.7.4 +charset-normalizer==3.3.2 +crontab==1.0.1 +frozenlist==1.4.1 +h11==0.14.0 +health_ping==1.0.1 +httpcore==1.0.5 +httpx==0.27.0 +idna==3.7 +magic-filter==1.0.12 +multidict==6.0.5 +pydantic==2.8.2 +pydantic_core==2.20.1 +python-dotenv==1.0.1 +python-telegram-bot==21.4 +requests==2.32.3 +sniffio==1.3.1 +typing_extensions==4.12.2 +urllib3==2.2.2 +yarl==1.9.4