Notice for the development of public account
A lot of people just began to contact the public number development, the public number background server Settings are very confused, do not know how to configure. Let me give you a brief introduction.
1, if you want to doInformation reply
Function, is that you send the relevant keywords to the public number, the public number directly to you reply to the relevant content. You just need to configure these places
2, if you want to do the development of other functions, such as to obtain the basic personal information of wechat users, you need to configure this place more (the first point above should also be configured first)
### Case demonstration
rendering
Implementation steps
1. Registration of public account (not demonstrated here, my public account is the authentication service number) 2. Back-end development of docking public account function development 3
Operation demonstration start
1, registered public number here is not demonstrated, if you can not contact xiaobian 2, back-end development docking public number function development 2.1, create a Springboot project
Project directory structure:
2.2. Modify the POM. XML file and import the relevant JAR coordinates
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 1.3.1 < / version > < / dependency > < the dependency > <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.9</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> The < version > 2.4 < / version > < classifier > jdk15 < / classifier > < / dependency > < / dependencies >Copy the code
2.3. Modify the application.properties file
Server.port = 80 spring.datasource. Driver-class-name = com.mysql.jdbc.driver spring.datasource.url=jdbc:mysql://localhost:3306/websocket spring.datasource.username=root spring.datasource.password=123456Copy the code
Note: mysql configuration, although not in the project, but also need to configure, otherwise the project will not start, you can create a database can do nothing
Error screenshot:
2.4. Write the tool class EchostrCheckutil.java for verifying the interface parameters of the public account
import javax.servlet.http.HttpServletRequest; import java.util.Arrays; /** * Public class EchostrCheckUtil {private static Final String token = "Enter your token, which must be the same as the public token "; /** * The developer verifies the request by checking the signature (see below). * If you confirm that the GET request is from the wechat server, please return the echostr parameter content as is, then the access takes effect and you become a developer successfully. Otherwise, the access fails. The encryption/verification process is as follows: * 1) lexicographical ordering of token, timestamp and nonce parameters * 2) splicing the three parameter strings into a string for sha1 encryption * 3) Developers can obtain the encrypted string to be compared with signature, */ public static String checkSignature(HttpServletRequest Request) {// String signature = request.getParameter("signature"); // Timestamp String timestamp = request.getparameter ("timestamp"); // Random number String nonce = request.getParameter("nonce"); // Random String String echostr = request.getParameter("echostr"); String[] str = new String[]{token, timestamp, nonce}; / / sorting Arrays. Sort (STR); // Concatenate string StringBuffer buffer = new StringBuffer(); for (int i = 0; i < str.length; i++) { buffer.append(str[i]); } String temp = sha1.encode (buffer.tostring ()); If (signature.equals(temp)) {return echostr; } else { return null; }}}Copy the code
Here you can refer to the public development documentation: developers.weixin.qq.com/doc/offiacc…
2.4. Write the public number access verification class interface wxController.java
@controller public class WxController {// Token Authentication access @getMapping ("wx") public String login(HttpServletRequest Request){ return EchostrCheckUtil.checkSignature(request); }}Copy the code
Note: the requested address here is http://your domain name /wx, which needs to be consistent with the server address of the public account. 2.5. Start the project, and then go to the background of the public account to configure the server information.
3.1. You need to map your local server through the internal network, so that the external network can access your computer project. I used Ngrok
Installation demo (do not understand can practice small make up 960513)
Pay attention to
As shown above:Forwarding
The corresponding address in the back is the mapping address generated for you by your Intranet penetration. When you start the project on your local computer, you can access the project on your computer by using your mobile phone.
4. Realization of sending message reply function
(Start to send, what we send to the public account, the public account will respond to the corresponding function)
Before developing the reply function, let me give you a brief introduction. 1. The types of reply messages on the public account include: 1 reply text message 2 reply picture message 3 Reply voice message 4 reply video message 5 Reply music message 6 reply text message 2 reply format is XML
XML format of the reply text message
<xml> <ToUserName><! [CDATA[toUser]]></ToUserName> <FromUserName><! [CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><! [CDATA[text]]></MsgType> <Content><! [CDATA[hello]]></Content> </ XML >Copy the code
4.1. Create a POJO class for text messages, TextMessage.java
public class TextMessage { protected String ToUserName; // Recipient account protected String FromUserName; // protected long CreateTime; // Message creation time protected String MsgType; // Message type private String Content; // Text message content private String MsgId; // Message ID, 64-bit integer... (omit get and set methods here)}Copy the code
4.2. Create a common text reply tool class. Before replying the message, convert it into the XML format required by wechat public account
Public class TextMessageUtil implements BaseMessageUtil<TextMessage> {// PoJO implements BaseMessageUtil String messageToxml(TextMessage message) { XStream xstream = new XStream(); xstream.alias("xml", message.getClass()); return xstream.toXML(message); } @override public String FromUserName (String ToUserName) {return null; } public String initMessage(String FromUserName, String ToUserName, String Content) { TextMessage text = new TextMessage(); text.setToUserName(FromUserName); text.setFromUserName(ToUserName); Text.setcontent (" What you typed is: "+ Content); text.setCreateTime(System.currentTimeMillis()); text.setMsgType("text"); return messageToxml(text); }}Copy the code
4.3 Create a MessageController class, used to receive the information sent by users to the public number, after receiving the message to reply the interface messagecontroller.java
@RestController public class MessageController { @PostMapping("/wx") public String answerMessage(HttpServletRequest request, HttpServletResponse response){ response.setCharacterEncoding("utf-8"); // Convert wechat request XML to map format and obtain required parameters map <String,String> map = messageutil.xmltomap (request); String ToUserName = map.get("ToUserName"); // Sender account (an OpenID) String FromUserName = map.get("FromUserName"); Text String MsgType = map.get("MsgType"); // Text message Content String Content = map.get("Content"); String message = null; If ("text".equals(MsgType)){TextMessageUtil textMessage = new TextMessageUtil(); message = textMessage.initMessage(FromUserName, ToUserName,Content); return message; }else { return null; }}}Copy the code
Note: This request address needs to be the same as the request address used to verify token access in order to obtain the content sent by the user to the public account. Results:
– the –
The complete source code for this case
Gitee.com/xuxiaofei19…