图灵机器人api,12年左右好像就有了,当时就接触了,玩了一段时间,然后就扔了,没有整理,现在整理一下。
图灵管网地址
http://www.tuling123.com/
项目地址
#项目源码地址
https://gitee.com/yellowcong/api/tree/master/tuling
##编译好的jar包
#使用的时候,
#第一种方法:需要导入这个jar包和相应的依赖包即可
#第二种方法:导入maven项目,这种是有需要在源码基础上扩展业务的处理
http://yellowcong.qiniudn.com/tuling-0.0.1-SNAPSHOT.jar
获取机器人的aip地址和key
项目结构
功能说明
类名称 | 作用 |
---|---|
TulingClient | 核心类,用于直接处理用户发送的消息 |
Message | 模型类,用于存发送返回的数据 |
HttpClientUtils | 用于发送请求给图灵服务器 |
JsonUtils | json字符串的相互转化 |
依赖包
<!-- JSON解析 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<!-- 用于反射,将json字符串转化为实体类 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<!-- 请求列,发送数据到图灵服务器上 -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.1</version>
</dependency>
核心代码
简单来说,这个项目只是通过发送http请求给服务器,然后回调数据,没啥难的,
方法说明
方法名 | 参数说明 | 例子 |
---|---|---|
sendMessage(String …args) | 用户直接发送简单请求给服务器 | sendMessage(”逗比”) |
sendMessage(Map< String,String> params) | 发送复杂的请求给服务器 | sendMessage({“info”:”逗比”,”userid”:”xx”…..}) 等。 这个是伪代码 |
备注:这个地方的参数,可以查看官方的字段,自己可以更好的扩展处理。
package com.yellowcong.tuling.client;
import java.util.HashMap;
import java.util.Map;
import com.yellowcong.tuling.model.Message;
import com.yellowcong.tuling.utils.HttpClientUtils;
import com.yellowcong.tuling.utils.JsonUtils;
/**
* 通过这个对象来搞定发送数据
* @author yellowcong
* @date 2016年3月26日
*
*/
public class TulingClient {
//访问的key,是每一个用户自己的
private String apikey;
//接口地址
private String url;
public TulingClient( String url,String apikey) {
super();
this.apikey = apikey;
this.url = url;
}
/**
* 创建日期:2018年1月14日<br/>
* 创建时间:下午12:09:56<br/>
* 创建用户:yellowcong<br/>
* 机能概要:发送数据到图灵
* @param info 发送的消息,必须写
* @param userid 可写可不写
* @return 返回消息
*/
public Message sendMessage(String ...args){
Map<String,String> params = new HashMap<String, String>();
params.put("key", this.apikey);
//第一个是 发送的消息
//info
if(args.length > 0){
params.put("info", args[0]);
}
//第二个是 userid
if(args.length > 1){
params.put("userid", args[1]);
}
//发送数据
String json = HttpClientUtils.post(this.url, params);
//System.out.println(json);
//将json数据转化为object
return JsonUtils.json2Object(json, Message.class);
}
/**
* 创建日期:2018年1月14日<br/>
* 创建时间:下午12:15:58<br/>
* 创建用户:yellowcong<br/>
* 机能概要:直接传递Map 集合,直接自己写一个集合,然后传递给图灵机器人
* @param params
* @return 返回的是一个消息对象
*/
public Message sendMessage(Map<String,String> params){
if(!params.containsKey("key")){
params.put("key", this.apikey);
}
//发送数据
String json = HttpClientUtils.post(this.url, params);
//将json数据转化为object
return JsonUtils.json2Object(json, Message.class);
}
}
简单使用
主要的api有两个,一个是直接发送字符串,另一个是发送Map集合给服务器。
package com.yellowcong.tuling.client;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.junit.Before;
import org.junit.Test;
import com.yellowcong.tuling.model.Message;
import com.yellowcong.tuling.utils.JsonUtils;
import junit.framework.Assert;
/**
* 通过这个对象来搞定发送数据
* @author yellowcong
* @date 2016年3月26日
*
*/
public class TestTulingClient {
private TulingClient client = null;
@Before
public void setUp(){
//api地址
String url = "http://www.tuling123.com/openapi/api";
//用户的密钥
String apikey = "708ff78bebc109d9631a4995f068530b";
//实例化连接
client = new TulingClient(url,apikey );
}
/**
* 创建日期:2018年1月14日<br/>
* 创建时间:下午12:41:00<br/>
* 创建用户:yellowcong<br/>
* 机能概要:测试直接发送string类型数据
*/
@Test
public void testTuling(){
Message msg = client.sendMessage("你是逗逼吗");
Assert.assertNotNull(msg);
System.out.println(msg.getText());
msg = client.sendMessage("我艹");
System.out.println(msg.getText());
Assert.assertNotNull(msg);
//讲个故事
msg = client.sendMessage("讲个故事");
System.out.println(msg.getText());
Assert.assertNotNull(msg);
msg = client.sendMessage("美女图片");
System.out.println(JsonUtils.object2Json(msg));
}
/**
* 创建日期:2018年1月14日<br/>
* 创建时间:下午12:40:39<br/>
* 创建用户:yellowcong<br/>
* 机能概要:测试发送 map集合
*/
@Test
public void testMap(){
Map<String,String> params = new HashMap<String,String>();
params.put("info", "逗比");
Message msg = client.sendMessage(params);
System.out.println(JsonUtils.object2Json(msg));
org.junit.Assert.assertNotNull(msg);
}
}
测试结果
引用Demo
引用有两种方法,一种是直接引用maven的项目,第二个是引用编译好的jar包,我这个地方,给大家测试第二种方法
配置pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>yellowcong</groupId>
<artifactId>tuling_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tuling_demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- JSON解析 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<!-- 用于反射,将json字符串转化为实体类 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<!-- 请求列,发送数据到图灵服务器上 -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- 引用jar包 -->
<dependency>
<groupId>yellowcong</groupId>
<artifactId>tuling</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/tuling-0.0.1-SNAPSHOT.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
调用自己封装好的api
package com.yellowcong.tuling.demo;
import com.yellowcong.tuling.client.TulingClient;
import com.yellowcong.tuling.model.Message;
/**
* 创建日期:2018年1月14日<br/>
* 创建时间:下午12:54:24<br/>
* 创建者 :yellowcong<br/>
* 机能概要:
*/
public class Demo {
public static void main(String[] args) {
//api地址
String url = "http://www.tuling123.com/openapi/api";
//用户的密钥
String apikey = "708ff78bebc109d9631a4995f068530b";
//实例化连接
TulingClient client = new TulingClient(url,apikey );
Message msg = client.sendMessage("你是都比吗?");
System.out.println(msg.getText());
}
}