|  | @@ -1,6 +1,7 @@
 | 
	
		
			
				|  |  |  from random import uniform
 | 
	
		
			
				|  |  |  from asyncio import sleep
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +from aiohttp import ClientSession
 | 
	
		
			
				|  |  |  from tortoise.contrib.postgres.functions import Random
 | 
	
		
			
				|  |  |  from telethon.utils import get_input_document
 | 
	
		
			
				|  |  |  from telethon.tl.functions.stickers import (
 | 
	
	
		
			
				|  | @@ -15,6 +16,7 @@ from telethon.tl.types import (
 | 
	
		
			
				|  |  |      InputMediaUploadedDocument,
 | 
	
		
			
				|  |  |      InputPeerSelf,
 | 
	
		
			
				|  |  |  )
 | 
	
		
			
				|  |  | +from telethon.errors import MessageEmptyError
 | 
	
		
			
				|  |  |  from tortoise.expressions import F
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  from models import (
 | 
	
	
		
			
				|  | @@ -27,7 +29,7 @@ from models import (
 | 
	
		
			
				|  |  |      AllowedChat,
 | 
	
		
			
				|  |  |      MarkovChat,
 | 
	
		
			
				|  |  |  )
 | 
	
		
			
				|  |  | -from utils import is_valid_name, is_valid_ip
 | 
	
		
			
				|  |  | +from utils import is_valid_name, is_valid_ip, remove_ansi_escapes
 | 
	
		
			
				|  |  |  from config import config
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -288,4 +290,48 @@ async def markov_say(bot, peer_id, reply_to=None, init_state=None):
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          await sleep(min(amount, 8))
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    await bot.send_message(peer_id, message=text, reply_to=reply_to)
 | 
	
		
			
				|  |  | +    await bot.send_message(peer_id, message=text, reply_to=reply_to)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +async def run(text):
 | 
	
		
			
				|  |  | +    match = re.match(r"^(\w+)(?:\s|\n)((?:\n|.)*)$", text)
 | 
	
		
			
				|  |  | +    if not match:
 | 
	
		
			
				|  |  | +        return "Пожалуйста, не оставляйте ввод пустым!"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    language_name, text = match.groups()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    if text.startswith("```") and text.endswith("```"):
 | 
	
		
			
				|  |  | +        text = text[3:-3]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    text = text.replace("\xa0", " ")  # i hate telegram
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    async with ClientSession() as session:
 | 
	
		
			
				|  |  | +        try:
 | 
	
		
			
				|  |  | +            async with session.post(
 | 
	
		
			
				|  |  | +                f"https://farlands.txlyre.website/run/{language_name}", data=text
 | 
	
		
			
				|  |  | +            ) as resp:
 | 
	
		
			
				|  |  | +                if resp.status in (404, 500):
 | 
	
		
			
				|  |  | +                    info = await resp.json()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    return f"Произошла ошибка при попытке обращения к API… :(\nОтвет API: {info['detail']}"
 | 
	
		
			
				|  |  | +                elif resp.status != 200:
 | 
	
		
			
				|  |  | +                    return "Сервер API временно недоступен. Пожалуйста, попробуйте ещё раз чуть позже."
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                text = await resp.read()
 | 
	
		
			
				|  |  | +                text = text.decode("UTF-8")[:4096]
 | 
	
		
			
				|  |  | +        except:
 | 
	
		
			
				|  |  | +            return "Произошла ошибка при попытке обращения к API… :("
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    text = remove_ansi_escapes(text).strip()
 | 
	
		
			
				|  |  | +    text = filter(
 | 
	
		
			
				|  |  | +        lambda c: (c in " \t\n" or ord(c) >= 32) and ord(c) not in range(128, 159), text
 | 
	
		
			
				|  |  | +    )
 | 
	
		
			
				|  |  | +    text = "".join(text)
 | 
	
		
			
				|  |  | +    text = text.replace("`", "")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    if not text:
 | 
	
		
			
				|  |  | +        return "<пусто>"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    try:
 | 
	
		
			
				|  |  | +        return f"```\n{text}```"
 | 
	
		
			
				|  |  | +    except ValueError, MessageEmptyError:
 | 
	
		
			
				|  |  | +        return "<Telegram не смог декодировать текст сообщения>"
 |