【问题标题】:How to connect Python chatbot and Java chat room如何连接 Python 聊天机器人和 Java 聊天室
【发布时间】:2017-07-25 06:42:56
【问题描述】:

我使用 Python 编写聊天机器人程序。当它收到一条消息时,它会计算要说什么并返回一条消息。

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

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

【问题讨论】:

标签: java python artificial-intelligence chatbot


【解决方案1】:

您可以使用运行时类来做到这一点。示例代码:

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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    相关资源
    最近更新 更多