安卓用企业微信机器人API收集用户反馈
正文
打开「企业微信APP」,找到预推送消息到企业群,点击「右上角…」进入群详情,点击「群机器人」,点击「添加机器人」,点击「新建」,输入机器人信息,点击「添加」,复制得到的Webhook地址,提取key
package j8.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class WebHookWX {
public static String WX_="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
public static String txt(String s){
return "{\"msgtype\":\"text\",\"text\":{\"content\":\""+s+"\"}}";
}
public static String post(String url,String s) throws IOException {
URLConnection c = new URL(url).openConnection();
((HttpURLConnection)c).setRequestMethod("POST");
c.connect();
OutputStream os=c.getOutputStream();
os.write(s.getBytes());
InputStream is = c.getInputStream();
byte[] b = readInputStream(is);
return new String(b);
}
public static byte[] readInputStream(InputStream is) throws IOException {
ByteArrayOutputStream ba=new ByteArrayOutputStream();
if (is != null) {
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
ba.write(buffer, 0, len);
}
}
return ba.toByteArray();
}
public static String PUSH(String key, String s) throws IOException {
return post(WX_+key,txt(s));
}
public static void push(String key,String s) {
new Thread(()-> Push(key, s),"wx-webhook").start();
}
public static void Push(String key, String s) {
try {
PUSH(key,s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
PUSH()不能在主线程用,直接push()创建线程
假设webhook是:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
,提取key:693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
复制代码 隐藏代码
WebHookWX.push("693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa","用户反馈");
机器人支持MD,只能打开企业微信查看。
用TXT,能在微信直接看