Java Realizes Personal Wechat Automation with itchat4j Plug-in

Always want to achieve personal intelligence Weixin, do not need their own operation can achieve communication with friends, to achieve the intelligence of Weixin. However, in the process of implementation, it is found that the corresponding API has been encapsulated by God, and can be implemented by making simple calls. The Git address of the component is as follows: https://github.com/yaphone/itchat4j After cloning, it is imported into the project and a new project is created to introduce it. In this way, the basic environment will be set up:

  • First create a message processing Handel
    This is the key to the implementation of Intelligent Wechat, which needs to instantiate the IMsgHandlerFace interface in Api. The code is as follows:
public class WechatMsgHandlerFace implements IMsgHandlerFace {
	//Text message processing, where relevant rules can be defined for processing
	@Override
	public String textMsgHandle(BaseMsg msg) {
		String text = msg.getText();
		String result = "Text information received: " + text;
		return result;
	}

	//Picture message
	@Override
	public String picMsgHandle(BaseMsg msg) {
		String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".jpg"; // Here we use the time when the picture was received as the file name.
		String picPath = GlobalData.picMsgPath + File.separator + fileName; // Path to save pictures
		DownloadTools.getDownloadFn(msg, MsgTypeEnum.PIC.getType(), picPath); // Call this method to save the picture
		return "Successful Picture Preservation";
	}

	//Voice message
	@Override
	public String voiceMsgHandle(BaseMsg msg) {
		String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".mp3"; // Here use the time when the voice is received as the file name
		String voicePath = GlobalData.voiceMsgPath + File.separator + fileName; // The Path of Preserving Speech
		DownloadTools.getDownloadFn(msg, MsgTypeEnum.VOICE.getType(), voicePath); // Call this method to save voice
		return "Successful Sound Preservation";
	}

	//Video message
	@Override
	public String viedoMsgHandle(BaseMsg msg) {
		String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".mp4"; // Here we use the time of receiving a small video as the file name.
		String viedoPath = GlobalData.videoMsgPath + File.separator + fileName;// Paths to save small videos
		DownloadTools.getDownloadFn(msg, MsgTypeEnum.VIEDO.getType(), viedoPath);// Call this method to save small videos
		return "Video Save Successfully";
	}

	@Override
	public String nameCardMsgHandle(BaseMsg arg0) {
		return "Receiving Business Card Message";
	}

	@Override
	public void sysMsgHandle(BaseMsg msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String verifyAddFriendMsgHandle(BaseMsg msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String mediaMsgHandle(BaseMsg msg) {
		// TODO Auto-generated method stub
		return null;
	}
}
  • Create Service Entry
public class MainFrame {
	public static void main(String[] args) {
		IMsgHandlerFace msgHandler=new WechatMsgHandlerFace();
		Wechat wechat = new Wechat(msgHandler, GlobalData.loginImgPath);
		wechat.start();
	}
}

The essence of this service is Wechat Web page version, which simulates the implementation of web crawler. After running, it pops up the two-dimensional code of login, records the information of friends after successful login, and carries on the next logical processing. The following Api functions are generally provided:

  1. Get the list of friends'nicknames WechatTools.getContactNickNameList()
    This method returns a list of friends'nicknames whose function is declared as:
public static List<String> getContactNickNameList()
  1. Get a complete list of friends, WechatTools.getContactList()
    This method returns complete information about friends, such as nicknames, comments, regions, Avatar links, etc. Its function is declared as:
public static List<JSONObject> getContactList()
  1. Get the group list WechatTools.getGroupIdList()
    The group list is different from the friend list. After login, the group list is empty. Only when a group message is sent or received actively, can the group information be obtained. The group list records the id of the group. Its format is @@d052d34b9c922883013ee53deb461404f80ea353dbdd8fc9391cbf5f1c46. Calling this method returns a list of known groups. Its declarative function is:
public static List<String> getGroupIdList()
  1. Get group member WechatTools.getMemberListByGroupId() based on group ID
    This method obtains the list of group members according to the group ID (format @@d052d34b9c92288303013ee53deb461404f80ea353dbdd8fc9391cbf5f1c46). Its function is declared as:
public static JSONArray getMemberListByGroupId(String groupId)
  1. Exit WechatTools.logout()
    Exit itchat4j and no longer process messages. Its function is declared as:
public static void logout()
  1. Get WechatTools.getWechatStatus()
    Query the online status of Wechat, return true online and false offline. Its function is declared as
public static boolean getWechatStatus()
  1. Get the list of group nicknames WechatTools.getGroupNickNameList()
    Gets a list of group nicknames with the function declared as:
public static List<String> getGroupNickNameList()
  1. Modify the user's comment MessageTools.remarkNameByNickName(String nickName,
    String remName)
    Modify the user's comment name according to the user's nickname. Its function is declared as:
public static void remarkNameByNickName(String nickName, String remName)
  1. Send a text message based on a friend's nickname, MessageTools.sendMsgByNickName(String text, String)
    nickName)
    This method sends text messages according to user nicknames. Note that users need to be in your friends list, otherwise the sending will fail. If you have multiple users with the same nicknames in your friends list, you will only send messages to the first matching friend. Method accepts two parameters, text is the text message to be sent, nickName is the friend nickname to be sent, returns true when it is successfully sent, and false when it fails. Its function is declared as:
public static boolean sendMsgByNickName(String text, String nickName)
  1. Send text messages based on ID, MessageTools.sendMsgById(String text, String id)
    According to the ID, the sender ID can be obtained from MSG through msg.getString("FromUserName") in the format of @@d052d34b9c92288303013ee53deb461404f80ea353dbdd8fc9391cbf5f1c46 (group message) or @a257b99314d838362cd44abfe020f81 (non-group message), which can send messages to the specified ID. Its function is declared as:
public static void sendMsgById(String text, String id)
  1. Send a picture message based on a friend's nickname, MessageTools.sendPicMsgByNickName(String nickName,String filePath)
    This method sends a picture message according to a friend's nickname. filePath is the path of the picture file, such as D:/itchat4j/pic/test.jpg. It successfully returns true, but fails to return false. Its function is declared as:
public static boolean sendPicMsgByNickName(String nickName, String filePath)
  1. Send a picture message based on ID, MessageTools.sendPicMsgByUserId(String userId, String)
    filePath)
    This method sends a picture message according to the friend ID. filePath is the path of the picture file, such as D:/itchat4j/pic/test.jpg'. It successfully returns true, but fails to return false. Its function is declared as:
public static boolean sendPicMsgByUserId(String userId, String filePath)
  1. Send file messages according to your friend's nickname, MessageTools.sendFileMsgByNickName(String nickName,String filePath)
    This method sends file messages according to friends'nicknames. Files can be of various types, such as txt, PDF, small video, voice, excel, docx, etc. Please ensure that the file suffix name is correct when sending. Successful return true, failure return false. Its function is declared as:
public static boolean sendPicFileByNickName(String nickName, String filePath)
  1. Send file messages according to ID, MessageTools.sendFileMsgByNickName(String nickName,
    String filePath)
    This method sends file messages according to friends'nicknames, returns true successfully and false failures. Its function is declared as:
public static boolean sendFileMsgByUserId(String userId, String filePath)
  1. Processing Friend Add Request, MessageTools.addFriend(BaseMsg msg, boolean accept)
    When a friend adds a request, this function can be called to process it. msg adds a request message to the received friend, accept transmits true to accept a friend request, false to reject, and its function declares as follows:
public static void addFriend(BaseMsg msg, boolean accept)

Tags: git github Excel

Posted on Tue, 13 Aug 2019 00:32:41 -0700 by djcritch