java微信平臺,高階群發介面開發
阿新 • • 發佈:2019-01-07
一、微信訊息分組群發介面簡介
1、請求:該請求是使用post提交地址為:
https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN
其中ACCESS_TOKEN是我們動態獲取的 。
傳送的資料:(這裡使用圖文訊息示例)
{ "filter":{ "group_id":"2" }, "mpnews":{ "media_id":"123dsdajkasd231jhksad" }, "msgtype":"mpnews" }
filter,用於設定圖文訊息的接收者; group_id,群發到的分組的group_id(可在java微信介面之二—獲取使用者組);
mpnews,用於設定即將傳送的圖文訊息;
media_id, 用於群發的訊息的media_id(可在java微信介面之四—上傳素材中獲取); msgtype,群發的訊息型別,圖文訊息為mpnews,文字訊息為text,語音為voice,音樂為music,圖片為image,視訊為video.
2、響應:該響應也是以json方式返回的
正確的時候返回的資料:
{ "errcode":0, "errmsg":"send job submission success", "msg_id":34182 }
errcode 錯誤碼
errmsg 錯誤資訊
msg_id 訊息ID
錯誤的時候返回的資料:{"errcode":40004,"errmsg":"invalid media type"}
errcode,為錯誤程式碼,errmsg為錯誤資訊
二、關於java程式碼的呼叫
該介面的呼叫與java微信介面四—上傳素材一樣,需要使用到commons-httpclient。其中資料是構造成json資料後,放在post方法請求體裡面傳送給伺服器端。
三、程式碼實現
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";// 獲取access
29 public static final String UPLOAD_IMAGE_URL = "http://file.api.weixin.qq.com/cgi-bin/media/upload";// 上傳多媒體檔案
30 public static final String UPLOAD_FODDER_URL = "https://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 * 傳送訊息
39 *
40 * @param uploadurl
41 * apiurl
42 * @param access_token
43 * @param data
44 * 傳送資料
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.executeMethod(post);
65 if (status == HttpStatus.SC_OK)
66 {
67 String responseContent = post.getResponseBodyAsString();
68 System.out.println(responseContent);
69 JsonParser jsonparer = new JsonParser();// 初始化解析json格式的物件
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);// 獲取token在微信介面之一中獲取
92 if (accessToken != null)// token成功獲取
93 {
94 System.out.println(accessToken);
95 File file = new File("f:" + File.separator + "2000.JPG"); // 獲取本地檔案
96 String id = uploadImage(UPLOAD_IMAGE_URL, accessToken, "image",
97 file);// java微信介面之三—上傳多媒體檔案 可獲取
98 if (id != null)
99 {
100 // 構造資料
101 Map map = new HashMap();
102 map.put("thumb_media_id", id);
103 map.put("author", "wxx");
104 map.put("title", "標題");
105 map.put("content", "測試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.google.com");
113 map2.put("title", "標題");
114 map2.put("content", "測試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);// 轉換成json資料格式
125 String mediaId = uploadFodder(UPLOAD_FODDER_URL, accessToken,
126 result);
127 if (mediaId != null)
128 {
129 String ids = getGroups(GET_USER_GROUP, accessToken);// 在java微信介面之二—獲取使用者組
130 if (ids != null)
131 {
132 String[] idarray = ids.split(",");// 使用者組id陣列
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 }
159 }
發成功後會列印訊息,但由於微信伺服器的原因,訊息不會立即傳送,會過一段時間傳送。