Skip to content

Commit 5e173fc

Browse files
committed
新增发送图片方法,可根据nickName和userId发送图片消息
1 parent 27df211 commit 5e173fc

File tree

3 files changed

+50
-47
lines changed

3 files changed

+50
-47
lines changed

src/main/java/cn/zhouyafeng/itchat4j/api/MessageTools.java

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ public static void sendMsg(String text, String toUserName) {
134134
public static boolean sendMsgByNickName(String text, String nickName) {
135135
if (nickName != null) {
136136
String toUserName = WechatTools.getUserNameByNickName(nickName);
137-
sendRawMsg(1, text, toUserName);
138-
return true;
137+
if (toUserName != null) {
138+
sendRawMsg(1, text, toUserName);
139+
return true;
140+
}
139141
}
140142
return false;
141143

@@ -256,6 +258,41 @@ private static String uploadMediaToServer(String filePath) {
256258
return null;
257259
}
258260

261+
/**
262+
* 根据NickName发送图片消息
263+
*
264+
* @author https://github.com/yaphone
265+
* @date 2017年5月7日 下午10:32:45
266+
* @param nackName
267+
* @return
268+
*/
269+
public static boolean sendPicMsgByNickName(String nickName, String filePath) {
270+
if (nickName != null) {
271+
String toUserName = WechatTools.getUserNameByNickName(nickName);
272+
if (toUserName != null) {
273+
return sendPicMsgByUserId(toUserName, filePath);
274+
}
275+
}
276+
return false;
277+
}
278+
279+
/**
280+
* 根据用户id发送图片消息
281+
*
282+
* @author https://github.com/yaphone
283+
* @date 2017年5月7日 下午10:34:24
284+
* @param nickName
285+
* @param filePath
286+
* @return
287+
*/
288+
public static boolean sendPicMsgByUserId(String userId, String filePath) {
289+
String mediaId = uploadMediaToServer(filePath);
290+
if (mediaId != null) {
291+
return webWxSendMsgImg(userId, mediaId);
292+
}
293+
return false;
294+
}
295+
259296
/**
260297
* 发送图片消息,内部调用
261298
*
@@ -282,46 +319,29 @@ private static boolean webWxSendMsgImg(String userId, String mediaId) {
282319
paramMap.put("BaseRequest", baseRequestMap.get("BaseRequest"));
283320
paramMap.put("Msg", msgMap);
284321
String paramStr = JSON.toJSONString(paramMap);
285-
System.out.println(url);
286-
System.out.println(paramStr);
287322
HttpEntity entity = myHttpClient.doPost(url, paramStr);
288323
if (entity != null) {
289324
try {
290-
System.out.println(EntityUtils.toString(entity, "UTF-8"));
325+
String result = EntityUtils.toString(entity, "UTF-8");
326+
return JSON.parseObject(result).getJSONObject("BaseResponse").getInteger("Ret") == 0;
291327
} catch (Exception e) {
292328
logger.info(e.getMessage());
293329
}
294330
}
295-
return true;
296-
297-
}
298-
299-
/**
300-
* 根据昵称发送图片消息
301-
*
302-
* @author https://github.com/yaphone
303-
* @date 2017年5月7日 下午10:32:45
304-
* @param nackName
305-
* @return
306-
*/
307-
public static boolean sendPicMsgByNickName(String nickName, String filePath) {
308331
return false;
332+
309333
}
310334

311335
/**
312-
* 根据用户id发送图片消息
336+
* 根据用户id发送文件
313337
*
314338
* @author https://github.com/yaphone
315-
* @date 2017年5月7日 下午10:34:24
316-
* @param nickName
339+
* @date 2017年5月7日 下午11:57:36
340+
* @param userId
317341
* @param filePath
318342
* @return
319343
*/
320-
public static boolean sendPicMsgByUserId(String userId, String filePath) {
321-
String mediaId = uploadMediaToServer(filePath);
322-
if (mediaId != null) {
323-
return webWxSendMsgImg(userId, mediaId);
324-
}
344+
public static boolean sednFileMsgByUserId(String userId, String filePath) {
325345
return false;
326346
}
327347

src/test/java/cn/zhouyafeng/itchat4j/demo/SimpleDemo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.alibaba.fastjson.JSONObject;
88

99
import cn.zhouyafeng.itchat4j.Wechat;
10-
import cn.zhouyafeng.itchat4j.api.MessageTools;
1110
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;
1211
import cn.zhouyafeng.itchat4j.utils.DownloadTools;
1312
import cn.zhouyafeng.itchat4j.utils.MsgType;
@@ -24,9 +23,10 @@ public class SimpleDemo implements IMsgHandlerFace {
2423

2524
@Override
2625
public String textMsgHandle(JSONObject msg) {
27-
String filePath = "D:/itchat4j/pic/test.jpg";
28-
String userId = msg.getString("FromUserName");
29-
MessageTools.sendPicMsgByUserId(userId, filePath);
26+
// String filePath = "D:/itchat4j/pic/test.jpg";
27+
// String userId = msg.getString("FromUserName");
28+
// MessageTools.sendPicMsgByNickName("yaphone", filePath);
29+
// MessageTools.sendPicMsgByUserId(userId, filePath);
3030
String text = msg.getString("Text");
3131
return text;
3232
}

src/test/java/cn/zhouyafeng/itchat4j/demo/SimpleTest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)