WeChat public account development tutorial 9th-sending and receiving QQ expressions-PHP Tutorial

Source: Internet
Author: User
Tags comparison table
Public Account Development Tutorial 9th-sending and receiving QQ expressions. I think you will not be unfamiliar with QQ expressions. small portraits greatly enrich the fun of chatting, making chatting no longer simple text descriptions, I think you will not be unfamiliar with QQ expressions. small pictures greatly enrich the fun of chatting, making chatting no longer a simple text description, it can also be used with pictures that express the mood of a person, such as happiness, anger, sorrow, and joy. This article focuses on how to use QQ expressions on the public platform, that is, how to send QQ expressions to users in the public account development mode, and how to identify QQ expressions sent by users.

QQ emoticons code table

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

For QQ emoticons, text messages are sent, but emoticons are displayed. Therefore, each QQ emoticons must have a corresponding emoticons. The following is a list of QQ emoticons used in my public account:

A total of 105 QQ expressions are listed above. each expression provides the corresponding text code and symbol code (maybe these two expressions are not appropriate ), as for how the two types of code come from and how to use them, we will talk about them as soon as possible.

The user sends a QQ expression to the public account

When using a public account, how can I send QQ expressions. There is an image button for smiling faces next to the input box. Clicking it will bring up the facial expression selection interface. you can select QQ, symbolic, and animated expressions ". After clicking a QQ expression, we find that the text code of the expression is displayed in the input box. this is caused by a pair of brackets, as shown in:

In fact, when we are very familiar with the text code for using QQ emotices, we can also directly enter the emoticon code in the input box, without the need to pop up the emoticon selection box. As shown in:

As you can see, in the input box, the "[fangya]", "/fangya", and "/: D" codes serve the same purpose. they are QQ expressions sent to fangya. At this time, let's go back and look at the QQ emoticons code comparison table at the beginning of the article to understand what is going on.

The public account sends a QQ face to the user

Similar to sending a QQ expression to a public account, in development mode, a public account can use the same facial expression code (text code or symbolic code) to send a QQ expression to the user. The code snippet is as follows:

// Text message if (msgType. equals (MessageUtil. REQ_MESSAGE_TYPE_TEXT) {// reply to the text message 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 ("[sad]/sad/: ("); // Convert a text message object to an xml string respMessage = MessageUtil. textMessageToXml (textMessage );}
The purpose of the above code snippet is to determine the type of message sent. if it is a text message (MsgType = text), three sad QQ expressions will be returned to the user. It can be seen that, whether a user sends a message to a public account or a public account sends a message to a user, the text code (such as [sad]/sad) and the symbol code (such /: :().

The public account identifies the QQ expression sent by the user

After learning how to send QQ expressions, let's take a look at how the public account can identify QQ expressions sent by users. What does this mean? When a user sends a QQ expression to a public account and receives the value in the background program, how can we know that the value is a QQ expression.

In fact, you only need to perform a simple test, for example, to output the received text messages to the log (you can use log4j or System. out. print), it is not difficult to find: Send a QQ expression to the public account, in the background program received the QQ expression symbol code.

The following is a simple encapsulation method that is implemented using regular expressions to determine whether a user sends a single QQ expression.

/*** Determine whether it is a QQ expression ** @ param content * @ return */public static boolean isQqFace (String content) {boolean result = false; // judge the regular expression String qqfaceRegex = "/: \) of the QQ expression | /::~ |/: B |/: \ |/: 8-\) |/: <|/: $ |/: X | /:: Z |/: '\ (|/:-\ |/: @ |/: P |/: D |/: O | /:: \ (|/: \ + |/: -- B |/: Q |/: T |/:, @ P | /:, @-D |/: d |/:, @ o |/: g |/: \ |-\) | /::! |/: L |/:> |/:, @ |/:, @ f |/:-S | /:\\? |/:, @ X |/:, @ |/: 8 | /:,@! | /:!!! |/: Xx |/: bye |/: wipe |/: dig |/: handclap |/: &-\ (|/: B-\) | /: <@ |/: @> |/:-O |/:>-\ |/: P-\ (|/: '\ | /: x-\) |/: \ * |/: @ x |/: 8 \ * |/: pd | /:
 
  
|/: 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 | /:
  
   
|/: Jump |/: shake | /:
   
    
|/: 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 ;}
   
  
 
The following is how to use it to implement a simple function: the user sends a QQ expression to the public account, and the public account returns a QQ expression to the user (xiaoqrobot does this ). The implementation code is as follows:

// Text message if (msgType. equals (MessageUtil. REQ_MESSAGE_TYPE_TEXT) {// text message content String content = requestMap. get ("Content"); // determines whether the user sends a single QQ expression if (XiaoqUtil. isQqFace (content) {// reply to the text message 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); // if the user sends a QQ expression, the user returns a QQ expression textMessage. setContent (content); // converts a text message object to an xml string respMessage = MessageUtil. textMessageToXml (textMessage );}}
Well, there are so many introductions on the use of QQ expressions in public accounts. In fact, I don't want beginners to simply copy the code I posted and implement the functions they want, I hope that new students can learn a way to think about and solve problems through this article.


...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.