godot_repl_server/bot/main.py

58 lines
1.5 KiB
Python
Raw Permalink Normal View History

2022-05-19 12:18:00 +03:00
from urllib import response
from aiogram import types, Bot, Dispatcher
2022-05-19 15:09:57 +03:00
from aiogram.utils.text_decorations import markdown_decoration
2022-05-19 12:18:00 +03:00
import requests
from dotenv import load_dotenv
import os
load_dotenv()
TOKEN = os.getenv("TOKEN")
HOST = os.getenv("HOST", "http://127.0.0.1:8080")
if not TOKEN:
print("Token not specified")
exit(1)
bot: Bot = Bot(TOKEN)
dp: Dispatcher = Dispatcher()
2022-05-19 15:09:57 +03:00
def escape_md(text):
return markdown_decoration.quote(text)
2022-05-19 12:18:00 +03:00
@dp.message(commands=['run'])
async def message_handler(message: types.Message):
2022-05-19 15:09:57 +03:00
if not message.reply_to_message:
return
2022-05-19 12:18:00 +03:00
msg = message.reply_to_message
2022-05-19 15:09:57 +03:00
if not msg.entities:
return
2022-05-19 12:18:00 +03:00
print(msg.text)
for entity in msg.entities:
2022-05-19 15:09:57 +03:00
if entity.type != "pre" and entity.type != "code":
continue
2022-05-19 12:18:00 +03:00
code = entity.extract(msg.text)
resp = requests.post(HOST+"/run", data={'code': code})
json = resp.json()
if resp.status_code != 200:
2022-05-19 15:09:57 +03:00
if json['stderr'].strip() == "":
return await msg.reply("Таймаут скрипта")
return await msg.reply("Произошла ошибка:\n" + json["stderr"])
result = json['result'][:4095]
answer = f"{result}"
repr(result)
if result.strip() == "":
answer = "Скрипт выполнен, вывод пуст."
await msg.reply(answer)
2022-05-19 12:18:00 +03:00
def main():
dp.run_polling(bot)
2022-05-19 15:09:57 +03:00
2022-05-19 12:18:00 +03:00
if __name__ == "__main__":
2022-05-19 15:09:57 +03:00
main()