Home  >  Article  >  Backend Development  >  WeChat public account development tutorial Part 9 - Sending and receiving QQ emoticons_PHP tutorial

WeChat public account development tutorial Part 9 - Sending and receiving QQ emoticons_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:12:251550browse

I think everyone will be familiar with QQ emoticons. Each small avatar greatly enriches the fun of chatting, making chatting no longer a simple text narrative, but can also be accompanied by characters expressing happiness, anger, sadness, joy, etc. Small pictures of mood. The focus of this article is how to use QQ emoticons on the WeChat public platform, that is, in the WeChat public account development mode, how to send QQ emoticons to users, and how to identify that the users are sending QQ emoticons.

QQ emoticon code list

The first thing that needs to be made clear is that although QQ emoticons are presented as dynamic emoticon pictures, they are text messages in the messaging interface of the WeChat public platform; that is to say, when a user sends a QQ emoticon to a public account, The value of the message type MsgType received by the public account background program is text. As long as the above point can be understood, the following work can be carried out easily.

For QQ emoticons, what is sent is a text message, but an emoticon picture is displayed, so each QQ emoticon picture must have a corresponding emoticon code. Below is a comparison table of QQ emoticon codes used in WeChat public accounts:

A total of 105 QQ emoticons are listed above. Each emoticon has its corresponding text code and symbol code (perhaps these two names are not appropriate). As for how these two codes came from And how to use it will be discussed shortly below.

Users send QQ emoticons to public accounts

How to send QQ emoticons when using a public account on WeChat? I think few people don’t know how to do this. There is a smiley face picture button next to the input box. Clicking it will pop up the expression selection interface. The selectable expressions are "QQ emoticons", "symbol emoticons" and "animated emoticons". When we click to select a QQ emoticon, we find that the text code of the emoticon will be displayed in the input box, which is enclosed by a pair of square brackets, as shown in the following figure:

In fact, when we are familiar with the text codes for using QQ emoticons, we can also directly enter the emoticon code in the input box without popping up the emoticon selection box. As shown below:

As can be seen from the picture above, entering the three codes "[呲ya]", "/呲ya" and "/::D" in the input box have the same effect, they all send QQ emoticons of 呲ya . At this time, if you go back and look at the QQ emoticon code comparison table at the beginning of the article, you will understand what is going on.

Public accounts send QQ emoticons to users

Just like users sending QQ emoticons to public accounts, in development mode, public accounts can also use the same emoticon code (text code or symbol code) to reply to users with QQ emoticons. The code snippet is as follows:

// 文本消息
if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
	// 回复文本消息
	TextMessage textMessage = new TextMessage();
	textMessage.setToUserName(fromUserName);
	textMessage.setFromUserName(toUserName);
	textMessage.setCreateTime(new Date().getTime());
	textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
	textMessage.setFuncFlag(0);
	textMessage.setContent("[难过] /难过 /::(");
	
	// 文本消息对象转换成xml字符串
	respMessage = MessageUtil.textMessageToXml(textMessage);
}
​ The function of the above code snippet is to determine the type of message sent. If it is a text message (MsgType=text), then reply three sad QQ emoticons to the user. It can be seen that whether the user sends it to a public account or a public account sends it to a user, the text code (such as: [sad] /sad) and symbol code (such as /::() of QQ emoticons can be used.

Public accounts identify QQ emoticons sent by users

After mastering how to send QQ emoticons, let’s take a look at how public accounts can identify that users are sending QQ emoticons. What does this mean? When a user sends a QQ emoticon to a public account, what value will be received in the background program, and how do we know that this value is a QQ emoticon.

In fact, as long as you do a simple test, for example: output the received text message to the log (you can use log4j or System.out.print), it is not difficult to find: send a QQ emoticon to the public account, in the background What is received in the program is the symbol code of QQ emoticons.

The following is a method I simply encapsulated, implemented through regular expressions, to determine whether the user sends a single QQ emoticon.

/**
 * 判断是否是QQ表情
 * 
 * @param content
 * @return
 */
public static boolean isQqFace(String content) {
	boolean result = false;

	// 判断QQ表情的正则表达式
	String qqfaceRegex = "/::\\)|/::~|/::B|/::\\||/:8-\\)|/::<|/::$|/::X|/::Z|/::&#39;\\(|/::-\\||/::@|/::P|/::D|/::O|/::\\(|/::\\+|/:--b|/::Q|/::T|/:,@P|/:,@-D|/::d|/:,@o|/::g|/:\\|-\\)|/::!|/::L|/::>|/::,@|/:,@f|/::-S|/:\\?|/:,@x|/:,@@|/::8|/:,@!|/:!!!|/:xx|/:bye|/:wipe|/:dig|/:handclap|/:&-\\(|/:B-\\)|/:<@|/:@>|/::-O|/:>-\\||/:P-\\(|/::&#39;\\||/:X-\\)|/::\\*|/:@x|/:8\\*|/:pd|/:<W>|/:beer|/:basketb|/:oo|/:coffee|/:eat|/:pig|/:rose|/:fade|/:showlove|/:heart|/:break|/:cake|/:li|/:bome|/:kn|/:footb|/:ladybug|/:shit|/:moon|/:sun|/:gift|/:hug|/:strong|/:weak|/:share|/:v|/:@\\)|/:jj|/:@@|/:bad|/:lvu|/:no|/:ok|/:love|/:<L>|/:jump|/:shake|/:<O>|/:circle|/:kotow|/:turn|/:skip|/:oY|/:#-0|/:hiphot|/:kiss|/:<&|/:&>";
	Pattern p = Pattern.compile(qqfaceRegex);
	Matcher m = p.matcher(content);
	if (m.matches()) {
		result = true;
	}
	return result;
}
下面是方法的使用,实现了这样一个简单的功能:用户发什么QQ表情给公众帐号,公众帐号就回复什么QQ表情给用户(xiaoqrobot就是这么做的)。实现代码如下:

 

 

// 文本消息
if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
	// 文本消息内容
	String content = requestMap.get("Content");
	
	// 判断用户发送的是否是单个QQ表情
	if(XiaoqUtil.isQqFace(content)) {
		// 回复文本消息
		TextMessage textMessage = new TextMessage();
		textMessage.setToUserName(fromUserName);
		textMessage.setFromUserName(toUserName);
		textMessage.setCreateTime(new Date().getTime());
		textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
		textMessage.setFuncFlag(0);
		// 用户发什么QQ表情,就返回什么QQ表情
		textMessage.setContent(content);
		
		// 将文本消息对象转换成xml字符串
		respMessage = MessageUtil.textMessageToXml(textMessage);
	}
}
好了,关于微信公众帐号中QQ表情的使用就介绍这么多。其实,我并不希望初学者上来只是简单拷贝我贴出的代码,实现了自己想要的功能就完事了,更希望初学的朋友能够通过此文章学会一种思考问题和解决问题的方法。

 


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444568.htmlTechArticle我想大家对QQ表情一定不会陌生,一个个小头像极大丰富了聊天的乐趣,使得聊天不再是简单的文字叙述,还能够配上喜、怒、哀、乐等表...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn