时间:2021-05-19
本文实例为大家分享了java实现自动回复聊天机器人的具体代码,供大家参考,具体内容如下
聊天机器人
调用网上现有的接口,然后解析数据
以上是演示图片
源码下载地址
基本工作流程就是,调用API,解析返回的数据
HttpUtil类,调用API,获取返回的数据
package com;import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;/** * Created by zf on 2017/2/27. */public class HttpUtil { private static final String API = "xxxxxxxxxxxxxxxxx"; private static String MSG; private static HttpUtil INSTANCE; public static HttpUtil getInstance() { if (INSTANCE == null) { INSTANCE = new HttpUtil(); } return INSTANCE; } private HttpUtil() { } public String sendRequest2API(String msg) { if (msg.length() > 0) { this.MSG = msg; HttpURLConnection connection = null; StringBuilder response = new StringBuilder(); try { URL url = new URL(API + MSG); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { response.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } return response.toString(); } } return null; }}UI类,界面
package com;import com.google.gson.Gson;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Date;/** * Created by zf on 2017/2/27. */public class MainUI { private JFrame jFrame; private JPanel jPanel; private JButton sendMsgBtn; private JTextArea msgTextArea; private JTextArea historyTextArea; private static String MSG; private static StringBuilder history = new StringBuilder(); public MainUI() { jFrame = new JFrame("自动聊天"); jPanel = new JPanel(); sendMsgBtn = new JButton("发送"); msgTextArea = new JTextArea("这里发生消息"); historyTextArea = new JTextArea(20,20); historyTextArea.setBackground(new Color(255, 255, 255)); jPanel.add(historyTextArea); jPanel.add(msgTextArea); jPanel.add(sendMsgBtn); jFrame.add(jPanel); jFrame.setSize(500, 500); jFrame.setLocationRelativeTo(null); jFrame.setVisible(true); sendMsgBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { MSG = msgTextArea.getText(); history.append("我:" + "\n" + MSG + "\n"); Gson gson = new Gson(); RobotAnswer robotAnswer = gson.fromJson(HttpUtil.getInstance().sendRequest2API(MSG), RobotAnswer.class); history.append(robotAnswer.getAnswer()); historyTextArea.setText(history.toString()); System.out.println(history); } }); } public static void main(String[] args) { new MainUI(); }}机器人回复类
package com;import java.util.Date;/** * Created by zf on 2017/2/27. */public class RobotAnswer { private int result; private String content; private String answer; public RobotAnswer() { } public String getAnswer() { if (result == 0) { answer = "AI:" + "\n" + content; } else { answer = "....."; } return answer; }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
itchat库模拟微信网页登录通过pythoncode接受/发送微信消息实现微信聊天机器人:调用聊天机器人api,将接收到的微信消息传给api,再将api返回的
在之前的一篇文章Python利用AIML和Tornado搭建聊天机器人微信订阅号中用aiml实现了一个简单的英文聊天机器人订阅号。但是只能处理英文消息,现在用图
借助Python的AIML包,我们很容易实现人工智能聊天机器人。AIML,全名为ArtificialIntelligenceMarkupLanguage(人工智
从今天开始,FacebookMessenger推出一个新的聊天机器人,它整合了在线票务平台Fandango的服务。当美国用户的会话主题转向电影时,机器人能够自动
最近听说一个很好玩的图灵机器人api,正好可以用它做一个微信聊天机器人,下面是实现#test.pyimportrequestsimportitchat#这是一个