我使用 Python 编写聊天机器人程序。当它收到消息时,它会计算要说什么并返回消息。
我的 friend 用Java写了一个聊天室。这是一个常见的聊天室,但是当人类发送消息时,会将其发送给聊天机器人。
如何连接它们?它们在同一台电脑上运行并且不使用互联网。
最佳答案
您可以使用运行时类来完成此操作。示例代码:
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();
}
关于java - 如何连接Python聊天机器人和Java聊天室,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45295734/