图灵机器人api的封装与使用-yellowcong

图灵机器人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用于发送请求给图灵服务器
JsonUtilsjson字符串的相互转化

依赖包

<!-- 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());
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值