Skip to content
This repository was archived by the owner on Jun 5, 2018. It is now read-only.

Commit cc13330

Browse files
committed
添加图灵机器人和测试代码
1 parent cc472e8 commit cc13330

File tree

9 files changed

+336
-8
lines changed

9 files changed

+336
-8
lines changed

pom.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
<version>3.8.0</version>
3939
</dependency>
4040

41+
<dependency>
42+
<groupId>junit</groupId>
43+
<artifactId>junit</artifactId>
44+
<version>4.12</version>
45+
<scope>test</scope>
46+
</dependency>
47+
4148
</dependencies>
4249

4350
<build>
@@ -66,8 +73,8 @@
6673
<artifactId>maven-compiler-plugin</artifactId>
6774
<version>3.2</version>
6875
<configuration>
69-
<source>1.8</source>
70-
<target>1.8</target>
76+
<source>1.6</source>
77+
<target>1.6</target>
7178
<encoding>UTF8</encoding>
7279
</configuration>
7380
</plugin>

src/main/java/io/github/biezhi/wechat/Application.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.biezhi.wechat;
22

3-
import io.github.biezhi.wechat.handle.MoliHandler;
3+
import io.github.biezhi.wechat.robot.MoliRobot;
44
import io.github.biezhi.wechat.model.Environment;
55
import io.github.biezhi.wechat.ui.StartUI;
66

@@ -17,7 +17,7 @@ public static void main(String[] args) throws Exception {
1717

1818
StartUI startUI = new StartUI(environment);
1919

20-
startUI.setMsgHandle(new MoliHandler(environment));
20+
startUI.setMsgHandle(new MoliRobot(environment));
2121
startUI.start();
2222
}
2323

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.biezhi.wechat.robot;
2+
3+
import java.security.Key;
4+
import java.security.MessageDigest;
5+
import java.util.Base64;
6+
7+
import javax.crypto.Cipher;
8+
import javax.crypto.spec.IvParameterSpec;
9+
import javax.crypto.spec.SecretKeySpec;
10+
11+
/**
12+
* aes加密算法
13+
*
14+
* @author 图灵机器人
15+
*/
16+
public class Aes {
17+
18+
private Key key;
19+
/**
20+
* AES CBC模式使用的Initialization Vector
21+
*/
22+
private IvParameterSpec iv;
23+
/**
24+
* Cipher 物件
25+
*/
26+
private Cipher cipher;
27+
28+
/**
29+
* 构造方法
30+
*
31+
* @param strKet 密钥
32+
*/
33+
public Aes(String strKey) {
34+
try {
35+
this.key = new SecretKeySpec(getHash("MD5", strKey), "AES");
36+
this.iv = new IvParameterSpec(new byte[]{0, 0, 0, 0, 0, 0, 0, 0,
37+
0, 0, 0, 0, 0, 0, 0, 0});
38+
this.cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
39+
} catch (final Exception ex) {
40+
throw new RuntimeException(ex.getMessage());
41+
}
42+
}
43+
44+
/**
45+
* 加密方法
46+
* <p>
47+
* 说明:采用128位
48+
*
49+
* @return 加密结果
50+
*/
51+
public String encrypt(String strContent) {
52+
try {
53+
byte[] data = strContent.getBytes("UTF-8");
54+
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
55+
byte[] encryptData = cipher.doFinal(data);
56+
String encryptResult = new String(Base64.getEncoder().encode(encryptData), "UTF-8");
57+
return encryptResult;
58+
} catch (Exception ex) {
59+
throw new RuntimeException(ex.getMessage());
60+
}
61+
}
62+
63+
/**
64+
* @param algorithm
65+
* @param text
66+
* @return
67+
*/
68+
private static byte[] getHash(String algorithm, String text) {
69+
try {
70+
byte[] bytes = text.getBytes("UTF-8");
71+
final MessageDigest digest = MessageDigest.getInstance(algorithm);
72+
digest.update(bytes);
73+
return digest.digest();
74+
} catch (final Exception ex) {
75+
throw new RuntimeException(ex.getMessage());
76+
}
77+
}
78+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.github.biezhi.wechat.robot;
2+
3+
import java.security.MessageDigest;
4+
5+
/**
6+
* md5加密
7+
*
8+
* @author 图灵机器人
9+
*/
10+
public class Md5 {
11+
/**
12+
* MD5加密算法
13+
* <p>
14+
* 说明:32位加密算法
15+
*
16+
* @param 待加密的数据
17+
* @return 加密结果,全小写的字符串
18+
*/
19+
public static String MD5(String s) {
20+
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
21+
'a', 'b', 'c', 'd', 'e', 'f'};
22+
try {
23+
byte[] btInput = s.getBytes("utf-8");
24+
// 获得MD5摘要算法的 MessageDigest 对象
25+
MessageDigest mdInst = MessageDigest.getInstance("MD5");
26+
// 使用指定的字节更新摘要
27+
mdInst.update(btInput);
28+
// 获得密文
29+
byte[] md = mdInst.digest();
30+
// 把密文转换成十六进制的字符串形式
31+
int j = md.length;
32+
char str[] = new char[j * 2];
33+
int k = 0;
34+
for (int i = 0; i < j; i++) {
35+
byte byte0 = md[i];
36+
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
37+
str[k++] = hexDigits[byte0 & 0xf];
38+
}
39+
return new String(str);
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
return null;
43+
}
44+
}
45+
}

