Java micro-letter enterprise Development Send Message (text, pictures, voice) _java

Source: Internet
Author: User
Tags openid

The previous article describes the open callback mode, start callback mode, we will implement the chat function. Usually use micro-letter chat can send text messages, voice, pictures, video, and so on, here only to achieve some of these features and share.

Establish a connection with the micro-credit enterprise number
1, the enterprise application calls the enterprise to provide the interface, manages or inquires the enterprise to manage the resources in the background, or sends the message to the member, the following is called the active invocation mode.
2, the enterprise number of users sent messages or user-triggered events to the enterprise application, the Enterprise application processing, hereinafter called callback mode.
3, the user in the micro-letter reading enterprise application issued by the H5 page, the page can invoke the micro-letter provided by the native interface, using micro-letter open terminal capabilities, hereinafter called JSAPI mode.
This is written in the development documentation for the micro-credit enterprise, but we typically use the first two ways .
In fact, active invocation and callback are relative, this is standing in the perspective of the micro-letter server, micro-trust server to the micro-letter client through the enterprise to send messages is an active call, the micro-letter client to send messages to call the micro-trust server is a passive call (callback).

Second, the main passive invoke micro-trust server processing data format

1. Keynote: The server sends JSON-formatted data to the micro-trust client, and the data does not need to be encrypted

2. Callback: the message sent by the micro-trust client requires AES encryption, the server accepts the message is XML format

These two articles are shown in a picture as follows:

Three, chat schematic diagram

First, the micro-credit client sends the message to the server processing, the data is transmitted to the third party server in the XML format, the third party server converts the data into JSON format, transmits it to the micro-trust server and sends it to the client.

Four, code implementation
with the above rationale, here is the Code section

1.servlet

public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {requ
 Est.setcharacterencoding ("UTF-8"); 
 
    Response.setcharacterencoding ("UTF-8"); 
    
    String msg_signature = Request.getparameter ("Msg_signature"); 
    
    String timestamp = request.getparameter ("timestamp"); 
   
    String nonce = Request.getparameter ("nonce"); 
    InputStream InputStream = Request.getinputstream (); 
    String postdata = ioutils.tostring (InputStream, "UTF-8"); 
    
    System.out.println (PostData); 
    String msg = ""; 
    Wxbizmsgcrypt wxcpt = null; 
      try {wxcpt = new Wxbizmsgcrypt (Stoken, Sencodingaeskey, scorpid); msg = WXCPT. 
    Decryptmsg (msg_signature, timestamp, nonce, postdata);  
    catch (Aesexception e) {e.printstacktrace (); 
      
    } System.out.println ("msg=" + msg); Gets the interface access voucher String Accesstoken = Commonutil.gettoken ("Wxe510946434680dab", "Ewtaho766invp4e1mcsz1mhyut2daleb62req3vsFizhy4vtmwzpkweuxuvh33g0 "). Getaccesstoken ();
 try {@SuppressWarnings ("unused") Boolean flag = Chatservice1.sendmessage (accesstoken,msg);
 catch (Exception e) {e.printstacktrace (); 
 }
}

 2.service

public class ChatService1 {public static Boolean SendMessage (String accesstoken,string msg) throws exception{Boolean
 Flag = false; 
 map<string, string> requestmap = Messageutil.parsexml (msg);
 Sender String Fromusername = Requestmap.get ("Fromusername");
 
 Message type String Msgtype = Requestmap.get ("Msgtype"); if (Msgtype.equals (Messageutil.req_message_type_text)) {//If it is Text customer service message String Content = requestmap.get ("content");// Text message content//Assembled text customer service message, Parameter 1: User ID; parameter 2: sent content jsonmsg = Advancedutil.maketextcustommessage ("Lishehe|zhisheng|
 Zhangwenyuan|lixinjiang ", Content);
  }else if (msgtype.equals (messageutil.req_message_type_image)) {//Sent is a picture String mediaId = Requestmap.get ("MediaId");
 Jsonmsg = Advancedutil.makeimagecustommessage ("Lishehe|zhisheng|zhangwenyuan|lixinjiang", mediaId);
  }else if (msgtype.equals (Messageutil.req_message_type_voice)) {String mediaId = requestmap.get ("MediaId"); jsonmsg= advancedutil.makevoicecustommessage ("Lishehe|zhisheng|zhangwenyuan|lixinjiang"),MEDIAID);
 } flag = Advancedutil.sendcustommessage (Accesstoken, jsonmsg);
 return flag;

 }
}

3. Tool class

public class Advancedutil {private static Logger log = Loggerfactory.getlogger (Advancedutil.class); /** * Assemble send text message * @return/public static string Maketextcustommessage (string openId, string content) {content = con
 Tent.replace ("\" "," "\\\");
 String jsonmsg = "{\ Touser\": \ "%s\", \ "msgtype\": \ "text\", \ "agentid\": \ "%s\", \ "text\": {\ "content\": \ "%s\"}} ";
 Return String.Format (jsonmsg, OpenId, 14,content); /** * Assemble send picture message * * @return/public static string Makeimagecustommessage (String openId, String mediaId) {Stri
 ng jsonmsg = "touser\": \ "%s\", \ "msgtype\": \ "image\", \ "agentid\": \ "%s\", \ "image\": {\ "media_id\": \ "%s\"}} ";
 Return String.Format (jsonmsg, openid,14, mediaId); /** * Assembly SEND Voice Message * * @return/public static string Makevoicecustommessage (String openId, String mediaId) {Stri
 ng jsonmsg = "touser\": \ "%s\", \ "msgtype\": \ "voice\", \ "agentid\": \ "%s\", \ "voice\": {\ "media_id\": \ "%s\"}} ";
 Return String.Format (jsonmsg, openid,14, mediaId);

 }

Iv. Summary

This enables the receipt and push of messages, the Third-party server will accept the XML data first decrypted parsing, get the message type, then the message will be sent after encapsulation into JSON format, and sent to the micro-trust server on the send to the client.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.