Home  >  Article  >  Backend Development  >  WeChat Public Account Development Tutorial Part 11 - Sending Emoticons (Part 1)_PHP Tutorial

WeChat Public Account Development Tutorial Part 11 - Sending Emoticons (Part 1)_PHP Tutorial

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

Digression (can be skipped)

I believe this article has made everyone wait for too long. It’s not that I’m trying to be mysterious and whet everyone’s appetite, but it really takes too much time to write an article. Maybe it takes you 3-5 minutes to read an article and learn to master it, but it takes me 2-3 hours to complete it. Maybe only those who have carefully written the article can understand it. I hope everyone can learn from each other. Be considerate!

Someone once told me that the things I wrote were too elementary and were entry-level. Well, I admit that it is difficult to satisfy all readers. In addition, I am only a novice. I only heard about the term WeChat public platform 4 months ago. Thank you for motivating me in different ways. I will work harder!

The 9th article introduces the sending and receiving of QQ emoticons. After that, many friends asked me how to send emoji (called emoticons on WeChat), which gave me the determination to write this article. Before that, I conducted a lot of searches on the Internet and found that there were no articles introducing this aspect at all. I also asked questions in the official WeChat public account development communication group, and few people knew how to send emoji expressions. Today, let us uncover its mystery together!

Article Summary

This article focuses on how to send emoticons to users through program code in WeChat public account development mode. As for how to identify whether the user sends emoticons, I will not explain it here, leaving you with some room for learning and thinking. I just want to give you a tip: when a user sends an emoticon to a public account, it is actually a text message. This behaves the same as QQ. Since it is a text message, if the received emoticon content is printed to the log, then everyone will know Is there text corresponding to the expression? Haha, of course it is not that simple. Unlike other text messages, here you need to convert the encoding of the received emoticon message first. Okay, that’s all.

Understand emoticons

In the main interactive interface of the public account, there is a smiley face picture button next to the input box at the bottom of the window. Clicking it will pop up the emoticon selection interface. The selectable emoticons are "QQ emoticon", "symbol emoticon" and " "Animation Emoticon", we select "Symbol Emoticon" and you will see the interface as shown below:

It can be seen that compared to QQ emoticons, symbolic emoticons are more practical. Why do you say that? Because most QQ emoticons are facial expressions, and symbolic emoticons, in addition to facial expressions, there are many emoticons closely related to life, such as: animals, flowers, trees, television, telephones, computers, guitars, balls, vehicles, etc. . Would it be more lively and interesting if emoticons could be used in messages?

Let’s take a look at the effect of using emoticons in the little q robot. Let’s take a look at the two pictures:

The screenshot on the left is the main menu of the little q robot. The emoticon next to the text in the Q circle of friends is a symbolic emoticon. It is a girl and a boy. It means that you can make more friends in the Q circle of friends. Don’t Wrong thinking, ^_^. The screenshot on the right is a guide for using the face recognition function. The "camera" and "grimace" in it are also symbolic expressions. Doesn't this look more interesting? If it is pure text, it will definitely appear too monotonous and boring.

Classification of Emoji expressions

There are many versions of Emoji, including Unified, DoCoMo, KDDI, Softbank and Google, and the emoticon codes of different versions are also different. What’s even more disgusting is: different mobile operating systems, or even different versions of the same operating system. The supported emoji expressions are different. Therefore, perfectionists can stop, because currently emoji expressions are not guaranteed to work properly on all terminals.

Fortunately, I have tested the use of emoji expressions on more than 10 terminals, including iPhone 4S, iPhone 5, Android 2.2, Android 4.0+, Win8, and iPad2. Only a few terminals cannot display it. Or it is displayed as a small square, so it doesn’t have much impact, so you can use it with confidence!

Unified version of Emoji code list

The several versions of emoji expressions introduced above are all represented by unicode encoding. In other words, the unicode encoding values ​​corresponding to different versions of emoji expressions are also different. In this article, I first give the code list of the Unified version of emoji expressions, as shown below:

How to send emoji expressions to users from public accounts

The unified unicode code comparison table of emoji expressions has been given above. So how to use these codes to send the corresponding emoji expressions? If you simply write the emoji code directly in the Content of the text message like you use the QQ emoticon code, it will definitely be displayed as it is.

A Java method is needed here for conversion processing. The code of the method is as follows:

/**
 * emoji表情转换(hex -> utf-16)
 * 
 * @param hexEmoji
 * @return
 */
public static String emoji(int hexEmoji) {
	return String.valueOf(Character.toChars(hexEmoji));
}

Method description: For example, the unicode encoding value of "bicycle" is U+1F6B2. If we want to use the emoji expression "bicycle" in the program code, we need to use it like this:

String bike = String.valueOf(Character.toChars(0x1F6B2));

In fact, the previous emoji() method is a simple encapsulation of the above line of code. Now you know how to use the emoji expression code. In fact, it is to replace U+ in the code table with 0x, then call the emoji method to convert, put the converted result in the Content of the text message, and the emoji expression will be displayed when it is returned to the user. .

Below, I give a complete example of using emoji expressions, as follows:

package org.liufeng.course.service;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.liufeng.course.message.resp.TextMessage;
import org.liufeng.course.util.MessageUtil;

/**
 * 核心服务类
 * 
 * @author liufeng
 * @date 2013-05-20
 */
public class CoreService {
	/**
	 * 处理微信发来的请求
	 * 
	 * @param request
	 * @return
	 */
	public static String processRequest(HttpServletRequest request) {
		String respMessage = null;
		try {
			// xml请求解析
			Map<String, String> requestMap = MessageUtil.parseXml(request);

			// 发送方帐号(open_id)
			String fromUserName = requestMap.get("FromUserName");
			// 公众帐号
			String toUserName = requestMap.get("ToUserName");

			// 回复文本消息
			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("自行车" + emoji(0x1F6B2) + " 男性" + emoji(0x1F6B9) + " 钱袋" + emoji(0x1F4B0));
			respMessage = MessageUtil.textMessageToXml(textMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return respMessage;
	}

	/**
	 * emoji表情转换(hex -> utf-16)
	 * 
	 * @param hexEmoji
	 * @return
	 */
	public static String emoji(int hexEmoji) {
		return String.valueOf(Character.toChars(hexEmoji));
	}
}

The function of the above code is: no matter what type of message the user sends, it will return a text message containing three emoji expressions. If you don’t understand what’s going on with the CoreService class, please check out Part 5 of this series of tutorials, or you just need to read line 42 of the code carefully to know how to put the emoji expression code in the Content of the text message. Finally, let’s take a look at the screenshot of the running effect:

This is the end of this article, but the explanation of emoji expressions is not over yet. Why do you say that? Please look carefully at the second screenshot of this article, which is the text menu of the Xiaoq robot. The emoji expressions used in it cannot be found in the emoji code table given in this article (the emoji expressions and code table on WeChat Exactly the same), then how is this emoji expression sent? Please listen to the next chapter for the breakdown!

If you think the article is helpful to you, please support Liu Feng by leaving a message or following the WeChat public account xiaoqrobot!

When reposting, please indicate that this article comes from Liu Feng’s blog (http://blog.csdn.net/lyq8479). Please respect the results of other people’s hard work. Thank you!


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444566.htmlTechArticleOff-topic (can be skipped) I believe that everyone has been waiting for this article for too long. It’s not that I’m being mysterious. To whet everyone’s appetite, it really takes too much time to write an article. Maybe one...
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