src/main/java/io/github/biezhi/wechat/handle/MoliHandler.java renamed to src/main/java/io/github/biezhi/wechat/robot/MoliRobot.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package io.github.biezhi.wechat.handle;
1+
package io.github.biezhi.wechat.robot;
22

33
import com.google.gson.JsonObject;
44
import io.github.biezhi.wechat.Utils;
5+
import io.github.biezhi.wechat.handle.AbstractMessageHandler;
56
import io.github.biezhi.wechat.model.Environment;
67
import io.github.biezhi.wechat.model.GroupMessage;
78
import io.github.biezhi.wechat.model.UserMessage;
@@ -18,11 +19,11 @@
1819
* @author biezhi
1920
* 17/06/2017
2021
*/
21-
public class MoliHandler extends AbstractMessageHandler {
22+
public class MoliRobot extends AbstractMessageHandler {
2223

2324
private String baseUrl = "http://i.itpk.cn/api.php";
2425

25-
public MoliHandler(Environment environment) {
26+
public MoliRobot(Environment environment) {
2627
String apiKey = environment.get("moli.api_key");
2728
String apiSecret = environment.get("moli.api_secret");
2829
if (Utils.isNotBlank(apiKey) && Utils.isNotBlank(apiSecret)) {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package io.github.biezhi.wechat.robot;
2+
3+
import com.google.gson.JsonObject;
4+
import io.github.biezhi.wechat.Utils;
5+
import io.github.biezhi.wechat.handle.AbstractMessageHandler;
6+
import io.github.biezhi.wechat.model.Environment;
7+
import io.github.biezhi.wechat.model.GroupMessage;
8+
import io.github.biezhi.wechat.model.UserMessage;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.Request;
11+
import okhttp3.RequestBody;
12+
import okhttp3.Response;
13+
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
import java.util.concurrent.TimeUnit;
17+
18+
import static io.github.biezhi.wechat.api.WechatApi.JSON;
19+
20+
/**
21+
* 图灵机器人实现
22+
*
23+
* @author biezhi
24+
* 17/06/2017
25+
*/
26+
public class TulingRobot extends AbstractMessageHandler {
27+
28+
private String baseUrl = "http://www.tuling123.com/openapi/api";
29+
private String apiKey;
30+
private String apiSecret;
31+
32+
public TulingRobot(Environment environment) {
33+
this.apiKey = environment.get("tuling.api_key");
34+
this.apiSecret = environment.get("tuling.api_secret");
35+
}
36+
37+
@Override
38+
public void userMessage(UserMessage userMessage) {
39+
if (null == userMessage) {
40+
return;
41+
}
42+
String text = userMessage.getText();
43+
JsonObject raw_msg = userMessage.getRawMsg();
44+
String toUid = raw_msg.get("FromUserName").getAsString();
45+
String result = getResult(text);
46+
userMessage.sendText(result, toUid);
47+
}
48+
49+
@Override
50+
public void groupMessage(GroupMessage groupMessage) {
51+
System.out.println(groupMessage);
52+
String text = groupMessage.getText();
53+
if (groupMessage.getGroup_name().equals("测试群聊567") && Utils.isNotBlank(text)) {
54+
String result = getResult(groupMessage.getText());
55+
groupMessage.sendText(result, groupMessage.getGroupId());
56+
}
57+
}
58+
59+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
60+
.connectTimeout(10, TimeUnit.SECONDS)
61+
.readTimeout(30, TimeUnit.SECONDS)
62+
.build();
63+
64+
private String getResult(String question) {
65+
66+
Map<String, Object> data = new HashMap<String, Object>(2);
67+
data.put("key", apiKey);
68+
data.put("info", question);
69+
70+
//获取时间戳
71+
String timestamp = String.valueOf(System.currentTimeMillis());
72+
//生成密钥
73+
String keyParam = apiSecret + timestamp + apiKey;
74+
String key = Md5.MD5(keyParam);
75+
76+
//加密
77+
Aes mc = new Aes(key);
78+
String dataStr = mc.encrypt(Utils.toJson(data));
79+
80+
//封装请求参数
81+
Map<String, Object> json = new HashMap<String, Object>(3);
82+
json.put("key", apiKey);
83+
json.put("timestamp", timestamp);
84+
json.put("data", dataStr);
85+
86+
RequestBody requestBody = RequestBody.create(JSON, Utils.toJson(json));
87+
Request request = new Request.Builder()
88+
.url(baseUrl)
89+
.post(requestBody)
90+
.build();
91+
92+
try {
93+
Response response = okHttpClient.newCall(request).execute();
94+
TulingRet tulingRet = Utils.fromJson(response.body().string(), TulingRet.class);
95+
if (tulingRet.code == 00000) {
96+
return tulingRet.text;
97+
}
98+
} catch (Exception e) {
99+
return null;
100+
}
101+
return null;
102+
}
103+
104+
class TulingRet {
105+
int code;
106+
String text;
107+
108+
}
109+
110+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
# 超时配置
12
http.conn-time-out=10
23
http.read-time-out=20
34
http.write-time-out=10
5+
# 茉莉机器人
46
moli.api_key=
5-
moli.api_secret=
7+
moli.api_secret=
8+
# 图灵机器人
9+
tuling.api_key=
10+
tuling.api_secret=
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.biezhi.wechat.robot;
2+
3+
import okhttp3.MediaType;
4+
import okhttp3.OkHttpClient;
5+
6+
import java.util.concurrent.TimeUnit;
7+
8+
/**
9+
* @author biezhi
10+
* 18/06/2017
11+
*/
12+
public abstract class BaseTest {
13+
14+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
15+
.connectTimeout(10, TimeUnit.SECONDS)
16+
.readTimeout(30, TimeUnit.SECONDS)
17+
.build();
18+
19+
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
20+
21+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.biezhi.wechat.robot;
2+
3+
import io.github.biezhi.wechat.Utils;
4+
import okhttp3.Request;
5+
import okhttp3.RequestBody;
6+
import okhttp3.Response;
7+
import org.junit.Test;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
/**
13+
* @author biezhi
14+
* 18/06/2017
15+
*/
16+
public class RobotTest extends BaseTest {
17+
18+
@Test
19+
public void testMoli() throws Exception {
20+
String url = "http://i.itpk.cn/api.php?question=你好";
21+
Request request = new Request.Builder().url(url).build();
22+
Response response = okHttpClient.newCall(request).execute();
23+
System.out.println(response.body().string());
24+
}
25+
26+
@Test
27+
public void testTuling() throws Exception {
28+
String url = "http://www.tuling123.com/openapi/api";
29+
String apiKey = "";
30+
String secret = "";
31+
32+
Map<String, Object> data = new HashMap<String, Object>();
33+
data.put("key", apiKey);
34+
data.put("info", "你好");
35+
//获取时间戳
36+
String timestamp = String.valueOf(System.currentTimeMillis());
37+
//生成密钥
38+
String keyParam = secret + timestamp + apiKey;
39+
String key = Md5.MD5(keyParam);
40+
41+
//加密
42+
Aes mc = new Aes(key);
43+
String dataStr = mc.encrypt(Utils.toJson(data));
44+
45+
//封装请求参数
46+
Map<String, Object> json = new HashMap<String, Object>();
47+
json.put("key", apiKey);
48+
json.put("timestamp", timestamp);
49+
json.put("data", dataStr);
50+
51+
RequestBody requestBody = RequestBody.create(JSON, Utils.toJson(json));
52+
Request request = new Request.Builder()
53+
.url(url)
54+
.post(requestBody)
55+
.build();
56+
57+
Response response = okHttpClient.newCall(request).execute();
58+
System.out.println(response.body().string());
59+
}
60+
61+
}

0 commit comments

Comments
 (0)