4. WeChat public account development--text message processing case

text

        Not much to compare, just say the text directly;

        The processing of my text messages is encapsulated in the implementation class TextServiceImpl of TextService, in which I encapsulate the keyword reply processing and teaser functions. The keyword reply is easy to understand, that is, according to the information you send, I judge whether it contains certain If the keyword is included, it will reply to some corresponding information; the teaser function is a small function designed by myself, inspired by Baidu teaser.

        The user can send a message with a teaser mark to the official account, such as "[teaser] Lin Jia, I love you!" After receiving it in the background, it is judged that if there is a teaser mark, it is deemed to create a teaser message. After the creation is completed, This message will be saved, and then a 32-bit string key will be returned to the user. Any user can get the corresponding paragraph by replying to the key to the official account.

        The processing of text messages is relatively simple, including receiving, retrieving, setting results, and returning result sets; the receiving part has been processed in the control layer, and the business layer is responsible for information retrieval and return of result sets;

package com.wx.service.impl;

import java.util.List;
import java.util.Map;

import com.wx.dao.KeyWordDao;
import com.wx.dao.KeyWordDaoImpl;
import com.wx.model.WxCall;
import com.wx.model.WxKey;
import com.wx.service.TextMsgService;
import com.wx.service.TransmitEmotionService;
import com.wx.util.MessageUtil;
import life.book.util.Utils;

/**
 * User:Jiahengfei --> [email protected]
 *created by May 4, 2018 from Eclipse.
 * describe:Text messaging service implementation class
 */
public class TextMsgServiceImpl implements TextMsgService{
	private KeyWordDao dao = new KeyWordDaoImpl();
	private TransmitEmotionService transmit = new TransmitEmotionServiceImpl();

	@Override
	public String dispose(Map<String, String> map) {
		String response = null;
		boolean isChuanqing = false;
		
		//Special function priority retrieval
		if (response == null && isTransmit(map.get("Content"))) {
			//Publish a teaser message [judg whether the front search is effective - determine whether to carry a teaser sign]
			response = transmit.transmit(map);
		}
		if (response == null && map.get("Content").length()==32 && Utils.chineseStrNumber(map.get("Content"))==0) {
			//(High probability) Get the teaser message, whether the received key is a 32-bit key [judg whether the front search is effective - judge whether the message length is 32 bits - judge whether it contains Chinese characters]
			isChuanqing = true;
			response = transmit.obtain(map);
		}
		
		//keyword search
		if (response==null) {
			int result = search(map.get("Content"));
			if (result!=-1) {
				List<WxCall> calls = dao.selectCallBy(result);
				response = MessageUtil.Default.text(map, calls.get(Utils.gainNumber(calls.size())).getCall());
			}
		}
		
		//Function keyword search [judging whether the front search is effective - judge whether the teaser is effective]
		return response == null ? (isChuanqing ? MessageUtil.Default.text(map, "Invalid teaser code!") : MessageUtil.Default.text(map, "Huh?")) : response;
	}
	
	/**
	 * Whether the search characters contain keywords
	 * @param content character
	 * @return -1 is not included, other values ​​are the id of the included keyword
	 */
	private int search(String content){
		List<WxKey> keys = dao.selectKey();
		for (WxKey wxKey : keys) {
			if (content.indexOf(wxKey.getKeyword()) != -1) {
				return wxKey.getId();
			}
		}
		return -1;
	}
	
	/**
	 * Determine whether it is a teaser function
	 * @param content received string
	 */
	private boolean isTransmit(String content){
		if (content.length()>5) {
			if ("[传情]".equals(content.substring(0,4)) || "【传情】".equals(content.substring(0,4))) {
				return true;
			}else return false;
		}else return false;
	}

}

        The code of the dao layer will not be posted, it is just some basic additions, deletions and changes. Maintenance of two tables;

        After all, only two simple functions are implemented, and the amount of code is not very large, and it is easy to understand;

        I put it here as a public account of mine. Because at the beginning, many functions were written in the beta version, but my official account is a personal version, and many interface permissions are insufficient and cannot be used. However, it is still useful to write about the processing of text information; if you are interested, you can test it;

    

        There is no problem with the teaser function, you can experiment freely, but the keyword function is because I have limited time and there are not many keywords in the library, so I posted all the keywords, and I can chat with the public account within this range;

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325732238&siteId=291194637