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



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

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

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

您可以使用运行时类执行此操作。 示例代码:

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

最新更新