Java WeChat interface 5-message grouping

Source: Internet
Author: User

Java interface 5-message grouping
Fire_java interface five-message group one, message group interface Introduction 1, request: the request is submitted using post address: https://api.weixin.qq.com/cgi-bin/message/mass/sendall? Access_token = ACCESS_TOKEN, where ACCESS_TOKEN is obtained dynamically. Sent data: (use a text message example here) {"filter": {"group_id": "2"}, "mpnews": {"media_id": "123dsdajkasd231jhksad "}, "msgtype": "mpnews"} filter, used to set the receiver of the text message; group_id, group_id of the group to which the group is sent (you can obtain the user group in java interface 2); mpnews, it is used to set the text message to be sent; media_id is used to set the media_id of the group message (which can be obtained from java interface 4-upload material); msgtype is used to set the group message type, the text message is mpnews, the text message is text, the voice is voice, the music is music, the image is image, and the video is video. 2. Response: The returned data is {"errcode": 0, "errmsg": "send job subm Ission success "," msg_id ": 34182} errcode error code errmsg error message msg_id returned data when the Message ID is incorrect: {" errcode ": 40004," errmsg ": "invalid media type"} errcode, error code, errmsg for error information specific api can be viewed: http://mp.weixin.qq.com/wiki/index.php? Title = % E9 % AB % 98% E7 % BA % A7 % E7 % BE % A4 % E5 % 8F % 91% E6 % 8E % A5 % E5 % 8F % A3 II. About java code call this interface is the same as java interface 4-uploading materials, use commons-httpclient. The data is constructed as json data and sent to the server in the post Method Request body. 3. Copy code 1 package com. demo. test; 2 3 import java. io. file; 4 import java. util. arrayList; 5 import java. util. hashMap; 6 import java. util. list; 7 import java. util. map; 8 9 import org. apache. commons. httpclient. methods. postMethod; 10 import org. apache. commons. httpclient. methods. multipart. filePart; 11 import org. apache. commons. httpclient. methods. multipart. multipartRequestEntity; 12 import org. apache. Commons. httpclient. methods. multipart. part; 13 import org. apache. http. httpEntity; 14 import org. apache. http. httpResponse; 15 import org. apache. http. httpStatus; 16 import org. apache. http. client. httpClient; 17 import org. apache. http. client. methods. httpGet; 18 import org. apache. http. impl. client. defaultHttpClient; 19 import org. apache. http. util. entityUtils; 20 21 import com. google. gson. gson; 22 import Com. google. gson. jsonArray; 23 import com. google. gson. jsonObject; 24 import com. google. gson. jsonParser; 25 26 public class Test 27 {28 public static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token"; // get access 29 public static final String UPLOAD_IMAGE_URL = "http://file.api.weixin.qq.com/cgi-bin/media/upload "; // upload multimedia file 30 public static final String UPLOAD_FODDER_URL = "htt Ps: // api.weixin.qq.com/cgi-bin/media/uploadnews "; 31 public static final String GET_USER_GROUP =" https://api.weixin.qq.com/cgi-bin/groups/get "; // url 32 public static final String SEND_MESSAGE_URL =" https://api.weixin.qq.com/cgi-bin/message/mass/sendall "; 33 public static final String APP_ID = "wxa549b28c24cf341e"; 34 public static final String SECRET = "78d8a8cd7a4fa700142d06b96bf44a37"; 35 36 37/** 38 * send a message 39*40 * @ param uploadurl 41 * apiurl 42 * @ param access_token 43 * @ param data 44 * send data 45 * @ return 46 */47 public static String sendMsg (String url, string token, String data) 48 {49 org. apache. commons. httpclient. httpClient client = new org. apache. commons. httpclient. httpClient (); 50 String sendurl = String. format ("% s? Access_token = % s ", url, token); 51 PostMethod post = new PostMethod (sendurl); 52 post 53. setRequestHeader (54 "User-Agent", 55 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv: 30.0) Gecko/20100101 Firefox/30.0"); 56 57 post. setRequestHeader ("Host", "file.api.weixin.qq.com"); 58 post. setRequestHeader ("Connection", "Keep-Alive"); 59 post. setRequestHeader ("Cache-Control", "no-cache"); 60 String result = Null; 61 try 62 {63 post. setRequestBody (data); 64 int status = client.exe cuteMethod (post); 65 if (status = HttpStatus. SC _ OK) 66 {67 String responseContent = post. getResponseBodyAsString (); 68 System. out. println (responseContent); 69 JsonParser jsonparer = new JsonParser (); // Initialize an object in json format 70 JsonObject json = jsonparer. parse (responseContent) 71. getAsJsonObject (); 72 if (json. get ("errcode ")! = Null 73 & json. get ("errcode "). getAsString (). equals ("0") 74 {75 result = json. get ("errmsg "). getAsString (); 76} 77} 78} 79 catch (Exception e) 80 {81 e. printStackTrace (); 82} 83 finally 84 {85 return result; 86} 87} 88 89 public static void main (String [] args) throws Exception 90 {91 String accessToken = getToken (GET_TOKEN_URL, APP_ID, SECRET); // get token get 92 if (accessToken! = Null) // the token is successfully obtained 93 {94 System. out. println (accessToken); 95 File file = new File ("f:" + File. separator + "2000.JPG"); // obtain the local file 96 String id = uploadImage (UPLOAD_IMAGE_URL, accessToken," image ", 97 file ); // java Interface 3-you can obtain 98 if (id! = Null) 99 {100 // construct the data 101 Map map = new HashMap (); 102 map. put ("thumb_media_id", id); 103 map. put ("author", "wxx"); 104 map. put ("title", "title"); 105 map. put ("content", "test fdsfdsfsdfssfdsfsdfsdfs"); 106 map. put ("digest", "digest"); 107 map. put ("show_cover_pic", "0"); 108 109 Map map2 = new HashMap (); 110 map2.put ("thumb_media_id", id); 111 map2.put ("author ", "wxx"); 112 map2.put ("content_source_url", "www. goo Gle.com "); 113 map2.put (" title "," title "); 114 map2.put (" content "," test fdsfdsfsdfssfdsfsdfsdfs "); 115 map2.put (" digest ", "digest"); 116 117 Map map3 = new HashMap (); 118 List <Map> list = new ArrayList <Map> (); 119 list. add (map); 120 list. add (map2); 121 map3.put ("articles", list); 122 123 Gson gson = new Gson (); 124 String result = gson. toJson (map3); // convert to json data format 125 String mediaId = uploadFodder (UPLOAD_FODDER_UR L, accessToken, 126 result); 127 if (mediaId! = Null) 128 {129 String ids = getGroups (GET_USER_GROUP, accessToken); // obtain the user group 130 if (ids! = Null) 131 {132 String [] idarray = ids. split (","); // array of user group IDs 133 for (String gid: idarray) 134 {135 136 JsonObject jObj = new JsonObject (); 137 JsonObject filter = new JsonObject (); 138 filter. addProperty ("group_id", gid); 139 jObj. add ("filter", filter); 140 141 142 JsonObject mpnews = new JsonObject (); 143 mpnews. addProperty ("media_id", mediaId); 144 jObj. add ("mpnews", mpnews); 145 146 jObj. addProperty ("msgtype", "mpnews"); 147 System. out. println (jObj. toString (); 148 149 String result2 = sendMsg (SEND_MESSAGE_URL, 150 accessToken, jObj. toString (); 151 System. out. println (result2); 152} 153} 154} 155 156} 157} 158}

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.