[Micro-channel public platform] message is forwarded to customer service

On the micro-channel public platform, look interface documentation, encounter many problems, some of us might understand wrong, here I introduce to you the next issue I encountered during development, we want to help.
Let's look at what we want to achieve results:
1, we opened in public background customer service number: plug-ins to add functionality to find customer service, opened, and then enter the customer service function to add customer service page.
Here Insert Picture Description
2, the number of users of public attention, the public message number:
Here Insert Picture Description
3, then call in https://mpkf.weixin.qq.com log in, you will see there is a person to be accessed, click access, customer service and the user can a conversation.
Here Insert Picture Description
Now we look at specific ways:

1, the micro-channel public number back to find the basic configuration, server configuration url.
Here Insert Picture Description
url in the backend write their own interfaces.
For example, concerns, take off, etc., when the user clicks attention, call the url, conduct their business. Of course, we have to do is forward the message to the customer service also will write in this logic.
2. Tell me what network API:
Here Insert Picture Description
According to the official website of explanation, we need to return to transfer_customer_service MsgType news, did not understand the meaning of the phrase beginning, that is the message type users are in the public transfer_customer_service number sent, according to the log later see, is not the case users can send the public a variety of numbers of ordinary messages, including: text, video, voice, shortvideo , link, image , etc., these types of lists are required to forward to the customer service system, for example: click on the menu, location reporting and so we should not transfer this in the document there.
We need to do is to send these types of user message is converted into MsgType transfer_customer_service, then the micro-channel server receives the response message will be transmitted when the second forwarded to customer service system (this step is a micro-channel server do, we do not control ).

3, began to develop:
Here Insert Picture Description

4, the core code:

		//消息转发到客服
        if (msgType.equals(MsgUtil.MESSAGE_TYPE_TEXT) || msgType.equals(MsgUtil.MESSAGE_TYPE_IMAGE)
                || msgType.equals(MsgUtil.MESSAGE_TYPE_VOICE) || msgType.equals(MsgUtil.MESSAGE_TYPE_VIDEO)
                || msgType.equals(MsgUtil.MESSAGE_TYPE_LINK) || msgType.equals(MsgUtil.MESSAGE_TYPE_SHORT_VIDEO)){
            logger.info("用户{}转发客服{}",userOpenId,wxId);
            GraphicMessage message = new GraphicMessage();
            message.setToUserName(userOpenId);
            message.setFromUserName(wxId);
            message.setMsgType(MsgUtil.EVENT_TYPE_TRANSFER);
            message.setCreateTime(time);
            return MsgUtil.domainToXml(message);
        }

GraphicMessage:

@Data
public class GraphicMessage {
    // 接收方帐号(收到的OpenID)
    private String ToUserName;
    // 开发者微信号
    private String FromUserName;
    // 消息创建时间 (整型)
    private long CreateTime;
    // 消息类型(text/music/news/voice/image/video)
    private String MsgType;
}

MsgUtil:

	/**
	 * 转发客服消息对象转换成xml
	 *
	 * @param domain 转发客服消息对象
	 * @return xml
	 */
	public static String domainToXml(GraphicMessage domain){
		xstream.alias("xml", domain.getClass());
		return xstream.toXML(domain);
	}
	
	/**
	 * 扩展xstream,使其支持CDATA块
	 * 
	 * @date 2013-05-19
	 */
	private static XStream xstream = new XStream(new XppDriver() {
		public HierarchicalStreamWriter createWriter(Writer out) {
			return new PrettyPrintWriter(out) {
				// 对所有xml节点的转换都增加CDATA标记
				boolean cdata = true;

				@SuppressWarnings("unchecked")
				public void startNode(String name, Class clazz) {
					super.startNode(name, clazz);
				}

				protected void writeText(QuickWriter writer, String text) {
					if (cdata) {
						writer.write("<![CDATA[");
						writer.write(text);
						writer.write("]]>");
					} else {
						writer.write(text);
					}
				}
			};
		}

	});

5, to solve the problem: When testing, the public message number, error: No. public service fails, please try again later!
Here Insert Picture Description
Looked, finally found the reason: toUserName and fromUserName negated, the error of that micro letter toUserName and fromUserName made the same.
This blog for the "public service number fails, please try again later" problem, summarizes several reasons, worth a visit, please refer to the blog: https://blog.csdn.net/fanrenxiang/article/details/ 80,877,600

Reference Document Interface: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1458557405

Summary:
to see the document definition, do not think in the same micro-channel field name values are the same.

Published 253 original articles · won praise 76 · views 290 000 +

Guess you like

Origin blog.csdn.net/hongwei15732623364/article/details/88425922