如何连接 Python 聊天机器人和 Java 聊天室

How to connect Python chatbot and Java chat room

我使用 Python 编写聊天机器人程序。当它收到一条消息时,它会计算要说的内容并 return 一条消息。

我朋友用 Java 写了一个聊天室。这是一个普通的聊天室,但是当人类发送消息时,它会将其发送给聊天机器人。

如何连接它们?他们 运行 在同一台 PC 上并且不使用互联网。

您可以使用运行时 class 来完成。示例代码:

public String sendMessage(String message) throws IOException {
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("python /Users/user/bot.py " + message);

    BufferedReader stdInput = new BufferedReader(new
            InputStreamReader(proc.getInputStream()));

    BufferedReader stdError = new BufferedReader(new
            InputStreamReader(proc.getErrorStream()));

    // read the output from the command
    String s = null;
    StringBuilder answer = new StringBuilder();
    while ((s = stdInput.readLine()) != null) {
        answer.append(s);
    }

    return answer.toString();
}