Java 图灵机器人 自动回复

Posted 13rj1115

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 图灵机器人 自动回复相关的知识,希望对你有一定的参考价值。

先去http://www.tuling123.com/申请key

package com.rjgc13.tuling;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpGetRequest {
    
    public static String get(String url){
        try {
            HttpGet request=new HttpGet(url);
            HttpResponse response=HttpClients.createDefault().execute(request);
            String result="";
            if(response.getStatusLine().getStatusCode()==200){
                result=EntityUtils.toString(response.getEntity());
            }
            return result;
            
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
package com.rjgc13.tuling;

import net.sf.json.JSONObject;

public class TulingController {
    public static String getTulingRe(String info){
        String url="http://www.tuling123.com/openapi/api?key=********************************&info="+info;
        String tlResult =HttpGetRequest.get(url);
        JSONObject json=JSONObject.fromObject(tlResult);
        tlResult=json.getString("text");
        return tlResult;
    }
}
package com.rjgc13.tuling;

import java.util.Properties;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame {

    private static JFrame frame;
    private static JTextArea viewArea;
    private static JTextField viewField;
    private static JButton button1;
    private static JButton button2;
    private static JTextField MyName;
    public static String str="";
    public static void main(String[] args) {
        frame = new JFrame("Chat Room");
        viewArea = new JTextArea(10, 30);
        viewField = new JTextField(50);
        button1 = new JButton("Send");
        button2 = new JButton("Quit");
        MyName = new JTextField();
        MyName.setColumns(9);
        MyName.setText("Saber_Lily:  ");
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(8, 1));
        panel.add(MyName);
        panel.add(button1);
        panel.add(button2);
        JScrollPane sp = new JScrollPane(viewArea);
        viewArea.setLineWrap(true);
        sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        frame.add("Center", sp);
        frame.add("East", panel);
        frame.add("South", viewField);
        viewField.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent arg0) {
                if (arg0.getKeyChar() == ‘\n‘) {
                    String message = "";
                    message = MyName.getText() + viewField.getText();
                    str=str+viewField.getText()+"\n";
                    String ans = "小U:  ";
                    ans = ans + TulingController.getTulingRe(viewField.getText());
                    viewField.setText("");
                    viewArea.setText(viewArea.getText() + message + "\n");
                    viewArea.setText(viewArea.getText() + ans + "\n");
                    viewField.setFocusable(true);
                    viewField.requestFocus();
                }

            }

            public void keyReleased(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }

            public void keyPressed(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }
        });
        frame.setSize(700, 500);
        frame.setVisible(true);
        // 居中显示
        frame.setLocationRelativeTo(null);
        button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                String message = "";
                message = MyName.getText() + viewField.getText();
                String ans = "小U:  ";
                ans = ans + TulingController.getTulingRe(viewField.getText());
                viewField.setText("");
                viewArea.setText(viewArea.getText() + message + "\n");
                viewArea.setText(viewArea.getText() + ans + "\n");
                viewField.setFocusable(true);
                viewField.requestFocus();

            }
        });
        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        viewField.setFocusable(true);
        viewField.requestFocus();
        // frame.setFocusable(true);
        // frame.requestFocus();
    }
}

 

源代码下载 链接:http://pan.baidu.com/s/1kU7iQc3 密码:4bbk

以上是关于Java 图灵机器人 自动回复的主要内容,如果未能解决你的问题,请参考以下文章

微信公众号接入图灵机器人实现自动回复消息

编程实践利用 Python 调用图灵机器人 API 实现实时语音聊天及自动回复

微信自动聊天机器人

教你搭建微信公众号自动答复机器人(上)

基于Python开发的微信图灵机器人

基于Python开发的微信图灵机器人