Вероятно, вам нужно что-то вроде этого:
import asyncio
import logging
import subprocess
from threading import Thread
from aiogram import Bot, Dispatcher, executor, types
logging.basicConfig(level=logging.INFO)
bot = Bot(token='ТВОЙ ТОКЕН')
dp = Dispatcher(bot)
loop = asyncio.get_event_loop()
proc = None
text_to_send = ''
chat_id = -1
def read_stdout():
global text_to_send
while True:
output = proc.stdout.readline()
if output != '':
text_to_send += output.decode()
async def sender():
global text_to_send
while True:
await asyncio.sleep(3)
if text_to_send != '':
await bot.send_message(chat_id, text_to_send)
text_to_send = ''
@dp.message_handler(commands=['start'])
async def start(msg: types.Message):
global chat_id, proc
if not proc:
proc = subprocess.Popen(
[java8_path, '-jar', 'spigot-1.12.2.jar'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
cwd=server_path,
)
chat_id = msg.chat.id
th = Thread(target=read_stdout)
th.start()
@dp.message_handler()
async def input_msg(msg: types.Message):
global chat_id
if proc:
proc.stdin.write((msg.text + '\n').encode())
proc.stdin.flush()
if __name__ == '__main__':
loop.create_task(sender())
executor.start_polling(dp)