From 7ef0e6d40c84b75a9d38ab7991f4597f50b10c64 Mon Sep 17 00:00:00 2001 From: maloriki Date: Sun, 10 Mar 2024 16:15:13 +0300 Subject: [PATCH 1/3] tg bot arrived --- tg_bot/bot.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tg_bot/bot.py diff --git a/tg_bot/bot.py b/tg_bot/bot.py new file mode 100644 index 0000000..f670dcb --- /dev/null +++ b/tg_bot/bot.py @@ -0,0 +1,41 @@ +import telebot +from telebot import types +import pika + +token = '7026760925:AAHpUd9zOHVm_Vmo_OqJ1Kgc4AlRNvUQSG4' +bot = telebot.TeleBot(token) +help_text = "Это бот для сдачи решений по программированию в контест систему (ДТС)" + + +@bot.message_handler(commands=['start']) +def start_message(message): + bot.send_message(message.chat.id, "Привет это бот для проекта по питону") + keyboard = types.InlineKeyboardMarkup(row_width=2) + button1 = types.InlineKeyboardButton("Сдать решение", callback_data='btn1') + button2 = types.InlineKeyboardButton("help", callback_data='btn2') + keyboard.add(button1, button2) + + +@bot.callback_query_handler(func=lambda call: True) +def callback_handler(call): + if call.data == 'btn1': + bot.send_message(call.message.chat.id, "Загрузите своей решение файлом, либо текстом.") + elif call.data == 'btn2': + bot.send_message(call.message.chat.id, help_text) + + @bot.message_handler(content_types=['text']) + def handle_text(message): + text_input = message.text + bot.send_message(message.chat.id, f"You entered text: {text_input}") + + @bot.message_handler(content_types=['document']) + def handle_file(message): + file = bot.get_file(message.document.file_id) + downloaded_file = bot.download_file(file.file_path) + with open("file_received.txt", 'wb') as file: + file.write(downloaded_file) + + bot.send_message(message.chat.id, "File successfully saved.") + + +bot.infinity_polling() \ No newline at end of file From 5a6eb9d888aec244a60108491b04d9cafa056f7c Mon Sep 17 00:00:00 2001 From: maloriki Date: Sun, 10 Mar 2024 16:32:31 +0300 Subject: [PATCH 2/3] hide token --- tg_bot/bot.py | 5 ++++- tg_bot/config.yaml | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 tg_bot/config.yaml diff --git a/tg_bot/bot.py b/tg_bot/bot.py index f670dcb..6a52ef8 100644 --- a/tg_bot/bot.py +++ b/tg_bot/bot.py @@ -1,8 +1,11 @@ import telebot from telebot import types import pika +import yaml -token = '7026760925:AAHpUd9zOHVm_Vmo_OqJ1Kgc4AlRNvUQSG4' +with open("config.yaml", "r") as file: + data = yaml.safe_load(file) +token = data['token'] bot = telebot.TeleBot(token) help_text = "Это бот для сдачи решений по программированию в контест систему (ДТС)" diff --git a/tg_bot/config.yaml b/tg_bot/config.yaml new file mode 100644 index 0000000..57c5672 --- /dev/null +++ b/tg_bot/config.yaml @@ -0,0 +1 @@ +token : '7026760925:AAHpUd9zOHVm_Vmo_OqJ1Kgc4AlRNvUQSG4' \ No newline at end of file From da2b2ad9ab0bbb89ba350ccb6390a76c3d218c28 Mon Sep 17 00:00:00 2001 From: maloriki Date: Mon, 11 Mar 2024 14:51:43 +0300 Subject: [PATCH 3/3] prikol --- tg_bot/bot.py | 75 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 21 deletions(-) diff --git a/tg_bot/bot.py b/tg_bot/bot.py index 6a52ef8..4b60c7d 100644 --- a/tg_bot/bot.py +++ b/tg_bot/bot.py @@ -2,43 +2,76 @@ from telebot import types import pika import yaml +import json with open("config.yaml", "r") as file: data = yaml.safe_load(file) token = data['token'] bot = telebot.TeleBot(token) help_text = "Это бот для сдачи решений по программированию в контест систему (ДТС)" +user_states = {} @bot.message_handler(commands=['start']) def start_message(message): - bot.send_message(message.chat.id, "Привет это бот для проекта по питону") - keyboard = types.InlineKeyboardMarkup(row_width=2) - button1 = types.InlineKeyboardButton("Сдать решение", callback_data='btn1') - button2 = types.InlineKeyboardButton("help", callback_data='btn2') + keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True) + button1 = types.KeyboardButton("Сдать решение") + button2 = types.KeyboardButton("help") keyboard.add(button1, button2) + bot.send_message(message.chat.id, "Привет это бот для проекта по питону", reply_markup=keyboard) -@bot.callback_query_handler(func=lambda call: True) -def callback_handler(call): - if call.data == 'btn1': - bot.send_message(call.message.chat.id, "Загрузите своей решение файлом, либо текстом.") - elif call.data == 'btn2': - bot.send_message(call.message.chat.id, help_text) +@bot.message_handler(func=lambda message: message.text == 'Сдать решение') +def handle_button1(message): + user_states[message.chat.id] = 'Сдать решение' + bot.register_next_step_handler(message, compilation_choice) - @bot.message_handler(content_types=['text']) - def handle_text(message): - text_input = message.text - bot.send_message(message.chat.id, f"You entered text: {text_input}") - @bot.message_handler(content_types=['document']) - def handle_file(message): - file = bot.get_file(message.document.file_id) - downloaded_file = bot.download_file(file.file_path) - with open("file_received.txt", 'wb') as file: - file.write(downloaded_file) +def compilation_choice(message): + solution_text = message.text + keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True) + button1 = types.KeyboardButton("gcc") + button2 = types.KeyboardButton("python") + keyboard.add(button1, button2) + bot.send_message(message.chat.id, "Выберите компилятор для решения.", reply_markup=keyboard) + bot.register_next_step_handler(message, lambda message: send_solution(message, solution_text)) + + +def send_solution(message, solution_text): + if message.text == 'gcc': + c_plus_plus_solution(message, solution_text) + elif message.text == 'python': + pass + + +def c_plus_plus_solution(message, solution_text): + connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) + channel = connection.channel() + + channel.queue_declare(queue='test') + json_message = { + "submissionId": 0, + "buildId": 0, + "userData": solution_text, + "taskId": 0, + "memoryLimit": 10241024, + "cpuTimeLimitMilliSeconds": 2000 + } + + print(json.dumps(json_message)) + + channel.basic_publish( + exchange='', + routing_key='test', + body=json.dumps(json_message) + ) + bot.send_message(message.chat.id, 'Ваше решение отправлено на сервер!') + - bot.send_message(message.chat.id, "File successfully saved.") +@bot.message_handler(func=lambda message: message.text == 'help') +def handle_button2(message): + user_states[message.chat.id] = 'help' + bot.send_message(message.chat.id, help_text) bot.infinity_polling() \ No newline at end of file