[英]How to connect Python chatbot and Java chat room
我使用Python编写chatbot程序。 收到消息后,它将计算要说的内容并返回消息。
我的朋友使用Java写聊天室。 这是一个普通的聊天室,但是当人类发送消息时,它会将其发送到chatbot。
如何连接它们? 它们在同一台PC上运行,并且不使用Internet。
您可以使用运行时类来实现。 示例代码:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.