async def run(session: CommandSession): supported_languages = ", ".join(sorted(SUPPORTED_LANGUAGES.keys())) language = session.get('language', prompt='你想运行的代码是什么语言呢亲亲?\n' f'目前支持 {supported_languages}') if language not in SUPPORTED_LANGUAGES: session.finish('暂时不支持运行你输入的编程语言呢亲亲') code = session.get('code', prompt='请把代码发一下呢亲亲') await session.send('正在运行,请稍等……') resp = await requests.post( RUN_API_URL_FORMAT.format(language), json={ 'files': [{ 'name': (SUPPORTED_LANGUAGES[language].get('name', 'main') + f'.{SUPPORTED_LANGUAGES[language]["ext"]}'), "content": code }], 'stdin': '', 'command': '' }) if not resp.ok: session.finish('运行失败,服务可能暂时不可用了呢亲亲') payload = await resp.json() if not isinstance(payload, dict): session.finish('运行失败,服务可能暂时不可用了呢亲亲') sent = False for k in ['stdout', 'stderr', 'error']: v = payload.get(k) lines = v.splitlines() lines, remained_lines = lines[:10], lines[10:] out = '\n'.join(lines) out, remained_out = out[:60 * 10], out[60 * 10:] if remained_lines or remained_out: out += f'\n(输出过多,已忽略剩余内容)' out = message_escape(out) if out: await session.send(f'{k}:\n\n{out}') sent = True if not sent: session.finish('运行成功,没有任何输出呢')
async def run(session: CommandSession): supported_languages = ", ".join(sorted(SUPPORTED_LANGUAGES.keys())) language = session.get( "language", prompt="你想运行的代码是什么语言?\n" f"目前支持 {supported_languages}" ) code = session.get("code", prompt="你想运行的代码是?") await session.send("正在运行,请稍等……") async with aiohttp.ClientSession(headers=headers) as sess: async with sess.post( RUN_API_URL_FORMAT.format(language), json={ "files": [ { "name": ( SUPPORTED_LANGUAGES[language].get("name", "main") + f'.{SUPPORTED_LANGUAGES[language]["ext"]}' ), "content": code, } ], "stdin": "", "command": "", }, ) as resp: if resp.status != 200: session.finish("运行失败,服务可能暂时不可用,请稍后再试") payload = await resp.json() if not isinstance(payload, dict): session.finish("运行失败,服务可能暂时不可用,请稍后再试") sent = False for k in ["stdout", "stderr", "error"]: v = payload.get(k) lines = v.splitlines() lines, remained_lines = lines[:10], lines[10:] out = "\n".join(lines) out, remained_out = out[: 60 * 10], out[60 * 10 :] if remained_lines or remained_out: out += f"\n(输出过多,已忽略剩余内容)" out = message_escape(out) if out: await session.send(f"{k}:\n\n{out}") sent = True if not sent: session.finish("运行成功,没有任何输出")
async def run(session: CommandSession): api_token = env("GLOT_IO_TOKEN", None) if not api_token: logger.error("未设置 `GLOT_IO_TOKEN`") session.finish("运行代码功能未启用") supported_languages = ", ".join(sorted(SUPPORTED_LANGUAGES)) language = session.get( "language", prompt=f"你想运行的代码是什么语言?\n目前支持 {supported_languages}", arg_filters=[ controllers.handle_cancellation(session), str.lstrip, validators.not_empty("请输入有效内容哦~"), ], ) code = session.get( "code", prompt="你想运行的代码是?", arg_filters=[ controllers.handle_cancellation(session), str.lstrip, validators.not_empty("请输入有效内容哦~"), ], ) await session.send("正在运行,请稍等……") async with httpx.AsyncClient() as client: resp = await client.post( RUN_API_URL_FORMAT.format(language), json={ "files": [{ "name": (SUPPORTED_LANGUAGES[language].get("name", "main")) + f'.{SUPPORTED_LANGUAGES[language]["ext"]}', "content": code, }], }, headers={"Authorization": f"Token {api_token}"}, ) if not resp: session.finish("运行失败,服务可能暂时不可用,请稍后再试。") payload = resp.json() sent = False for k in ["stdout", "stderr", "error"]: v = payload.get(k) lines = v.splitlines() lines, remained_lines = lines[:10], lines[10:] out = "\n".join(lines) out, remained_out = out[:60 * 10], out[60 * 10:] if remained_lines or remained_out: out += "\n(输出过多,已忽略剩余内容)" out = message_escape(out) if out: await session.send(f"{k}:\n\n{out}") sent = True if not sent: session.finish("运行成功,没有任何输出")